This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
issue.ts
483 lines (438 loc) · 12 KB
/
issue.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as Octokit from '@octokit/rest';
import {
Flags,
Issue,
IssueResult,
Repo,
IssuesApiResponse,
ApiIssue,
} from './types';
import {octo, repos, teams} from './util';
import {request, GaxiosResponse} from 'gaxios';
import Table = require('cli-table');
const truncate = require('truncate');
const CSV = require('csv-string');
const apiKey = process.env.DRIFT_API_KEY;
if (!apiKey) {
throw new Error(
'Please access the `Yoshi Drift Key` secret in go/valentine, and set the `DRIFT_API_KEY` env var.'
);
}
export async function getIssues(flags?: Flags): Promise<IssueResult[]> {
const promises = new Array<Promise<IssueResult>>();
repos.forEach(repo => {
// if we're filtering by --language, don't even snag the issues
if (flags && flags.language && repo.language !== flags.language) {
return;
}
promises.push(getRepoIssues(repo, flags));
});
return Promise.all(promises);
}
async function getRepoIssues(repo: Repo, flags?: Flags): Promise<IssueResult> {
const [owner, name] = repo.repo.split('/');
const result = {issues: new Array<Issue>(), repo};
let res!: GaxiosResponse<IssuesApiResponse>;
const rootUrl = 'https://drghs.endpoints.devrel-prod.cloud.goog/api/v1';
const url = `${rootUrl}/${repo.repo}/issues?key=${apiKey}&closed=false`;
try {
res = await request<IssuesApiResponse>({url});
} catch (e) {
console.warn(`Error fetching issues for ${repo.repo}.`);
//console.warn(e);
return result;
}
if (!res.data || !res.data.Issues) {
return result;
}
res!.data.Issues.forEach(r => {
const api = getApi(r, repo);
const issue: Issue = {
owner,
name,
language: repo.language,
repo: repo.repo,
types: getTypes(r),
api,
team: getTeam(r.Repo, api),
isOutOfSLO: isOutOfSLO(r),
isTriaged: isTriaged(r),
pri: r.PriorityUnknown ? undefined : r.Priority,
isPR: isPullRequest(r),
number: r.IssueID,
createdAt: r.Created,
title: r.Title,
url: r.URL,
labels: r.Labels || [],
assignees: r.Assignees ? r.Assignees.map(x => x.Login) : [],
};
let use = true;
if (flags) {
if (flags.api) {
const apiTypes = flags.api
.split(',')
.map(t => t.trim())
.filter(t => t.length > 0);
if (!issue.api || apiTypes.indexOf(issue.api) === -1) {
use = false;
}
}
if (flags.repo && r.Repo !== flags.repo) {
use = false;
}
if (flags.outOfSlo && !issue.isOutOfSLO) {
use = false;
}
if (flags.untriaged && issue.isTriaged) {
use = false;
}
if (flags.team && issue.team !== flags.team) {
use = false;
}
if (flags.pri && `p${issue.pri}` !== flags.pri) {
use = false;
}
if (flags.pr && !issue.isPR) {
use = false;
}
if (flags.type) {
const flagTypes = flags.type
.split(',')
.map(t => t.trim())
.filter(t => t.length > 0);
let found = false;
for (const flagType of flagTypes) {
for (const issueType of issue.types) {
if (flagType === issueType) {
found = true;
}
}
}
if (!found) {
use = false;
}
}
}
if (use) {
result.issues.push(issue);
}
});
return result;
}
export interface IssueOptions {
csv?: boolean;
untriaged?: boolean;
outOfSLO?: boolean;
repository?: string;
language?: string;
api?: string;
pr?: boolean;
}
export async function tagIssues() {
const promises = new Array<Promise<void | Octokit.AnyResponse>>();
const repos = await getIssues();
repos.forEach(r => {
r.issues.forEach(i => {
const [owner, name] = r.repo.repo.split('/');
i.repo = name;
i.owner = owner;
const hasTriageMeLabel = i.labels.includes('triage me');
const hasOOSLOLabel = i.labels.includes(':rotating_light:');
if (!i.isTriaged && !hasTriageMeLabel && hoursOld(i.createdAt) > 16) {
console.log(`Tagging ${i.repo}#${i.number} with 'triage me'`);
promises.push(tagIssue(i, 'triage me'));
} else if (i.isTriaged && hasTriageMeLabel) {
console.log(`Un-Tagging ${i.repo}#${i.number} with 'triage me'`);
promises.push(untagIssue(i, 'triage me'));
}
if (i.isOutOfSLO && !hasOOSLOLabel) {
console.log(`Tagging ${i.repo}#${i.number} with '🚨'`);
promises.push(tagIssue(i, ':rotating_light:'));
} else if (!i.isOutOfSLO && hasOOSLOLabel) {
console.log(`Un-tagging ${i.repo}#${i.number} with '🚨'`);
promises.push(untagIssue(i, ':rotating_light:'));
}
});
});
await Promise.all(promises);
}
function tagIssue(
i: Issue,
label: string
): Promise<void | Octokit.AnyResponse> {
return octo.issues
.addLabels({
labels: [label],
issue_number: i.number,
owner: i.owner,
repo: i.repo,
})
.catch(e => {
console.error(`Error tagging ${i.repo}#${i.number} with '${label}'`);
console.error(e);
});
}
function untagIssue(
i: Issue,
label: string
): Promise<void | Octokit.AnyResponse> {
return octo.issues
.removeLabel({
name: label,
issue_number: i.number,
owner: i.owner,
repo: i.repo,
})
.catch(e => {
console.error(`Error un-tagging ${i.repo}#${i.number} with '${label}'`);
console.error(e);
});
}
// tslint:disable-next-line no-any
export async function showIssues(options: Flags) {
const repos = await getIssues(options);
const issues = new Array<Issue>();
repos.forEach(r => {
r.issues.forEach(i => issues.push(i));
});
let table: Table;
const output = new Array<string>();
const head = [
'Issue',
'Triaged',
'InSLO',
'Title',
'Language',
'API',
'Pri',
'Team',
];
if (options.csv) {
output.push(CSV.stringify(head));
} else {
table = new Table({head});
}
issues.forEach(issue => {
const values = [
issue.url,
options.csv ? issue.isTriaged : issue.isTriaged ? '🦖' : '🚨',
options.csv ? !issue.isOutOfSLO : !issue.isOutOfSLO ? '🦖' : '🚨',
truncate(issue.title, 75),
issue.language,
issue.api || '',
issue.pri || '',
issue.team,
];
if (options.csv) {
output.push(CSV.stringify(values));
} else {
table.push(values);
}
});
if (table!) {
output.push(table!.toString());
}
output.forEach(l => process.stdout.write(l));
process.stdout.write('\n');
}
function getTypes(i: ApiIssue) {
const types = new Array<string>();
if (i.Labels) {
for (const label of i.Labels) {
if (label.startsWith('type: ')) {
types.push(label.slice(6));
}
}
}
return types;
}
function isPullRequest(i: ApiIssue) {
return !!i.PullRequest;
}
function getApi(i: ApiIssue, repo: Repo) {
if (i.Labels) {
for (const label of i.Labels) {
if (label.startsWith('api: ')) {
return label.slice(5);
}
}
}
return repo.apiHint;
}
function getTeam(repo: string, api?: string) {
if (api) {
const t = teams.find(x => (x.apis || []).includes(api));
if (t) {
return t.name;
}
}
const t = teams.find(x => (x.repos || []).includes(repo));
if (t) {
return t.name;
}
return 'core';
}
/**
* Determine if an issue has a `priority: ` label.
* @param i Issue to analyze
*/
function hasPriority(i: ApiIssue) {
return hasLabel(i, 'priority: ');
}
/**
* Determine if an issue has a `type: ` label.
* @param i Issue to analyze
*/
function hasType(i: ApiIssue) {
return hasLabel(i, 'type: ');
}
/**
* Determine if an issue has a `type: bug` label.
* @param i Issue to analyze
*/
function isBug(i: ApiIssue) {
return hasLabel(i, 'type: bug');
}
function isAssigned(i: ApiIssue) {
return i.Assignees && i.Assignees.length > 0;
}
/**
* Check if there is a label that matches the given text.
* @param issue Issue to analyze
* @param label Label text to look for
*/
function hasLabel(issue: ApiIssue, label: string) {
return (
issue.Labels &&
issue.Labels.filter(x => x.toLowerCase().indexOf(label) > -1).length > 0
);
}
/**
* For a given issue, figure out if it's out of SLO.
* @param i Issue to analyze
*/
function isOutOfSLO(i: ApiIssue) {
const pri = i.PriorityUnknown ? undefined : i.Priority;
// Previously we applied rules around Pull Request closure SLOs.
// It had the unintended consequence of folks feeling forced to rush landing
// PRs early, when the intent was to cast a light on PRs that may have been
// forgotten. We will come back around and use a bot to solve this in the
// future.
if (isPullRequest(i)) {
return false;
}
// Ignore any issues which are blocked by external APIs
if (isExternal(i)) {
return false;
}
// Priority is only a trigger for an SLO if it's one of the following types:
// - question
// - bug
// - docs
const isCustomerIssue =
hasLabel(i, 'type: question') ||
hasLabel(i, 'type: docs') ||
hasLabel(i, 'type: bug');
if (isCustomerIssue) {
// All P0 issues must receive a reply within 1 day, an update at least daily,
// and be resolved within 5 days.
if (pri === 0) {
if (daysOld(i.Created) > 5 || daysOld(i.UpdatedAt) > 1) {
return true;
}
}
// All P1 issues must receive a reply within 5 days, an update at least every
// 5 days thereafter, and be resolved within 42 days (six weeks).
if (pri === 1) {
if (daysOld(i.Created) > 42 || daysOld(i.UpdatedAt) > 5) {
return true;
}
}
// All P2 issues must receive a reply within 5 days, and be resolved within
// 180 days. In practice, we use fix-it weeks to burn down the P2 backlog.
if (pri === 2) {
if (daysOld(i.Created) > 180) {
return true;
}
}
}
// All questions must receive a reply within 5 days.
if (hasLabel(i, 'type: question')) {
if (!i.UpdatedAt && daysOld(i.Created) > 5) {
return true;
}
}
// All feature requests must receive a reply within 5 days
if (hasLabel(i, 'type: feature')) {
if (!i.UpdatedAt && daysOld(i.Created) > 5) {
return true;
}
// We decided in a team meeting to drop this requirement.
// if (daysOld(i.created_at) > 180) {
// return true;
// }
}
// Make sure if it hasn't been triaged, it's less than 5 days old
if (!isTriaged(i) && daysOld(i.Created) > 5) {
return true;
}
// It's all good then!
return false;
}
/**
* Determine how many days old an issue is
* @param date Date to compare
*/
function daysOld(date: string) {
return (Date.now() - new Date(date).getTime()) / 1000 / 60 / 60 / 24;
}
/**
* Determine how many hours old an issue is
* @param date Date to compare
*/
function hoursOld(date: string) {
return (Date.now() - new Date(date).getTime()) / 1000 / 60 / 60;
}
function isExternal(i: ApiIssue) {
return hasLabel(i, 'external');
}
/**
* Determine if an issue has been triaged. An issue is triaged if:
* - It has a `priority` label OR
* - It has a `type` label
* - For `type: bug`, there must be a `priority` label
* - For a P0 or P1 issue, it must have an asignee
* - Pull requests don't count.
* @param i Issue to analyze
*/
function isTriaged(i: ApiIssue) {
if (isPullRequest(i)) {
return true;
}
if (hasPriority(i)) {
return true;
}
if (hasType(i)) {
if (isBug(i)) {
return hasPriority(i);
}
return true;
}
if (hasLabel(i, 'status: investigating')) {
return true;
}
return false;
}