| 1 | import { StaleAttemptError } from "./errors.ts"; |
| 2 | |
| 3 | const REQUEST_TIMEOUT_MS = 30_000 |
| 4 | // Backoff between tries of one callback: the API restarting must not cost the import, but a dead API must not hold it past its lease either. |
| 5 | const RETRY_DELAYS_MS = [ |
| 6 | |
| 7 | export type ImportedLabel |
| 8 | name: string |
| 9 | color: string |
| 10 | description: string |
| 11 | }; |
| 12 | |
| 13 | export type ImportedPullRequest |
| 14 | state: "open |
| 15 | draft: boolean |
| 16 | baseRef: string |
| 17 | headRef: string |
| 18 | headSha: string |
| 19 | headInRepository: boolean |
| 20 | mergeCommitSha: string |
| 21 | mergedAt: string |
| 22 | }; |
| 23 | |
| 24 | export type ImportedIssue |
| 25 | number: number |
| 26 | title: string |
| 27 | body: string |
| 28 | authorLogin: string |
| 29 | state: "open |
| 30 | createdAt: string |
| 31 | updatedAt: string |
| 32 | closedAt: string |
| 33 | labels: ImportedLabel |
| 34 | pullRequest?: ImportedPullRequest |
| 35 | }; |
| 36 | |
| 37 | export type ImportedRelease |
| 38 | tagName: string |
| 39 | name: string |
| 40 | body: string |
| 41 | isDraft: boolean |
| 42 | isPrerelease: boolean |
| 43 | createdAt: string |
| 44 | publishedAt: string |
| 45 | }; |
| 46 | |
| 47 | export type ImportedComment |
| 48 | issueNumber: number |
| 49 | authorLogin: string |
| 50 | body: string |
| 51 | createdAt: string |
| 52 | updatedAt: string |
| 53 | }; |
| 54 | |
| 55 | export type Outcome |
| 56 | | { succeeded |
| 57 | | { succeeded |
| 58 | |
| 59 | /** The API's callbacks for one attempt. Every call carries the attempt, and a 409 means the API moved on. */ |
| 60 | export class GhostCallbacks |
| 61 | constructor( |
| 62 | private readonly apiUrl |
| 63 | private readonly secret |
| 64 | private readonly importId |
| 65 | private readonly attempt |
| 66 | private readonly fetchImpl |
| 67 | private readonly sleep |
| 68 | ) {} |
| 69 | |
| 70 | heartbeat() { |
| 71 | return this.post |
| 72 | } |
| 73 | |
| 74 | releases(releases: |
| 75 | return this.post |
| 76 | } |
| 77 | |
| 78 | issues(issues: |
| 79 | return this.post |
| 80 | } |
| 81 | |
| 82 | comments(comments: |
| 83 | return this.post |
| 84 | } |
| 85 | |
| 86 | finish(outcome: |
| 87 | return this.post |
| 88 | } |
| 89 | |
| 90 | private async post |
| 91 | const url = |
| 92 | `/api/internal/imports/${this.importId |
| 93 | this.apiUrl, |
| 94 | ); |
| 95 | const payload = |
| 96 | |
| 97 | for (let attempt |
| 98 | const response = |
| 99 | method: "POST", |
| 100 | headers: { |
| 101 | "Content-Type": "application/json", |
| 102 | Authorization: `Bearer ${this |
| 103 | }, |
| 104 | body: payload, |
| 105 | signal: AbortSignal.timeout |
| 106 | }).catch((error |
| 107 | |
| 108 | if (response instanceof |
| 109 | if (response.ok |
| 110 | if (response.status |
| 111 | if (response.status |
| 112 | throw new Error( |
| 113 | `The API refused ${path} with ${ |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | const delay = |
| 119 | if (delay === |
| 120 | const reason = |
| 121 | response instanceof Response |
| 122 | ? `status ${response. |
| 123 | : response.message; |
| 124 | throw new Error( |
| 125 | } |
| 126 | await this.sleep |
| 127 | } |
| 128 | } |
| 129 | } |