|
| 1 | +import { execSync } from "node:child_process"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import parseChangeset from "@changesets/parse"; |
| 4 | + |
| 5 | +/* eslint-disable turbo/no-undeclared-env-vars */ |
| 6 | +if (require.main === module) { |
| 7 | + const parsedLabels = JSON.parse(process.env.LABELS as string) as string[]; |
| 8 | + if ( |
| 9 | + isWranglerPatch(process.env.FILES as string) && |
| 10 | + !parsedLabels.includes("skip-v3-pr") |
| 11 | + ) { |
| 12 | + // Create a new branch for the v3 maintenance PR |
| 13 | + execSync(`git checkout -b v3-maintenance-${process.env.PR_NUMBER} -f`); |
| 14 | + |
| 15 | + execSync(`git push origin HEAD --force`); |
| 16 | + |
| 17 | + try { |
| 18 | + // Open PR |
| 19 | + execSync( |
| 20 | + `gh pr create --base main --head v3-maintenance-${process.env.PR_NUMBER} --label "skip-pr-description-validation" --label "skip-v3-pr" --title "Backport #${process.env.PR_NUMBER} to Wrangler v3" --body "This is an automatically opened PR to backport patch changes from #${process.env.PR_NUMBER} to Wrangler v3"` |
| 21 | + ); |
| 22 | + } catch { |
| 23 | + // Ignore "PR already created failures" |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export function isWranglerPatch(changedFilesJson: string) { |
| 29 | + const changedFiles = JSON.parse(changedFilesJson) as string[]; |
| 30 | + const changesets = changedFiles |
| 31 | + .filter((f) => f.startsWith(".changeset/")) |
| 32 | + .map((c) => parseChangeset(readFileSync(c, "utf8"))); |
| 33 | + |
| 34 | + let hasWranglerPatch = false; |
| 35 | + for (const changeset of changesets) { |
| 36 | + for (const release of changeset.releases) { |
| 37 | + if (release.name === "wrangler" && release.type === "patch") { |
| 38 | + hasWranglerPatch = true; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + return hasWranglerPatch; |
| 43 | +} |
0 commit comments