From 693adee29e697bb9fbb87b86436b6d8fac279f30 Mon Sep 17 00:00:00 2001 From: Steeve Pastorelli Date: Thu, 4 Jan 2024 12:22:57 +0100 Subject: [PATCH] Pass head commit timestamp on push event when triggering gitlab import --- integrations/gitlab/src/webhooks.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/integrations/gitlab/src/webhooks.ts b/integrations/gitlab/src/webhooks.ts index 56180dc64..a439caf9c 100644 --- a/integrations/gitlab/src/webhooks.ts +++ b/integrations/gitlab/src/webhooks.ts @@ -6,6 +6,21 @@ import { triggerImport } from './sync'; 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; @@ -20,7 +35,7 @@ interface GitLabPushEvent { project_id: number; project: GitLabProject; repository: any; - commits: any[]; + commits: GitLabCommit[]; total_commits_count: number; } @@ -94,6 +109,12 @@ export async function handlePushEvent(context: GitLabRuntimeContext, payload: Gi `handling push event on ref "${payload.ref}" of "${payload.repository.id}" (project "${payload.project.id}"): ${spaceInstallations.length} space configurations are affected` ); + // 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); + await Promise.all( spaceInstallations.map(async (spaceInstallation) => { try { @@ -111,7 +132,9 @@ export async function handlePushEvent(context: GitLabRuntimeContext, payload: Gi authToken: installationAPIToken.token, }); - await triggerImport(context, spaceInstallation); + await triggerImport(context, spaceInstallation, { + eventCreatedAt: headCommit ? new Date(headCommit.timestamp) : undefined, + }); } catch (error) { logger.error( `error while triggering import for space ${spaceInstallation.space}`,