| 1 | package github |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | ) |
| 6 | |
| 7 | // PRClient provides methods for interacting with GitHub PRs via the gh CLI. |
| 8 | // Deprecated: Use GHCLIClient (which implements the Client interface) instead. |
| 9 | // PRClient is kept for backward compatibility with existing test helpers. |
| 10 | type PRClient = GHCLIClient |
| 11 | |
| 12 | // NewPRClient creates a PRClient for the given owner/repo. |
| 13 | // Deprecated: Use NewGHCLIClient instead. |
| 14 | func NewPRClient(repo string) *PRClient { |
| 15 | return NewGHCLIClient(repo) |
| 16 | } |
| 17 | |
| 18 | // ParseCIStatus categorizes "gh pr checks" output into passing/failing/pending/unknown. |
| 19 | // This is a standalone pure-parsing function, not tied to any client. |
| 20 | func ParseCIStatus(output string) string { |
| 21 | var passing, failing, pending int |
| 22 | for _, line := range strings.Split(strings.TrimSpace(output), "\n") { |
| 23 | if line == "" { |
| 24 | continue |
| 25 | } |
| 26 | lower := strings.ToLower(line) |
| 27 | if strings.Contains(lower, "pass") || strings.Contains(lower, "skipping") { |
| 28 | passing++ |
| 29 | } else if strings.Contains(lower, "fail") { |
| 30 | failing++ |
| 31 | } else { |
| 32 | pending++ |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if failing > 0 { |
| 37 | return "failing" |
| 38 | } |
| 39 | if pending > 0 { |
| 40 | return "pending" |
| 41 | } |
| 42 | if passing > 0 { |
| 43 | return "passing" |
| 44 | } |
| 45 | // No checks matched any category |
| 46 | return "unknown" |
| 47 | } |
| 48 | |
| 49 | // MergeArgs returns arguments for merging a PR. Exported for testing. |
| 50 | func MergeArgs(prNumber, mergeMethod string, deleteBranch bool, repo string) []string { |
| 51 | args := []string{"pr", "merge"} |
| 52 | switch mergeMethod { |
| 53 | case "squash": |
| 54 | args = append(args, "--squash") |
| 55 | case "merge": |
| 56 | args = append(args, "--merge") |
| 57 | case "rebase": |
| 58 | args = append(args, "--rebase") |
| 59 | } |
| 60 | if deleteBranch { |
| 61 | args = append(args, "--delete-branch") |
| 62 | } |
| 63 | if repo != "" { |
| 64 | args = append(args, "--repo", repo) |
| 65 | } |
| 66 | args = append(args, "--", prNumber) |
| 67 | return args |
| 68 | } |
| 69 | |
| 70 | // UpdateBranchArgs builds the `gh pr update-branch` arg list. This is the |
| 71 | // native GitHub "Update branch" operation: it merges the base branch into the |
| 72 | // PR head so a behind-but-mergeable PR satisfies an up-to-date-branch |
| 73 | // protection rule. |
| 74 | func UpdateBranchArgs(prNumber, repo string) []string { |
| 75 | args := []string{"pr", "update-branch"} |
| 76 | if repo != "" { |
| 77 | args = append(args, "--repo", repo) |
| 78 | } |
| 79 | args = append(args, "--", prNumber) |
| 80 | return args |
| 81 | } |