Skip to content

Commit

Permalink
Send event timestamp when triggering Git sync imports and exports (#335)
Browse files Browse the repository at this point in the history
* Update triggerImport & triggerExport to pass the event timestamp to the API request

* Pass revision created date as event timestamp when trigger export

* Pass head commit timestamp on push as event timestamp when trigger github import

* Pass head pr timestamp on pr webhooks when triggering github import

* Pass head mr timestamp on mr webhooks when triggering gitlab import

* Pass head commit timestamp on push event when triggering gitlab import

* Review: rename eventCreatedAt to eventTimestamp

* Do not send event timestamp for imports of merge/pull requests previews

* Update after merge

* Slighly safer checks
  • Loading branch information
spastorelli authored Jan 5, 2024
1 parent a7a3c37 commit 057569a
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 9 deletions.
4 changes: 3 additions & 1 deletion integrations/github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ const handleSpaceContentUpdated: EventCallback<
return;
}

await triggerExport(context, spaceInstallation);
await triggerExport(context, spaceInstallation, {
eventTimestamp: new Date(revision.createdAt),
});
};

/*
Expand Down
22 changes: 20 additions & 2 deletions integrations/github/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ export async function triggerImport(

/** Whether the git info should be updated on the space */
updateGitInfo?: boolean;

/**
* The timestamp of the event that triggers the import.
*
* 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;
} = {}
) {
const { api } = context;
const { force = false, updateGitInfo = false, standalone } = options;
const { force = false, updateGitInfo = false, standalone, eventTimestamp } = options;

const config = getSpaceConfigOrThrow(spaceInstallation);
assertIsDefined(config.branch, { label: 'config.branch' });
Expand All @@ -68,6 +76,7 @@ export async function triggerImport(
repoProjectDirectory: config.projectDirectory,
repoCacheID: config.key,
force,
timestamp: eventTimestamp && !force ? eventTimestamp.toISOString() : undefined,
standalone: !!standalone,
...(updateGitInfo ? { gitInfo: { provider: 'github', url: repoURL } } : {}),
});
Expand All @@ -85,10 +94,18 @@ export async function triggerExport(

/** Whether the git info should be updated on the space */
updateGitInfo?: boolean;

/**
* 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;
} = {}
) {
const { api } = context;
const { force = false, updateGitInfo = false } = options;
const { force = false, updateGitInfo = false, eventTimestamp } = options;

const config = getSpaceConfigOrThrow(spaceInstallation);
assertIsDefined(config.branch, { label: 'config.branch' });
Expand All @@ -112,6 +129,7 @@ export async function triggerExport(
repoProjectDirectory: config.projectDirectory,
repoCacheID: config.key,
force,
timestamp: eventTimestamp && !force ? eventTimestamp.toISOString() : undefined,
commitMessage: getCommitMessageForRevision(config, revision),
...(updateGitInfo ? { gitInfo: { provider: 'github', url: repoURL } } : {}),
});
Expand Down
3 changes: 2 additions & 1 deletion integrations/github/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function handleImportDispatchForSpaces(
context: GithubRuntimeContext,
payload: IntegrationTaskImportSpaces['payload']
): Promise<number | undefined> {
const { configQuery, page, standaloneRef } = payload;
const { configQuery, page, standaloneRef, eventTimestamp } = payload;

logger.debug(`handling import dispatch for spaces with payload: ${JSON.stringify(payload)}`);

Expand Down Expand Up @@ -92,6 +92,7 @@ export async function handleImportDispatchForSpaces(
ref: standaloneRef,
}
: undefined,
eventTimestamp,
});
} catch (error) {
logger.error(
Expand Down
7 changes: 7 additions & 0 deletions integrations/github/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
>;

Expand Down
3 changes: 3 additions & 0 deletions integrations/github/src/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
4 changes: 3 additions & 1 deletion integrations/gitlab/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ const handleSpaceContentUpdated: EventCallback<
return;
}

await triggerExport(context, spaceInstallation);
await triggerExport(context, spaceInstallation, {
eventTimestamp: new Date(revision.createdAt),
});
};

/*
Expand Down
22 changes: 20 additions & 2 deletions integrations/gitlab/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ export async function triggerImport(

/** Whether the git info should be updated on the space */
updateGitInfo?: boolean;

/**
* The timestamp of the event that triggers the import.
*
* 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;
} = {}
) {
const { api } = context;
const { force = false, updateGitInfo = false, standalone } = options;
const { force = false, updateGitInfo = false, standalone, eventTimestamp } = options;

const config = getSpaceConfigOrThrow(spaceInstallation);
assertIsDefined(config.branch, { label: 'config.branch' });
Expand All @@ -68,6 +76,7 @@ export async function triggerImport(
repoProjectDirectory: config.projectDirectory,
repoCacheID: config.key,
force,
timestamp: eventTimestamp && !force ? eventTimestamp.toISOString() : undefined,
standalone: !!standalone,
...(updateGitInfo ? { gitInfo: { provider: 'gitlab', url: repoURL } } : {}),
});
Expand All @@ -85,10 +94,18 @@ export async function triggerExport(

/** Whether the git info should be updated on the space */
updateGitInfo?: boolean;

/**
* 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;
} = {}
) {
const { api } = context;
const { force = false, updateGitInfo = false } = options;
const { force = false, updateGitInfo = false, eventTimestamp } = options;

const config = getSpaceConfigOrThrow(spaceInstallation);
assertIsDefined(config.branch, { label: 'config.branch' });
Expand All @@ -112,6 +129,7 @@ export async function triggerExport(
repoProjectDirectory: config.projectDirectory,
repoCacheID: config.key,
force,
timestamp: eventTimestamp && !force ? eventTimestamp.toISOString() : undefined,
commitMessage: getCommitMessageForRevision(config, revision),
...(updateGitInfo ? { gitInfo: { provider: 'gitlab', url: repoURL } } : {}),
});
Expand Down
3 changes: 2 additions & 1 deletion integrations/gitlab/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function handleImportDispatchForSpaces(
context: GitLabRuntimeContext,
payload: IntegrationTaskImportSpaces['payload']
): Promise<number | undefined> {
const { configQuery, page, standaloneRef } = payload;
const { configQuery, page, standaloneRef, eventTimestamp } = payload;

logger.debug(`handling import dispatch for spaces with payload: ${JSON.stringify(payload)}`);

Expand Down Expand Up @@ -92,6 +92,7 @@ export async function handleImportDispatchForSpaces(
ref: standaloneRef,
}
: undefined,
eventTimestamp,
});
} catch (error) {
logger.error(
Expand Down
7 changes: 7 additions & 0 deletions integrations/gitlab/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
>;

Expand Down
24 changes: 23 additions & 1 deletion integrations/gitlab/src/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import { handleImportDispatchForSpaces } from './tasks';
import { GitLabRuntimeContext } from './types';
import { computeConfigQueryKey } from './utils';

interface GitLabCommit {
id: string;
message: string;
title: string;
timestamp: string;
url: string;
author: {
name: string;
email: string;
};
added: string[];
modified: string[];
removed: string[];
}

interface GitLabPushEvent {
object_kind: string;
before: string;
Expand All @@ -18,7 +33,7 @@ interface GitLabPushEvent {
project_id: number;
project: GitLabProject;
repository: any;
commits: any[];
commits: GitLabCommit[];
total_commits_count: number;
}

Expand Down Expand Up @@ -88,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?.timestamp ? new Date(headCommit.timestamp) : undefined,
});

logger.debug(`${total} space configurations are affected`);
Expand Down

0 comments on commit 057569a

Please sign in to comment.