88jobs :
99 main :
1010 timeout-minutes : 5
11- name : Add new entry
1211 runs-on : ubuntu-latest
1312 if : github.actor == 'renovate[bot]'
1413 steps :
1514 - name : Checkout
16- uses : actions/checkout@v4.1.1
15+ uses : actions/checkout@v4
1716 with :
1817 fetch-depth : 2
1918 ref : ${{ github.head_ref }}
@@ -22,77 +21,96 @@ jobs:
2221 git config --global user.email adbayb@gmail.com
2322 git config --global user.name 'Ayoub Adib'
2423 - name : Create changelog entry
25- uses : actions/github-script@v7.0.1
24+ uses : actions/github-script@v7
2625 # Credits to https://github.com/backstage/backstage/blob/bcc11651add5a2589898dfb9d665ebb9d412201b/.github/workflows/sync_renovate-changesets.yml
2726 with :
2827 script : |
2928 const { promises: fs } = require("fs");
30- // Parses package.json files and returns the package names
29+
3130 async function getPackagesNames(files) {
3231 const names = [];
32+
3333 for (const file of files) {
3434 const data = JSON.parse(await fs.readFile(file, "utf8"));
35+
3536 if (!data.private) {
3637 names.push(data.name);
3738 }
3839 }
40+
3941 return names;
4042 }
4143
4244 async function createChangeset(fileName, packageBumps, packages) {
4345 let message = "";
46+
4447 for (const [pkg, bump] of packageBumps) {
4548 message = message + `Updated dependency \`${pkg}\` to \`${bump}\`.\n`;
4649 }
4750
48- const pkgs = packages.map((pkg) => `' ${pkg}': patch `).join("\n");
51+ const pkgs = packages.map((pkg) => `" ${pkg}": minor `).join("\n");
4952 const body = `---\n${pkgs}\n---\n\n${message.trim()}\n`;
53+
5054 await fs.writeFile(fileName, body);
5155 }
5256
5357 async function getBumps(files) {
5458 const bumps = new Map();
59+
5560 for (const file of files) {
5661 const { stdout: changes } = await exec.getExecOutput("git", [
5762 "show",
5863 file,
5964 ]);
65+
6066 for (const change of changes.split("\n")) {
61- if (!change.startsWith("+ " )) {
67+ if (!change.match(/^\+\s/ )) {
6268 continue;
6369 }
70+
6471 const match = change.match(/"(.*?)"/g);
72+
6573 bumps.set(match[0].replace(/"/g, ""), match[1].replace(/"/g, ""));
6674 }
6775 }
76+
6877 return bumps;
6978 }
7079
7180 const branch = await exec.getExecOutput("git branch --show-current");
81+
7282 if (!branch.stdout.startsWith("renovate/")) {
7383 console.log("Not a renovate branch, skipping");
84+
7485 return;
7586 }
87+
7688 const diffOutput = await exec.getExecOutput("git diff --name-only HEAD~1");
7789 const diffFiles = diffOutput.stdout.split("\n");
90+
7891 if (diffFiles.find((f) => f.startsWith(".changeset"))) {
7992 console.log("Changeset already exists, skipping");
93+
8094 return;
8195 }
96+
8297 const files = diffFiles
8398 .filter((file) => file !== "package.json") // skip root package.json
8499 .filter((file) => file.includes("package.json"));
85100 const packageNames = await getPackagesNames(files);
101+
86102 if (!packageNames.length) {
87103 console.log("No package.json changes to published packages, skipping");
104+
88105 return;
89106 }
107+
90108 const { stdout: shortHash } = await exec.getExecOutput(
91109 "git rev-parse --short HEAD"
92110 );
93111 const fileName = `.changeset/renovate-${shortHash.trim()}.md`;
94-
95112 const packageBumps = await getBumps(files);
113+
96114 await createChangeset(fileName, packageBumps, packageNames);
97115 await exec.exec("git", ["add", fileName]);
98116 await exec.exec("git commit -C HEAD --amend --no-edit");
0 commit comments