| 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 | githubId: number |
| 49 | issueNumber: number |
| 50 | authorLogin: string |
| 51 | body: string |
| 52 | createdAt: string |
| 53 | updatedAt: string |
| 54 | }; |
| 55 | |
| 56 | export type Outcome |
| 57 | | { succeeded |
| 58 | | { succeeded |
| 59 | |
| 60 | /** Postgres cannot store U+0000 in text, and GitHub text can carry it, such as a pasted error message about a binary file. */ |
| 61 | function withoutNul(_key: |
| 62 | return typeof value |
| 63 | } |
| 64 | |
| 65 | /** The API's callbacks for one attempt. Every call carries the attempt, and a 409 means the API moved on. */ |
| 66 | export class GhostCallbacks |
| 67 | constructor( |
| 68 | private readonly apiUrl |
| 69 | private readonly secret |
| 70 | private readonly importId |
| 71 | private readonly attempt |
| 72 | private readonly fetchImpl |
| 73 | private readonly sleep |
| 74 | ) {} |
| 75 | |
| 76 | heartbeat() { |
| 77 | return this.post |
| 78 | } |
| 79 | |
| 80 | releases(releases: |
| 81 | return this.post |
| 82 | } |
| 83 | |
| 84 | issues(issues: |
| 85 | return this.post |
| 86 | } |
| 87 | |
| 88 | comments(comments: |
| 89 | return this.post |
| 90 | } |
| 91 | |
| 92 | finish(outcome: |
| 93 | return this.post |
| 94 | } |
| 95 | |
| 96 | private async post |
| 97 | const url = |
| 98 | `/api/internal/imports/${this.importId |
| 99 | this.apiUrl, |
| 100 | ); |
| 101 | const payload = |
| 102 | { attempt: this. |
| 103 | withoutNul, |
| 104 | ); |
| 105 | |
| 106 | for (let attempt |
| 107 | const response = |
| 108 | method: "POST", |
| 109 | headers: { |
| 110 | "Content-Type": "application/json", |
| 111 | Authorization: `Bearer ${this |
| 112 | }, |
| 113 | body: payload, |
| 114 | signal: AbortSignal.timeout |
| 115 | }).catch((error |
| 116 | |
| 117 | if (response instanceof |
| 118 | if (response.ok |
| 119 | if (response.status |
| 120 | if (response.status |
| 121 | throw new Error( |
| 122 | `The API refused ${path} with ${ |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | const delay = |
| 128 | if (delay === |
| 129 | const reason = |
| 130 | response instanceof Response |
| 131 | ? `status ${response. |
| 132 | : response.message; |
| 133 | throw new Error( |
| 134 | } |
| 135 | await this.sleep |
| 136 | } |
| 137 | } |
| 138 | } |