-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from backstage/blam/renovate-changesets
feat: update rennovate changesets
- Loading branch information
Showing
17 changed files
with
332 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.