Skip to content

Commit a4c52e6

Browse files
chore: add workflow to check v3 patch status (#8962)
Co-authored-by: lrapoport-cf <[email protected]>
1 parent b0571fd commit a4c52e6

File tree

3 files changed

+137
-2
lines changed

3 files changed

+137
-2
lines changed

.github/workflows/open-v3-maintenance-prs.yml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ name: v3 Maintenance
22

33
on:
44
pull_request:
5-
branches: main
5+
branches: [main, v3-maintenance]
66
types: [synchronize, opened, reopened, labeled, unlabeled]
77
paths:
88
- ".changeset/**.md"
99

1010
jobs:
1111
open-pr:
12-
if: ${{ github.repository_owner == 'cloudflare' && !contains(github.event.pull_request.labels.*.name, 'skip-v3-pr') }}
12+
if: ${{
13+
github.repository_owner == 'cloudflare' &&
14+
github.base_ref == 'main' &&
15+
github.event.pull_request.draft == false &&
16+
contains(github.event.pull_request.labels.*.name, 'skip-v3-pr') == false
17+
}}
1318
name: Open backport PR for patches
1419
runs-on: macos-latest
1520
concurrency:
@@ -55,3 +60,26 @@ jobs:
5560
header: v3-backport
5661
message: |
5762
These changes have been automatically backported to Wrangler v3 :tada: You can view the automatically updated PR at https://github.com/cloudflare/workers-sdk/compare/v3-maintenance...v3-maintenance-${{ github.event.number }}. Please check that PR for correctness, and make sure it's merged after this one. Thank you for helping us keep Wrangler v3 supported!
63+
64+
check-status:
65+
if: ${{ github.repository_owner == 'cloudflare' && github.base_ref == 'v3-maintenance' }}
66+
name: Check v3 patch status
67+
runs-on: macos-latest
68+
concurrency:
69+
group: ${{ github.workflow }}-${{ github.ref }}
70+
cancel-in-progress: true
71+
timeout-minutes: 30
72+
steps:
73+
- name: Checkout Repo
74+
uses: actions/checkout@v4
75+
with:
76+
fetch-depth: 0
77+
78+
- name: Install Dependencies
79+
uses: ./.github/actions/install-dependencies
80+
81+
- name: Check if the original PR is merged
82+
run: node -r esbuild-register tools/deployments/check-v3-patch-status.ts
83+
env:
84+
GH_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
85+
BRANCH_NAME: ${{ github.head_ref }}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { validateBackportPR } from "../check-v3-patch-status";
3+
4+
describe("validateBackportPR", () => {
5+
it("should return false if the branch name does not match the pattern", () => {
6+
const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
7+
const consoleError = vi
8+
.spyOn(console, "error")
9+
.mockImplementation(() => {});
10+
const result = validateBackportPR("example/v3-maintenance-demo", () => {
11+
throw new Error("Unexpected");
12+
});
13+
14+
expect(result).toBe(false);
15+
expect(consoleLog).not.toBeCalled();
16+
expect(consoleError).toBeCalledWith(
17+
`❌ Branch name "example/v3-maintenance-demo" does not match the expected pattern "v3-maintenance-<PR_NUMBER>"`
18+
);
19+
});
20+
21+
it("should return false if the original PR is not merged", () => {
22+
const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
23+
const consoleError = vi
24+
.spyOn(console, "error")
25+
.mockImplementation(() => {});
26+
const isMergedMock = vi.fn(() => false);
27+
const result = validateBackportPR("v3-maintenance-13579", isMergedMock);
28+
29+
expect(result).toBe(false);
30+
expect(isMergedMock).toBeCalledWith("13579");
31+
expect(consoleLog).toBeCalledWith(`🔍 Checking if PR #13579 is merged...`);
32+
expect(consoleError).toBeCalledWith(`❌ PR #13579 is not merged.`);
33+
});
34+
35+
it("should return true if the original PR is merged", () => {
36+
const consoleLog = vi.spyOn(console, "log").mockImplementation(() => {});
37+
const isMergedMock = vi.fn(() => true);
38+
const result = validateBackportPR("v3-maintenance-2468", isMergedMock);
39+
40+
expect(result).toBe(true);
41+
expect(isMergedMock).toBeCalledWith("2468");
42+
expect(consoleLog).toBeCalledWith(`🔍 Checking if PR #2468 is merged...`);
43+
expect(consoleLog).toBeCalledWith(`✅ PR #2468 is merged.`);
44+
});
45+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { execSync } from "node:child_process";
2+
3+
/* eslint-disable turbo/no-undeclared-env-vars */
4+
if (require.main === module) {
5+
const branchName = process.env.BRANCH_NAME;
6+
7+
if (!branchName) {
8+
console.error("Missing BRANCH_NAME environment variables.");
9+
process.exit(1);
10+
}
11+
12+
if (!validateBackportPR(branchName, isPullRequestMerged)) {
13+
process.exit(1);
14+
}
15+
}
16+
17+
export function validateBackportPR(
18+
branchName: string,
19+
isMerged: (prNumber: string) => boolean
20+
): boolean {
21+
const prNumber = extractPullRequestNumber(branchName);
22+
23+
if (!prNumber) {
24+
console.error(
25+
`❌ Branch name "${branchName}" does not match the expected pattern "v3-maintenance-<PR_NUMBER>"`
26+
);
27+
return false;
28+
}
29+
30+
console.log(`🔍 Checking if PR #${prNumber} is merged...`);
31+
32+
try {
33+
if (isMerged(prNumber)) {
34+
console.log(`✅ PR #${prNumber} is merged.`);
35+
return true;
36+
} else {
37+
console.error(`❌ PR #${prNumber} is not merged.`);
38+
return false;
39+
}
40+
} catch (exception) {
41+
console.error(`❌ Failed to fetch PR #${prNumber}:`, exception);
42+
return false;
43+
}
44+
}
45+
46+
export function extractPullRequestNumber(branchName: string): string | null {
47+
const match = branchName.match(/^v3-maintenance-(\d+)$/);
48+
49+
if (match && match[1]) {
50+
return match[1];
51+
}
52+
53+
return null;
54+
}
55+
56+
export function isPullRequestMerged(prNumber: string): boolean {
57+
const result = execSync(`gh pr view ${prNumber} --json state --jq .state`)
58+
.toString()
59+
.trim();
60+
61+
return result === "MERGED";
62+
}

0 commit comments

Comments
 (0)