Skip to content

Commit

Permalink
Merge pull request #150 from backstage/blam/renovate-changesets
Browse files Browse the repository at this point in the history
feat: update rennovate changesets
  • Loading branch information
benjdlambert authored Apr 25, 2024
2 parents 4e62efd + a0d0f77 commit 6ec24e8
Show file tree
Hide file tree
Showing 17 changed files with 332 additions and 16 deletions.
21 changes: 21 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file modified .yarn/install-state.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions .yarn/sdks/typescript/bin/tsc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {createRequire} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
const absRequire = createRequire(absPnpApiPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
Expand Down
4 changes: 2 additions & 2 deletions .yarn/sdks/typescript/bin/tsserver
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {createRequire} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
const absRequire = createRequire(absPnpApiPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
Expand Down
4 changes: 2 additions & 2 deletions .yarn/sdks/typescript/lib/tsc.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {createRequire} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
const absRequire = createRequire(absPnpApiPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
Expand Down
6 changes: 4 additions & 2 deletions .yarn/sdks/typescript/lib/tsserver.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {createRequire} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
const absRequire = createRequire(absPnpApiPath);

const moduleWrapper = tsserver => {
if (!process.versions.pnp) {
Expand Down Expand Up @@ -109,6 +109,8 @@ const moduleWrapper = tsserver => {
str = `zip:${str}`;
} break;
}
} else {
str = str.replace(/^\/?/, process.platform === `win32` ? `` : `/`);
}
}

Expand Down
6 changes: 4 additions & 2 deletions .yarn/sdks/typescript/lib/tsserverlibrary.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {createRequire} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
const absRequire = createRequire(absPnpApiPath);

const moduleWrapper = tsserver => {
if (!process.versions.pnp) {
Expand Down Expand Up @@ -109,6 +109,8 @@ const moduleWrapper = tsserver => {
str = `zip:${str}`;
} break;
}
} else {
str = str.replace(/^\/?/, process.platform === `win32` ? `` : `/`);
}
}

Expand Down
10 changes: 5 additions & 5 deletions .yarn/sdks/typescript/lib/typescript.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env node

const {existsSync} = require(`fs`);
const {createRequire, createRequireFromPath} = require(`module`);
const {createRequire} = require(`module`);
const {resolve} = require(`path`);

const relPnpApiPath = "../../../../.pnp.cjs";

const absPnpApiPath = resolve(__dirname, relPnpApiPath);
const absRequire = (createRequire || createRequireFromPath)(absPnpApiPath);
const absRequire = createRequire(absPnpApiPath);

if (existsSync(absPnpApiPath)) {
if (!process.versions.pnp) {
// Setup the environment to be able to require typescript/lib/typescript.js
// Setup the environment to be able to require typescript
require(absPnpApiPath).setup();
}
}

// Defer to the real typescript/lib/typescript.js your application uses
module.exports = absRequire(`typescript/lib/typescript.js`);
// Defer to the real typescript your application uses
module.exports = absRequire(`typescript`);
6 changes: 5 additions & 1 deletion .yarn/sdks/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"name": "typescript",
"version": "4.7.4-sdk",
"main": "./lib/typescript.js",
"type": "commonjs"
"type": "commonjs",
"bin": {
"tsc": "./bin/tsc",
"tsserver": "./bin/tsserver"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"@actions/core": "^1.9.0",
"@actions/exec": "^1.1.1",
"@actions/github": "^5.0.3",
"@manypkg/get-packages": "^2.2.1",
"@octokit/auth-app": "^3.6.1",
Expand Down
11 changes: 11 additions & 0 deletions renovate-changesets/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Backstage Renovate Changeset Creator
description: Create changesets on the renovate bot PR's if needed
inputs:
multiple-workspaces:
description: If it's this repository is a collection of workspaces
required: false

outputs: {}
runs:
using: node16
main: ./entry.js
8 changes: 8 additions & 0 deletions renovate-changesets/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('../.pnp.cjs').setup();

require('ts-node').register({
transpileOnly: true,
project: require('path').resolve(__dirname, '../tsconfig.json'),
});

require('./index');
145 changes: 145 additions & 0 deletions renovate-changesets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import * as core from '@actions/core';
import {
commitAndPush,
createChangeset,
getBranchName,
getBumps,
getChangedFiles,
getChangesetFilename,
listPackages,
} from './renovateChangesets';
import { relative as relativePath, resolve as resolvePath } from 'path';

async function main() {
core.info('Running Renovate Changesets');

const isMultipleWorkspaces = core.getBooleanInput('multiple-workspaces', {
required: false,
});

const branchName = await getBranchName();

if (!branchName.startsWith('renovate/')) {
core.info('Not a renovate branch, skipping');
return;
}

const allPackages = await listPackages({
isMultipleWorkspaces,
includeRoots: true,
});

// Need to remove the topmost package if we're in a multi-workspace setup
const packageList = isMultipleWorkspaces
? allPackages.filter(p => p.dir !== process.cwd())
: allPackages;

const changedFiles = await getChangedFiles();

// Group file changes by workspace, and drop workspaces without changes
const changedFilesByWorkspace = new Map<string, string[]>(
packageList
.filter(p => p.isRoot)
.map(p => [
p.dir,
changedFiles
.filter(f => f.startsWith(p.relativeDir))
.map(f => relativePath(p.dir, f)),
])
.filter((workspaceChanges): workspaceChanges is [string, string[]] => {
const [_, files] = workspaceChanges;
return files.length > 0;
}),
);

// Check if those workspaces have changesets
const changedWorkspacesWithChangeset = new Map<string, boolean>(
Array.from(changedFilesByWorkspace.entries()).map(([workspace, files]) => [
workspace,
files.some(f => f.startsWith('.changeset/')),
]),
);

// If all packages have a changeset already then exit early.
if (
!changedWorkspacesWithChangeset.size ||
Array.from(changedWorkspacesWithChangeset.values()).every(v => v)
) {
core.info(
'No changesets to create, or all workspaces have changesets already',
);
return;
}

// Get all package.jsons that were changed
const changedPackageJsons = new Map<
string,
{
path: string;
localPath: string;
packageJson: { name: string; version: string };
}[]
>(
Array.from(changedFilesByWorkspace.entries())
.map(([workspace, files]) => [
workspace,
files.filter(f => f.endsWith('package.json')),
])
.filter((workspaceChanges): workspaceChanges is [string, string[]] => {
const [_, files] = workspaceChanges;
return files.length > 0;
})
.map(([workspace, files]) => [
workspace,
files.map(f => ({
path: f,
localPath: relativePath(process.cwd(), resolvePath(workspace, f)),
packageJson: require(resolvePath(workspace, f)),
})),
]),
);

if (!changedPackageJsons.size) {
core.info('Seems that no package.jsons were changed in this PR');
return;
}

// Get the bumps that happened in the last commit made by rennovate in the diff
const bumps = await Promise.all(
Array.from(changedPackageJsons.entries()).map(
async ([workspace, packages]) => {
const changes = await getBumps(packages.map(p => p.localPath));

return {
workspace,
packages,
changes,
};
},
),
);

const changesetFilename = await getChangesetFilename();
const changesetFiles: string[] = [];

// Create a changeset for each of the workspaces in the right place
for (const bump of bumps) {
const changesetFilePath = resolvePath(bump.workspace, changesetFilename);
changesetFiles.push(changesetFilePath);

await createChangeset(
changesetFilePath,
bump.changes,
bump.packages.map(p => p.packageJson.name),
);
}

// Commit and push all the changesets.
await commitAndPush(changesetFiles);
}

main().catch(error => {
core.error(error.stack);
core.setFailed(String(error));
process.exit(1);
});
Loading

0 comments on commit 6ec24e8

Please sign in to comment.