File tree Expand file tree Collapse file tree 3 files changed +34
-11
lines changed
Expand file tree Collapse file tree 3 files changed +34
-11
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { Octokit } from "@octokit/core";
55import { throttling } from "@octokit/plugin-throttling" ;
66import { assignIssue } from "./assignIssue.js" ;
77import { getLinkedPR } from "./getLinkedPR.js" ;
8- import { sleep } from "./sleep .js" ;
8+ import { retry } from "./retry .js" ;
99import { Issue } from "./Issue.js" ;
1010const OctokitWithThrottling = Octokit . plugin ( throttling ) ;
1111
@@ -51,8 +51,7 @@ export default async function () {
5151 core . info (
5252 `Assigned ${ issue . owner } /${ issue . repository } #${ issue . issueNumber } to Copilot!`
5353 ) ;
54- await sleep ( 5000 ) ; // Wait for Copilot to open a PR
55- const pullRequest = await getLinkedPR ( octokit , issue ) ;
54+ const pullRequest = await retry ( ( ) => getLinkedPR ( octokit , issue ) ) ;
5655 if ( pullRequest ) {
5756 fixing . pullRequest = pullRequest ;
5857 core . info (
Original file line number Diff line number Diff line change 1+ /**
2+ * Sleep for a given number of milliseconds.
3+ * @param ms Time to sleep, in milliseconds.
4+ */
5+ function sleep ( ms : number ) : Promise < void > {
6+ return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( ) , ms ) ) ;
7+ }
8+
9+ /**
10+ * Retry a function with exponential backoff.
11+ * @param fn The function to retry.
12+ * @param maxAttempts The maximum number of retry attempts.
13+ * @param baseDelay The base delay between attempts.
14+ * @param attempt The current attempt number.
15+ * @returns The result of the function or undefined if all attempts fail.
16+ */
17+ export async function retry < T > (
18+ fn : ( ) => Promise < T | null | undefined > | T | null | undefined ,
19+ maxAttempts = 6 ,
20+ baseDelay = 2000 ,
21+ attempt = 1
22+ ) : Promise < T | undefined > {
23+ const value = await fn ( ) ;
24+ if ( value != null ) return value ;
25+ if ( attempt >= maxAttempts ) return undefined ;
26+ /** Exponential backoff, capped at 30s */
27+ const delay = Math . min ( 30000 , baseDelay * 2 ** ( attempt - 1 ) ) ;
28+ /** ±10% jitter */
29+ const jitter = 1 + ( Math . random ( ) - 0.5 ) * 0.2 ;
30+ await sleep ( Math . round ( delay * jitter ) ) ;
31+ return retry ( fn , maxAttempts , baseDelay , attempt + 1 ) ;
32+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments