| 217 | } |
| 218 | |
| 219 | async function main() { |
| 220 | console.log("Fetching open PRs with beta label...") |
| 221 | |
| 222 | const stdout = |
| 223 | await $`gh pr list --state open --draft=false --label beta --json number,title,author,labels --limit 100`.text() |
| 224 | const prs: PR[] = JSON.parse(stdout).sort((a: PR, b: PR) => a.number - b.number) |
| 225 | |
| 226 | console.log(`Found ${prs.length} open PRs with beta label`) |
| 227 | |
| 228 | if (prs.length === 0) { |
| 229 | console.log("No team PRs to merge") |
| 230 | return |
| 231 | } |
| 232 | |
| 233 | console.log("Fetching latest dev branch...") |
| 234 | await $`git fetch origin dev` |
| 235 | |
| 236 | console.log("Checking out beta branch...") |
| 237 | await $`git checkout -B beta origin/dev` |
| 238 | |
| 239 | const applied: number[] = [] |
| 240 | const failed: FailedPR[] = [] |
| 241 | |
| 242 | for (const [idx, pr] of prs.entries()) { |
| 243 | console.log() |
| 244 | using _ = group(`Processing PR ${idx + 1}/${prs.length} #${pr.number}: ${pr.title}`) |
| 245 | console.log(" Fetching PR head...") |
| 246 | try { |
| 247 | await $`git fetch origin pull/${pr.number}/head:pr/${pr.number}` |
| 248 | } catch (err) { |
| 249 | console.log(` Failed to fetch: ${err}`) |
| 250 | failed.push({ number: pr.number, title: pr.title, reason: "Fetch failed" }) |
| 251 | await commentOnPR(pr.number, "Fetch failed") |
| 252 | continue |
| 253 | } |
| 254 | |
| 255 | console.log(" Merging...") |
| 256 | try { |
| 257 | await $`git merge --no-commit --no-ff pr/${pr.number}` |
| 258 | } catch { |
| 259 | const files = await conflicts() |
| 260 | if (files.length > 0) { |
| 261 | console.log(" Failed to merge (conflicts)") |
| 262 | if (!(await fix(pr, files, prs, applied, idx))) { |
| 263 | await cleanup() |
| 264 | failed.push({ number: pr.number, title: pr.title, reason: "Merge conflicts" }) |
| 265 | await commentOnPR(pr.number, "Merge conflicts with dev branch") |
| 266 | continue |
| 267 | } |
| 268 | } else { |
| 269 | console.log(" Failed to merge") |
| 270 | await cleanup() |
| 271 | failed.push({ number: pr.number, title: pr.title, reason: "Merge failed" }) |
| 272 | await commentOnPR(pr.number, "Merge failed") |
| 273 | continue |
| 274 | } |
| 275 | } |
| 276 | |