forked from w3c/webref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest-pr-review.js
92 lines (82 loc) · 2.69 KB
/
request-pr-review.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
/**
* Request a review on a pending pre-release PR
*/
const Octokit = require("./octokit");
// Repository to process and PR reviewers
const owner = "w3c";
const repo = "webref";
const reviewers = ["dontcallmedom", "foolip", "tidoust"];
/**
* Create or update pre-release pull request
*
* @function
* @param {String} type Package name
*/
async function requestReview(type) {
console.log(`Check pre-release PR for the @webref/${type} package`);
const searchResponse = await octokit.search.issuesAndPullRequests({
q: `repo:${owner}/${repo} type:pr state:open head:release-${type}-`
});
const found = searchResponse?.data?.items?.[0];
const pendingPRResponse = found ?
await octokit.pulls.get({
owner, repo,
pull_number: found.number
}) :
null;
const pendingPR = pendingPRResponse?.data;
console.log(pendingPR ?
`- Found pending pre-release PR: ${pendingPR.title} (#${pendingPR.number})` :
"- No pending pre-release PR");
if (!pendingPR) {
return;
}
console.log(`- Targeted list of reviewers: ${reviewers.join(", ")}`);
console.log(`- Pending PR was created by: ${pendingPR.user.login}`);
const currentReviewers = pendingPR.requested_reviewers.map(r => r.login);
console.log(`- Current reviewers: ${currentReviewers.length > 0 ? currentReviewers.join(", ") : "none"}`);
const reviewersToAdd = reviewers.filter(login => !currentReviewers.includes(login) && pendingPR.user.login !== login);
console.log(`- Reviewers to add: ${reviewersToAdd.length > 0 ? reviewersToAdd.join(", ") : "none"}`);
if (reviewersToAdd.length > 0) {
await octokit.pulls.requestReviewers({
owner,
repo,
pull_number: pendingPR.number,
reviewers: reviewersToAdd
});
console.log("- Reviewers added");
}
}
/*******************************************************************************
Retrieve GH_TOKEN from environment, prepare Octokit and kick things off
*******************************************************************************/
const GH_TOKEN = (() => {
try {
return require("../config.json").GH_TOKEN;
} catch {
return process.env.GH_TOKEN;
}
})();
if (!GH_TOKEN) {
console.error("GH_TOKEN must be set to some personal access token as an env variable or in a config.json file");
process.exit(1);
}
const octokit = new Octokit({
auth: GH_TOKEN,
//log: console
});
requestReview("css")
.then(() => console.log())
.then(() => requestReview("elements"))
.then(() => console.log())
.then(() => requestReview("events"))
.then(() => console.log())
.then(() => requestReview("idl"))
.then(() => {
console.log();
console.log("== The end ==");
})
.catch(err => {
console.error(err);
process.exit(1);
});