|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + |
| 10 | + "github.com/cli/go-gh/v2/pkg/api" |
| 11 | +) |
| 12 | + |
| 13 | +func CombinePRs(ctx context.Context, graphQlClient *api.GraphQLClient, restClient *api.RESTClient, owner, repo string, matchedPRs []struct { |
| 14 | + Number int |
| 15 | + Title string |
| 16 | + Branch string |
| 17 | + Base string |
| 18 | + BaseSHA string |
| 19 | +}) error { |
| 20 | + // Define the combined branch name |
| 21 | + workingBranchName := combineBranchName + workingBranchSuffix |
| 22 | + |
| 23 | + baseBranchSHA, err := getBranchSHA(ctx, restClient, owner, repo, baseBranch) |
| 24 | + if err != nil { |
| 25 | + return fmt.Errorf("failed to get SHA of main branch: %w", err) |
| 26 | + } |
| 27 | + |
| 28 | + // Delete any pre-existing working branch |
| 29 | + err = deleteBranch(ctx, restClient, owner, repo, workingBranchName) |
| 30 | + if err != nil { |
| 31 | + Logger.Debug("Working branch not found, continuing", "branch", workingBranchName) |
| 32 | + } |
| 33 | + |
| 34 | + // Delete any pre-existing combined branch |
| 35 | + err = deleteBranch(ctx, restClient, owner, repo, combineBranchName) |
| 36 | + if err != nil { |
| 37 | + Logger.Debug("Combined branch not found, continuing", "branch", combineBranchName) |
| 38 | + } |
| 39 | + |
| 40 | + // Create the combined branch |
| 41 | + err = createBranch(ctx, restClient, owner, repo, combineBranchName, baseBranchSHA) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to create combined branch: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Create the working branch |
| 47 | + err = createBranch(ctx, restClient, owner, repo, workingBranchName, baseBranchSHA) |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("failed to create working branch: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Merge all PR branches into the working branch |
| 53 | + var combinedPRs []string |
| 54 | + var mergeFailedPRs []string |
| 55 | + for _, pr := range matchedPRs { |
| 56 | + err := mergeBranch(ctx, restClient, owner, repo, workingBranchName, pr.Branch) |
| 57 | + if err != nil { |
| 58 | + Logger.Warn("Failed to merge branch", "branch", pr.Branch, "error", err) |
| 59 | + mergeFailedPRs = append(mergeFailedPRs, fmt.Sprintf("#%d", pr.Number)) |
| 60 | + } else { |
| 61 | + Logger.Info("Merged branch", "branch", pr.Branch) |
| 62 | + combinedPRs = append(combinedPRs, fmt.Sprintf("#%d - %s", pr.Number, pr.Title)) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Update the combined branch to the latest commit of the working branch |
| 67 | + err = updateRef(ctx, restClient, owner, repo, combineBranchName, workingBranchName) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to update combined branch: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + // Delete the temporary working branch |
| 73 | + err = deleteBranch(ctx, restClient, owner, repo, workingBranchName) |
| 74 | + if err != nil { |
| 75 | + Logger.Warn("Failed to delete working branch", "branch", workingBranchName, "error", err) |
| 76 | + } |
| 77 | + |
| 78 | + // Create the combined PR |
| 79 | + prBody := generatePRBody(combinedPRs, mergeFailedPRs) |
| 80 | + prTitle := "Combined PRs" |
| 81 | + err = createPullRequest(ctx, restClient, owner, repo, prTitle, combineBranchName, baseBranch, prBody) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("failed to create combined PR: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// Get the SHA of a given branch |
| 90 | +func getBranchSHA(ctx context.Context, client *api.RESTClient, owner, repo, branch string) (string, error) { |
| 91 | + var ref struct { |
| 92 | + Object struct { |
| 93 | + SHA string `json:"sha"` |
| 94 | + } `json:"object"` |
| 95 | + } |
| 96 | + endpoint := fmt.Sprintf("repos/%s/%s/git/ref/heads/%s", owner, repo, branch) |
| 97 | + err := client.Get(endpoint, &ref) |
| 98 | + if err != nil { |
| 99 | + return "", fmt.Errorf("failed to get SHA of branch %s: %w", branch, err) |
| 100 | + } |
| 101 | + return ref.Object.SHA, nil |
| 102 | +} |
| 103 | + |
| 104 | +// generatePRBody generates the body for the combined PR |
| 105 | +func generatePRBody(combinedPRs, mergeFailedPRs []string) string { |
| 106 | + body := "✅ The following pull requests have been successfully combined:\n" |
| 107 | + for _, pr := range combinedPRs { |
| 108 | + body += "- " + pr + "\n" |
| 109 | + } |
| 110 | + if len(mergeFailedPRs) > 0 { |
| 111 | + body += "\n⚠️ The following pull requests could not be merged due to conflicts:\n" |
| 112 | + for _, pr := range mergeFailedPRs { |
| 113 | + body += "- " + pr + "\n" |
| 114 | + } |
| 115 | + } |
| 116 | + return body |
| 117 | +} |
| 118 | + |
| 119 | +// deleteBranch deletes a branch in the repository |
| 120 | +func deleteBranch(ctx context.Context, client *api.RESTClient, owner, repo, branch string) error { |
| 121 | + endpoint := fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", owner, repo, branch) |
| 122 | + return client.Delete(endpoint, nil) |
| 123 | +} |
| 124 | + |
| 125 | +// createBranch creates a new branch in the repository |
| 126 | +func createBranch(ctx context.Context, client *api.RESTClient, owner, repo, branch, sha string) error { |
| 127 | + endpoint := fmt.Sprintf("repos/%s/%s/git/refs", owner, repo) |
| 128 | + payload := map[string]string{ |
| 129 | + "ref": "refs/heads/" + branch, |
| 130 | + "sha": sha, |
| 131 | + } |
| 132 | + body, err := encodePayload(payload) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("failed to encode payload: %w", err) |
| 135 | + } |
| 136 | + return client.Post(endpoint, body, nil) |
| 137 | +} |
| 138 | + |
| 139 | +// mergeBranch merges a branch into the base branch |
| 140 | +func mergeBranch(ctx context.Context, client *api.RESTClient, owner, repo, base, head string) error { |
| 141 | + endpoint := fmt.Sprintf("repos/%s/%s/merges", owner, repo) |
| 142 | + payload := map[string]string{ |
| 143 | + "base": base, |
| 144 | + "head": head, |
| 145 | + } |
| 146 | + body, err := encodePayload(payload) |
| 147 | + if err != nil { |
| 148 | + return fmt.Errorf("failed to encode payload: %w", err) |
| 149 | + } |
| 150 | + return client.Post(endpoint, body, nil) |
| 151 | +} |
| 152 | + |
| 153 | +// updateRef updates a branch to point to the latest commit of another branch |
| 154 | +func updateRef(ctx context.Context, client *api.RESTClient, owner, repo, branch, sourceBranch string) error { |
| 155 | + // Get the SHA of the source branch |
| 156 | + var ref struct { |
| 157 | + Object struct { |
| 158 | + SHA string `json:"sha"` |
| 159 | + } `json:"object"` |
| 160 | + } |
| 161 | + endpoint := fmt.Sprintf("repos/%s/%s/git/ref/heads/%s", owner, repo, sourceBranch) |
| 162 | + err := client.Get(endpoint, &ref) |
| 163 | + if err != nil { |
| 164 | + return fmt.Errorf("failed to get SHA of source branch: %w", err) |
| 165 | + } |
| 166 | + |
| 167 | + // Update the branch to point to the new SHA |
| 168 | + endpoint = fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", owner, repo, branch) |
| 169 | + payload := map[string]interface{}{ |
| 170 | + "sha": ref.Object.SHA, |
| 171 | + "force": true, |
| 172 | + } |
| 173 | + body, err := encodePayload(payload) |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf("failed to encode payload: %w", err) |
| 176 | + } |
| 177 | + return client.Patch(endpoint, body, nil) |
| 178 | +} |
| 179 | + |
| 180 | +// createPullRequest creates a new pull request |
| 181 | +func createPullRequest(ctx context.Context, client *api.RESTClient, owner, repo, title, head, base, body string) error { |
| 182 | + endpoint := fmt.Sprintf("repos/%s/%s/pulls", owner, repo) |
| 183 | + payload := map[string]string{ |
| 184 | + "title": title, |
| 185 | + "head": head, |
| 186 | + "base": base, |
| 187 | + "body": body, |
| 188 | + } |
| 189 | + requestBody, err := encodePayload(payload) |
| 190 | + if err != nil { |
| 191 | + return fmt.Errorf("failed to encode payload: %w", err) |
| 192 | + } |
| 193 | + return client.Post(endpoint, requestBody, nil) |
| 194 | +} |
| 195 | + |
| 196 | +// encodePayload encodes a payload as JSON and returns an io.Reader |
| 197 | +func encodePayload(payload interface{}) (io.Reader, error) { |
| 198 | + data, err := json.Marshal(payload) |
| 199 | + if err != nil { |
| 200 | + return nil, err |
| 201 | + } |
| 202 | + return bytes.NewReader(data), nil |
| 203 | +} |
0 commit comments