diff --git a/block-formatter.ts b/block-formatter.ts index f396522..290731f 100644 --- a/block-formatter.ts +++ b/block-formatter.ts @@ -1,5 +1,6 @@ +import * as github from '@actions/github'; import * as logfmt from 'logfmt'; -import matchAll from 'string.prototype.matchall' +const matchAll = require('string.prototype.matchall') export const findCodeBlocks = (source: string): ([string, number])[] => { const pat = /(^```\r?\n)(.+?)^```/gms; @@ -44,7 +45,6 @@ export const patchCodeBlocks = (source: string): [patched: string, patchCount: n logFmtMatches++ return formatLogItem(time, level, msg, fields) } else { - console.log(`!LF: ${line}`) // Did not include the usual log fields, probably not in logfmt, just skip it return line } @@ -59,4 +59,50 @@ export const patchCodeBlocks = (source: string): [patched: string, patchCount: n // Copy all unmatched lines after the last match return [patched + source.substring(sourcePos), logFmtMatches] +} + +export const patchIssue = async (authToken: string, owner: string, repo: string, number: number) => { + + console.log(`Retrieving details for issue #${number} in ${owner}/${repo}...`) + + const octokit = github.getOctokit(authToken); + + const response = await octokit.request("GET /repos/{owner}/{repo}/issues/{issue_number}", { + owner, + repo, + issue_number: number, + }); + + if(response.status != 200) { + throw new Error(`Failed to fetch issue data. Server responded with ${response.status}`) + } + + const issue = response.data; + + console.log(`Issue title: ${issue.title}`) + console.log(`Patching issue body...`) + + const [patchedBody, patchCount] = patchCodeBlocks(issue.body); + + if(patchCount < 1) { + console.log('No lines where patched. Skipping update.') + // No need to update the issue body, since we found no logfmt lines + return + } + + console.log(`Patch count: ${patchCount}`) + console.log(`Saving issue body...`) + + const saveResponse = await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', { + owner, + repo, + issue_number: number, + body: patchedBody, + }) + + if(saveResponse.status != 200) { + throw new Error(`Failed to save issue data. Server responded with ${response.status}`) + } + + console.log('Done!') } \ No newline at end of file diff --git a/cli.ts b/cli.ts new file mode 100644 index 0000000..f3dc964 --- /dev/null +++ b/cli.ts @@ -0,0 +1,14 @@ +import { patchIssue } from './block-formatter'; + +if(process.argv.length != 6) { + console.log(process.argv) + console.log(`Usage:\ncli TOKEN OWNER REPO ISSUE`) +} else { + + const [, , authToken, owner, repo, number] = process.argv; + + patchIssue(authToken, owner, repo, parseInt(number)).catch(error => { + console.error('Failed:', error.message) + process.exit(1) + }) +} \ No newline at end of file diff --git a/index.ts b/index.ts index 66242f1..296f941 100644 --- a/index.ts +++ b/index.ts @@ -1,60 +1,13 @@ import * as core from '@actions/core'; import * as github from '@actions/github'; -import { patchCodeBlocks } from './block-formatter'; +import { patchIssue } from './block-formatter'; (async () => { const authToken = core.getInput('repo-token'); - const octokit = github.getOctokit(authToken); - const {owner, repo, number} = github.context.issue; - console.log(`Repo: ${owner}/${repo}`) - console.log(`Issue: ${number}`) - - console.log('Retrieving issue details...') - - const response = await octokit.request("GET /repos/{owner}/{repo}/issues/{issue_number}", { - owner, - repo, - issue_number: number, - }); - - if(response.status != 200) { - core.setFailed(`Failed to fetch issue data. Server responded with ${response.status}`) - } - - const issue = response.data; - - console.log(`Issue title: ${issue.title}`) - console.log(`Issue body:\n${issue.body}\n`) - console.log(`Patching issue body...`) - - const [patchedBody, patchCount] = patchCodeBlocks(issue.body); - - if(patchCount < 1) { - console.log('No lines where patched. Skipping update.') - // No need to update the issue body, since we found no logfmt lines - return - } - - console.log(`Patch count: ${patchCount}`) - console.log(`Saving issue body...`) - - const saveResponse = await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', { - owner, - repo, - issue_number: number, - body: patchedBody, - }) - - console.log('Response:') - console.log(saveResponse.data) - - if(saveResponse.status != 200) { - core.setFailed(`Failed to save issue data. Server responded with ${response.status}`) - } - -})().catch(error => core.setFailed(error.message)) + await patchIssue(authToken, owner, repo, number) +})().catch(error => core.setFailed(error.message)) \ No newline at end of file diff --git a/package.json b/package.json index 8647b21..3b11b7b 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.1", "description": " GitHub Action for patching logfmt logs in issues", "repository": "https://github.com/containrrr/logmt-patcher", - "main": "index.ts", + "main": "cli.ts", "scripts": { "build": "ncc build index.ts --license licenses.txt", "test": "jest"