bot: delete backport branches when a backport is merged (#14068)

The backport workflow runs when any PR is merged to any banch.
Prior to this change, the workflow was a no-op if the target branch
was a release branch.

Update the workflow so that when it runs on merged backports
it deletes the auto-generated backport branch.

Also fixes #12412
This commit is contained in:
Zac Bergquist
2022-08-01 19:47:27 +00:00
committed by GitHub
parent 10af7c58ca
commit 2e32065ea6
3 changed files with 20 additions and 1 deletions
@@ -50,6 +50,17 @@ func (b *Bot) Backport(ctx context.Context) error {
return trace.Wrap(err)
}
// If this workflow is running on a release branch, then it means
// a backport was merged and there's no need to open additional
// backport PRs. We can safely delete the remote branch though,
// because we know this is an internal contributor's merged PR.
if isReleaseBranch(b.c.Environment.UnsafeBase) &&
isBotBackportBranch(b.c.Environment.UnsafeHead) &&
!pull.Fork {
log.Printf("backport merged to %v, deleting branch %v", b.c.Environment.UnsafeBase, b.c.Environment.UnsafeHead)
return trace.Wrap(git("push", "origin", "--delete", b.c.Environment.UnsafeHead))
}
// Extract backport branches names from labels attached to the Pull
// Request. If no backports were requested, return right away.
branches := findBranches(pull.UnsafeLabels)
@@ -151,3 +151,11 @@ func isAutoGeneratedFile(name string) bool {
strings.HasSuffix(name, "_pb.d.ts") ||
strings.Contains(name, "webassets/")
}
func isReleaseBranch(branch string) bool {
return strings.HasPrefix(branch, "branch/")
}
func isBotBackportBranch(branch string) bool {
return strings.HasPrefix(branch, "bot/backport")
}
@@ -61,7 +61,7 @@ func (b *Bot) labels(ctx context.Context, files []github.PullRequestFile) ([]str
var labels []string
// The branch name is unsafe, but here we are simply adding a label.
if strings.HasPrefix(b.c.Environment.UnsafeHead, "branch/") {
if isReleaseBranch(b.c.Environment.UnsafeBase) {
log.Println("Label: Found backport branch.")
labels = append(labels, "backport")
}