Skip to content

Commit

Permalink
feat(changesets-renovate): add option to skip branch check (#2133)
Browse files Browse the repository at this point in the history
* feat(changesets-renovate): add option to skip branch check

* feat(changeset-renovate): add option to have custom renovate branch prefix

* chore(changeset-renovate): update readme

---------

Co-authored-by: ImOverlord <[email protected]>
Co-authored-by: philibeaux <[email protected]>
  • Loading branch information
3 people authored Aug 13, 2024
1 parent 724f188 commit 2597f61
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fresh-zebras-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scaleway/changesets-renovate": minor
---

Add `SKIP_BRANCH_CHECK` as an option to not check branch name
12 changes: 12 additions & 0 deletions packages/changesets-renovate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ To skip committing the changeset.
SKIP_COMMIT=TRUE changesets-renovate
```

To have a custom prefix for renovate branch name instead of `renovate/`

```bash
BRANCH_PREFIX=dep-upgrade changesets-renovate
```

To skip checking the branch name starts `renovate/`

```bash
SKIP_BRANCH_CHECK=TRUE changesets-renovate
```

It's inspired by this GitHub Action from Backstage: https://github.com/backstage/backstage/blob/master/.github/workflows/sync_renovate-changesets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,49 @@ Updated dependency \`package2\` to \`version2\`.
],
}
`;
exports[`generate changeset file > should not skip if branch starts with custom branch prefix 1`] = `
[MockFunction spy] {
"calls": [
[
".changeset/renovate-test.md",
"---
'packageName': patch
---
Updated dependency \`package\` to \`version\`.
Updated dependency \`package2\` to \`version2\`.
",
],
],
"results": [
{
"type": "return",
"value": undefined,
},
],
}
`;
exports[`generate changeset file > should not skip if not in renovate branch, when branch check skip is true 1`] = `
[MockFunction spy] {
"calls": [
[
".changeset/renovate-test.md",
"---
'packageName': patch
---
Updated dependency \`package\` to \`version\`.
Updated dependency \`package2\` to \`version2\`.
",
],
],
"results": [
{
"type": "return",
"value": undefined,
},
],
}
`;
100 changes: 100 additions & 0 deletions packages/changesets-renovate/src/__tests__/generate-changeset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,106 @@ describe('generate changeset file', () => {
expect(console.log).toHaveBeenCalledWith('Not a renovate branch, skipping')
})

it('should not skip if branch starts with custom branch prefix', async () => {
const rev = 'test'
const fileName = `.changeset/renovate-${rev}.md`
const file = 'test/package.json'
const revparse = vi.fn().mockReturnValue(rev)
const add = vi.fn()
const commit = vi.fn()
const push = vi.fn()

mockSimpleGit.mockReturnValue({
branch: () => ({
current: 'dep-upgrade/test',
}),
diffSummary: () => ({
files: [
{
file,
},
],
}),
show: () => `
+ "package": "version"
+ "package2": "version2"
`,
revparse,
add,
commit,
push,
})

fs.readFile = vi
.fn()
.mockResolvedValueOnce(`{}`)
.mockResolvedValueOnce(`{"name":"packageName","version":"1.0.0"}`)
fs.writeFile = vi.fn()

process.env['BRANCH_PREFIX'] = 'dep-upgrade/'
await run()
process.env['BRANCH_PREFIX'] = undefined

expect(console.log).not.toHaveBeenCalledWith(
'Not a renovate branch, skipping',
)
expect(fs.readFile).toHaveBeenCalledWith(file, 'utf8')
expect(fs.writeFile).toMatchSnapshot()
expect(add).toHaveBeenCalledWith(fileName)
expect(commit).toHaveBeenCalledWith(`chore: add changeset renovate-${rev}`)
expect(push).toHaveBeenCalledTimes(1)
})

it('should not skip if not in renovate branch, when branch check skip is true', async () => {
const rev = 'test'
const fileName = `.changeset/renovate-${rev}.md`
const file = 'test/package.json'
const revparse = vi.fn().mockReturnValue(rev)
const add = vi.fn()
const commit = vi.fn()
const push = vi.fn()

mockSimpleGit.mockReturnValue({
branch: () => ({
current: 'main',
}),
diffSummary: () => ({
files: [
{
file,
},
],
}),
show: () => `
+ "package": "version"
+ "package2": "version2"
`,
revparse,
add,
commit,
push,
})

fs.readFile = vi
.fn()
.mockResolvedValueOnce(`{}`)
.mockResolvedValueOnce(`{"name":"packageName","version":"1.0.0"}`)
fs.writeFile = vi.fn()

process.env['SKIP_BRANCH_CHECK'] = 'TRUE'
await run()
process.env['SKIP_BRANCH_CHECK'] = undefined

expect(console.log).not.toHaveBeenCalledWith(
'Not a renovate branch, skipping',
)
expect(fs.readFile).toHaveBeenCalledWith(file, 'utf8')
expect(fs.writeFile).toMatchSnapshot()
expect(add).toHaveBeenCalledWith(fileName)
expect(commit).toHaveBeenCalledWith(`chore: add changeset renovate-${rev}`)
expect(push).toHaveBeenCalledTimes(1)
})

it('should skip if .changeset is already modified', async () => {
mockSimpleGit.mockReturnValue({
...defaultGitValues,
Expand Down
6 changes: 5 additions & 1 deletion packages/changesets-renovate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ async function getBumps(files: string[]): Promise<Map<string, string>> {

export async function run() {
const branch = await simpleGit().branch()
const branchPrefix = process.env['BRANCH_PREFIX'] ?? 'renovate/'

console.log('Detected branch:', branch)

if (!branch.current.startsWith('renovate/')) {
if (
!branch.current.startsWith(branchPrefix) &&
!process.env['SKIP_BRANCH_CHECK']
) {
console.log('Not a renovate branch, skipping')

return
Expand Down

0 comments on commit 2597f61

Please sign in to comment.