Skip to content

Commit 3c8a98f

Browse files
committed
add cli
1 parent d539def commit 3c8a98f

File tree

4 files changed

+66
-53
lines changed

4 files changed

+66
-53
lines changed

block-formatter.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import * as github from '@actions/github';
12
import * as logfmt from 'logfmt';
2-
import matchAll from 'string.prototype.matchall'
3+
const matchAll = require('string.prototype.matchall')
34

45
export const findCodeBlocks = (source: string): ([string, number])[] => {
56
const pat = /(^```\r?\n)(.+?)^```/gms;
@@ -44,7 +45,6 @@ export const patchCodeBlocks = (source: string): [patched: string, patchCount: n
4445
logFmtMatches++
4546
return formatLogItem(time, level, msg, fields)
4647
} else {
47-
console.log(`!LF: ${line}`)
4848
// Did not include the usual log fields, probably not in logfmt, just skip it
4949
return line
5050
}
@@ -59,4 +59,50 @@ export const patchCodeBlocks = (source: string): [patched: string, patchCount: n
5959

6060
// Copy all unmatched lines after the last match
6161
return [patched + source.substring(sourcePos), logFmtMatches]
62+
}
63+
64+
export const patchIssue = async (authToken: string, owner: string, repo: string, number: number) => {
65+
66+
console.log(`Retrieving details for issue #${number} in ${owner}/${repo}...`)
67+
68+
const octokit = github.getOctokit(authToken);
69+
70+
const response = await octokit.request("GET /repos/{owner}/{repo}/issues/{issue_number}", {
71+
owner,
72+
repo,
73+
issue_number: number,
74+
});
75+
76+
if(response.status != 200) {
77+
throw new Error(`Failed to fetch issue data. Server responded with ${response.status}`)
78+
}
79+
80+
const issue = response.data;
81+
82+
console.log(`Issue title: ${issue.title}`)
83+
console.log(`Patching issue body...`)
84+
85+
const [patchedBody, patchCount] = patchCodeBlocks(issue.body);
86+
87+
if(patchCount < 1) {
88+
console.log('No lines where patched. Skipping update.')
89+
// No need to update the issue body, since we found no logfmt lines
90+
return
91+
}
92+
93+
console.log(`Patch count: ${patchCount}`)
94+
console.log(`Saving issue body...`)
95+
96+
const saveResponse = await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
97+
owner,
98+
repo,
99+
issue_number: number,
100+
body: patchedBody,
101+
})
102+
103+
if(saveResponse.status != 200) {
104+
throw new Error(`Failed to save issue data. Server responded with ${response.status}`)
105+
}
106+
107+
console.log('Done!')
62108
}

cli.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { patchIssue } from './block-formatter';
2+
3+
if(process.argv.length != 6) {
4+
console.log(process.argv)
5+
console.log(`Usage:\ncli TOKEN OWNER REPO ISSUE`)
6+
} else {
7+
8+
const [, , authToken, owner, repo, number] = process.argv;
9+
10+
patchIssue(authToken, owner, repo, parseInt(number)).catch(error => {
11+
console.error('Failed:', error.message)
12+
process.exit(1)
13+
})
14+
}

index.ts

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,13 @@
11
import * as core from '@actions/core';
22
import * as github from '@actions/github';
3-
import { patchCodeBlocks } from './block-formatter';
3+
import { patchIssue } from './block-formatter';
44

55
(async () => {
66

77
const authToken = core.getInput('repo-token');
88

9-
const octokit = github.getOctokit(authToken);
10-
119
const {owner, repo, number} = github.context.issue;
1210

13-
console.log(`Repo: ${owner}/${repo}`)
14-
console.log(`Issue: ${number}`)
15-
16-
console.log('Retrieving issue details...')
17-
18-
const response = await octokit.request("GET /repos/{owner}/{repo}/issues/{issue_number}", {
19-
owner,
20-
repo,
21-
issue_number: number,
22-
});
23-
24-
if(response.status != 200) {
25-
core.setFailed(`Failed to fetch issue data. Server responded with ${response.status}`)
26-
}
27-
28-
const issue = response.data;
29-
30-
console.log(`Issue title: ${issue.title}`)
31-
console.log(`Issue body:\n${issue.body}\n`)
32-
console.log(`Patching issue body...`)
33-
34-
const [patchedBody, patchCount] = patchCodeBlocks(issue.body);
35-
36-
if(patchCount < 1) {
37-
console.log('No lines where patched. Skipping update.')
38-
// No need to update the issue body, since we found no logfmt lines
39-
return
40-
}
41-
42-
console.log(`Patch count: ${patchCount}`)
43-
console.log(`Saving issue body...`)
44-
45-
const saveResponse = await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
46-
owner,
47-
repo,
48-
issue_number: number,
49-
body: patchedBody,
50-
})
51-
52-
console.log('Response:')
53-
console.log(saveResponse.data)
54-
55-
if(saveResponse.status != 200) {
56-
core.setFailed(`Failed to save issue data. Server responded with ${response.status}`)
57-
}
58-
59-
})().catch(error => core.setFailed(error.message))
11+
await patchIssue(authToken, owner, repo, number)
6012

13+
})().catch(error => core.setFailed(error.message))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.1",
44
"description": " GitHub Action for patching logfmt logs in issues",
55
"repository": "https://github.com/containrrr/logmt-patcher",
6-
"main": "index.ts",
6+
"main": "cli.ts",
77
"scripts": {
88
"build": "ncc build index.ts --license licenses.txt",
99
"test": "jest"

0 commit comments

Comments
 (0)