Skip to content

Commit 41b5fe7

Browse files
authored
fix: handle installation events in migrate webhook (#332)
The webhook was only handling installation_repositories events but when a user first installs the app with repos selected, GitHub sends an installation event with action=created instead. Now handles both: - installation (action: created) with repositories array - installation_repositories (action: added) with repositories_added array
1 parent 925da4d commit 41b5fe7

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

src/helpers/migrationHandler.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,46 @@ const DOCUMENTATION_REPO = 'worlddriven/documentation';
1515
const TARGET_ORG = 'worlddriven';
1616

1717
/**
18-
* Handle installation_repositories webhook from worlddriven-migrate app
18+
* Handle installation or installation_repositories webhook from worlddriven-migrate app
1919
* @param {object} payload - Webhook payload
20+
* @param {string} eventType - The GitHub event type
2021
*/
21-
export async function handleMigrateInstallationWebhook(payload) {
22-
const { action, repositories_added, installation } = payload;
23-
24-
if (action !== 'added' || !repositories_added?.length) {
25-
console.log('[Migration] Ignoring non-add action or empty repositories');
22+
export async function handleMigrateInstallationWebhook(payload, eventType) {
23+
const { action, repositories_added, repositories, installation } = payload;
24+
25+
// Handle both event types:
26+
// - installation (action: created) -> repositories array
27+
// - installation_repositories (action: added) -> repositories_added array
28+
let repos = [];
29+
30+
if (
31+
eventType === 'installation' &&
32+
action === 'created' &&
33+
repositories?.length
34+
) {
35+
repos = repositories;
36+
console.log(
37+
`[Migration] New installation with ${repos.length} repository(ies)`
38+
);
39+
} else if (
40+
eventType === 'installation_repositories' &&
41+
action === 'added' &&
42+
repositories_added?.length
43+
) {
44+
repos = repositories_added;
45+
console.log(
46+
`[Migration] Repositories added to existing installation: ${repos.length}`
47+
);
48+
} else {
49+
console.log(
50+
`[Migration] Ignoring: event=${eventType}, action=${action}, repos=${repositories?.length || repositories_added?.length || 0}`
51+
);
2652
return { info: 'No action needed' };
2753
}
2854

29-
console.log(
30-
`[Migration] App installed on ${repositories_added.length} repository(ies)`
31-
);
32-
3355
const results = [];
3456

35-
for (const repo of repositories_added) {
57+
for (const repo of repos) {
3658
const repoFullName = repo.full_name;
3759
console.log(`[Migration] Processing: ${repoFullName}`);
3860

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,11 @@ async function startServer() {
751751
// Process webhook asynchronously
752752
(async () => {
753753
try {
754-
if (eventType === 'installation_repositories') {
755-
await handleMigrateInstallationWebhook(data);
754+
if (
755+
eventType === 'installation_repositories' ||
756+
eventType === 'installation'
757+
) {
758+
await handleMigrateInstallationWebhook(data, eventType);
756759
console.log(
757760
`[Migration] Webhook ${eventType} (${deliveryId}) processed successfully`
758761
);

0 commit comments

Comments
 (0)