* Sync webhook registration with current tracked projects.
(provider: ProviderType)
| 238 | * Sync webhook registration with current tracked projects. |
| 239 | */ |
| 240 | async syncWebhook(provider: ProviderType): Promise<void> { |
| 241 | const logger = useLogger(); |
| 242 | |
| 243 | logger.debug(`[webhook:${provider}] Starting webhook sync`); |
| 244 | |
| 245 | const config = await this.readConfig(provider); |
| 246 | |
| 247 | const projectsService = new ItemsService<DeploymentProject>('directus_deployment_projects', { |
| 248 | knex: this.knex, |
| 249 | schema: this.schema, |
| 250 | accountability: null, |
| 251 | }); |
| 252 | |
| 253 | const projects = await projectsService.readByQuery({ |
| 254 | filter: { deployment: { _eq: config.id } }, |
| 255 | limit: -1, |
| 256 | }); |
| 257 | |
| 258 | const projectExternalIds = projects.map((p) => p.external_id); |
| 259 | const driver = await this.getDriver(provider); |
| 260 | |
| 261 | // No projects → unregister webhooks if any exist |
| 262 | if (projectExternalIds.length === 0) { |
| 263 | if (config.webhook_ids && config.webhook_ids.length > 0) { |
| 264 | logger.debug(`[webhook:${provider}] No projects, unregistering ${config.webhook_ids.length} webhook(s)`); |
| 265 | |
| 266 | try { |
| 267 | await driver.unregisterWebhook(config.webhook_ids); |
| 268 | } catch (err) { |
| 269 | logger.warn(`[webhook:${provider}] Failed to unregister: ${err}`); |
| 270 | } |
| 271 | |
| 272 | await super.updateOne(config.id, { webhook_ids: null, webhook_secret: null } as Partial<DeploymentConfig>); |
| 273 | } |
| 274 | |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | // Unregister existing webhooks before re-registering |
| 279 | if (config.webhook_ids && config.webhook_ids.length > 0) { |
| 280 | logger.debug(`[webhook:${provider}] Unregistering ${config.webhook_ids.length} existing webhook(s)`); |
| 281 | |
| 282 | try { |
| 283 | await driver.unregisterWebhook(config.webhook_ids); |
| 284 | } catch (err) { |
| 285 | logger.warn(`[webhook:${provider}] Failed to unregister: ${err}`); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | const publicUrl = env['PUBLIC_URL'] as string; |
| 290 | const webhookUrl = `${publicUrl}/deployments/webhooks/${provider}`; |
| 291 | |
| 292 | logger.debug( |
| 293 | `[webhook:${provider}] Registering webhook → ${webhookUrl} for ${projectExternalIds.length} project(s)`, |
| 294 | ); |
| 295 | |
| 296 | const result = await driver.registerWebhook(webhookUrl, projectExternalIds); |
| 297 |
no test coverage detected