| 1 | import { PermanentImportError } from "./errors.ts"; |
| 2 | |
| 3 | const API = " |
| 4 | const REQUEST_TIMEOUT_MS = 30_000 |
| 5 | // Longer than this and the import is better retried later than held open. |
| 6 | const MAX_RATE_LIMIT_WAIT_MS = 15 |
| 7 | |
| 8 | export type GitHubUser |
| 9 | |
| 10 | export type GitHubLabel |
| 11 | name: string |
| 12 | color: string |
| 13 | description: string |
| 14 | }; |
| 15 | |
| 16 | export type GitHubRepository |
| 17 | |
| 18 | export type GitHubRelease |
| 19 | tag_name: string |
| 20 | name: string |
| 21 | body: string |
| 22 | draft: boolean |
| 23 | prerelease: boolean |
| 24 | created_at: string |
| 25 | published_at: string |
| 26 | }; |
| 27 | |
| 28 | export type GitHubIssue |
| 29 | number: number |
| 30 | title: string |
| 31 | body: string |
| 32 | user: GitHubUser |
| 33 | state: "open |
| 34 | labels: GitHubLabel |
| 35 | created_at: string |
| 36 | updated_at: string |
| 37 | closed_at: string |
| 38 | pull_request?: unknown |
| 39 | }; |
| 40 | |
| 41 | export type GitHubPull |
| 42 | number: number |
| 43 | state: "open |
| 44 | draft: boolean |
| 45 | merged_at: string |
| 46 | merge_commit_sha: string |
| 47 | base: { |
| 48 | head: { |
| 49 | }; |
| 50 | |
| 51 | export type GitHubComment |
| 52 | id: number |
| 53 | issue_url: string |
| 54 | user: GitHubUser |
| 55 | body: string |
| 56 | created_at: string |
| 57 | updated_at: string |
| 58 | }; |
| 59 | |
| 60 | export type Wait |
| 61 | |
| 62 | export const wait: |
| 63 | new Promise((resolve |
| 64 | if (signal.aborted |
| 65 | const timer = |
| 66 | signal.addEventListener( |
| 67 | "abort", |
| 68 | () => { |
| 69 | clearTimeout(timer); |
| 70 | reject(signal.reason |
| 71 | }, |
| 72 | { once: true }, |
| 73 | ); |
| 74 | }); |
| 75 | |
| 76 | export class GitHubClient |
| 77 | constructor( |
| 78 | private readonly token |
| 79 | private readonly signal |
| 80 | private readonly fetchImpl |
| 81 | private readonly sleep |
| 82 | ) {} |
| 83 | |
| 84 | async get<T |
| 85 | const response = |
| 86 | return (await response |
| 87 | } |
| 88 | |
| 89 | /** Follows `Link: rel="next"` and yields one page at a time, so a repository with fifty thousand issues is never held in memory at once. */ |
| 90 | async *pages<T |
| 91 | let next: |
| 92 | next.searchParams.set |
| 93 | while (next) { |
| 94 | const response = |
| 95 | yield (await response |
| 96 | next = nextLink( |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private async request |
| 101 | for (;;) { |
| 102 | const response = |
| 103 | headers: { |
| 104 | Accept: "application/vnd.github+json", |
| 105 | Authorization: `Bearer ${this |
| 106 | "X-GitHub-Api-Version": "2022-11-28", |
| 107 | }, |
| 108 | signal: AbortSignal.any |
| 109 | this.signal, |
| 110 | AbortSignal.timeout(REQUEST_TIMEOUT_MS |
| 111 | ]), |
| 112 | }); |
| 113 | if (response.ok |
| 114 | |
| 115 | const delay = |
| 116 | if (delay !== |
| 117 | if (delay > |
| 118 | throw new Error( |
| 119 | `GitHub rate limit reached, it resets in ${Math.ceil |
| 120 | ); |
| 121 | } |
| 122 | await this.sleep |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | const detail = |
| 127 | if ( |
| 128 | response.status === |
| 129 | response.status === |
| 130 | response.status === |
| 131 | ) { |
| 132 | throw new PermanentImportError( |
| 133 | } |
| 134 | throw new Error( |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** How long GitHub asks to be left alone, or null when the response is not a rate limit. */ |
| 140 | export function rateLimitDelay(response |
| 141 | if (response.status |
| 142 | const retryAfter = |
| 143 | if (retryAfter) return |
| 144 | if (response.headers |
| 145 | const reset = |
| 146 | return Math.max |
| 147 | } |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | export function nextLink(header |
| 152 | const match = |
| 153 | return match ? |
| 154 | } |
| 155 | |
| 156 | /** Comments name their issue only by URL. */ |
| 157 | export function issueNumberOf(issueUrl |
| 158 | return Number(issueUrl |
| 159 | } |
| 160 | |
| 161 | /** GitHub shows a deleted account as `ghost`, and does the same here. */ |
| 162 | export function loginOf(user |
| 163 | return user?.login |
| 164 | } |