-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add status notifier adapters #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import type { GitHubPullRequestStatus } from "../github/provider.js"; | ||
|
|
||
| export type StatusNotifierErrorCode = "invalid_notifier" | "delivery_failed"; | ||
|
|
||
| export class StatusNotifierError extends Error { | ||
| readonly code: StatusNotifierErrorCode; | ||
| readonly details?: unknown; | ||
|
|
||
| constructor(code: StatusNotifierErrorCode, message: string, details?: unknown) { | ||
| super(message); | ||
| this.name = "StatusNotifierError"; | ||
| this.code = code; | ||
| this.details = details; | ||
| } | ||
| } | ||
|
|
||
| export interface PullRequestStatusNotificationEvent { | ||
| event_kind: "pull_request_status"; | ||
| emitted_at: string; | ||
| repository?: string; | ||
| pull_request: GitHubPullRequestStatus; | ||
| } | ||
|
|
||
| export type StatusNotificationEvent = PullRequestStatusNotificationEvent; | ||
|
|
||
| export interface StatusNotifier { | ||
| adapter_id: string; | ||
| notify(event: StatusNotificationEvent): Promise<void>; | ||
| } | ||
|
|
||
| export interface StatusNotificationDelivery { | ||
| adapter_id: string; | ||
| delivery_status: "delivered" | "failed"; | ||
| message: string; | ||
| } | ||
|
|
||
| export interface EmitStatusNotificationInput { | ||
| pull_request: GitHubPullRequestStatus; | ||
| repository?: string; | ||
| emitted_at?: Date; | ||
| notifiers: StatusNotifier[]; | ||
| } | ||
|
|
||
| type FetchLike = (input: string, init?: { | ||
| method?: string; | ||
| headers?: Record<string, string>; | ||
| body?: string; | ||
| }) => Promise<{ ok: boolean; status: number }>; | ||
|
|
||
| export function createWebhookStatusNotifier(input: { | ||
| webhook_url: string; | ||
| adapter_id?: string; | ||
| fetch?: FetchLike; | ||
| }): StatusNotifier { | ||
| const webhookUrl = normalizeWebhookUrl(input.webhook_url); | ||
| const adapterId = normalizeAdapterId(input.adapter_id ?? "webhook"); | ||
| const fetchImpl = input.fetch ?? resolveGlobalFetch(); | ||
|
|
||
| return { | ||
| adapter_id: adapterId, | ||
| async notify(event) { | ||
| try { | ||
| const response = await fetchImpl(webhookUrl, { | ||
| method: "POST", | ||
| headers: { | ||
| "content-type": "application/json", | ||
| "x-specforge-event-kind": event.event_kind | ||
| }, | ||
| body: JSON.stringify(event) | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new StatusNotifierError( | ||
| "delivery_failed", | ||
| `Webhook delivery failed with HTTP ${response.status}.` | ||
| ); | ||
| } | ||
iKwesi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| if (error instanceof StatusNotifierError) { | ||
| throw error; | ||
| } | ||
|
|
||
| throw new StatusNotifierError( | ||
| "delivery_failed", | ||
| error instanceof Error ? error.message : String(error), | ||
| error | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| export async function emitStatusNotification( | ||
| input: EmitStatusNotificationInput | ||
| ): Promise<StatusNotificationDelivery[]> { | ||
| const event: StatusNotificationEvent = { | ||
| event_kind: "pull_request_status", | ||
| emitted_at: (input.emitted_at ?? new Date()).toISOString(), | ||
| ...(input.repository ? { repository: input.repository } : {}), | ||
| pull_request: input.pull_request | ||
| }; | ||
|
|
||
| return Promise.all( | ||
| input.notifiers.map(async (notifier) => { | ||
| try { | ||
| await notifier.notify(event); | ||
| return { | ||
| adapter_id: notifier.adapter_id, | ||
| delivery_status: "delivered", | ||
| message: "Status event delivered." | ||
| } as StatusNotificationDelivery; | ||
| } catch (error) { | ||
| return { | ||
| adapter_id: notifier.adapter_id, | ||
| delivery_status: "failed", | ||
| message: error instanceof Error ? error.message : String(error) | ||
| } as StatusNotificationDelivery; | ||
iKwesi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| function normalizeWebhookUrl(value: string): string { | ||
| if (typeof value !== "string" || value.trim().length === 0) { | ||
| throw new StatusNotifierError( | ||
| "invalid_notifier", | ||
| "webhook_url must be a non-empty http(s) URL." | ||
| ); | ||
| } | ||
| const trimmed = value.trim(); | ||
|
|
||
| let url: URL; | ||
| try { | ||
| url = new URL(trimmed); | ||
| } catch (error) { | ||
| throw new StatusNotifierError( | ||
| "invalid_notifier", | ||
| "webhook_url must be a valid http(s) URL.", | ||
| error | ||
| ); | ||
| } | ||
|
|
||
| if (url.protocol !== "http:" && url.protocol !== "https:") { | ||
| throw new StatusNotifierError( | ||
| "invalid_notifier", | ||
| "webhook_url must use http or https." | ||
| ); | ||
| } | ||
|
|
||
| return url.toString(); | ||
| } | ||
|
|
||
| function normalizeAdapterId(value: unknown): string { | ||
| if (typeof value !== "string" || value.trim().length === 0) { | ||
| throw new StatusNotifierError( | ||
| "invalid_notifier", | ||
| "adapter_id must be a non-empty string.", | ||
| { adapter_id: value } | ||
| ); | ||
| } | ||
|
|
||
| return value.trim(); | ||
| } | ||
|
|
||
| function resolveGlobalFetch(): FetchLike { | ||
| if (typeof globalThis.fetch !== "function") { | ||
| throw new StatusNotifierError( | ||
| "invalid_notifier", | ||
| "Global fetch is unavailable; provide a fetch implementation explicitly." | ||
| ); | ||
| } | ||
|
|
||
| return async (input, init) => { | ||
| const response = await globalThis.fetch(input, init); | ||
| return { | ||
| ok: response.ok, | ||
| status: response.status | ||
| }; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.