From 632d1bd85b0266c0d617cfc625fd40044979a66e Mon Sep 17 00:00:00 2001 From: Alex Tugarev Date: Thu, 14 Sep 2023 11:43:43 +0000 Subject: [PATCH] evaluate prebuildDefaultBranchOnly in PrebuildManager --- .../server/src/prebuilds/bitbucket-app.ts | 2 +- .../src/prebuilds/bitbucket-server-app.ts | 2 +- components/server/src/prebuilds/github-app.ts | 4 ++-- .../src/prebuilds/github-enterprise-app.ts | 2 +- components/server/src/prebuilds/gitlab-app.ts | 2 +- .../server/src/prebuilds/prebuild-manager.ts | 20 ++++++++++++++++--- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/components/server/src/prebuilds/bitbucket-app.ts b/components/server/src/prebuilds/bitbucket-app.ts index 9a702a4fbdacb2..5d2edf4acdebff 100644 --- a/components/server/src/prebuilds/bitbucket-app.ts +++ b/components/server/src/prebuilds/bitbucket-app.ts @@ -136,7 +136,7 @@ export class BitbucketApp { commit: context.revision, }); const config = await this.prebuildManager.fetchConfig({ span }, user, context); - if (!this.prebuildManager.shouldPrebuild({ config, project })) { + if (!this.prebuildManager.shouldPrebuild({ config, project, context })) { log.info("Bitbucket push event: No prebuild.", { config, context }); await this.webhookEvents.updateEvent(event.id, { prebuildStatus: "ignored_unconfigured", diff --git a/components/server/src/prebuilds/bitbucket-server-app.ts b/components/server/src/prebuilds/bitbucket-server-app.ts index b913d11b91970e..25f89960529e17 100644 --- a/components/server/src/prebuilds/bitbucket-server-app.ts +++ b/components/server/src/prebuilds/bitbucket-server-app.ts @@ -137,7 +137,7 @@ export class BitbucketServerApp { commit: context.revision, }); const config = await this.prebuildManager.fetchConfig({ span }, user, context); - if (!this.prebuildManager.shouldPrebuild({ config, project })) { + if (!this.prebuildManager.shouldPrebuild({ config, project, context })) { log.info("Bitbucket Server push event: No prebuild.", { config, context }); await this.webhookEvents.updateEvent(event.id, { prebuildStatus: "ignored_unconfigured", diff --git a/components/server/src/prebuilds/github-app.ts b/components/server/src/prebuilds/github-app.ts index e12129c11049b7..69a1b0ab6182b9 100644 --- a/components/server/src/prebuilds/github-app.ts +++ b/components/server/src/prebuilds/github-app.ts @@ -310,7 +310,7 @@ export class GithubApp { }); const runPrebuild = - this.prebuildManager.shouldPrebuild({ config, project }) && + this.prebuildManager.shouldPrebuild({ config, project, context }) && this.appRules.shouldRunPrebuild(config, branch == repo.default_branch, false, false); if (!runPrebuild) { const reason = `Not running prebuild, the user did not enable it for this context or did not configure prebuild task(s)`; @@ -538,7 +538,7 @@ export class GithubApp { const isFork = pr.head.repo.id !== pr.base.repo.id; const runPrebuild = - this.prebuildManager.shouldPrebuild({ config, project }) && + this.prebuildManager.shouldPrebuild({ config, project, context }) && this.appRules.shouldRunPrebuild(config, false, true, isFork); if (runPrebuild) { const commitInfo = await this.getCommitInfo(user, ctx.payload.repository.html_url, pr.head.sha); diff --git a/components/server/src/prebuilds/github-enterprise-app.ts b/components/server/src/prebuilds/github-enterprise-app.ts index d3af9cef1f416c..f813c3f2a67734 100644 --- a/components/server/src/prebuilds/github-enterprise-app.ts +++ b/components/server/src/prebuilds/github-enterprise-app.ts @@ -169,7 +169,7 @@ export class GitHubEnterpriseApp { const config = await this.prebuildManager.fetchConfig({ span }, user, context); if ( - !this.prebuildManager.shouldPrebuild({ config, project }) || + !this.prebuildManager.shouldPrebuild({ config, project, context }) || !this.appRules.shouldRunPrebuild(config, context.ref === context.repository.defaultBranch, false, false) ) { log.info("GitHub Enterprise push event: No prebuild.", { config, context }); diff --git a/components/server/src/prebuilds/gitlab-app.ts b/components/server/src/prebuilds/gitlab-app.ts index c324558e1e40ce..6acaf820d69dc9 100644 --- a/components/server/src/prebuilds/gitlab-app.ts +++ b/components/server/src/prebuilds/gitlab-app.ts @@ -161,7 +161,7 @@ export class GitLabApp { }); const config = await this.prebuildManager.fetchConfig({ span }, user, context); - if (!this.prebuildManager.shouldPrebuild({ config, project })) { + if (!this.prebuildManager.shouldPrebuild({ config, project, context })) { log.info("GitLab push event: No prebuild.", { config, context }); await this.webhookEvents.updateEvent(event.id, { prebuildStatus: "ignored_unconfigured", diff --git a/components/server/src/prebuilds/prebuild-manager.ts b/components/server/src/prebuilds/prebuild-manager.ts index 1dafc6b178ec72..21cfdd36275e64 100644 --- a/components/server/src/prebuilds/prebuild-manager.ts +++ b/components/server/src/prebuilds/prebuild-manager.ts @@ -341,8 +341,8 @@ export class PrebuildManager { } } - shouldPrebuild(params: { config: WorkspaceConfig; project: Project }): boolean { - const { config, project } = params; + shouldPrebuild(params: { config: WorkspaceConfig; project: Project; context: CommitContext }): boolean { + const { config, project, context } = params; if (!config || !config._origin || config._origin !== "repo") { // we demand an explicit gitpod config return false; @@ -353,7 +353,21 @@ export class PrebuildManager { return false; } - return Project.isPrebuildsEnabled(project); + const isPrebuildsEnabled = Project.isPrebuildsEnabled(project); + if (!isPrebuildsEnabled) { + return false; + } + + if (!project.settings?.prebuildDefaultBranchOnly) { + return true; + } + + const defaultBranch = context.repository.defaultBranch; + if (!defaultBranch) { + log.debug("CommitContext is missing the default branch.", { context }); + return true; + } + return context.ref === defaultBranch; } protected shouldPrebuildIncrementally(cloneUrl: string, project: Project): boolean {