This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotations.js
223 lines (199 loc) · 5.15 KB
/
annotations.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import meow from 'meow';
import {getCheckRunsForCommit, cleanupCheckRuns, getCheckRunAnnotations} from './github/checkruns.js';
import {getLatestCommits} from './github/commits.js';
import { octokit } from './github/octokit.js';
import {createObjectCsvWriter} from 'csv-writer';
const cli = meow({
importMeta: import.meta,
flags: {
org: {
type: 'string',
default: 'getsentry',
},
repo: {
type: 'string',
},
commits: {
type: 'number',
default: 10,
},
verbose: {
type: 'boolean',
alias: 'v',
},
allIssues: {
type: 'boolean',
default: false,
},
csv: {
type: 'string',
}
}
});
async function getCommits(org, repo) {
return await getLatestCommits(org, repo, cli.flags.commits);
}
const urlReg = /.*(https:\/\/github.blog\/changelog\/[^\/]*\/).*/;
function githubURLFromMsg(msg) {
const m = msg.match(urlReg);
if (!m) {
return null;
}
return m[1];
}
const actionsReg = /.*Please update the following actions [^:]*:\s*(.*)/;
function actionsFromMsg(msg) {
const m = msg.match(actionsReg);
if (!m) {
return [];
}
return m[1].split(", ");
}
async function processAnnotations(owner, repo, checkRuns) {
const issues = {};
for (const cr of checkRuns) {
if (cr.output.annotations_count == 0) {
continue;
}
const annotations = await getCheckRunAnnotations(owner, repo, cr.id);
for (const a of annotations) {
const ghurl = githubURLFromMsg(a.message);
if (!ghurl && !cli.flags.allIssues) {
continue;
}
const id = ghurl || a.message;
if (!issues[id]) {
issues[id] = {
count: 0,
id: id,
message: a.message,
annotations: [],
checkRuns: {},
actions: [],
};
}
issues[id].count++;
issues[id].annotations.push(a);
issues[id].checkRuns[cr.name] = cr;
const as = actionsFromMsg(a.message);
for (const ac of as) {
if (issues[id].actions.indexOf(ac) == -1) {
issues[id].actions.push(ac);
}
}
}
}
const issuesArr = [];
for (const i of Object.values(issues)) {
issuesArr.push(i);
}
issuesArr.sort((a, b) => b.count - a.count);
return issuesArr;
}
async function logAnnotations(owner, repo, annotations) {
if (annotations.length == 0 && cli.flags.verbose) {
console.log(`✅ ${owner}/${repo}`);
console.log();
}
if (annotations.length == 0) {
return;
}
console.log(`❌ ${owner}/${repo}`);
if (cli.flags.verbose) {
for (const i of annotations) {
console.log(` (${i.count}) ${i.id}`);
console.log(` Affected Check Runs:`);
for (const cr of Object.values(i.checkRuns)) {
console.log(` ${cr.name}: ${cr.html_url}`);
}
if (i.actions.length > 0) {
console.log(` Affected Actions:`);
for (const a of i.actions) {
console.log(` ${a}`);
}
}
console.log();
}
}
for (const i of annotations) {
console.log(` 🟡 (${i.count}) ${i.id}`);
}
console.log();
}
async function analyzeCommits(owner, repo, commits) {
const checkRuns = [];
for (const c of commits) {
const crs = cleanupCheckRuns(await getCheckRunsForCommit(owner, repo, c));
if (!crs) {
continue;
}
checkRuns.push(...crs);
}
const annotations = await processAnnotations(owner, repo, checkRuns);
logAnnotations(owner, repo, annotations);
return annotations;
}
async function saveCSV(org, repoannotations) {
if (!cli.flags.csv) {
return;
}
const header = [
{ id: 'org', title: 'Owner' },
{ id: 'repo', title: 'Repo' },
{ id: 'count', title: 'Issue Count' },
{ id: 'links', title: 'Issue Link / Description' },
{ id: 'actions', title: 'Affected Actions' },
{ id: 'example_runs', title: 'Example runs' },
];
const data = [];
for (const [repo, ras] of Object.entries(repoannotations)) {
if (ras.length == 0) {
data.push({
org,
repo,
count:0,
});
continue;
}
for (const ra of ras) {
data.push({
org,
repo,
count: ra.count,
links: ra.id,
actions: ra.actions.join("\n"),
example_runs: Object.values(ra.checkRuns).map(cr => cr.html_url).join("\n"),
});
}
const csvWriter = createObjectCsvWriter({
path: cli.flags.csv,
header,
});
await csvWriter.writeRecords(data);
}
console.log(`CSV file written: ${cli.flags.csv}`);
}
async function runOnRepo(org, repo) {
const commits = await getCommits(org, repo);
return await analyzeCommits(org, repo, commits);
}
async function runOnRepos(org, repos) {
const ra = {};
for (const r of repos) {
const annotations = await runOnRepo(org, r);
ra[r] = annotations;
}
await saveCSV(org, ra);
}
async function main(org) {
if (cli.flags.repo) {
return await runOnRepos(org, [cli.flags.repo]);
}
const repos = await octokit.paginate(octokit.repos.listForOrg, {
org,
})
const reponames = repos.map(r => r.name)
reponames.sort((a, b) => a.localeCompare(b));
await runOnRepos(org, reponames);
}
main(cli.flags.org);