-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgit-manager.js
More file actions
612 lines (546 loc) · 16.5 KB
/
Copy pathgit-manager.js
File metadata and controls
612 lines (546 loc) · 16.5 KB
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import { Octokit } from "@octokit/rest";
import { throttling } from "@octokit/plugin-throttling";
import config from "./config-loader.js";
import Promise from "bluebird";
import _ from "underscore";
import debug from "./debug.js";
import utils from "./utils.js";
import Pull from "../models/pull.js";
import Issue from "../models/issue.js";
import Comment from "../models/comment.js";
import Review from "../models/review.js";
import Label from "../models/label.js";
import Status from "../models/status.js";
import Signature from "../models/signature.js";
import getLogin from "./get-user-login.js";
const MyOctokit = Octokit.plugin(throttling);
const gitDebug = debug("pulldasher:github");
console.log(config.github);
const github = new MyOctokit({
auth: config.github.token,
throttle: {
onRateLimit: (retryAfter, options) => {
github.log.warn(
`Request quota exhausted for request ${options.method} ${options.url}`
);
// Retry five times after hitting a rate limit error, then give up
if (options.request.retryCount <= 5) {
github.log.debug(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onSecondaryRateLimit: (retryAfter, options) => {
// does not retry, only logs a warning
github.log.warn(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`,
);
},
onAbuseLimit: (retryAfter, options) => {
// does not retry, only logs a warning
github.log.warn(
`Abuse detected for request ${options.method} ${options.url}`
);
},
},
});
const githubRest = github.rest;
export default {
github: githubRest,
/**
* Returns a promise which resolves to a GitHub API response to
* a query for a particular Pull Request.
*/
getPull: function (repo, number) {
return logErrors(
githubRest.pulls
.get(params({ pull_number: number }, repo))
.then((res) => res.data),
"Getting pull %s",
number
);
},
/**
* Get all *open* pull requests for a repo.
*
* Returns a promise which resolves to an array of all open pull requests
*/
getOpenPulls: function (repo) {
return logErrors(
github.paginate(githubRest.pulls.list, params({ state: "open" }, repo)),
"Getting open pulls in repo %s",
repo
);
},
/**
* Get *all* pull requests for a repo.
*
* Returns a promise which resolves to an array of all pull requests
*/
getAllPulls: function (repo) {
return logErrors(
github.paginate(githubRest.pulls.list, params({ state: "all" }, repo)),
"Getting all pulls in repo %s",
repo
);
},
/**
* Get an issue for a repo.
*
* Returns a promise which resolves to a github issue
*/
getIssue,
/**
* Get all open issues for a repo.
*
* Returns a promise which resolves to an array of all open issues
*/
getOpenIssues: function (repo) {
const searchParams = params({ state: "open" }, repo);
return logErrors(
github
.paginate(githubRest.issues.listForRepo, searchParams)
.then(filterOutPulls)
.then(addRepo(searchParams)),
"Getting open issues in repo %s",
repo
);
},
/**
* Get *all* issues for a repo.
*
* Returns a promise which resolves to an array of all issues
*/
getAllIssues: function (repo) {
const searchParams = params({ state: "all" }, repo);
return logErrors(
github
.paginate(githubRest.issues.listForRepo, searchParams)
.then(filterOutPulls)
.then(addRepo(searchParams)),
"Getting all issues in repo %s",
repo
);
},
/**
* Takes a promise that resolves to a GitHub pull request API response,
* parses it, and returns a promise that resolves to a Pull objects.
*/
parse: function (githubPull) {
gitDebug(
"Getting all information for pull %s in repo %s",
githubPull.number,
githubPull.base.repo.full_name
);
// We've occasionally noticed a null pull body, so lets fix it upfront
// before errors happen.
githubPull.body = githubPull.body || "";
var repo = githubPull.base.repo.full_name;
var reviewComments = getPullReviewComments(repo, githubPull.number);
var comments = getIssueComments(repo, githubPull.number);
var headCommit = getCommit(repo, githubPull.head.sha);
var commitStatuses = getCommitStatuses(repo, githubPull.head.sha);
var jobRuns = getAllJobRuns(repo, githubPull.head.sha);
var events = getIssueEvents(repo, githubPull.number);
// Only so we have the canonical list of labels.
var ghIssue = getIssue(repo, githubPull.number);
var reviews = getReviews(repo, githubPull.number);
// Returned to the map function. Each element of githubPulls maps to
// a promise that resolves to a Pull.
return Promise.all([
reviewComments,
comments,
headCommit,
commitStatuses,
jobRuns,
events,
ghIssue,
reviews,
]).then(function (results) {
var reviewComments = results[0],
comments = results[1],
headCommit = results[2],
commitStatuses = results[3],
jobRuns = results[4],
events = results[5],
ghIssue = results[6],
reviews = results[7];
// Array of Signature objects.
var commentSignatures = comments.reduce(function (sigs, comment) {
var commentSigs = Signature.parseComment(
comment,
repo,
githubPull.number
);
return sigs.concat(commentSigs);
}, []);
var signatures = reviews.reduce(function (sigs, review) {
var reviewSigs = Signature.parseReview(review, repo, githubPull.number);
return sigs.concat(reviewSigs);
}, commentSignatures);
// Signoffs from before the most recent commit are no longer active.
var headCommitDate = new Date(headCommit.commit.committer.date);
signatures.forEach(function (signature) {
if (
(signature.data.type === "CR" || signature.data.type === "QA") &&
new Date(signature.data.created_at) < headCommitDate
) {
signature.data.active = false;
}
});
// Array of Comment objects.
comments = comments.map(function (commentData) {
commentData.number = githubPull.number;
commentData.repo = repo;
commentData.type = "issue";
return new Comment(commentData);
});
// Array of Comment objects.
comments = comments.concat(
reviewComments.map(function (commentData) {
commentData.number = githubPull.number;
commentData.repo = repo;
commentData.type = "review";
return new Comment(commentData);
})
);
reviews = reviews.map(function (reviewData) {
reviewData.number = githubPull.number;
reviewData.repo = repo;
return new Review(reviewData);
});
let statuses = commitStatuses.map(function (commitStatus) {
let state = commitStatus.state;
let desc = commitStatus.description;
let url = commitStatus.target_url;
let context = commitStatus.context;
return new Status({
repo: repo,
sha: githubPull.head.sha,
state: state,
description: desc,
target_url: url,
context: context,
started_at: commitStatus.created_at,
completed_at: state == "pending" ? null : commitStatus.updated_at,
});
});
let checks = jobRuns.map(function (jobRun) {
let conclusion = jobRun.conclusion || jobRun.status;
let state = utils.mapCheckToStatus(conclusion);
let desc = conclusion;
let url = jobRun.html_url;
let context = jobRun.name;
return new Status({
repo: repo,
sha: githubPull.head.sha,
state: state,
description: desc,
target_url: url,
context: context,
started_at: jobRun.started_at,
completed_at: jobRun.completed_at,
});
});
let allCommitStatuses = statuses.concat(checks);
// Array of Label objects.
const labels = getLabelsFromEvents(events, ghIssue);
const pull = Pull.fromGithubApi(
githubPull,
signatures,
comments,
reviews,
allCommitStatuses,
labels
);
return pull.syncToIssue();
});
},
/**
* Takes a GitHub issue API response
* parses it, and returns a promise that resolves to an Issue object.
*/
parseIssue: function (ghIssue) {
gitDebug(
"Getting all information for issue %s in repo %s",
ghIssue.number,
ghIssue.repo
);
return getIssueEvents(ghIssue.repo, ghIssue.number).then(function (events) {
// Array of Label objects.
// Note: using the repo name from the config for now until we support
// multiple repos. The ghIssue object doesn't contain the repo name.
var labels = getLabelsFromEvents(events, ghIssue);
// The issue object has no assignment timestamp; derive it from events.
ghIssue.date_assigned = getAssignedDateFromEvents(events, ghIssue);
return Issue.getFromGH(ghIssue, labels);
});
},
};
function getIssue(repo, number) {
const searchParams = params({ issue_number: number }, repo);
return logErrors(
githubRest.issues
.get(searchParams)
.then((res) => res.data)
.then(addRepo(searchParams)),
"Getting issue %s in repo %s",
number,
repo
);
}
/**
* Get array of Label objects from complete list of a Issue's events.
*
* Note: ghIssue at this point has always come from one of the
* get*Issues() commands and thus has been augmented with the
* issue.repo property.
*/
function getLabelsFromEvents(events, ghIssue) {
gitDebug(
"Extracting label assignments from %s issue events for #%s",
events.length,
ghIssue.number
);
// Narrow list to relevant labeled/unlabeled events.
events = _.filter(events, function (event) {
return event.event === "labeled" || event.event === "unlabeled";
});
gitDebug("Found %s label events for #%s", events.length, ghIssue.number);
// Build simple Event objects with all the info we care about.
events = events.map(function (event) {
return {
type: event.event,
name: event.label.name,
user: getLogin(event.actor),
created_at: utils.fromDateString(event.created_at),
};
});
// Group label events by label name.
var labels = _.groupBy(events, "name");
// Get a list of the most recent events for each label.
labels = _.map(labels, function (events) {
events = _.sortBy(events, "created_at");
return _.last(events);
});
labels = _.filter(labels, function (event) {
return event.type === "labeled";
});
gitDebug("Found %s unique labels for #%s", labels.length, ghIssue.number);
// If these are available, use them as the canonical source, only augmented
// by the data from events. If a label is renamed, the events will retain
// the old name but the list of labels on the issue itself will be correct.
// So, if a label is renamed, we'll lose the labeler and the date.
if (ghIssue.labels && ghIssue.labels.length) {
gitDebug("Using %s labels from the github issue", ghIssue.labels.length);
// Includes labeller and a time from the events api
var eventLabels = _.indexBy(labels, "name");
return ghIssue.labels.map(function (label) {
var eventLabel = eventLabels[label.name];
return new Label(
{ name: label.name },
ghIssue.number,
ghIssue.repo,
eventLabel && eventLabel.user,
eventLabel && eventLabel.created_at
);
});
}
// Construct Label objects.
return labels.map(function (label) {
return new Label(
{ name: label.name },
ghIssue.number,
ghIssue.repo,
label.user,
label.created_at
);
});
}
/**
* Find when the issue's current assignee was assigned, from its events.
*
* The issue object carries the assignee but no assignment time, so we read it
* from the most recent `assigned` event for that assignee. Returns the event's
* raw `created_at` timestamp (normalized by `getFromGH`, like `closed_at`), or
* null when the issue is unassigned or no matching event exists.
*/
function getAssignedDateFromEvents(events, ghIssue) {
var assignee = ghIssue.assignee && ghIssue.assignee.login;
if (!assignee) {
return null;
}
var assignments = _.filter(events, function (event) {
return (
event.event === "assigned" &&
event.assignee &&
event.assignee.login === assignee
);
});
var latest = _.last(_.sortBy(assignments, "created_at"));
return latest ? latest.created_at : null;
}
/**
* Return the default api params merged with the overrides
*/
function params(apiParams, fullRepoName) {
const [owner, repo] = parseRepo(fullRepoName);
return _.extend(
{
owner,
repo,
},
apiParams
);
}
/**
* Returns a function that uses the search parameters to add the "repo"
* property to all the results. When we ask for a list of open issues from the
* API for a particular repo, those results don't have references to the repo
* we asked about, so we have to inject them to normalize the structure of the
* object.
*/
function addRepo({ owner, repo }) {
function addRepositoryField(ghIssue) {
ghIssue.repo = owner + "/" + repo;
}
return function (results) {
if (Array.isArray(results)) {
results.forEach(addRepositoryField);
} else {
addRepositoryField(results);
}
return results;
};
}
/**
* Splits the repo into the owner and repo name.
*/
function parseRepo(repo) {
return repo.split("/");
}
/**
* Return a promise for all issue events for the given issue / pull
*/
function getIssueEvents(repo, number) {
return logErrors(
github.paginate(
githubRest.issues.listEvents,
params({ issue_number: number }, repo)
),
"Getting events for issue %s:%s",
repo,
number
);
}
function getIssueComments(repo, number) {
return logErrors(
github.paginate(
githubRest.issues.listComments,
params({ issue_number: number }, repo)
),
"Getting comments for issue %s:%s",
repo,
number
);
}
function getReviews(repo, number) {
return logErrors(
github.paginate(
githubRest.pulls.listReviews,
params({ pull_number: number }, repo)
),
"Getting reviews for pull %s:%s",
repo,
number
);
}
function getPullReviewComments(repo, number) {
return logErrors(
github.paginate(
githubRest.pulls.listReviewComments,
params({ pull_number: number }, repo)
),
"Getting pull review comments for pull %s:%s",
repo,
number
);
}
function getCommit(repo, sha) {
return logErrors(
githubRest.repos
.getCommit(params({ ref: sha }, repo))
.then((res) => res.data),
"Getting commit for %s:%s",
repo,
sha
);
}
function getCommitStatuses(repo, ref) {
return logErrors(
githubRest.repos
.getCombinedStatusForRef(params({ ref }, repo))
.then((res) => res.data.statuses)
.then((statuses) => statuses || []),
"Getting commit status for %s:%s",
repo,
ref
);
}
function getAllJobRuns(repo, ref) {
return logErrors(
github
.paginate(
githubRest.actions.listWorkflowRunsForRepo,
params(
{
head_sha: ref,
exclude_pull_requests: true,
},
repo
)
)
.then((runs) => Promise.all((runs || []).map(getJobRunsFromWorkflow)))
.then((runs) => runs.flat(1)),
"Getting workflow runs for %s:%s",
repo,
ref
);
}
function getJobRunsFromWorkflow(workflowRun) {
return logErrors(
github
.paginate(
githubRest.actions.listJobsForWorkflowRun,
params(
{
run_id: workflowRun.id,
},
workflowRun.repository.full_name
)
)
.then((jobs) => jobs || []),
"Getting jobs runs for %s:%s",
workflowRun.repository.full_name,
workflowRun.name
);
}
/**
* Remove all entries that have the pull_request key set to something truthy
*/
function filterOutPulls(issues) {
gitDebug("Filtering out pulls from list of %s issues", issues.length);
issues = _.filter(
issues,
(issue) => !issue.pull_request || !issue.pull_request.url
);
gitDebug("Filtered down to %s issues", issues.length);
return issues;
}
function logErrors(promise, ...messageAndArgs) {
gitDebug(...messageAndArgs);
return promise.catch((err) => {
messageAndArgs[0] = "Error: Failed while: " + messageAndArgs[0];
gitDebug(...messageAndArgs);
throw err;
});
}