-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathgithub.go
More file actions
195 lines (184 loc) · 4.74 KB
/
github.go
File metadata and controls
195 lines (184 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"errors"
"os/exec"
"slices"
"strconv"
"strings"
"time"
)
// ghOutput runs a gh CLI command and returns trimmed stdout.
func ghOutput(args ...string) (string, error) {
cmd := exec.Command("gh", args...)
out, err := cmd.Output()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return "", exitErr
}
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// checkGHAuth verifies that the gh CLI is installed and
// authenticated. Returns true if gh is available.
func checkGHAuth() bool {
cmd := exec.Command("gh", "auth", "status")
cmd.Stdout = nil
cmd.Stderr = nil
return cmd.Run() == nil
}
// ghPR is a minimal pull request representation parsed from gh CLI
// JSON output.
type ghPR struct {
Number int `json:"number"`
Title string `json:"title"`
Author string `json:"author"`
Labels []string
}
// ghListOpenPRs returns open PRs targeting the given branch via
// the gh CLI.
func ghListOpenPRs(branch string) ([]ghPR, error) {
out, err := ghOutput("pr", "list",
"--repo", owner+"/"+repo,
"--base", branch,
"--state", "open",
"--json", "number,title,author",
"--jq", `.[] | "\(.number)\t\(.title)\t\(.author.login)"`,
)
if err != nil {
return nil, err
}
if out == "" {
return nil, nil
}
var prs []ghPR
for _, line := range strings.Split(out, "\n") {
parts := strings.SplitN(line, "\t", 3)
if len(parts) < 3 {
continue
}
num, _ := strconv.Atoi(parts[0])
prs = append(prs, ghPR{
Number: num,
Title: parts[1],
Author: parts[2],
})
}
return prs, nil
}
// ghListPRsWithLabel returns merged PRs targeting the given branch
// that have a specific label.
func ghListPRsWithLabel(branch, label string) ([]ghPR, error) {
out, err := ghOutput("pr", "list",
"--repo", owner+"/"+repo,
"--base", branch,
"--state", "merged",
"--label", label,
"--json", "number,title",
"--jq", `.[] | "\(.number)\t\(.title)"`,
)
if err != nil {
return nil, err
}
if out == "" {
return nil, nil
}
var prs []ghPR
for _, line := range strings.Split(out, "\n") {
parts := strings.SplitN(line, "\t", 2)
if len(parts) < 2 {
continue
}
num, _ := strconv.Atoi(parts[0])
prs = append(prs, ghPR{Number: num, Title: parts[1]})
}
return prs, nil
}
// prMetadata holds labels and author for a merged PR.
type prMetadata struct {
Labels []string
Author string
}
// prMetadataMaps holds PR metadata indexed by both merge-commit SHA
// and PR number. On release branches, commits are cherry-picked so
// their SHA differs from the original merge commit on main. The PR
// number (preserved in the commit title) provides a fallback lookup.
type prMetadataMaps struct {
bySHA map[string]prMetadata
byNumber map[int]prMetadata
}
// lookupCommit returns PR metadata for a commit, trying the full SHA
// first and falling back to PR number for cherry-picked commits.
func (m *prMetadataMaps) lookupCommit(fullSHA string, prNumber int) prMetadata {
if meta, ok := m.bySHA[fullSHA]; ok {
return meta
}
if prNumber > 0 {
return m.byNumber[prNumber]
}
return prMetadata{}
}
// ghBuildPRMetadataMap returns PR metadata indexed by both
// merge-commit SHA and PR number for merged PRs targeting main.
// This matches the bash script's approach of querying --base main
// with a date filter based on the oldest commit in the range.
func ghBuildPRMetadataMap(commits []commitEntry) (*prMetadataMaps, error) {
empty := &prMetadataMaps{
bySHA: make(map[string]prMetadata),
byNumber: make(map[int]prMetadata),
}
if len(commits) == 0 {
return empty, nil
}
// Find the earliest commit timestamp to scope the PR query.
earliest := commits[0].Timestamp
for _, c := range commits[1:] {
if c.Timestamp < earliest {
earliest = c.Timestamp
}
}
lookbackDate := time.Unix(earliest, 0).Format("2006-01-02")
out, err := ghOutput("pr", "list",
"--repo", owner+"/"+repo,
"--base", "main",
"--state", "merged",
"--limit", "10000",
"--search", "merged:>="+lookbackDate,
"--json", "number,mergeCommit,labels,author",
"--jq", `.[] | "\(.number)\t\(.mergeCommit.oid)\t\(.author.login)\t\([.labels[].name] | join(","))"`,
)
if err != nil {
return nil, err
}
if out == "" {
return empty, nil
}
result := &prMetadataMaps{
bySHA: make(map[string]prMetadata),
byNumber: make(map[int]prMetadata),
}
for _, line := range strings.Split(out, "\n") {
parts := strings.SplitN(line, "\t", 4)
if len(parts) < 4 {
continue
}
num, _ := strconv.Atoi(parts[0])
sha := parts[1]
author := parts[2]
var labels []string
if parts[3] != "" {
labels = strings.Split(parts[3], ",")
slices.Sort(labels)
}
meta := prMetadata{
Labels: labels,
Author: author,
}
result.bySHA[sha] = meta
if num > 0 {
result.byNumber[num] = meta
}
}
return result, nil
}