• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { Octokit } from "@octokit/rest";
2import minimist from "minimist";
3
4const options = minimist(process.argv.slice(2), {
5    boolean: ["help"],
6    string: ["token", "pull", "reviewer", "owner", "repo"],
7    alias: {
8        pr: "pull",
9        h: "help",
10        ["?"]: "help"
11    },
12    default: {
13        token: process.env.GH_TOKEN,
14        pull: process.env.GH_PULL_NUMBER,
15        reviewer: process.env.REQUESTED_REVIEWER,
16        owner: "microsoft",
17        repo: "TypeScript"
18    }
19});
20
21if (options.help) {
22    printHelpAndExit(0);
23}
24
25if (!options.token || !options.pull || !options.reviewer || !options.owner || !options.repo) {
26    console.error("Invalid arguments");
27    printHelpAndExit(-1);
28}
29
30const pull_number = +options.pull;
31if (!isFinite(pull_number)) {
32    console.error("Invalid arguments");
33    printHelpAndExit(-2);
34}
35
36const reviewers = Array.isArray(options.reviewer) ? options.reviewer : [options.reviewer];
37
38main().catch(console.error);
39
40async function main() {
41    const gh = new Octokit({ auth: options.token });
42    const response = await gh.pulls.requestReviewers({
43        owner: options.owner,
44        repo: options.repo,
45        pull_number,
46        reviewers,
47    });
48    if (response.status === 201) {
49        console.log(`Added ${reviewers.join(", ")} to ${response.data.url}`);
50    }
51    else {
52        console.log(`Failed to add ${reviewers.join(", ")} to the pull request.`);
53    }
54}
55
56/**
57 * @param {number} exitCode
58 */
59function printHelpAndExit(exitCode) {
60    console.log(`
61usage: request-pr-review.js [options]
62
63options:
64    --token    <token>     Your GitHub auth token. Uses %GH_TOKEN% if present.
65    --owner    <owner>     The GH user or organization for the repo (default: 'microsoft').
66    --repo     <repo>      The GH repo for the pull request (default: 'TypeScript').
67    --pull     <pr_number> The pull request number. Uses %GH_PULL_NUMBER% if present.
68    --reviewer <reviewer>  The GH username of reviewer to add. May be specified multiple times.
69                           Uses %REQUESTED_REVIEWER% if present.
70 -h --help                 Prints this help message.
71`);
72    return process.exit(exitCode);
73}
74