|
| 1 | +import { WebhookSchema, type Webhook } from '../models/webhook'; |
| 2 | +import { WebhookStatisticsSchema, type WebhookStatistics } from '../models/webhookStatistics'; |
| 3 | +import { |
| 4 | + GetWebhookStatisticsSummarySchema, |
| 5 | + type GetWebhookStatisticsSummary, |
| 6 | +} from '../models/getWebhookStatisticsSummary'; |
| 7 | +import type { GetWebhooks } from '../parameters/getWebhooks'; |
| 8 | +import type { CreateWebhook } from '../parameters/createWebhook'; |
| 9 | +import type { GetWebhook } from '../parameters/getWebhook'; |
| 10 | +import type { UpdateWebhook } from '../parameters/updateWebhook'; |
| 11 | +import type { DeleteWebhook } from '../parameters/deleteWebhook'; |
| 12 | +import type { GetWebhookStatistics } from '../parameters/getWebhookStatistics'; |
| 13 | +import type { GetWebhookStatisticsSummary as GetWebhookStatisticsSummaryParameters } from '../parameters/getWebhookStatisticsSummary'; |
| 14 | +import type { GetWebhookTransitions } from '../parameters/getWebhookTransitions'; |
| 15 | +import type { GetLatestWebhookInvocation } from '../parameters/getLatestWebhookInvocation'; |
| 16 | +import type { Client, SendRequestOptions } from '#/core'; |
| 17 | +import { z } from 'zod'; |
| 18 | + |
| 19 | +/** Returns the webhooks registered in this instance. Requires administrator permission. */ |
| 20 | +export async function getWebhooks(client: Client, parameters?: GetWebhooks): Promise<Webhook[]> { |
| 21 | + const config: SendRequestOptions<Webhook[]> = { |
| 22 | + url: '/rest/jira-webhook/1.0/webhooks', |
| 23 | + method: 'GET', |
| 24 | + searchParams: { |
| 25 | + event: parameters?.event, |
| 26 | + statistics: parameters?.statistics, |
| 27 | + start: parameters?.start, |
| 28 | + limit: parameters?.limit, |
| 29 | + }, |
| 30 | + schema: z.array(WebhookSchema), |
| 31 | + }; |
| 32 | + |
| 33 | + return await client.sendRequest(config); |
| 34 | +} |
| 35 | + |
| 36 | +/** Registers a webhook. Requires administrator permission. */ |
| 37 | +export async function createWebhook(client: Client, parameters: CreateWebhook): Promise<Webhook> { |
| 38 | + const config: SendRequestOptions<Webhook> = { |
| 39 | + url: '/rest/jira-webhook/1.0/webhooks', |
| 40 | + method: 'POST', |
| 41 | + body: { |
| 42 | + name: parameters.name, |
| 43 | + url: parameters.url, |
| 44 | + events: parameters.events, |
| 45 | + filters: parameters.filters, |
| 46 | + excludeBody: parameters.excludeBody, |
| 47 | + configuration: parameters.configuration, |
| 48 | + sslVerificationRequired: parameters.sslVerificationRequired, |
| 49 | + }, |
| 50 | + schema: WebhookSchema, |
| 51 | + }; |
| 52 | + |
| 53 | + return await client.sendRequest(config); |
| 54 | +} |
| 55 | + |
| 56 | +/** Returns a registered webhook. Requires administrator permission. */ |
| 57 | +export async function getWebhook(client: Client, parameters: GetWebhook): Promise<Webhook> { |
| 58 | + const config: SendRequestOptions<Webhook> = { |
| 59 | + url: `/rest/jira-webhook/1.0/webhooks/${parameters.webhookId}`, |
| 60 | + method: 'GET', |
| 61 | + schema: WebhookSchema, |
| 62 | + }; |
| 63 | + |
| 64 | + return await client.sendRequest(config); |
| 65 | +} |
| 66 | + |
| 67 | +/** Replaces a registered webhook. Requires administrator permission. */ |
| 68 | +export async function updateWebhook(client: Client, parameters: UpdateWebhook): Promise<Webhook> { |
| 69 | + const config: SendRequestOptions<Webhook> = { |
| 70 | + url: `/rest/jira-webhook/1.0/webhooks/${parameters.webhookId}`, |
| 71 | + method: 'PUT', |
| 72 | + body: { |
| 73 | + name: parameters.name, |
| 74 | + url: parameters.url, |
| 75 | + events: parameters.events, |
| 76 | + filters: parameters.filters, |
| 77 | + excludeBody: parameters.excludeBody, |
| 78 | + configuration: parameters.configuration, |
| 79 | + sslVerificationRequired: parameters.sslVerificationRequired, |
| 80 | + }, |
| 81 | + schema: WebhookSchema, |
| 82 | + }; |
| 83 | + |
| 84 | + return await client.sendRequest(config); |
| 85 | +} |
| 86 | + |
| 87 | +/** Unregisters a webhook. Requires administrator permission. */ |
| 88 | +export async function deleteWebhook(client: Client, parameters: DeleteWebhook): Promise<void> { |
| 89 | + const config: SendRequestOptions<void> = { |
| 90 | + url: `/rest/jira-webhook/1.0/webhooks/${parameters.webhookId}`, |
| 91 | + method: 'DELETE', |
| 92 | + }; |
| 93 | + |
| 94 | + return await client.sendRequest(config); |
| 95 | +} |
| 96 | + |
| 97 | +/** Returns how a webhook has been delivering. Requires administrator permission. */ |
| 98 | +export async function getWebhookStatistics( |
| 99 | + client: Client, |
| 100 | + parameters: GetWebhookStatistics, |
| 101 | +): Promise<WebhookStatistics> { |
| 102 | + const config: SendRequestOptions<WebhookStatistics> = { |
| 103 | + url: `/rest/jira-webhook/1.0/webhooks/${parameters.webhookId}/statistics`, |
| 104 | + method: 'GET', |
| 105 | + schema: WebhookStatisticsSchema, |
| 106 | + }; |
| 107 | + |
| 108 | + return await client.sendRequest(config); |
| 109 | +} |
| 110 | + |
| 111 | +/** Returns the delivery statistics of a webhook, one entry per event it delivers. Requires administrator permission. */ |
| 112 | +export async function getWebhookStatisticsSummary( |
| 113 | + client: Client, |
| 114 | + parameters: GetWebhookStatisticsSummaryParameters, |
| 115 | +): Promise<GetWebhookStatisticsSummary> { |
| 116 | + const config: SendRequestOptions<GetWebhookStatisticsSummary> = { |
| 117 | + url: `/rest/jira-webhook/1.0/webhooks/${parameters.webhookId}/statistics/summary`, |
| 118 | + method: 'GET', |
| 119 | + schema: GetWebhookStatisticsSummarySchema, |
| 120 | + }; |
| 121 | + |
| 122 | + return await client.sendRequest(config); |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Returns the transitions a webhook has been through. Requires administrator permission. The shape of an entry is not |
| 127 | + * described here: an instance that has never delivered a webhook answers with an empty list, and guessing what a |
| 128 | + * populated one holds would be worse than leaving it to the caller. |
| 129 | + */ |
| 130 | +export async function getWebhookTransitions(client: Client, parameters: GetWebhookTransitions): Promise<unknown> { |
| 131 | + const config: SendRequestOptions<unknown> = { |
| 132 | + url: `/rest/jira-webhook/1.0/webhooks/${parameters.webhookId}/transitions`, |
| 133 | + method: 'GET', |
| 134 | + }; |
| 135 | + |
| 136 | + return await client.sendRequest(config); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Returns the most recent delivery of a webhook. Requires administrator permission. Until the webhook has been |
| 141 | + * delivered once Jira answers 204 and this resolves to `undefined`; the 204 is deliberately not declared, because |
| 142 | + * declaring it is what makes the whole call type as `void` and hides the body that does arrive. |
| 143 | + */ |
| 144 | +export async function getLatestWebhookInvocation( |
| 145 | + client: Client, |
| 146 | + parameters: GetLatestWebhookInvocation, |
| 147 | +): Promise<unknown> { |
| 148 | + const config: SendRequestOptions<unknown> = { |
| 149 | + url: `/rest/jira-webhook/1.0/webhooks/${parameters.webhookId}/latest`, |
| 150 | + method: 'GET', |
| 151 | + }; |
| 152 | + |
| 153 | + return await client.sendRequest(config); |
| 154 | +} |
0 commit comments