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

Pass commit SHA to merge pull request #10482

Merged
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
47 changes: 25 additions & 22 deletions .ci/magician/cmd/generate_downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -299,43 +299,46 @@ 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 {
rnr.Mkdir(".changelog")
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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .ci/magician/cmd/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .ci/magician/cmd/mock_github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion .ci/magician/github/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading