Skip to content

Commit cd4075f

Browse files
authored
feat: add beta branch sync workflow for contributor PRs (#11190)
1 parent 33311e9 commit cd4075f

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

.github/workflows/beta.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: beta
2+
3+
on:
4+
push:
5+
branches: [dev]
6+
pull_request:
7+
types: [opened, synchronize, labeled, unlabeled]
8+
9+
jobs:
10+
sync:
11+
if: |
12+
github.event_name == 'push' ||
13+
(github.event_name == 'pull_request' &&
14+
contains(github.event.pull_request.labels.*.name, 'contributor'))
15+
runs-on: blacksmith-4vcpu-ubuntu-2404
16+
permissions:
17+
contents: write
18+
pull-requests: read
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
- name: Setup Bun
24+
uses: ./.github/actions/setup-bun
25+
26+
- name: Configure Git
27+
run: |
28+
git config user.name "github-actions[bot]"
29+
git config user.email "github-actions[bot]@users.noreply.github.com"
30+
31+
- name: Sync beta branch
32+
env:
33+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
run: bun script/beta.ts

.github/workflows/nix-desktop.yml.disabled

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
name: nix-desktop
22

33
on:
4+
push:
5+
branches: [dev]
6+
paths:
7+
- "flake.nix"
8+
- "flake.lock"
9+
- "nix/**"
10+
- "packages/app/**"
11+
- "packages/desktop/**"
12+
- ".github/workflows/nix-desktop.yml"
413
pull_request:
514
paths:
615
- "flake.nix"

.github/workflows/publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
branches:
77
- ci
88
- dev
9+
- beta
910
- snapshot-*
1011
workflow_dispatch:
1112
inputs:

script/beta.ts

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env bun
2+
3+
interface PR {
4+
number: number
5+
headRefName: string
6+
headRefOid: string
7+
createdAt: string
8+
isDraft: boolean
9+
title: string
10+
}
11+
12+
async function main() {
13+
console.log("Fetching open contributor PRs...")
14+
15+
const prsResult =
16+
await $`gh pr list --label contributor --state open --json number,headRefName,headRefOid,createdAt,isDraft,title --limit 100`.nothrow()
17+
if (prsResult.exitCode !== 0) {
18+
throw new Error(`Failed to fetch PRs: ${prsResult.stderr}`)
19+
}
20+
21+
const allPRs: PR[] = JSON.parse(prsResult.stdout)
22+
const prs = allPRs.filter((pr) => !pr.isDraft)
23+
24+
console.log(`Found ${prs.length} open non-draft contributor PRs`)
25+
26+
console.log("Fetching latest dev branch...")
27+
const fetchDev = await $`git fetch origin dev`.nothrow()
28+
if (fetchDev.exitCode !== 0) {
29+
throw new Error(`Failed to fetch dev branch: ${fetchDev.stderr}`)
30+
}
31+
32+
console.log("Checking out beta branch...")
33+
const checkoutBeta = await $`git checkout -B beta origin/dev`.nothrow()
34+
if (checkoutBeta.exitCode !== 0) {
35+
throw new Error(`Failed to checkout beta branch: ${checkoutBeta.stderr}`)
36+
}
37+
38+
const applied: number[] = []
39+
const skipped: Array<{ number: number; reason: string }> = []
40+
41+
for (const pr of prs) {
42+
console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
43+
44+
const fetchPR = await $`git fetch origin pull/${pr.number}/head:pr-${pr.number}`.nothrow()
45+
if (fetchPR.exitCode !== 0) {
46+
console.log(` Failed to fetch PR #${pr.number}, skipping`)
47+
skipped.push({ number: pr.number, reason: "Failed to fetch" })
48+
continue
49+
}
50+
51+
const merge = await $`git merge --squash pr-${pr.number}`.nothrow()
52+
if (merge.exitCode !== 0) {
53+
console.log(` Squash merge failed for PR #${pr.number}`)
54+
console.log(` Error: ${merge.stderr}`)
55+
await $`git reset --hard HEAD`.nothrow()
56+
skipped.push({ number: pr.number, reason: `Squash merge failed: ${merge.stderr}` })
57+
continue
58+
}
59+
60+
const add = await $`git add -A`.nothrow()
61+
if (add.exitCode !== 0) {
62+
console.log(` Failed to stage changes for PR #${pr.number}`)
63+
await $`git reset --hard HEAD`.nothrow()
64+
skipped.push({ number: pr.number, reason: "Failed to stage" })
65+
continue
66+
}
67+
68+
const status = await $`git status --porcelain`.nothrow()
69+
if (status.exitCode !== 0 || !status.stdout.trim()) {
70+
console.log(` No changes to commit for PR #${pr.number}, skipping`)
71+
await $`git reset --hard HEAD`.nothrow()
72+
skipped.push({ number: pr.number, reason: "No changes to commit" })
73+
continue
74+
}
75+
76+
const commitMsg = `Apply PR #${pr.number}: ${pr.title}`
77+
const commit = await Bun.spawn(["git", "commit", "-m", commitMsg], { stdout: "pipe", stderr: "pipe" })
78+
const commitExit = await commit.exited
79+
const commitStderr = await Bun.readableStreamToText(commit.stderr)
80+
81+
if (commitExit !== 0) {
82+
console.log(` Failed to commit PR #${pr.number}`)
83+
console.log(` Error: ${commitStderr}`)
84+
await $`git reset --hard HEAD`.nothrow()
85+
skipped.push({ number: pr.number, reason: `Commit failed: ${commitStderr}` })
86+
continue
87+
}
88+
89+
console.log(` Successfully applied PR #${pr.number}`)
90+
applied.push(pr.number)
91+
}
92+
93+
console.log("\n--- Summary ---")
94+
console.log(`Applied: ${applied.length} PRs`)
95+
applied.forEach((num) => console.log(` - PR #${num}`))
96+
console.log(`Skipped: ${skipped.length} PRs`)
97+
skipped.forEach((x) => console.log(` - PR #${x.number}: ${x.reason}`))
98+
99+
console.log("\nForce pushing beta branch...")
100+
const push = await $`git push origin beta --force`.nothrow()
101+
if (push.exitCode !== 0) {
102+
throw new Error(`Failed to push beta branch: ${push.stderr}`)
103+
}
104+
105+
console.log("Successfully synced beta branch")
106+
}
107+
108+
main().catch((err) => {
109+
console.error("Error:", err)
110+
process.exit(1)
111+
})
112+
113+
function $(strings: TemplateStringsArray, ...values: unknown[]) {
114+
const cmd = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ""), "")
115+
return {
116+
async nothrow() {
117+
const proc = Bun.spawn(cmd.split(" "), {
118+
stdout: "pipe",
119+
stderr: "pipe",
120+
})
121+
const exitCode = await proc.exited
122+
const stdout = await new Response(proc.stdout).text()
123+
const stderr = await new Response(proc.stderr).text()
124+
return { exitCode, stdout, stderr }
125+
},
126+
}
127+
}

0 commit comments

Comments
 (0)