Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete branches command #13543

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .ci/gcb-push-downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ steps:
args:
- "check-cassettes"

- name: 'gcr.io/graphite-docker-images/go-plus'
id: magician-delete-branches
waitFor: ["vcr-merge"]
entrypoint: '/workspace/.ci/scripts/go-plus/magician/exec.sh'
secretEnv:
- "GITHUB_TOKEN_CLASSIC"
args:
- "delete-branches"
- $COMMIT_SHA
- $BRANCH_NAME


# set extremely long 1 day timeout, in order to ensure that any jams / backlogs can be cleared.
timeout: 86400s
options:
Expand Down
127 changes: 127 additions & 0 deletions .ci/magician/cmd/delete_branches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2024 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd

import (
"fmt"
"magician/exec"
"magician/github"
"magician/source"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"
)

var deleteBranchesCmd = &cobra.Command{
Use: "delete-branches",
Short: "Delete the auto pr branches after pushing the given commit",
Long: `This command deletes auto pr branches after pushing the given commit SHA to downstreams.

It expects the following parameters:
1. COMMIT_SHA
2. BASE_BRANCH

It also expects the following environment variables:
1. GITHUB_TOKEN_CLASSIC
2. GOPATH`,
RunE: func(cmd *cobra.Command, args []string) error {
baseBranch := args[0]
sha := args[1]

githubToken, ok := os.LookupEnv("GITHUB_TOKEN_CLASSIC")
if !ok {
return fmt.Errorf("did not provide GITHUB_TOKEN_CLASSIC environment variable")
}

rnr, err := exec.NewRunner()
if err != nil {
return fmt.Errorf("error creating Runner: %s", err)
}

gh := github.NewClient(githubToken)
if err != nil {
return fmt.Errorf("error creating GitHub client: %s", err)
}

return execDeleteBranchesCmd(baseBranch, sha, githubToken, rnr, gh)
},
}

func execDeleteBranchesCmd(baseBranch, sha, githubToken string, runner ExecRunner, gh GithubClient) error {
prNumber, err := fetchPRNumber(sha, baseBranch, runner, gh)

if err != nil {
return err
}

err = deleteBranches(prNumber, githubToken, runner)

return err
}

func fetchPRNumber(sha, baseBranch string, runner ExecRunner, gh GithubClient) (string, error) {
message, err := gh.GetCommitMessage("hashicorp", "terraform-provider-google-beta", sha)
if err != nil {
return "", fmt.Errorf("error getting commit message: %s", err)
}

messageLines := strings.Split(message, "\n")

messageParts := strings.Split(messageLines[0], " ")

prNumber := strings.Trim(messageParts[len(messageParts)-1], "()#\n")

_, err = strconv.ParseInt(prNumber, 10, 64)
if err != nil {
return "", fmt.Errorf("error parsing PR number: %s", err)
}

return prNumber, nil
}

var repoList = []string{
"terraform-provider-google",
"terraform-provider-google-beta",
"terraform-google-conversion",
"tf-oics",
}

func deleteBranches(prNumber, githubToken string, runner source.Runner) error {
for _, repo := range repoList {
for _, branch := range []string{
fmt.Sprintf(":auto-pr-%s", prNumber),
fmt.Sprintf(":auto-pr-%s-old", prNumber),
} {
_, err := runner.Run("git", []string{
"push",
fmt.Sprintf("https://modular-magician:%[email protected]/modular-magician/%s", githubToken, repo),
branch,
}, nil)

if err != nil {
return err
}
}
}

return nil
}

func init() {
rootCmd.AddCommand(deleteBranchesCmd)
}
32 changes: 32 additions & 0 deletions .ci/magician/cmd/delete_branches_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"magician/exec"
"magician/github"
"os"
"testing"
)

func TestFetchPRNumber(t *testing.T) {
rnr, err := exec.NewRunner()
if err != nil {
t.Errorf("error creating Runner: %s", err)
}

githubToken, ok := os.LookupEnv("GITHUB_TOKEN_CLASSIC")
if !ok {
t.Errorf("did not provide GITHUB_TOKEN_CLASSIC environment variable")
}

gh := github.NewClient(githubToken)

prNumber, err := fetchPRNumber("8c6e61bb62d52c950008340deafc1e2a2041898a", "main", rnr, gh)

if err != nil {
t.Errorf("error fetching PR number: %s", err)
}

if prNumber != "6504" {
t.Errorf("PR number is %s, expected 6504", prNumber)
}
}
1 change: 1 addition & 0 deletions .ci/magician/cmd/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type GithubClient interface {
GetPullRequestRequestedReviewers(prNumber string) ([]github.User, error)
GetPullRequestPreviousReviewers(prNumber string) ([]github.User, error)
GetPullRequestComments(prNumber string) ([]github.PullRequestComment, error)
GetCommitMessage(owner, repo, sha string) (string, error)
GetUserType(user string) github.UserType
GetTeamMembers(organization, team string) ([]github.User, error)
MergePullRequest(owner, repo, prNumber, commitSha string) error
Expand Down
5 changes: 5 additions & 0 deletions .ci/magician/cmd/mock_github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (m *mockGithub) GetPullRequestComments(prNumber string) ([]github.PullReque
return m.pullRequestComments, nil
}

func (m *mockGithub) GetCommitMessage(owner, repo, sha string) (string, error) {
m.calledMethods["GetCommitMessage"] = append(m.calledMethods["GetCommitMessage"], []any{owner, repo, sha})
return "commit message", nil
}

func (m *mockGithub) GetTeamMembers(organization, team string) ([]github.User, error) {
m.calledMethods["GetTeamMembers"] = append(m.calledMethods["GetTeamMembers"], []any{organization, team})
if team == "" {
Expand Down
3 changes: 2 additions & 1 deletion .ci/magician/cmd/vcr_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ func TestVCRMergeRunE(t *testing.T) {
{
name: "GITHUB_TOKEN_CLASSIC is missing in env var",
envVars: map[string]string{
"BASE_BRANCH": "main",
"BASE_BRANCH": "main",
"GITHUB_TOKEN_CLASSIC": "",
},
},
{
Expand Down
17 changes: 17 additions & 0 deletions .ci/magician/github/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ func (gh *Client) GetPullRequestPreviousReviewers(prNumber string) ([]User, erro
return result, nil
}

func (gh *Client) GetCommitMessage(owner, repo, sha string) (string, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits/%s", owner, repo, sha)

var commit struct {
Commit struct {
Message string `json:"message"`
} `json:"commit"`
}

err := utils.RequestCall(url, "GET", gh.token, &commit, nil)
if err != nil {
return "", err
}

return commit.Commit.Message, nil
}

func (gh *Client) GetPullRequestComments(prNumber string) ([]PullRequestComment, error) {
url := fmt.Sprintf("https://api.github.com/repos/GoogleCloudPlatform/magic-modules/issues/%s/comments", prNumber)

Expand Down
1 change: 0 additions & 1 deletion .ci/magician/utility/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func makeHTTPRequest(url, method, credentials string, body any) (*http.Response,
}

fmt.Println("response status-code: ", resp.StatusCode)
fmt.Println("response body: ", string(respBodyBytes))
fmt.Println("")

return resp, respBodyBytes, nil
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/unit-test-magician.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ jobs:
run: |
cd .ci/magician
go test ./... -v
env:
GITHUB_TOKEN_CLASSIC: ${{ secrets.GITHUB_TOKEN }}

Loading