11const githubPRUrlPattern = / ^ \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \/ p u l l \/ ( \d + ) \/ ? $ / ;
22const startCandidateList = / \n p r e f e r e n c e s : [ ^ \n \S ] * ( # [ ^ \n ] * ) ? \n / ;
33
4+ const fetch2JSON = ( response : Awaited < ReturnType < typeof fetch > > ) =>
5+ response . ok
6+ ? response . json ( )
7+ : Promise . reject (
8+ new Error ( `Fetch error: ${ response . status } ${ response . statusText } ` , {
9+ cause : response ,
10+ } )
11+ ) ;
12+
413const branchInfoCache = new Map ( ) ;
514async function fetchVoteFilesInfo (
615 url : string | URL ,
@@ -20,12 +29,15 @@ async function fetchVoteFilesInfo(
2029 }
2130 const [ , owner , repo , number ] = prUrlMatch ;
2231
23- const data = await fetch ( `https://api.github.com/graphql` , {
24- ...fetchOptions ,
25- method : "POST" ,
26- body : JSON . stringify ( {
27- variables : { prid : Number ( number ) , owner, repo } ,
28- query : `query PR($prid: Int!, $owner: String!, $repo: String!) {
32+ let data ;
33+ // @ts -expect-error `headers` is provided by us as an object
34+ if ( fetchOptions ?. headers ?. Authorization ) {
35+ const graphQLData = await fetch ( `https://api.github.com/graphql` , {
36+ ...fetchOptions ,
37+ method : "POST" ,
38+ body : JSON . stringify ( {
39+ variables : { prid : Number ( number ) , owner, repo } ,
40+ query : `query PR($prid: Int!, $owner: String!, $repo: String!) {
2941 repository(owner: $owner, name: $repo) {
3042 pullRequest(number: $prid) {
3143 commits(first: 1) {
@@ -44,24 +56,41 @@ async function fetchVoteFilesInfo(
4456 }
4557 }
4658 }\n` ,
47- } ) ,
48- } ) . then ( ( response ) =>
49- response . ok
50- ? response . json ( )
51- : Promise . reject (
52- new Error ( `Fetch error: ${ response . status } ${ response . statusText } ` , {
53- cause : response ,
54- } )
55- )
56- ) ;
59+ } ) ,
60+ } ) . then ( fetch2JSON ) ;
61+
62+ if ( graphQLData . error ) {
63+ throw new Error ( "Unable to get required information for the vote PR" , {
64+ cause : graphQLData . error ,
65+ } ) ;
66+ }
67+
68+ data = graphQLData . data . repository . pullRequest ;
69+ } else {
70+ // GraphQL requires authenticated calls, we have to fallback to the REST API:
71+ const restData = await Promise . all ( [
72+ fetch (
73+ `https://api.github.com/repos/${ owner } /${ repo } /pulls/${ number } ` ,
74+ fetchOptions
75+ ) . then ( fetch2JSON ) ,
76+ fetch (
77+ `https://api.github.com/repos/${ owner } /${ repo } /pulls/${ number } /commits` ,
78+ fetchOptions
79+ ) . then ( fetch2JSON ) ,
80+ ] ) ;
5781
58- if ( data . error ) {
59- throw new Error ( "Unable to get required information for the vote PR" , {
60- cause : data . error ,
61- } ) ;
82+ data = {
83+ closed : restData [ 0 ] . state === "closed" ,
84+ merged : restData [ 0 ] . state === "merged" ,
85+ headRef : {
86+ name : restData [ 0 ] . head . ref ,
87+ repository : { url : restData [ 0 ] . head . repo . html_url } ,
88+ } ,
89+ commits : { nodes : [ { commit : { oid : restData [ 1 ] [ 0 ] . sha } } ] } ,
90+ } ;
6291 }
6392
64- const { closed, merged, commits, headRef } = data . data . repository . pullRequest ;
93+ const { closed, merged, commits, headRef } = data ;
6594
6695 if ( closed ) {
6796 throw new Error ( "The PR is marked as closed" ) ;
0 commit comments