(ctx: SubscriptionContext)
| 136 | }, |
| 137 | |
| 138 | async createSubscription(ctx: SubscriptionContext): Promise<SubscriptionResult | undefined> { |
| 139 | try { |
| 140 | const providerConfig = getProviderConfig(ctx.webhook) |
| 141 | const { apiKey, triggerId } = providerConfig as { |
| 142 | apiKey?: string |
| 143 | triggerId?: string |
| 144 | } |
| 145 | |
| 146 | if (!apiKey) { |
| 147 | throw new Error( |
| 148 | 'Rootly API key is required. Please provide your Rootly API key in the trigger configuration.' |
| 149 | ) |
| 150 | } |
| 151 | |
| 152 | const { rootlyEventTypesForTrigger } = await import('@/triggers/rootly/utils') |
| 153 | const eventTypes = rootlyEventTypesForTrigger(triggerId) |
| 154 | const notificationUrl = getNotificationUrl(ctx.webhook) |
| 155 | |
| 156 | const signingSecret = generateId() |
| 157 | |
| 158 | logger.info(`[${ctx.requestId}] Creating Rootly webhook endpoint`, { |
| 159 | triggerId, |
| 160 | eventTypes, |
| 161 | webhookId: ctx.webhook.id, |
| 162 | }) |
| 163 | |
| 164 | const requestBody = { |
| 165 | data: { |
| 166 | type: 'webhooks_endpoints', |
| 167 | attributes: { |
| 168 | name: `Sim (${triggerId || 'rootly'})`, |
| 169 | url: notificationUrl, |
| 170 | secret: signingSecret, |
| 171 | event_types: eventTypes, |
| 172 | enabled: true, |
| 173 | }, |
| 174 | }, |
| 175 | } |
| 176 | |
| 177 | const response = await fetch('https://api.rootly.com/v1/webhooks/endpoints', { |
| 178 | method: 'POST', |
| 179 | headers: { |
| 180 | Authorization: `Bearer ${apiKey}`, |
| 181 | 'Content-Type': 'application/vnd.api+json', |
| 182 | Accept: 'application/vnd.api+json', |
| 183 | }, |
| 184 | body: JSON.stringify(requestBody), |
| 185 | }) |
| 186 | |
| 187 | const responseBody = (await response.json().catch(() => ({}))) as Record<string, unknown> |
| 188 | |
| 189 | if (!response.ok) { |
| 190 | const errors = responseBody.errors as Array<Record<string, unknown>> | undefined |
| 191 | const errorDetail = |
| 192 | (errors?.[0]?.detail as string | undefined) || |
| 193 | (errors?.[0]?.title as string | undefined) || |
| 194 | (responseBody.error as string | undefined) |
| 195 |
nothing calls this directly
no test coverage detected