From fc55521b556466ec5a3c8b111fe1a0382d5c5a1b Mon Sep 17 00:00:00 2001 From: Scott Suarez Date: Thu, 18 Apr 2024 14:40:32 -0700 Subject: [PATCH] Pass commit SHA to merge pull request (#10482) --- .ci/magician/cmd/generate_downstream.go | 47 ++++++++++--------- .ci/magician/cmd/interfaces.go | 2 +- .ci/magician/cmd/mock_github_test.go | 4 +- .ci/magician/github/set.go | 3 +- .../docs/d/active_folder.html.markdown | 1 + 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/.ci/magician/cmd/generate_downstream.go b/.ci/magician/cmd/generate_downstream.go index 2e812d482069..58913d8f586c 100644 --- a/.ci/magician/cmd/generate_downstream.go +++ b/.ci/magician/cmd/generate_downstream.go @@ -135,13 +135,8 @@ func execGenerateDownstream(baseBranch, command, repo, version, ref string, gh G os.Exit(1) } - commitErr := createCommit(scratchRepo, commitMessage, rnr) - if commitErr != nil { - fmt.Println("Error creating commit: ", commitErr) - } - var pullRequest *github.PullRequest - if commitErr == nil && command == "downstream" { + if command == "downstream" { pullRequest, err = getPullRequest(baseBranch, ref, gh) if err != nil { fmt.Println("Error getting pull request: ", err) @@ -155,13 +150,18 @@ func execGenerateDownstream(baseBranch, command, repo, version, ref string, gh G } } + scratchCommitSha, commitErr := createCommit(scratchRepo, commitMessage, rnr) + if commitErr != nil { + fmt.Println("Error creating commit: ", commitErr) + } + if _, err := rnr.Run("git", []string{"push", ctlr.URL(scratchRepo), scratchRepo.Branch, "-f"}, nil); err != nil { fmt.Println("Error pushing commit: ", err) os.Exit(1) } if commitErr == nil && command == "downstream" { - if err := mergePullRequest(downstreamRepo, scratchRepo, pullRequest, rnr, gh); err != nil { + if err := mergePullRequest(downstreamRepo, scratchRepo, scratchCommitSha, pullRequest, rnr, gh); err != nil { fmt.Println("Error merging pull request: ", err) os.Exit(1) } @@ -299,26 +299,35 @@ func getPullRequest(baseBranch, ref string, gh GithubClient) (*github.PullReques return nil, fmt.Errorf("no pr found with merge commit sha %s and base branch %s", ref, baseBranch) } -func createCommit(scratchRepo *source.Repo, commitMessage string, rnr ExecRunner) error { +func createCommit(scratchRepo *source.Repo, commitMessage string, rnr ExecRunner) (string, error) { if err := rnr.PushDir(scratchRepo.Path); err != nil { - return err + return "", err } if err := setGitConfig(rnr); err != nil { - return err + return "", err } if _, err := rnr.Run("git", []string{"add", "."}, nil); err != nil { - return err + return "", err } if _, err := rnr.Run("git", []string{"checkout", "-b", scratchRepo.Branch}, nil); err != nil { - return err + return "", err } if _, err := rnr.Run("git", []string{"commit", "--signoff", "-m", commitMessage}, nil); err != nil { - return err + return "", err } - return nil + commitSha, err := rnr.Run("git", []string{"rev-parse", "HEAD"}, nil) + if err != nil { + fmt.Println("Error retrieving commit sha: ", err) + os.Exit(1) + } + + commitSha = strings.TrimSpace(commitSha) + fmt.Printf("Commit sha on the branch is: `%s`", commitSha) + + return commitSha, err } func addChangelogEntry(pullRequest *github.PullRequest, rnr ExecRunner) error { @@ -326,16 +335,10 @@ func addChangelogEntry(pullRequest *github.PullRequest, rnr ExecRunner) error { if err := rnr.WriteFile(filepath.Join(".changelog", fmt.Sprintf("%d.txt", pullRequest.Number)), strings.Join(changelogExp.FindAllString(pullRequest.Body, -1), "\n")); err != nil { return err } - if _, err := rnr.Run("git", []string{"add", "."}, nil); err != nil { - return err - } - if _, err := rnr.Run("git", []string{"commit", "--signoff", "--amend", "--no-edit"}, nil); err != nil { - return err - } return nil } -func mergePullRequest(downstreamRepo, scratchRepo *source.Repo, pullRequest *github.PullRequest, rnr ExecRunner, gh GithubClient) error { +func mergePullRequest(downstreamRepo, scratchRepo *source.Repo, scratchRepoSha string, pullRequest *github.PullRequest, rnr ExecRunner, gh GithubClient) error { fmt.Printf(`Base: %s:%s Head: %s:%s `, downstreamRepo.Owner, downstreamRepo.Branch, scratchRepo.Owner, scratchRepo.Branch) @@ -366,7 +369,7 @@ Head: %s:%s // Wait a few seconds, then merge the PR. time.Sleep(5 * time.Second) fmt.Println("Merging PR ", newPRURL) - if err := gh.MergePullRequest(downstreamRepo.Owner, downstreamRepo.Name, newPRNumber); err != nil { + if err := gh.MergePullRequest(downstreamRepo.Owner, downstreamRepo.Name, newPRNumber, scratchRepoSha); err != nil { return err } return nil diff --git a/.ci/magician/cmd/interfaces.go b/.ci/magician/cmd/interfaces.go index 950855a78373..948ab794c907 100644 --- a/.ci/magician/cmd/interfaces.go +++ b/.ci/magician/cmd/interfaces.go @@ -26,7 +26,7 @@ type GithubClient interface { GetPullRequestPreviousReviewers(prNumber string) ([]github.User, error) GetUserType(user string) github.UserType GetTeamMembers(organization, team string) ([]github.User, error) - MergePullRequest(owner, repo, prNumber string) error + MergePullRequest(owner, repo, prNumber, commitSha string) error PostBuildStatus(prNumber, title, state, targetURL, commitSha string) error PostComment(prNumber, comment string) error RequestPullRequestReviewers(prNumber string, reviewers []string) error diff --git a/.ci/magician/cmd/mock_github_test.go b/.ci/magician/cmd/mock_github_test.go index 69eaacd1a0d4..08b71599babb 100644 --- a/.ci/magician/cmd/mock_github_test.go +++ b/.ci/magician/cmd/mock_github_test.go @@ -93,7 +93,7 @@ func (m *mockGithub) CreateWorkflowDispatchEvent(workflowFileName string, inputs return nil } -func (m *mockGithub) MergePullRequest(owner, repo, prNumber string) error { - m.calledMethods["MergePullRequest"] = append(m.calledMethods["MergePullRequest"], []any{owner, repo, prNumber}) +func (m *mockGithub) MergePullRequest(owner, repo, prNumber, commitSha string) error { + m.calledMethods["MergePullRequest"] = append(m.calledMethods["MergePullRequest"], []any{owner, repo, prNumber, commitSha}) return nil } diff --git a/.ci/magician/github/set.go b/.ci/magician/github/set.go index 2ed68e292809..fcc430bb0c11 100644 --- a/.ci/magician/github/set.go +++ b/.ci/magician/github/set.go @@ -117,10 +117,11 @@ func (gh *Client) CreateWorkflowDispatchEvent(workflowFileName string, inputs ma return nil } -func (gh *Client) MergePullRequest(owner, repo, prNumber string) error { +func (gh *Client) MergePullRequest(owner, repo, prNumber, commitSha string) error { url := fmt.Sprintf("https://api.github.com/repos/%s/%s/pulls/%s/merge", owner, repo, prNumber) err := utils.RequestCall(url, "PUT", gh.token, nil, map[string]any{ "merge_method": "squash", + "sha": commitSha, }) if err != nil { diff --git a/mmv1/third_party/terraform/website/docs/d/active_folder.html.markdown b/mmv1/third_party/terraform/website/docs/d/active_folder.html.markdown index a258e46f5b83..14954f70091d 100644 --- a/mmv1/third_party/terraform/website/docs/d/active_folder.html.markdown +++ b/mmv1/third_party/terraform/website/docs/d/active_folder.html.markdown @@ -27,6 +27,7 @@ The following arguments are supported: * `api_method` - (Optional) The API method to use to search for the folder. Valid values are `LIST` and `SEARCH`. Default Value is `LIST`. `LIST` is [strongly consistent](https://cloud.google.com/resource-manager/reference/rest/v3/folders/list#:~:text=list()%20provides%20a-,strongly%20consistent,-view%20of%20the) and requires `resourcemanager.folders.list` on the parent folder, while `SEARCH` is [eventually consistent](https://cloud.google.com/resource-manager/reference/rest/v3/folders/search#:~:text=eventually%20consistent) and only returns folders that the user has `resourcemanager.folders.get` permission on. + ## Attributes Reference In addition to the arguments listed above, the following attributes are exported: