Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/helpers/migrationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
Expand Down