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.
(commits []commitEntry)
| 134 | // This matches the bash script's approach of querying --base main |
| 135 | // with a date filter based on the oldest commit in the range. |
| 136 | func ghBuildPRMetadataMap(commits []commitEntry) (*prMetadataMaps, error) { |
| 137 | empty := &prMetadataMaps{ |
| 138 | bySHA: make(map[string]prMetadata), |
| 139 | byNumber: make(map[int]prMetadata), |
| 140 | } |
| 141 | if len(commits) == 0 { |
| 142 | return empty, nil |
| 143 | } |
| 144 | // Find the earliest commit timestamp to scope the PR query. |
| 145 | earliest := commits[0].Timestamp |
| 146 | for _, c := range commits[1:] { |
| 147 | if c.Timestamp < earliest { |
| 148 | earliest = c.Timestamp |
| 149 | } |
| 150 | } |
| 151 | lookbackDate := time.Unix(earliest, 0).Format("2006-01-02") |
| 152 | |
| 153 | out, err := ghOutput("pr", "list", |
| 154 | "--repo", owner+"/"+repo, |
| 155 | "--base", "main", |
| 156 | "--state", "merged", |
| 157 | "--limit", "10000", |
| 158 | "--search", "merged:>="+lookbackDate, |
| 159 | "--json", "number,mergeCommit,labels,author", |
| 160 | "--jq", `.[] | "\(.number)\t\(.mergeCommit.oid)\t\(.author.login)\t\([.labels[].name] | join(","))"`, |
| 161 | ) |
| 162 | if err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | if out == "" { |
| 166 | return empty, nil |
| 167 | } |
| 168 | result := &prMetadataMaps{ |
| 169 | bySHA: make(map[string]prMetadata), |
| 170 | byNumber: make(map[int]prMetadata), |
| 171 | } |
| 172 | for _, line := range strings.Split(out, "\n") { |
| 173 | parts := strings.SplitN(line, "\t", 4) |
| 174 | if len(parts) < 4 { |
| 175 | continue |
| 176 | } |
| 177 | num, _ := strconv.Atoi(parts[0]) |
| 178 | sha := parts[1] |
| 179 | author := parts[2] |
| 180 | var labels []string |
| 181 | if parts[3] != "" { |
| 182 | labels = strings.Split(parts[3], ",") |
| 183 | slices.Sort(labels) |
| 184 | } |
| 185 | meta := prMetadata{ |
| 186 | Labels: labels, |
| 187 | Author: author, |
| 188 | } |
| 189 | result.bySHA[sha] = meta |
| 190 | if num > 0 { |
| 191 | result.byNumber[num] = meta |
| 192 | } |
| 193 | } |
no test coverage detected