1/// <reference lib="esnext.asynciterable" /> 2/// <reference lib="es2015.promise" /> 3import octokit = require("@octokit/rest"); 4import Octokit = octokit.Octokit; 5import minimist = require("minimist"); 6 7const options = minimist(process.argv.slice(2), { 8 boolean: ["help"], 9 string: ["token", "pull", "reviewer", "owner", "repo"], 10 alias: { 11 pr: "pull", 12 h: "help", 13 ["?"]: "help" 14 }, 15 default: { 16 token: process.env.GH_TOKEN, 17 pull: process.env.GH_PULL_NUMBER, 18 reviewer: process.env.REQUESTED_REVIEWER, 19 owner: "microsoft", 20 repo: "TypeScript" 21 } 22}); 23 24if (options.help) { 25 printHelpAndExit(0); 26} 27 28if (!options.token || !options.pull || !options.reviewer || !options.owner || !options.repo) { 29 console.error("Invalid arguments"); 30 printHelpAndExit(-1); 31} 32 33const pull_number = +options.pull; 34if (!isFinite(pull_number)) { 35 console.error("Invalid arguments"); 36 printHelpAndExit(-2); 37} 38 39const reviewers = Array.isArray(options.reviewer) ? options.reviewer : [options.reviewer]; 40 41main().catch(console.error); 42 43async function main() { 44 const gh = new Octokit({ auth: options.token }); 45 const response = await gh.pulls.requestReviewers({ 46 owner: options.owner, 47 repo: options.repo, 48 pull_number, 49 reviewers, 50 }); 51 if (response.status === 201) { 52 console.log(`Added ${reviewers.join(", ")} to ${response.data.url}`); 53 } 54 else { 55 console.log(`Failed to add ${reviewers.join(", ")} to the pull request.`); 56 } 57} 58 59function printHelpAndExit(exitCode: number) { 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}