Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add workflow to automate renovate changesets #168

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/automate_renovate_changesets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Automate Renovate changeset
on:
pull_request_target:
paths:
- '.github/workflows/automate_renovate_changesets.yml'
- '**/yarn.lock'

jobs:
generate-changeset:
runs-on: ubuntu-latest
if: github.actor == 'renovate[bot]' && github.repository == 'redhat-developer/rhdh-plugins'
steps:
- name: Harden Runner
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
egress-policy: audit

- name: Checkout
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4.1.3
with:
fetch-depth: 2
ref: ${{ github.head_ref }}
token: ${{ secrets.RHDH_BOT_TOKEN }}

- name: Set up Node
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4
with:
node-version: 22
registry-url: https://registry.npmjs.org/ # Needed for auth

- name: Configure Git
run: |
git config --global user.email [email protected]
git config --global user.name 'Github changeset workflow'

- name: Generate changesets
run: node ./scripts/ci/generate-bump-changesets.js
128 changes: 128 additions & 0 deletions scripts/ci/generate-bump-changesets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { promisify } from 'util';
import { exec as execOriginal } from 'child_process';
import fs from 'fs/promises';
import path from 'path';

const exec = promisify(execOriginal);

async function main() {
console.log('Generating changesets for package.json dependency bumps...');
const { stdout: diffOutput } = await exec('git diff --name-only HEAD~1');

const diffFiles = diffOutput.split('\n');
if (diffFiles.find(f => path.matchesGlob(f, 'workspaces/*/.changeset/*'))) {
console.log('Changeset already exists, skipping');
return;
}
const files = diffFiles
.filter(file => file !== 'package.json') // skip root package.json
.filter(file =>
path.matchesGlob(file, 'workspaces/*/plugins/*/package.json'),
);

const workspaces = await getPackagesNamesByWorkspace(files);
if (!Object.keys(workspaces).length) {
console.log('No package.json changes found, skipping');
return;
}

const packageBumps = await getBumps(files);
if (packageBumps.size === 0) {
console.log('No bumps in published packages, skipping');
return;
}

const { stdout: shortHash } = await exec('git rev-parse --short HEAD');

for (const workspace of Object.keys(workspaces)) {
const fileName = `workspaces/${workspace}/.changeset/renovate-${shortHash.trim()}.md`;
console.log(`Creating changeset ${fileName}`);
await createChangeset(fileName, packageBumps, workspaces[workspace]);
await exec(`git add ${fileName}`);
}
await exec('git commit --amend --no-edit');
await exec('git push -f');
console.log(`Added changeset for commit ${shortHash.trim()}`);
}

// Parses package.json files and returns the package names
async function getPackagesNamesByWorkspace(files) {
const workspaces = {};
for (const file of files) {
const data = JSON.parse(await fs.readFile(file, 'utf8'));
const workspace = path
.dirname(path.resolve(file, '../..'))
.split(path.sep)
.pop();

if (!data.private) {
const names = workspaces[workspace] || [];
if (!workspaces[workspace]) {
workspaces[workspace] = names;
}
names.push(data.name);
}
}
return workspaces;
}

async function getBumps(files) {
const bumps = new Map();
for (const file of files) {
const { stdout: changes } = await exec(`git show ${file}`);
const packageDef = JSON.parse(
await fs.readFile(path.join(process.cwd(), file), 'utf8'),
);
for (const change of changes.split('\n')) {
if (!change.startsWith('+ ')) {
continue;
}

const match = change.match(/"(.*?)"/g);
const pkg = match[0].replace(/"/g, '');
const version = match[1].replace(/"/g, '');

// Only generate changesets for published packages
if (packageDef.private) {
console.log(`Ignoring bump in private package ${packageDef.name}`);
continue;
}

bumps.set(pkg, version);
}
}

return bumps;
}

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);
}

main().catch(error => {
console.error(error.stack || error);
process.exit(1);
});
Loading