-
Notifications
You must be signed in to change notification settings - Fork 1
feat: implement PR combine #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3f0df3d
implement logic for checking CI status for a given commit and PR appr…
GrantBirki 1bea40f
only make the graphql call once
GrantBirki b7da517
add combine PRs logic
GrantBirki 94fd274
vendor
GrantBirki d45807b
use baseBranch defined by cli args
GrantBirki b6bba2e
remove `doNotCombineFromScratch` for now as it is confusing
GrantBirki 094ca7a
make `combineBranchName` and `workingBranchSuffix` cli args with defa…
GrantBirki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/cli/go-gh/v2/pkg/api" | ||
) | ||
|
||
func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClient *api.RESTClient, owner, repo string, matchedPRs []struct { | ||
Number int | ||
Title string | ||
Branch string | ||
Base string | ||
BaseSHA string | ||
}) error { | ||
// Define the combined branch name | ||
workingBranchName := combineBranchName + workingBranchSuffix | ||
|
||
baseBranchSHA, err := getBranchSHA(ctx, restClient, owner, repo, baseBranch) | ||
if err != nil { | ||
return fmt.Errorf("failed to get SHA of main branch: %w", err) | ||
} | ||
|
||
// Delete any pre-existing working branch | ||
err = deleteBranch(ctx, restClient, owner, repo, workingBranchName) | ||
if err != nil { | ||
Logger.Debug("Working branch not found, continuing", "branch", workingBranchName) | ||
} | ||
|
||
// Delete any pre-existing combined branch | ||
err = deleteBranch(ctx, restClient, owner, repo, combineBranchName) | ||
if err != nil { | ||
Logger.Debug("Combined branch not found, continuing", "branch", combineBranchName) | ||
} | ||
|
||
// Create the combined branch | ||
err = createBranch(ctx, restClient, owner, repo, combineBranchName, baseBranchSHA) | ||
if err != nil { | ||
return fmt.Errorf("failed to create combined branch: %w", err) | ||
} | ||
|
||
// Create the working branch | ||
err = createBranch(ctx, restClient, owner, repo, workingBranchName, baseBranchSHA) | ||
if err != nil { | ||
return fmt.Errorf("failed to create working branch: %w", err) | ||
} | ||
|
||
// Merge all PR branches into the working branch | ||
var combinedPRs []string | ||
var mergeFailedPRs []string | ||
for _, pr := range matchedPRs { | ||
err := mergeBranch(ctx, restClient, owner, repo, workingBranchName, pr.Branch) | ||
if err != nil { | ||
Logger.Warn("Failed to merge branch", "branch", pr.Branch, "error", err) | ||
mergeFailedPRs = append(mergeFailedPRs, fmt.Sprintf("#%d", pr.Number)) | ||
} else { | ||
Logger.Info("Merged branch", "branch", pr.Branch) | ||
combinedPRs = append(combinedPRs, fmt.Sprintf("#%d - %s", pr.Number, pr.Title)) | ||
} | ||
} | ||
|
||
// Update the combined branch to the latest commit of the working branch | ||
err = updateRef(ctx, restClient, owner, repo, combineBranchName, workingBranchName) | ||
if err != nil { | ||
return fmt.Errorf("failed to update combined branch: %w", err) | ||
} | ||
|
||
// Delete the temporary working branch | ||
err = deleteBranch(ctx, restClient, owner, repo, workingBranchName) | ||
if err != nil { | ||
Logger.Warn("Failed to delete working branch", "branch", workingBranchName, "error", err) | ||
} | ||
|
||
// Create the combined PR | ||
prBody := generatePRBody(combinedPRs, mergeFailedPRs) | ||
prTitle := "Combined PRs" | ||
err = createPullRequest(ctx, restClient, owner, repo, prTitle, combineBranchName, baseBranch, prBody) | ||
if err != nil { | ||
return fmt.Errorf("failed to create combined PR: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Get the SHA of a given branch | ||
func getBranchSHA(ctx context.Context, client *api.RESTClient, owner, repo, branch string) (string, error) { | ||
var ref struct { | ||
Object struct { | ||
SHA string `json:"sha"` | ||
} `json:"object"` | ||
} | ||
endpoint := fmt.Sprintf("repos/%s/%s/git/ref/heads/%s", owner, repo, branch) | ||
err := client.Get(endpoint, &ref) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get SHA of branch %s: %w", branch, err) | ||
} | ||
return ref.Object.SHA, nil | ||
} | ||
|
||
// generatePRBody generates the body for the combined PR | ||
func generatePRBody(combinedPRs, mergeFailedPRs []string) string { | ||
body := "✅ The following pull requests have been successfully combined:\n" | ||
for _, pr := range combinedPRs { | ||
body += "- " + pr + "\n" | ||
} | ||
if len(mergeFailedPRs) > 0 { | ||
body += "\n⚠️ The following pull requests could not be merged due to conflicts:\n" | ||
for _, pr := range mergeFailedPRs { | ||
body += "- " + pr + "\n" | ||
} | ||
} | ||
return body | ||
} | ||
|
||
// deleteBranch deletes a branch in the repository | ||
func deleteBranch(ctx context.Context, client *api.RESTClient, owner, repo, branch string) error { | ||
endpoint := fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", owner, repo, branch) | ||
return client.Delete(endpoint, nil) | ||
} | ||
|
||
// createBranch creates a new branch in the repository | ||
func createBranch(ctx context.Context, client *api.RESTClient, owner, repo, branch, sha string) error { | ||
endpoint := fmt.Sprintf("repos/%s/%s/git/refs", owner, repo) | ||
payload := map[string]string{ | ||
"ref": "refs/heads/" + branch, | ||
"sha": sha, | ||
} | ||
body, err := encodePayload(payload) | ||
if err != nil { | ||
return fmt.Errorf("failed to encode payload: %w", err) | ||
} | ||
return client.Post(endpoint, body, nil) | ||
} | ||
|
||
// mergeBranch merges a branch into the base branch | ||
func mergeBranch(ctx context.Context, client *api.RESTClient, owner, repo, base, head string) error { | ||
endpoint := fmt.Sprintf("repos/%s/%s/merges", owner, repo) | ||
payload := map[string]string{ | ||
"base": base, | ||
"head": head, | ||
} | ||
body, err := encodePayload(payload) | ||
if err != nil { | ||
return fmt.Errorf("failed to encode payload: %w", err) | ||
} | ||
return client.Post(endpoint, body, nil) | ||
} | ||
|
||
// updateRef updates a branch to point to the latest commit of another branch | ||
func updateRef(ctx context.Context, client *api.RESTClient, owner, repo, branch, sourceBranch string) error { | ||
// Get the SHA of the source branch | ||
var ref struct { | ||
Object struct { | ||
SHA string `json:"sha"` | ||
} `json:"object"` | ||
} | ||
endpoint := fmt.Sprintf("repos/%s/%s/git/ref/heads/%s", owner, repo, sourceBranch) | ||
err := client.Get(endpoint, &ref) | ||
if err != nil { | ||
return fmt.Errorf("failed to get SHA of source branch: %w", err) | ||
} | ||
|
||
// Update the branch to point to the new SHA | ||
endpoint = fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", owner, repo, branch) | ||
payload := map[string]interface{}{ | ||
"sha": ref.Object.SHA, | ||
"force": true, | ||
} | ||
body, err := encodePayload(payload) | ||
if err != nil { | ||
return fmt.Errorf("failed to encode payload: %w", err) | ||
} | ||
return client.Patch(endpoint, body, nil) | ||
} | ||
|
||
// createPullRequest creates a new pull request | ||
func createPullRequest(ctx context.Context, client *api.RESTClient, owner, repo, title, head, base, body string) error { | ||
endpoint := fmt.Sprintf("repos/%s/%s/pulls", owner, repo) | ||
payload := map[string]string{ | ||
"title": title, | ||
"head": head, | ||
"base": base, | ||
"body": body, | ||
} | ||
requestBody, err := encodePayload(payload) | ||
if err != nil { | ||
return fmt.Errorf("failed to encode payload: %w", err) | ||
} | ||
return client.Post(endpoint, requestBody, nil) | ||
} | ||
|
||
// encodePayload encodes a payload as JSON and returns an io.Reader | ||
func encodePayload(payload interface{}) (io.Reader, error) { | ||
data, err := json.Marshal(payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bytes.NewReader(data), nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.