diff --git a/src/helpers/migrationHandler.js b/src/helpers/migrationHandler.js index e985f57..aacb4e6 100644 --- a/src/helpers/migrationHandler.js +++ b/src/helpers/migrationHandler.js @@ -15,24 +15,46 @@ const DOCUMENTATION_REPO = 'worlddriven/documentation'; const TARGET_ORG = 'worlddriven'; /** - * Handle installation_repositories webhook from worlddriven-migrate app + * Handle installation or installation_repositories webhook from worlddriven-migrate app * @param {object} payload - Webhook payload + * @param {string} eventType - The GitHub event type */ -export async function handleMigrateInstallationWebhook(payload) { - const { action, repositories_added, installation } = payload; - - if (action !== 'added' || !repositories_added?.length) { - console.log('[Migration] Ignoring non-add action or empty repositories'); +export async function handleMigrateInstallationWebhook(payload, eventType) { + const { action, repositories_added, repositories, installation } = payload; + + // Handle both event types: + // - installation (action: created) -> repositories array + // - installation_repositories (action: added) -> repositories_added array + let repos = []; + + if ( + eventType === 'installation' && + action === 'created' && + repositories?.length + ) { + repos = repositories; + console.log( + `[Migration] New installation with ${repos.length} repository(ies)` + ); + } else if ( + eventType === 'installation_repositories' && + action === 'added' && + repositories_added?.length + ) { + repos = repositories_added; + console.log( + `[Migration] Repositories added to existing installation: ${repos.length}` + ); + } else { + console.log( + `[Migration] Ignoring: event=${eventType}, action=${action}, repos=${repositories?.length || repositories_added?.length || 0}` + ); return { info: 'No action needed' }; } - console.log( - `[Migration] App installed on ${repositories_added.length} repository(ies)` - ); - const results = []; - for (const repo of repositories_added) { + for (const repo of repos) { const repoFullName = repo.full_name; console.log(`[Migration] Processing: ${repoFullName}`); diff --git a/src/index.js b/src/index.js index 88ca28c..7a72685 100644 --- a/src/index.js +++ b/src/index.js @@ -751,8 +751,11 @@ async function startServer() { // Process webhook asynchronously (async () => { try { - if (eventType === 'installation_repositories') { - await handleMigrateInstallationWebhook(data); + if ( + eventType === 'installation_repositories' || + eventType === 'installation' + ) { + await handleMigrateInstallationWebhook(data, eventType); console.log( `[Migration] Webhook ${eventType} (${deliveryId}) processed successfully` );