1
+ /// <reference lib="esnext.asynciterable" />
2
+ /// <reference lib="es2015.promise" />
3
+ import octokit = require( "@octokit/rest" ) ;
4
+ import Octokit = octokit . Octokit ;
5
+ import minimist = require( "minimist" ) ;
6
+
7
+ const 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
+
24
+ if ( options . help ) {
25
+ printHelpAndExit ( 0 ) ;
26
+ }
27
+
28
+ if ( ! options . token || ! options . pull || ! options . reviewer || ! options . owner || ! options . repo ) {
29
+ console . error ( "Invalid arguments" ) ;
30
+ printHelpAndExit ( - 1 ) ;
31
+ }
32
+
33
+ const pull_number = + options . pull ;
34
+ if ( ! isFinite ( pull_number ) ) {
35
+ console . error ( "Invalid arguments" ) ;
36
+ printHelpAndExit ( - 2 ) ;
37
+ }
38
+
39
+ const reviewers = Array . isArray ( options . reviewer ) ? options . reviewer : [ options . reviewer ] ;
40
+
41
+ main ( ) . catch ( console . error ) ;
42
+
43
+ async function main ( ) {
44
+ const gh = new Octokit ( { auth : options . token } ) ;
45
+ const response = await gh . pulls . createReviewRequest ( {
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
+
59
+ function printHelpAndExit ( exitCode : number ) {
60
+ console . log ( `
61
+ usage: request-pr-review.js [options]
62
+
63
+ options:
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
+ }
0 commit comments