|
| 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 | +} |
0 commit comments