* Returns the caller's subscription for exactly this PR and event-type set, creating it if absent. * Returns undefined when the set does not exist and the caller is at the subscription limit: * creating an existing subscription succeeds even at the limit, so nothing exists to clean up.
(prUrl: string, types: readonly EventType[])
| 33 | * creating an existing subscription succeeds even at the limit, so nothing exists to clean up. |
| 34 | */ |
| 35 | async subscribe(prUrl: string, types: readonly EventType[]): Promise<Subscription | undefined> { |
| 36 | let body: Record<string, unknown>; |
| 37 | try { |
| 38 | body = record(await this.api<unknown>("POST", "/event-subscriptions", { |
| 39 | resource_url: prUrl, event_types: types, |
| 40 | })); |
| 41 | } catch (error) { |
| 42 | if (error instanceof ApiError && error.status === 422 && /Subscription limit exceeded/.test(error.message)) { |
| 43 | return undefined; |
| 44 | } |
| 45 | throw error; |
| 46 | } |
| 47 | if (typeof body.id !== "string" || !/^sub_[A-Za-z0-9]+$/.test(body.id) || body.resource_url !== prUrl) { |
| 48 | throw new Error("Subscription response is missing the ID or matching resource URL needed for safe cleanup"); |
| 49 | } |
| 50 | return { id: body.id, resource_url: body.resource_url, event_types: [...types] }; |
| 51 | } |
| 52 | |
| 53 | /** Lists every active subscription of the caller, following pagination links. */ |
| 54 | async subscriptions(): Promise<Subscription[]> { |
no test coverage detected