Skip to content

Commit d2b39a9

Browse files
Fix/fallback to title parsing (#182)
* fix: fallback to PR title parsing for non package.json modules * test: remove .only from tests * test: add more PR title parsing test cases
1 parent 137e8aa commit d2b39a9

File tree

5 files changed

+104
-18
lines changed

5 files changed

+104
-18
lines changed

dist/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9338,7 +9338,9 @@ module.exports = async function run() {
93389338
}
93399339

93409340
const prDiff = await client.getPullRequestDiff(pr.number)
9341-
const moduleChanges = getModuleVersionChanges(prDiff)
9341+
9342+
// Get changed modules from diff if available or from PR title as fallback
9343+
const moduleChanges = getModuleVersionChanges(prDiff) || parsePrTitle(pr)
93429344

93439345
if (TARGET !== targetOptions.any) {
93449346
logInfo(`Checking if the changes in the PR can be merged`)
@@ -9389,6 +9391,19 @@ function isAMajorReleaseBump(change) {
93899391
return diff === targetOptions.major
93909392
}
93919393

9394+
function parsePrTitle(pullRequest) {
9395+
const expression = /bump (\S+) from (\S+) to (\S+)/i
9396+
const match = expression.exec(pullRequest.title)
9397+
9398+
if (!match) {
9399+
return {}
9400+
}
9401+
9402+
const [, packageName, oldVersion, newVersion] = match
9403+
9404+
return { [packageName]: { delete: semverCoerce(oldVersion).raw, insert: semverCoerce(newVersion).raw } }
9405+
}
9406+
93929407

93939408
/***/ }),
93949409

@@ -9569,7 +9584,7 @@ const getModuleVersionChanges = (prDiff) => {
95699584
const parsedDiffFiles = parse(prDiff)
95709585
const packageJsonChanges = parsedDiffFiles.find((file) => file.newPath === 'package.json')
95719586
if (!packageJsonChanges) {
9572-
return {}
9587+
return false
95739588
}
95749589

95759590
const moduleChanges = {}

src/action.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ module.exports = async function run() {
4848
}
4949

5050
const prDiff = await client.getPullRequestDiff(pr.number)
51-
const moduleChanges = getModuleVersionChanges(prDiff)
51+
52+
// Get changed modules from diff if available or from PR title as fallback
53+
const moduleChanges = getModuleVersionChanges(prDiff) || parsePrTitle(pr)
5254

5355
if (TARGET !== targetOptions.any) {
5456
logInfo(`Checking if the changes in the PR can be merged`)
@@ -98,3 +100,16 @@ function isAMajorReleaseBump(change) {
98100
const diff = semverDiff(semverCoerce(from), semverCoerce(to))
99101
return diff === targetOptions.major
100102
}
103+
104+
function parsePrTitle(pullRequest) {
105+
const expression = /bump (\S+) from (\S+) to (\S+)/i
106+
const match = expression.exec(pullRequest.title)
107+
108+
if (!match) {
109+
return {}
110+
}
111+
112+
const [, packageName, oldVersion, newVersion] = match
113+
114+
return { [packageName]: { delete: semverCoerce(oldVersion).raw, insert: semverCoerce(newVersion).raw } }
115+
}

src/moduleVersionChanges.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const getModuleVersionChanges = (prDiff) => {
4545
const parsedDiffFiles = parse(prDiff)
4646
const packageJsonChanges = parsedDiffFiles.find((file) => file.newPath === 'package.json')
4747
if (!packageJsonChanges) {
48-
return {}
48+
return false
4949
}
5050

5151
const moduleChanges = {}

test/action.test.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,20 @@ tap.test('should check submodules semver when target is set', async () => {
289289
sinon.assert.notCalled(stubs.mergeStub)
290290
})
291291

292-
tap.test('should merge if no changes were made to package.json', async () => {
292+
tap.test('should merge major bump using PR title', async () => {
293293
const PR_NUMBER = Math.random()
294+
294295
const { action, stubs } = buildStubbedAction({
295296
payload: {
296297
pull_request: {
297298
number: PR_NUMBER,
298299
user: { login: BOT_NAME },
300+
title: 'build(deps): bump actions/checkout from 2 to 3'
299301
}
300302
},
301303
inputs: {
302304
PR_NUMBER,
303-
TARGET: 'main',
305+
TARGET: 'major',
304306
EXCLUDE_PKGS: ['react'],
305307
}
306308
})
@@ -312,3 +314,56 @@ tap.test('should merge if no changes were made to package.json', async () => {
312314
sinon.assert.called(stubs.approveStub)
313315
sinon.assert.called(stubs.mergeStub)
314316
})
317+
318+
tap.test('should forbid major bump using PR title', async () => {
319+
const PR_NUMBER = Math.random()
320+
321+
const { action, stubs } = buildStubbedAction({
322+
payload: {
323+
pull_request: {
324+
number: PR_NUMBER,
325+
user: { login: BOT_NAME },
326+
title: 'build(deps): bump actions/checkout from 2 to 3'
327+
}
328+
},
329+
inputs: {
330+
PR_NUMBER,
331+
TARGET: 'minor',
332+
EXCLUDE_PKGS: ['react'],
333+
}
334+
})
335+
336+
stubs.prDiffStub.resolves(diffs.noPackageJsonChanges)
337+
338+
await action()
339+
340+
sinon.assert.notCalled(stubs.approveStub)
341+
sinon.assert.notCalled(stubs.mergeStub)
342+
})
343+
344+
345+
tap.test('should not merge major bump if updating github-action-merge-dependabot', async () => {
346+
const PR_NUMBER = Math.random()
347+
348+
const { action, stubs } = buildStubbedAction({
349+
payload: {
350+
pull_request: {
351+
number: PR_NUMBER,
352+
user: { login: BOT_NAME },
353+
title: 'build(deps): bump github-action-merge-dependabot from 2 to 3'
354+
}
355+
},
356+
inputs: {
357+
PR_NUMBER,
358+
TARGET: 'any',
359+
EXCLUDE_PKGS: ['react'],
360+
}
361+
})
362+
363+
stubs.prDiffStub.resolves(diffs.noPackageJsonChanges)
364+
365+
await action()
366+
367+
sinon.assert.notCalled(stubs.approveStub)
368+
sinon.assert.notCalled(stubs.mergeStub)
369+
})

test/moduleChanges.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,19 @@ index d3dfd3d..bd28161 100644
148148
+ "github-action-merge-dependabot": "2.1.0",
149149
`,
150150
noPackageJsonChanges: `
151-
diff --git a/test/action.test.js b/test/action.test.js
152-
index e8c6572..751e69d 100644
153-
--- a/test/action.test.js
154-
+++ b/test/action.test.js
155-
@@ -9,6 +9,7 @@ const core = require('@actions/core')
156-
const github = require('@actions/github')
157-
const toolkit = require('actions-toolkit')
158-
159-
+
160-
const { diffs } = require('./moduleChanges')
161-
const actionLog = require('../src/log')
162-
const actionUtil = require('../src/util')
151+
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
152+
index e790278..678e751 100644
153+
--- a/.github/workflows/ci.yml
154+
+++ b/.github/workflows/ci.yml
155+
@@ -4,7 +4,7 @@ jobs:
156+
build:
157+
runs-on: ubuntu-latest
158+
steps:
159+
- - uses: actions/checkout@v2
160+
+ - uses: actions/checkout@v3
161+
- uses: actions/setup-node@v3
162+
with:
163+
node-version-file: '.nvmrc'
163164
`
164165
}
165166

0 commit comments

Comments
 (0)