Skip to content

Commit 4f88786

Browse files
committed
Revert trigger strategy on webhook event
1 parent 6254927 commit 4f88786

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

components/server/src/prebuilds/bitbucket-app.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ export class BitbucketApp {
130130
try {
131131
const projectOwner = await this.findProjectOwner(project, user);
132132

133+
if (project.settings?.prebuilds?.triggerStrategy === "activity-based") {
134+
await this.projectService.updateProject(projectOwner, {
135+
id: project.id,
136+
settings: {
137+
...project.settings,
138+
prebuilds: {
139+
...project.settings.prebuilds,
140+
triggerStrategy: "webhook-based",
141+
},
142+
},
143+
});
144+
log.info(`Reverted configuration ${project.id} to webhook-based prebuilds`);
145+
}
146+
133147
const contextURL = this.createContextUrl(data);
134148
span.setTag("contextURL", contextURL);
135149
const context = (await this.contextParser.handle({ span }, user, contextURL)) as CommitContext;

components/server/src/prebuilds/bitbucket-server-app.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ export class BitbucketServerApp {
127127
try {
128128
const projectOwner = await this.findProjectOwner(project, user);
129129

130+
if (project.settings?.prebuilds?.triggerStrategy === "activity-based") {
131+
await this.projectService.updateProject(projectOwner, {
132+
id: project.id,
133+
settings: {
134+
...project.settings,
135+
prebuilds: {
136+
...project.settings.prebuilds,
137+
triggerStrategy: "webhook-based",
138+
},
139+
},
140+
});
141+
log.info(`Reverted configuration ${project.id} to webhook-based prebuilds`);
142+
}
143+
130144
const contextUrl = this.createBranchContextUrl(payload);
131145
span.setTag("contextUrl", contextUrl);
132146
const context = await this.contextParser.handle({ span }, user, contextUrl);

components/server/src/prebuilds/github-app.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ export class GithubApp {
289289
const user = await this.findProjectOwner(project, installationOwner);
290290
const config = await this.prebuildManager.fetchConfig({ span }, user, context, project?.teamId);
291291

292+
if (project.settings?.prebuilds?.triggerStrategy === "activity-based") {
293+
await this.projectService.updateProject(user, {
294+
id: project.id,
295+
settings: {
296+
...project.settings,
297+
prebuilds: {
298+
...project.settings.prebuilds,
299+
triggerStrategy: "webhook-based",
300+
},
301+
},
302+
});
303+
log.info(`Reverted configuration ${project.id} to webhook-based prebuilds`);
304+
}
305+
292306
await this.webhookEvents.updateEvent(event.id, {
293307
authorizedUserId: user.id,
294308
projectId: project.id,

components/server/src/prebuilds/github-enterprise-app.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ export class GitHubEnterpriseApp {
162162
try {
163163
const projectOwner = await this.findProjectOwner(project, user);
164164

165+
if (project.settings?.prebuilds?.triggerStrategy === "activity-based") {
166+
await this.projectService.updateProject(projectOwner, {
167+
id: project.id,
168+
settings: {
169+
...project.settings,
170+
prebuilds: {
171+
...project.settings.prebuilds,
172+
triggerStrategy: "webhook-based",
173+
},
174+
},
175+
});
176+
log.info(`Reverted configuration ${project.id} to webhook-based prebuilds`);
177+
}
178+
165179
await this.webhookEvents.updateEvent(event.id, {
166180
authorizedUserId: user.id,
167181
projectId: project.id,

components/server/src/prebuilds/gitlab-app.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ export class GitLabApp {
151151
try {
152152
const projectOwner = await this.findProjectOwner(project, user);
153153

154+
if (project.settings?.prebuilds?.triggerStrategy === "activity-based") {
155+
await this.projectService.updateProject(projectOwner, {
156+
id: project.id,
157+
settings: {
158+
...project.settings,
159+
prebuilds: {
160+
...project.settings.prebuilds,
161+
triggerStrategy: "webhook-based",
162+
},
163+
},
164+
});
165+
log.info(`Reverted configuration ${project.id} to webhook-based prebuilds`);
166+
}
167+
154168
const contextURL = this.createBranchContextUrl(body);
155169
log.debug({ userId: user.id }, "GitLab push hook: Context URL", { context: body, contextURL });
156170
span.setTag("contextURL", contextURL);

components/server/src/prebuilds/prebuild-manager.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ export class PrebuildManager {
127127
): Promise<WebhookEvent | undefined> {
128128
const context = (await this.contextParser.handle(ctx, user, project.cloneUrl)) as CommitContext;
129129
const maxDepth = 15;
130+
const maxAge = 7 * 24 * 60 * 60; // 1 week
130131

131132
const events = await this.webhookEventDb.findByCloneUrl(project.cloneUrl, maxDepth);
132133
if (events.length === 0) {
133-
// return undefined;
134+
return undefined;
134135
}
135136

136137
const hostContext = this.hostContextProvider.get(context.repository.host);
@@ -148,7 +149,15 @@ export class PrebuildManager {
148149
if (!history) {
149150
throw new ApplicationError(ErrorCodes.NOT_FOUND, `commit history not found`);
150151
}
151-
const matchingEvent = events.find((event) => history.find((commit) => commit === event.commit));
152+
const matchingEvent = events.find((event) =>
153+
history.find((commit) => {
154+
if (Date.now() - new Date(event.creationTime).getTime() > maxAge) {
155+
return false;
156+
}
157+
158+
return commit === event.commit;
159+
}),
160+
);
152161

153162
return matchingEvent;
154163
}

0 commit comments

Comments
 (0)