diff --git a/integrations/github/src/tasks.ts b/integrations/github/src/tasks.ts index 6001d72ee..ed3b84881 100644 --- a/integrations/github/src/tasks.ts +++ b/integrations/github/src/tasks.ts @@ -50,7 +50,7 @@ export async function handleImportDispatchForSpaces( context: GithubRuntimeContext, payload: IntegrationTaskImportSpaces['payload'] ): Promise { - const { configQuery, page, standaloneRef } = payload; + const { configQuery, page, standaloneRef, eventTimestamp } = payload; logger.debug(`handling import dispatch for spaces with payload: ${JSON.stringify(payload)}`); @@ -92,6 +92,7 @@ export async function handleImportDispatchForSpaces( ref: standaloneRef, } : undefined, + eventTimestamp, }); } catch (error) { logger.error( diff --git a/integrations/github/src/types.ts b/integrations/github/src/types.ts index 45732eb72..9a4131f32 100644 --- a/integrations/github/src/types.ts +++ b/integrations/github/src/types.ts @@ -93,6 +93,13 @@ export type IntegrationTaskImportSpaces = BaseIntegrationTask< configQuery: string; page?: string; standaloneRef?: string; + /** + * The timestamp of the event that triggers the export. + * + * This is to help ensures that Git sync import and export operations are executed + * in the same order on GitBook and on the remote repository. + */ + eventTimestamp?: Date; } >; diff --git a/integrations/github/src/webhooks.ts b/integrations/github/src/webhooks.ts index 9a8a82d1f..b85944e7a 100644 --- a/integrations/github/src/webhooks.ts +++ b/integrations/github/src/webhooks.ts @@ -65,6 +65,9 @@ export async function handlePushEvent( const total = await handleImportDispatchForSpaces(context, { configQuery: queryKey, + eventTimestamp: payload.head_commit?.timestamp + ? new Date(payload.head_commit?.timestamp) + : undefined, }); logger.debug(`${total} space configurations are affected`); diff --git a/integrations/gitlab/src/tasks.ts b/integrations/gitlab/src/tasks.ts index 163eaeb67..96298b167 100644 --- a/integrations/gitlab/src/tasks.ts +++ b/integrations/gitlab/src/tasks.ts @@ -50,7 +50,7 @@ export async function handleImportDispatchForSpaces( context: GitLabRuntimeContext, payload: IntegrationTaskImportSpaces['payload'] ): Promise { - const { configQuery, page, standaloneRef } = payload; + const { configQuery, page, standaloneRef, eventTimestamp } = payload; logger.debug(`handling import dispatch for spaces with payload: ${JSON.stringify(payload)}`); @@ -92,6 +92,7 @@ export async function handleImportDispatchForSpaces( ref: standaloneRef, } : undefined, + eventTimestamp, }); } catch (error) { logger.error( diff --git a/integrations/gitlab/src/types.ts b/integrations/gitlab/src/types.ts index fa7a18507..b1414bc9d 100644 --- a/integrations/gitlab/src/types.ts +++ b/integrations/gitlab/src/types.ts @@ -87,6 +87,13 @@ export type IntegrationTaskImportSpaces = BaseIntegrationTask< configQuery: string; page?: string; standaloneRef?: string; + /** + * The timestamp of the event that triggers the export. + * + * This is to help ensures that Git sync import and export operations are executed + * in the same order on GitBook and on the remote repository. + */ + eventTimestamp?: Date; } >; diff --git a/integrations/gitlab/src/webhooks.ts b/integrations/gitlab/src/webhooks.ts index e2d3e240c..8e2345898 100644 --- a/integrations/gitlab/src/webhooks.ts +++ b/integrations/gitlab/src/webhooks.ts @@ -103,8 +103,15 @@ export async function handlePushEvent(context: GitLabRuntimeContext, payload: Gi const queryKey = computeConfigQueryKey(gitlabProjectId, gitlabRef); + // Gitlab push events do not include a head_commit property so we need to get it from + // the commits attribute which should contains the newest 20 commits: + // https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#push-events + const headCommitSha = payload.after; + const headCommit = payload.commits.find((commit) => commit.id === headCommitSha); + const total = await handleImportDispatchForSpaces(context, { configQuery: queryKey, + eventTimestamp: headCommit ? new Date(headCommit.timestamp) : undefined, }); logger.debug(`${total} space configurations are affected`);