Skip to content

Commit 693adee

Browse files
committed
Pass head commit timestamp on push event when triggering gitlab import
1 parent d003852 commit 693adee

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

integrations/gitlab/src/webhooks.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ import { triggerImport } from './sync';
66
import { GitLabRuntimeContext } from './types';
77
import { computeConfigQueryKey } from './utils';
88

9+
interface GitLabCommit {
10+
id: string;
11+
message: string;
12+
title: string;
13+
timestamp: string;
14+
url: string;
15+
author: {
16+
name: string;
17+
email: string;
18+
};
19+
added: string[];
20+
modified: string[];
21+
removed: string[];
22+
}
23+
924
interface GitLabPushEvent {
1025
object_kind: string;
1126
before: string;
@@ -20,7 +35,7 @@ interface GitLabPushEvent {
2035
project_id: number;
2136
project: GitLabProject;
2237
repository: any;
23-
commits: any[];
38+
commits: GitLabCommit[];
2439
total_commits_count: number;
2540
}
2641

@@ -94,6 +109,12 @@ export async function handlePushEvent(context: GitLabRuntimeContext, payload: Gi
94109
`handling push event on ref "${payload.ref}" of "${payload.repository.id}" (project "${payload.project.id}"): ${spaceInstallations.length} space configurations are affected`
95110
);
96111

112+
// Gitlab push events do not include a head_commit property so we need to get it from
113+
// the commits attribute which should contains the newest 20 commits:
114+
// https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html#push-events
115+
const headCommitSha = payload.after;
116+
const headCommit = payload.commits.find((commit) => commit.id === headCommitSha);
117+
97118
await Promise.all(
98119
spaceInstallations.map(async (spaceInstallation) => {
99120
try {
@@ -111,7 +132,9 @@ export async function handlePushEvent(context: GitLabRuntimeContext, payload: Gi
111132
authToken: installationAPIToken.token,
112133
});
113134

114-
await triggerImport(context, spaceInstallation);
135+
await triggerImport(context, spaceInstallation, {
136+
eventCreatedAt: headCommit ? new Date(headCommit.timestamp) : undefined,
137+
});
115138
} catch (error) {
116139
logger.error(
117140
`error while triggering import for space ${spaceInstallation.space}`,

0 commit comments

Comments
 (0)