From eade82bc8ce98948c7809b92b9c70220927e7afa Mon Sep 17 00:00:00 2001 From: Tim Ross Date: Mon, 3 Feb 2025 18:53:36 -0500 Subject: [PATCH] Fix: fetch merged commit for backporting This was missed in https://github.com/gravitational/shared-workflows/pull/313. Without this change all backports will fail with a similar `fatal: bad object c64253265b7cb675c7ca336c0454a65b7db4dd5d` error. --- bot/internal/bot/backport.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/bot/internal/bot/backport.go b/bot/internal/bot/backport.go index caad629..5a92433 100644 --- a/bot/internal/bot/backport.go +++ b/bot/internal/bot/backport.go @@ -91,9 +91,6 @@ func (b *Bot) Backport(ctx context.Context) error { // Create and push git branch for backport to GitHub. err := b.createBackportBranch(ctx, - b.c.Environment.Organization, - b.c.Environment.Repository, - b.c.Environment.Number, base, pull, head, @@ -164,9 +161,6 @@ func (b *Bot) BackportLocal(ctx context.Context, branch string) error { } err = b.createBackportBranch(ctx, - b.c.Environment.Organization, - b.c.Environment.Repository, - b.c.Environment.Number, branch, pull, b.backportBranchName(branch), @@ -178,7 +172,7 @@ func (b *Bot) BackportLocal(ctx context.Context, branch string) error { return nil } -const botBackportBranchPrefix = "bot/backport" +const botBackportBranchPrefix = "bot/backport-test" func (b *Bot) backportBranchName(base string) string { return fmt.Sprintf("%s-%v-%v", botBackportBranchPrefix, b.c.Environment.Number, base) @@ -211,7 +205,7 @@ func findBranches(labels []string) []string { // // TODO(russjones): Refactor to use go-git (so similar git library) instead of // executing git from disk. -func (b *Bot) createBackportBranch(ctx context.Context, organization string, repository string, number int, base string, pull github.PullRequest, newHead string, git func(...string) error) error { +func (b *Bot) createBackportBranch(ctx context.Context, base string, pull github.PullRequest, newHead string, git func(...string) error) error { log.Println("--> Backporting to", base, "<--") if err := git("config", "--global", "user.name", "github-actions"); err != nil { @@ -221,8 +215,8 @@ func (b *Bot) createBackportBranch(ctx context.Context, organization string, rep log.Printf("Failed to set user.email: %v.", err) } - // Fetch the refs for the base branch and the Github PR. - if err := git("fetch", "origin", base, fmt.Sprintf("pull/%d/head", number)); err != nil { + // Fetch the refs for the base branch and the merged commit. + if err := git("fetch", "origin", base, pull.MergeCommitSHA); err != nil { return trace.Wrap(err) }