Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1009e2755 | ||
|
|
9fe85a27d8 |
@@ -5,6 +5,10 @@ on:
|
|||||||
schedule:
|
schedule:
|
||||||
- cron: "0 * * * *"
|
- cron: "0 * * * *"
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: beta-sync
|
||||||
|
cancel-in-progress: false
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
sync:
|
sync:
|
||||||
runs-on: blacksmith-4vcpu-ubuntu-2404
|
runs-on: blacksmith-4vcpu-ubuntu-2404
|
||||||
|
|||||||
+162
-28
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import { $ } from "bun"
|
import { $ } from "bun"
|
||||||
|
|
||||||
|
const COMMENT_MARKER = "<!-- beta-sync:conflict:v1 -->"
|
||||||
|
|
||||||
interface PR {
|
interface PR {
|
||||||
number: number
|
number: number
|
||||||
title: string
|
title: string
|
||||||
@@ -13,24 +15,117 @@ interface FailedPR {
|
|||||||
number: number
|
number: number
|
||||||
title: string
|
title: string
|
||||||
reason: string
|
reason: string
|
||||||
|
conflicts: string[]
|
||||||
|
blockers: number[]
|
||||||
}
|
}
|
||||||
|
|
||||||
async function commentOnPR(prNumber: number, reason: string) {
|
interface AppliedPR {
|
||||||
const body = `⚠️ **Blocking Beta Release**
|
number: number
|
||||||
|
title: string
|
||||||
|
files: Set<string>
|
||||||
|
}
|
||||||
|
|
||||||
This PR cannot be merged into the beta branch due to: **${reason}**
|
interface IssueComment {
|
||||||
|
id: number
|
||||||
|
body: string | null
|
||||||
|
}
|
||||||
|
|
||||||
Please resolve this issue to include this PR in the next beta release.`
|
function blockerNumbers(conflicts: string[], applied: AppliedPR[]) {
|
||||||
|
return applied
|
||||||
|
.map((pr) => {
|
||||||
|
const score = conflicts.filter((file) => pr.files.has(file)).length
|
||||||
|
return { number: pr.number, score }
|
||||||
|
})
|
||||||
|
.filter((item) => item.score > 0)
|
||||||
|
.sort((a, b) => b.score - a.score || b.number - a.number)
|
||||||
|
.slice(0, 3)
|
||||||
|
.map((item) => item.number)
|
||||||
|
}
|
||||||
|
|
||||||
|
function commentBody(failed: FailedPR) {
|
||||||
|
const blockers = failed.blockers.length > 0 ? failed.blockers.map((num) => `#${num}`).join(", ") : "none identified"
|
||||||
|
const files = failed.conflicts.slice(0, 10)
|
||||||
|
const extra = failed.conflicts.length - files.length
|
||||||
|
const fileLines = files.length > 0 ? files.map((file) => `- \`${file}\``).join("\n") : "- none captured"
|
||||||
|
const extraLine = extra > 0 ? `\n- ...and ${extra} more` : ""
|
||||||
|
|
||||||
|
return `${COMMENT_MARKER}
|
||||||
|
⚠️ **Blocking Beta Release**
|
||||||
|
|
||||||
|
This PR cannot be merged into the beta branch.
|
||||||
|
|
||||||
|
**Reason:** ${failed.reason}
|
||||||
|
**Likely conflicting beta PR(s):** ${blockers}
|
||||||
|
|
||||||
|
**Conflicted files:**
|
||||||
|
${fileLines}${extraLine}
|
||||||
|
|
||||||
|
Please rebase onto latest \`dev\` and resolve conflicts with the listed beta PR(s) before the next beta sync.`
|
||||||
|
}
|
||||||
|
|
||||||
|
async function repositoryName() {
|
||||||
|
const repo = process.env["GITHUB_REPOSITORY"] ?? process.env["GH_REPO"]
|
||||||
|
if (repo) return repo
|
||||||
|
return (await $`gh repo view --json nameWithOwner --jq .nameWithOwner`.text()).trim()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function prFiles(prNumber: number, cache: Map<number, Set<string>>) {
|
||||||
|
const cached = cache.get(prNumber)
|
||||||
|
if (cached) return cached
|
||||||
|
const stdout = await $`gh pr view ${prNumber} --json files --jq .files[].path`.text()
|
||||||
|
const files = new Set<string>(
|
||||||
|
stdout
|
||||||
|
.split("\n")
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter((line) => line.length > 0),
|
||||||
|
)
|
||||||
|
cache.set(prNumber, files)
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
|
async function conflictFiles() {
|
||||||
|
const stdout = await $`git diff --name-only --diff-filter=U`.text()
|
||||||
|
return stdout
|
||||||
|
.split("\n")
|
||||||
|
.map((line) => line.trim())
|
||||||
|
.filter((line) => line.length > 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function cleanupMergeState() {
|
||||||
|
await $`git merge --abort`.nothrow()
|
||||||
|
await $`git checkout -- .`.nothrow()
|
||||||
|
await $`git clean -fd`.nothrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function upsertComment(repo: string, prNumber: number, body: string) {
|
||||||
|
const stdout = await $`gh api repos/${repo}/issues/${prNumber}/comments --paginate`.text()
|
||||||
|
const comments = JSON.parse(stdout) as IssueComment[]
|
||||||
|
const existing = comments.filter((item) => item.body?.includes(COMMENT_MARKER)).at(-1)
|
||||||
|
if (!existing) {
|
||||||
|
await $`gh api repos/${repo}/issues/${prNumber}/comments --method POST --field body=${body}`
|
||||||
|
console.log(` Posted comment on PR #${prNumber}`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (existing.body?.trim() === body.trim()) {
|
||||||
|
console.log(` Comment already up to date on PR #${prNumber}`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await $`gh api repos/${repo}/issues/comments/${existing.id} --method PATCH --field body=${body}`
|
||||||
|
console.log(` Updated comment on PR #${prNumber}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function commentOnPR(repo: string, failed: FailedPR) {
|
||||||
|
const body = commentBody(failed)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await $`gh pr comment ${prNumber} --body ${body}`
|
await upsertComment(repo, failed.number, body)
|
||||||
console.log(` Posted comment on PR #${prNumber}`)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(` Failed to post comment on PR #${prNumber}: ${err}`)
|
console.log(` Failed to post comment on PR #${failed.number}: ${err}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
|
const repo = await repositoryName()
|
||||||
console.log("Fetching open PRs with beta label...")
|
console.log("Fetching open PRs with beta label...")
|
||||||
|
|
||||||
const stdout = await $`gh pr list --state open --label beta --json number,title,author,labels --limit 100`.text()
|
const stdout = await $`gh pr list --state open --label beta --json number,title,author,labels --limit 100`.text()
|
||||||
@@ -49,19 +144,33 @@ async function main() {
|
|||||||
console.log("Checking out beta branch...")
|
console.log("Checking out beta branch...")
|
||||||
await $`git checkout -B beta origin/dev`
|
await $`git checkout -B beta origin/dev`
|
||||||
|
|
||||||
const applied: number[] = []
|
const applied: AppliedPR[] = []
|
||||||
const failed: FailedPR[] = []
|
const failed: FailedPR[] = []
|
||||||
|
const cache = new Map<number, Set<string>>()
|
||||||
|
|
||||||
for (const pr of prs) {
|
for (const pr of prs) {
|
||||||
console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
|
console.log(`\nProcessing PR #${pr.number}: ${pr.title}`)
|
||||||
|
let files = new Set<string>()
|
||||||
|
try {
|
||||||
|
files = await prFiles(pr.number, cache)
|
||||||
|
} catch (err) {
|
||||||
|
console.log(` Failed to fetch PR files metadata: ${err}`)
|
||||||
|
}
|
||||||
|
|
||||||
console.log(" Fetching PR head...")
|
console.log(" Fetching PR head...")
|
||||||
try {
|
try {
|
||||||
await $`git fetch origin pull/${pr.number}/head:pr/${pr.number}`
|
await $`git fetch origin pull/${pr.number}/head:pr/${pr.number}`
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(` Failed to fetch: ${err}`)
|
console.log(` Failed to fetch: ${err}`)
|
||||||
failed.push({ number: pr.number, title: pr.title, reason: "Fetch failed" })
|
const failure = {
|
||||||
await commentOnPR(pr.number, "Fetch failed")
|
number: pr.number,
|
||||||
|
title: pr.title,
|
||||||
|
reason: "Fetch failed",
|
||||||
|
conflicts: [],
|
||||||
|
blockers: [],
|
||||||
|
}
|
||||||
|
failed.push(failure)
|
||||||
|
await commentOnPR(repo, failure)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,17 +179,22 @@ async function main() {
|
|||||||
await $`git merge --no-commit --no-ff pr/${pr.number}`
|
await $`git merge --no-commit --no-ff pr/${pr.number}`
|
||||||
} catch {
|
} catch {
|
||||||
console.log(" Failed to merge (conflicts)")
|
console.log(" Failed to merge (conflicts)")
|
||||||
try {
|
const conflicts = await conflictFiles().catch(() => [])
|
||||||
await $`git merge --abort`
|
const blockers = blockerNumbers(conflicts, applied)
|
||||||
} catch {}
|
const reason =
|
||||||
try {
|
blockers.length > 0
|
||||||
await $`git checkout -- .`
|
? `Merge conflicts with beta stack (likely: ${blockers.map((num) => `#${num}`).join(", ")})`
|
||||||
} catch {}
|
: "Merge conflicts while applying onto beta stack"
|
||||||
try {
|
const failure = {
|
||||||
await $`git clean -fd`
|
number: pr.number,
|
||||||
} catch {}
|
title: pr.title,
|
||||||
failed.push({ number: pr.number, title: pr.title, reason: "Merge conflicts" })
|
reason,
|
||||||
await commentOnPR(pr.number, "Merge conflicts with dev branch")
|
conflicts,
|
||||||
|
blockers,
|
||||||
|
}
|
||||||
|
await cleanupMergeState()
|
||||||
|
failed.push(failure)
|
||||||
|
await commentOnPR(repo, failure)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,8 +209,16 @@ async function main() {
|
|||||||
await $`git add -A`
|
await $`git add -A`
|
||||||
} catch {
|
} catch {
|
||||||
console.log(" Failed to stage changes")
|
console.log(" Failed to stage changes")
|
||||||
failed.push({ number: pr.number, title: pr.title, reason: "Staging failed" })
|
await cleanupMergeState()
|
||||||
await commentOnPR(pr.number, "Failed to stage changes")
|
const failure = {
|
||||||
|
number: pr.number,
|
||||||
|
title: pr.title,
|
||||||
|
reason: "Staging failed",
|
||||||
|
conflicts: [],
|
||||||
|
blockers: [],
|
||||||
|
}
|
||||||
|
failed.push(failure)
|
||||||
|
await commentOnPR(repo, failure)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,22 +227,34 @@ async function main() {
|
|||||||
await $`git commit -m ${commitMsg}`
|
await $`git commit -m ${commitMsg}`
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(` Failed to commit: ${err}`)
|
console.log(` Failed to commit: ${err}`)
|
||||||
failed.push({ number: pr.number, title: pr.title, reason: "Commit failed" })
|
await cleanupMergeState()
|
||||||
await commentOnPR(pr.number, "Failed to commit changes")
|
const failure = {
|
||||||
|
number: pr.number,
|
||||||
|
title: pr.title,
|
||||||
|
reason: "Commit failed",
|
||||||
|
conflicts: [],
|
||||||
|
blockers: [],
|
||||||
|
}
|
||||||
|
failed.push(failure)
|
||||||
|
await commentOnPR(repo, failure)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(" Applied successfully")
|
console.log(" Applied successfully")
|
||||||
applied.push(pr.number)
|
applied.push({ number: pr.number, title: pr.title, files })
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("\n--- Summary ---")
|
console.log("\n--- Summary ---")
|
||||||
console.log(`Applied: ${applied.length} PRs`)
|
console.log(`Applied: ${applied.length} PRs`)
|
||||||
applied.forEach((num) => console.log(` - PR #${num}`))
|
applied.forEach((pr) => console.log(` - PR #${pr.number}`))
|
||||||
|
|
||||||
if (failed.length > 0) {
|
if (failed.length > 0) {
|
||||||
console.log(`Failed: ${failed.length} PRs`)
|
console.log(`Failed: ${failed.length} PRs`)
|
||||||
failed.forEach((f) => console.log(` - PR #${f.number}: ${f.reason}`))
|
failed.forEach((item) => {
|
||||||
|
const blockers =
|
||||||
|
item.blockers.length > 0 ? ` (likely with ${item.blockers.map((num) => `#${num}`).join(", ")})` : ""
|
||||||
|
console.log(` - PR #${item.number}: ${item.reason}${blockers}`)
|
||||||
|
})
|
||||||
throw new Error(`${failed.length} PR(s) failed to merge`)
|
throw new Error(`${failed.length} PR(s) failed to merge`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user