Skip to content

Commit e9e2937

Browse files
committed
Add delete branches command
1 parent 9351ef0 commit e9e2937

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

.ci/gcb-push-downstream.yml

+12
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ steps:
211211
args:
212212
- "check-cassettes"
213213

214+
- name: 'gcr.io/graphite-docker-images/go-plus'
215+
id: magician-delete-branches
216+
waitFor: ["vcr-merge"]
217+
entrypoint: '/workspace/.ci/scripts/go-plus/magician/exec.sh'
218+
secretEnv:
219+
- "GITHUB_TOKEN_CLASSIC"
220+
args:
221+
- "delete-branches"
222+
- $COMMIT_SHA
223+
- $BRANCH_NAME
224+
225+
214226
# set extremely long 1 day timeout, in order to ensure that any jams / backlogs can be cleared.
215227
timeout: 86400s
216228
options:

.ci/magician/cmd/delete_branches.go

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2024 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
"magician/exec"
21+
"magician/source"
22+
"os"
23+
"strings"
24+
25+
"github.com/spf13/cobra"
26+
)
27+
28+
var deleteBranchesCmd = &cobra.Command{
29+
Use: "delete-branches",
30+
Short: "Delete the auto pr branches after pushing the given commit",
31+
Long: `This command deletes auto pr branches after pushing the given commit SHA to downstreams.
32+
33+
It expects the following parameters:
34+
1. COMMIT_SHA
35+
2. BASE_BRANCH
36+
37+
It also expects the following environment variables:
38+
1. GITHUB_TOKEN_CLASSIC
39+
2. GOPATH`,
40+
RunE: func(cmd *cobra.Command, args []string) error {
41+
baseBranch := args[0]
42+
sha := args[1]
43+
44+
githubToken, ok := os.LookupEnv("GITHUB_TOKEN_CLASSIC")
45+
if !ok {
46+
return fmt.Errorf("did not provide GITHUB_TOKEN_CLASSIC environment variable")
47+
}
48+
49+
goPath, ok := os.LookupEnv("GOPATH")
50+
if !ok {
51+
return fmt.Errorf("did not provide GOPATH environment variable")
52+
}
53+
54+
rnr, err := exec.NewRunner()
55+
if err != nil {
56+
return fmt.Errorf("error creating Runner: %s", err)
57+
}
58+
59+
ctlr := source.NewController(goPath, "modular-magician", githubToken, rnr)
60+
61+
return execDeleteBranchesCmd(baseBranch, sha, githubToken, rnr, ctlr)
62+
},
63+
}
64+
65+
func execDeleteBranchesCmd(baseBranch, sha, githubToken string, runner source.Runner, controller *source.Controller) error {
66+
prNumber, err := fetchPRNumber(sha, baseBranch, runner, controller)
67+
68+
if err != nil {
69+
return err
70+
}
71+
72+
err = deleteBranches(prNumber, githubToken, runner)
73+
74+
return err
75+
}
76+
77+
func fetchPRNumber(sha, baseBranch string, runner source.Runner, controller *source.Controller) (string, error) {
78+
repo := &source.Repo{
79+
Name: "terraform-provider-google-beta",
80+
Branch: baseBranch,
81+
}
82+
controller.SetPath(repo)
83+
84+
if err := controller.Clone(repo); err != nil {
85+
return "", err
86+
}
87+
88+
controller.SetPath(repo)
89+
90+
if err := runner.PushDir(repo.Path); err != nil {
91+
return "", err
92+
}
93+
94+
message, err := runner.Run("git", []string{
95+
"show",
96+
"-s",
97+
"--format=%s",
98+
sha,
99+
}, nil)
100+
if err != nil {
101+
return "", fmt.Errorf("error getting commit message: %s", err)
102+
}
103+
104+
messageParts := strings.Split(message, " ")
105+
106+
prNumber := messageParts[len(messageParts)-1]
107+
108+
return strings.Trim(prNumber, "()#\n"), nil
109+
}
110+
111+
var repoList = []string{
112+
"terraform-provider-google",
113+
"terraform-provider-google-beta",
114+
"terraform-google-conversion",
115+
"tf-oics",
116+
}
117+
118+
func deleteBranches(prNumber, githubToken string, runner source.Runner) error {
119+
for _, repo := range repoList {
120+
for _, branch := range []string{
121+
fmt.Sprintf(":auto-pr-%s", prNumber),
122+
fmt.Sprintf(":auto-pr-%s-old", prNumber),
123+
} {
124+
_, err := runner.Run("git", []string{
125+
"push",
126+
fmt.Sprintf("https://modular-magician:%[email protected]/modular-magician/%s", githubToken, repo),
127+
branch,
128+
}, nil)
129+
130+
if err != nil {
131+
return err
132+
}
133+
}
134+
}
135+
136+
return nil
137+
}
138+
139+
func init() {
140+
rootCmd.AddCommand(deleteBranchesCmd)
141+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"magician/exec"
5+
"magician/source"
6+
"testing"
7+
)
8+
9+
func TestFetchPRNumber(t *testing.T) {
10+
rnr, err := exec.NewRunner()
11+
if err != nil {
12+
t.Errorf("error creating Runner: %s", err)
13+
}
14+
15+
ctlr := source.NewController("", "modular-magician", "", rnr)
16+
17+
prNumber, err := fetchPRNumber("8c6e61bb62d52c950008340deafc1e2a2041898a", "main", rnr, ctlr)
18+
19+
if err != nil {
20+
t.Errorf("error fetching PR number: %s", err)
21+
}
22+
23+
if prNumber != "6504" {
24+
t.Errorf("PR number is %s, expected 6504", prNumber)
25+
}
26+
}

0 commit comments

Comments
 (0)