-
-
Notifications
You must be signed in to change notification settings - Fork 33
109 lines (103 loc) · 4.33 KB
/
sync-renovate-changesets.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
name: Sync Renovate changeset
on:
workflow_dispatch:
pull_request_target:
paths:
- ".github/workflows/sync-renovate-changesets.yml"
- "pnpm-lock.yaml"
jobs:
generate-changeset:
runs-on: ubuntu-latest
if: github.actor == 'renovate[bot]' && github.repository == 'psteinroe/supabase-cache-helpers'
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2
ref: ${{ github.head_ref }}
- name: Configure Git
run: |
git config --global user.email [email protected]
git config --global user.name 'Philipp Steinrötter (Bot)'
- name: Generate changeset
uses: actions/github-script@v7
with:
script: |
const { promises: fs } = require("fs");
// Parses package.json files and returns the package names
async function getPackagesNames(files) {
const names = [];
for (const file of files) {
const data = JSON.parse(await fs.readFile(file, "utf8"));
if (!data.private) {
names.push(data.name);
}
}
return names;
}
async function createChangeset(fileName, packageBumps, packages) {
let message = "";
for (const [pkg, bump] of packageBumps) {
message = message + `Updated dependency \`${pkg}\` to \`${bump}\`.\n`;
}
const pkgs = packages.map((pkg) => `'${pkg}': patch`).join("\n");
const body = `---\n${pkgs}\n---\n\n${message.trim()}\n`;
await fs.writeFile(fileName, body);
}
async function getBumps(files) {
const bumps = new Map();
for (const file of files) {
const data = JSON.parse(await fs.readFile(file, "utf8"));
const { stdout: changes } = await exec.getExecOutput("git", [
"show",
file,
]);
for (const change of changes.split("\n")) {
if (!change.startsWith("+ ")) {
continue;
}
const match = change.match(/"(.*?)"/g);
const pkg = match[0].replace(/"/g, "");
const bump = match[1].replace(/"/g, "");
// do not add changeset for dev dependencies
if ((data.dependencies && data.dependencies[pkg]) || (data.peerDependencies && data.peerDependencies[pkg])) {
bumps.set(pkg, bump);
} else {
console.log(`"${pkg}" is a dev dependency, skipping`)
}
}
}
return bumps;
}
const branch = await exec.getExecOutput("git branch --show-current");
if (!branch.stdout.startsWith("renovate/")) {
console.log("Not a renovate branch, skipping");
return;
}
const diffOutput = await exec.getExecOutput("git diff --name-only HEAD~1");
const diffFiles = diffOutput.stdout.split("\n");
if (diffFiles.find((f) => f.startsWith(".changeset"))) {
console.log("Changeset already exists, skipping");
return;
}
const files = diffFiles
.filter((file) => file !== "package.json") // skip root package.json
.filter((file) => file.includes("package.json"));
const packageNames = await getPackagesNames(files);
if (!packageNames.length) {
console.log("No package.json changes to published packages, skipping");
return;
}
const { stdout: shortHash } = await exec.getExecOutput(
"git rev-parse --short HEAD"
);
const fileName = `.changeset/renovate-${shortHash.trim()}.md`;
const packageBumps = await getBumps(files);
if (packageBumps.size === 0) {
console.log("No valid bumps, skipping");
return;
}
await createChangeset(fileName, packageBumps, packageNames);
await exec.exec("git", ["add", fileName]);
await exec.exec("git commit -C HEAD --amend --no-edit");
await exec.exec("git push --force");