Skip to content

Commit 2597f61

Browse files
ImOverlordphilibea
andauthored
feat(changesets-renovate): add option to skip branch check (#2133)
* 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]>
1 parent 724f188 commit 2597f61

File tree

5 files changed

+168
-1
lines changed

5 files changed

+168
-1
lines changed

.changeset/fresh-zebras-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@scaleway/changesets-renovate": minor
3+
---
4+
5+
Add `SKIP_BRANCH_CHECK` as an option to not check branch name

packages/changesets-renovate/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ To skip committing the changeset.
2222
SKIP_COMMIT=TRUE changesets-renovate
2323
```
2424

25+
To have a custom prefix for renovate branch name instead of `renovate/`
26+
27+
```bash
28+
BRANCH_PREFIX=dep-upgrade changesets-renovate
29+
```
30+
31+
To skip checking the branch name starts `renovate/`
32+
33+
```bash
34+
SKIP_BRANCH_CHECK=TRUE changesets-renovate
35+
```
36+
2537
It's inspired by this GitHub Action from Backstage: https://github.com/backstage/backstage/blob/master/.github/workflows/sync_renovate-changesets.yml

packages/changesets-renovate/src/__tests__/__snapshots__/generate-changeset.test.ts.snap

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,49 @@ Updated dependency \`package2\` to \`version2\`.
4545
],
4646
}
4747
`;
48+
49+
exports[`generate changeset file > should not skip if branch starts with custom branch prefix 1`] = `
50+
[MockFunction spy] {
51+
"calls": [
52+
[
53+
".changeset/renovate-test.md",
54+
"---
55+
'packageName': patch
56+
---
57+
58+
Updated dependency \`package\` to \`version\`.
59+
Updated dependency \`package2\` to \`version2\`.
60+
",
61+
],
62+
],
63+
"results": [
64+
{
65+
"type": "return",
66+
"value": undefined,
67+
},
68+
],
69+
}
70+
`;
71+
72+
exports[`generate changeset file > should not skip if not in renovate branch, when branch check skip is true 1`] = `
73+
[MockFunction spy] {
74+
"calls": [
75+
[
76+
".changeset/renovate-test.md",
77+
"---
78+
'packageName': patch
79+
---
80+
81+
Updated dependency \`package\` to \`version\`.
82+
Updated dependency \`package2\` to \`version2\`.
83+
",
84+
],
85+
],
86+
"results": [
87+
{
88+
"type": "return",
89+
"value": undefined,
90+
},
91+
],
92+
}
93+
`;

packages/changesets-renovate/src/__tests__/generate-changeset.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,106 @@ describe('generate changeset file', () => {
2525
expect(console.log).toHaveBeenCalledWith('Not a renovate branch, skipping')
2626
})
2727

28+
it('should not skip if branch starts with custom branch prefix', async () => {
29+
const rev = 'test'
30+
const fileName = `.changeset/renovate-${rev}.md`
31+
const file = 'test/package.json'
32+
const revparse = vi.fn().mockReturnValue(rev)
33+
const add = vi.fn()
34+
const commit = vi.fn()
35+
const push = vi.fn()
36+
37+
mockSimpleGit.mockReturnValue({
38+
branch: () => ({
39+
current: 'dep-upgrade/test',
40+
}),
41+
diffSummary: () => ({
42+
files: [
43+
{
44+
file,
45+
},
46+
],
47+
}),
48+
show: () => `
49+
+ "package": "version"
50+
+ "package2": "version2"
51+
`,
52+
revparse,
53+
add,
54+
commit,
55+
push,
56+
})
57+
58+
fs.readFile = vi
59+
.fn()
60+
.mockResolvedValueOnce(`{}`)
61+
.mockResolvedValueOnce(`{"name":"packageName","version":"1.0.0"}`)
62+
fs.writeFile = vi.fn()
63+
64+
process.env['BRANCH_PREFIX'] = 'dep-upgrade/'
65+
await run()
66+
process.env['BRANCH_PREFIX'] = undefined
67+
68+
expect(console.log).not.toHaveBeenCalledWith(
69+
'Not a renovate branch, skipping',
70+
)
71+
expect(fs.readFile).toHaveBeenCalledWith(file, 'utf8')
72+
expect(fs.writeFile).toMatchSnapshot()
73+
expect(add).toHaveBeenCalledWith(fileName)
74+
expect(commit).toHaveBeenCalledWith(`chore: add changeset renovate-${rev}`)
75+
expect(push).toHaveBeenCalledTimes(1)
76+
})
77+
78+
it('should not skip if not in renovate branch, when branch check skip is true', async () => {
79+
const rev = 'test'
80+
const fileName = `.changeset/renovate-${rev}.md`
81+
const file = 'test/package.json'
82+
const revparse = vi.fn().mockReturnValue(rev)
83+
const add = vi.fn()
84+
const commit = vi.fn()
85+
const push = vi.fn()
86+
87+
mockSimpleGit.mockReturnValue({
88+
branch: () => ({
89+
current: 'main',
90+
}),
91+
diffSummary: () => ({
92+
files: [
93+
{
94+
file,
95+
},
96+
],
97+
}),
98+
show: () => `
99+
+ "package": "version"
100+
+ "package2": "version2"
101+
`,
102+
revparse,
103+
add,
104+
commit,
105+
push,
106+
})
107+
108+
fs.readFile = vi
109+
.fn()
110+
.mockResolvedValueOnce(`{}`)
111+
.mockResolvedValueOnce(`{"name":"packageName","version":"1.0.0"}`)
112+
fs.writeFile = vi.fn()
113+
114+
process.env['SKIP_BRANCH_CHECK'] = 'TRUE'
115+
await run()
116+
process.env['SKIP_BRANCH_CHECK'] = undefined
117+
118+
expect(console.log).not.toHaveBeenCalledWith(
119+
'Not a renovate branch, skipping',
120+
)
121+
expect(fs.readFile).toHaveBeenCalledWith(file, 'utf8')
122+
expect(fs.writeFile).toMatchSnapshot()
123+
expect(add).toHaveBeenCalledWith(fileName)
124+
expect(commit).toHaveBeenCalledWith(`chore: add changeset renovate-${rev}`)
125+
expect(push).toHaveBeenCalledTimes(1)
126+
})
127+
28128
it('should skip if .changeset is already modified', async () => {
29129
mockSimpleGit.mockReturnValue({
30130
...defaultGitValues,

packages/changesets-renovate/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,14 @@ async function getBumps(files: string[]): Promise<Map<string, string>> {
9494

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

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

100-
if (!branch.current.startsWith('renovate/')) {
101+
if (
102+
!branch.current.startsWith(branchPrefix) &&
103+
!process.env['SKIP_BRANCH_CHECK']
104+
) {
101105
console.log('Not a renovate branch, skipping')
102106

103107
return

0 commit comments

Comments
 (0)