-
Notifications
You must be signed in to change notification settings - Fork 0
/
block-formatter.ts
108 lines (82 loc) · 3.7 KB
/
block-formatter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as github from '@actions/github';
import * as logfmt from 'logfmt';
const matchAll = require('string.prototype.matchall')
export const findCodeBlocks = (source: string): ([string, number])[] => {
const pat = /(^```\r?\n)(.+?)^```/gms;
return Array.from(matchAll(source, pat), (m: any) => [m[2], m.index + m[1].length]);
}
export const formatLogItem = (time: string, level: string, msg: string, fields: [key: string, value: any][]): string => {
const levelChar = level[0].toUpperCase();
// Only include timezone with hour resolution
const trimmedTime = time.substr(0, 22);
const prefix = `${trimmedTime} [${levelChar}] `;
const prefixIndent = ' '.repeat(prefix.length)
// LogItems is the `msg` field (including newlines) and other fields (each on a new line)
const logItems = [msg ?? '', ...fields.map(([k, v]) => `${k}: ${v}`)].join('\n');
// Indent all extra lines to line up with the prefix
const lines = logItems.replace(/\n/g, '\n' + prefixIndent)
return prefix + lines
}
export const patchCodeBlocks = (source: string): [patched: string, patchCount: number] => {
const blocks = findCodeBlocks(source);
console.log(`Blocks found: ${blocks.length}`)
let patched = '';
let logFmtMatches = 0
let sourcePos = 0;
for (const [blockContent, start] of blocks) {
// Copy all unmatched lines between the last match and this one
patched += source.substring(sourcePos, start)
const blockLines = blockContent.split('\n').map(line => {
const parsed = logfmt.parse(line.replace(/\\n/g, '\n'));
const {time, level, msg} = parsed;
if (time && level) {
const fields = Object.entries(parsed)
.filter(([k]) => !['msg', 'level', 'time'].includes(k))
logFmtMatches++
return formatLogItem(time, level, msg, fields)
} else {
// Did not include the usual log fields, probably not in logfmt, just skip it
return line
}
})
// Add the processed block lines to the output
patched += blockLines.join('\n')
// Update the source cursor to the end of the match
sourcePos = start + blockContent.length;
}
// 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!')
}