11const MINIMUM_RELEASE_GAP_MS = 6 * 60 * 60 * 1000 ;
2+ // Waits before each retry of a read that failed with a 5xx or a network error.
3+ const RETRY_DELAYS_MS = [ 2_000 , 5_000 , 10_000 ] ;
4+
5+ const defaultSleep = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
6+
7+ // Octokit reports network failures as status 500, so this also covers resets.
8+ // Auth, permission, validation, and GraphQL query errors fail at once.
9+ async function withRetry ( label , request , { core, sleep = defaultSleep } = { } ) {
10+ for ( let attempt = 0 ; ; attempt ++ ) {
11+ try {
12+ return await request ( ) ;
13+ } catch ( error ) {
14+ const delay = RETRY_DELAYS_MS [ attempt ] ;
15+ if ( delay === undefined || ! ( error ?. status >= 500 ) ) throw error ;
16+ core ?. warning ( `${ label } failed with status ${ error . status } . Retrying in ${ delay / 1000 } s.` ) ;
17+ await sleep ( delay ) ;
18+ }
19+ }
20+ }
221
322function repositoryDefaultBranch ( context ) {
423 const branch = context . payload . repository ?. default_branch ;
@@ -8,21 +27,22 @@ function repositoryDefaultBranch(context) {
827 return branch ;
928}
1029
11- async function assertCommitOnDefaultBranch ( { github, context, sha } ) {
30+ async function assertCommitOnDefaultBranch ( { github, context, core , sleep , sha } ) {
1231 const defaultBranch = repositoryDefaultBranch ( context ) ;
13- const { data : comparison } = await github . rest . repos . compareCommitsWithBasehead ( {
14- ...context . repo ,
15- basehead : `${ sha } ...${ defaultBranch } ` ,
16- per_page : 1 ,
17- } ) ;
32+ const basehead = `${ sha } ...${ defaultBranch } ` ;
33+ const { data : comparison } = await withRetry (
34+ `Compare ${ basehead } ` ,
35+ ( ) => github . rest . repos . compareCommitsWithBasehead ( { ...context . repo , basehead, per_page : 1 } ) ,
36+ { core, sleep } ,
37+ ) ;
1838 if ( comparison . status !== "ahead" && comparison . status !== "identical" ) {
1939 throw new Error (
2040 `Release commit ${ sha } is not contained in ${ defaultBranch } (${ comparison . status } ).` ,
2141 ) ;
2242 }
2343}
2444
25- async function assertReleaseSource ( { github, context, releaseChannel } ) {
45+ async function assertReleaseSource ( { github, context, core , sleep , releaseChannel } ) {
2646 if ( releaseChannel === "preview" ) return ;
2747 if ( releaseChannel !== "stable" && releaseChannel !== "nightly" ) {
2848 throw new Error ( `Unsupported release channel: ${ releaseChannel } ` ) ;
@@ -35,58 +55,92 @@ async function assertReleaseSource({ github, context, releaseChannel }) {
3555 ) ;
3656 }
3757
38- await assertCommitOnDefaultBranch ( { github, context, sha : context . sha } ) ;
58+ await assertCommitOnDefaultBranch ( { github, context, core , sleep , sha : context . sha } ) ;
3959}
4060
4161const isNightlyTag = ( tag ) => / ^ v .* - n i g h t l y \. / . test ( tag ) || tag . startsWith ( "nightly-v" ) ;
4262
43- async function findLatestNightly ( { github, context } ) {
44- const releases = await github . paginate ( github . rest . repos . listReleases , {
45- ...context . repo ,
46- per_page : 100 ,
47- } ) ;
48- return releases
49- . filter ( ( release ) => ! release . draft && release . published_at && isNightlyTag ( release . tag_name ) )
50- . sort ( ( a , b ) => Date . parse ( b . published_at ) - Date . parse ( a . published_at ) ) [ 0 ] ;
63+ // Only the three fields the gate reads. The REST list also returns every asset,
64+ // which made a full scan tens of megabytes.
65+ const RELEASES_QUERY = `
66+ query ($owner: String!, $repo: String!, $cursor: String) {
67+ repository(owner: $owner, name: $repo) {
68+ releases(first: 100, after: $cursor, orderBy: { field: CREATED_AT, direction: DESC }) {
69+ pageInfo { hasNextPage endCursor }
70+ nodes { tagName isDraft publishedAt }
71+ }
72+ }
73+ }
74+ ` ;
75+
76+ // Returns the nightly with the newest publishedAt. Release order follows the
77+ // tagged commit date, not publication, so every page is read unless `isSettled`
78+ // says the newest nightly seen so far already decides the caller's answer.
79+ async function findLatestNightly ( { github, context, core, sleep, isSettled = ( ) => false } ) {
80+ let latest ;
81+ let cursor = null ;
82+ do {
83+ const { repository } = await withRetry (
84+ "List releases" ,
85+ ( ) => github . graphql ( RELEASES_QUERY , { ...context . repo , cursor } ) ,
86+ { core, sleep } ,
87+ ) ;
88+ const { nodes, pageInfo } = repository . releases ;
89+ for ( const release of nodes ) {
90+ if ( release . isDraft || ! release . publishedAt || ! isNightlyTag ( release . tagName ) ) continue ;
91+ if ( ! latest || Date . parse ( release . publishedAt ) > Date . parse ( latest . publishedAt ) ) {
92+ latest = release ;
93+ }
94+ }
95+ if ( latest && isSettled ( latest ) ) return latest ;
96+ cursor = pageInfo . hasNextPage ? pageInfo . endCursor : null ;
97+ } while ( cursor ) ;
98+ return latest ;
5199}
52100
53- async function shouldReleaseNightly ( { github, context, core, now = Date . now ( ) } ) {
54- const lastNightly = await findLatestNightly ( { github, context } ) ;
101+ async function shouldReleaseNightly ( { github, context, core, sleep, now = Date . now ( ) } ) {
102+ const isInGap = ( release ) => now - Date . parse ( release . publishedAt ) < MINIMUM_RELEASE_GAP_MS ;
103+ // Any nightly inside the gap means the newest one is too, so stop reading.
104+ const lastNightly = await findLatestNightly ( { github, context, core, sleep, isSettled : isInGap } ) ;
55105
56106 if ( ! lastNightly ) {
57107 core . info ( "No published nightly found. Proceeding with release." ) ;
58108 return true ;
59109 }
60110
61- if ( now - Date . parse ( lastNightly . published_at ) < MINIMUM_RELEASE_GAP_MS ) {
62- core . info ( `Nightly ${ lastNightly . tag_name } was published less than six hours ago. Skipping.` ) ;
111+ const tag = lastNightly . tagName ;
112+ if ( isInGap ( lastNightly ) ) {
113+ core . info ( `Nightly ${ tag } was published less than six hours ago. Skipping.` ) ;
63114 return false ;
64115 }
65116
66- const { data : comparison } = await github . rest . repos . compareCommitsWithBasehead ( {
67- ...context . repo ,
68- basehead : `${ lastNightly . tag_name } ...${ context . sha } ` ,
69- per_page : 1 ,
70- } ) ;
117+ const basehead = `${ tag } ...${ context . sha } ` ;
118+ const { data : comparison } = await withRetry (
119+ `Compare ${ basehead } ` ,
120+ ( ) => github . rest . repos . compareCommitsWithBasehead ( { ...context . repo , basehead, per_page : 1 } ) ,
121+ { core, sleep } ,
122+ ) ;
71123 if ( comparison . status !== "ahead" ) {
72- core . info (
73- `Candidate commit is ${ comparison . status } relative to ${ lastNightly . tag_name } . Skipping.` ,
74- ) ;
124+ core . info ( `Candidate commit is ${ comparison . status } relative to ${ tag } . Skipping.` ) ;
75125 return false ;
76126 }
77127
78- core . info ( `New commits since ${ lastNightly . tag_name } , and the six-hour gap has passed.` ) ;
128+ core . info ( `New commits since ${ tag } , and the six-hour gap has passed.` ) ;
79129 return true ;
80130}
81131
82- async function resolveLatestNightlyCommit ( { github, context, core } ) {
83- const lastNightly = await findLatestNightly ( { github, context } ) ;
132+ async function resolveLatestNightlyCommit ( { github, context, core, sleep } ) {
133+ const lastNightly = await findLatestNightly ( { github, context, core , sleep } ) ;
84134 if ( ! lastNightly ) {
85135 throw new Error ( "No published nightly found. Stable releases build the latest nightly commit." ) ;
86136 }
87137
88- const tag = lastNightly . tag_name ;
89- const { data : commit } = await github . rest . repos . getCommit ( { ...context . repo , ref : tag } ) ;
138+ const tag = lastNightly . tagName ;
139+ const { data : commit } = await withRetry (
140+ `Read commit ${ tag } ` ,
141+ ( ) => github . rest . repos . getCommit ( { ...context . repo , ref : tag } ) ,
142+ { core, sleep } ,
143+ ) ;
90144 const version = / ^ (?: n i g h t l y - ) ? v ( \d + \. \d + \. \d + ) - n i g h t l y \. / . exec ( tag ) ?. [ 1 ] ;
91145 if ( ! version ) {
92146 throw new Error ( `Cannot derive a stable version from nightly tag ${ tag } .` ) ;
0 commit comments