Skip to content

Commit

Permalink
add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Apr 18, 2021
1 parent d539def commit 3c8a98f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 53 deletions.
50 changes: 48 additions & 2 deletions block-formatter.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
}
Expand All @@ -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!')
}
14 changes: 14 additions & 0 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -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)
})
}
53 changes: 3 additions & 50 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3c8a98f

Please sign in to comment.