From 1544fbbc53b5df5d8c16149d1089c259cdda39e4 Mon Sep 17 00:00:00 2001 From: CerealBoy Date: Wed, 8 Jan 2025 15:42:57 +1100 Subject: [PATCH] Skip pushing the git commit metadata if BUILDKITE_COMMIT is set --- internal/job/checkout.go | 10 ++++++++++ .../checkout_git_mirrors_integration_test.go | 6 ------ internal/job/integration/checkout_integration_test.go | 6 ------ 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/internal/job/checkout.go b/internal/job/checkout.go index cf51995ec0..8184758d2a 100644 --- a/internal/job/checkout.go +++ b/internal/job/checkout.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "strings" "time" @@ -780,12 +781,21 @@ func gitFetchCommitWithFallback(ctx context.Context, shell *shell.Shell, gitFetc const CommitMetadataKey = "buildkite:git:commit" +var potentiallyValidGitSHA = regexp.MustCompile(`^[a-f0-9]{40}$`) + // sendCommitToBuildkite sends commit information (commit, author, subject, body) to Buildkite, as the BK backend doesn't // have access to user's VCSes. To do this, we set a special meta-data key in the build, but only if it isn't already present // Functionally, this means that the first job in a build (usually a pipeline upload or similar) will push the commit info // to buildkite, which uses this info to display commit info in the UI eg in the title for the build // note that we bail early if the key already exists, as we don't want to overwrite it func (e *Executor) sendCommitToBuildkite(ctx context.Context) error { + commitRef, _ := e.shell.Env.Get("BUILDKITE_COMMIT") + if commitRef != "HEAD" && potentiallyValidGitSHA.MatchString(commitRef) { + // we can skip the metadata shenanigans here and push straight through + e.shell.Commentf("Skipping the meta-data git commit steps and assuming %q is correct...", commitRef) + return nil + } + e.shell.Commentf("Checking to see if git commit information needs to be sent to Buildkite...") cmd := e.shell.Command("buildkite-agent", "meta-data", "exists", CommitMetadataKey) if err := cmd.Run(ctx); err == nil { diff --git a/internal/job/integration/checkout_git_mirrors_integration_test.go b/internal/job/integration/checkout_git_mirrors_integration_test.go index f0b180a750..5119116085 100644 --- a/internal/job/integration/checkout_git_mirrors_integration_test.go +++ b/internal/job/integration/checkout_git_mirrors_integration_test.go @@ -93,15 +93,9 @@ func TestWithResolvingCommitExperiment_WithGitMirrors(t *testing.T) { {"fetch", "-v", "--", "origin", "main"}, {"checkout", "-f", "FETCH_HEAD"}, {"clean", "-fdq"}, - {"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg}, {"rev-parse", "HEAD"}, }) - // Mock out the meta-data calls to the agent after checkout - agent := tester.MockAgent(t) - agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1) - agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern) - tester.RunAndCheck(t, env...) } diff --git a/internal/job/integration/checkout_integration_test.go b/internal/job/integration/checkout_integration_test.go index 009061942d..ddaf94eb1c 100644 --- a/internal/job/integration/checkout_integration_test.go +++ b/internal/job/integration/checkout_integration_test.go @@ -59,15 +59,9 @@ func TestWithResolvingCommitExperiment(t *testing.T) { {"fetch", "-v", "--", "origin", "main"}, {"checkout", "-f", "FETCH_HEAD"}, {"clean", "-fdq"}, - {"--no-pager", "log", "-1", "HEAD", "-s", "--no-color", gitShowFormatArg}, {"rev-parse", "HEAD"}, }) - // Mock out the meta-data calls to the agent after checkout - agent := tester.MockAgent(t) - agent.Expect("meta-data", "exists", job.CommitMetadataKey).AndExitWith(1) - agent.Expect("meta-data", "set", job.CommitMetadataKey).WithStdin(commitPattern) - tester.RunAndCheck(t, env...) }