Skip to content

Commit

Permalink
Fix/fallback to title parsing (#182)
Browse files Browse the repository at this point in the history
* fix: fallback to PR title parsing for non package.json modules

* test: remove .only from tests

* test: add more PR title parsing test cases
  • Loading branch information
guilhermelimak committed Apr 12, 2022
1 parent 137e8aa commit d2b39a9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 18 deletions.
19 changes: 17 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9338,7 +9338,9 @@ module.exports = async function run() {
}

const prDiff = await client.getPullRequestDiff(pr.number)
const moduleChanges = getModuleVersionChanges(prDiff)

// Get changed modules from diff if available or from PR title as fallback
const moduleChanges = getModuleVersionChanges(prDiff) || parsePrTitle(pr)

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

function parsePrTitle(pullRequest) {
const expression = /bump (\S+) from (\S+) to (\S+)/i
const match = expression.exec(pullRequest.title)

if (!match) {
return {}
}

const [, packageName, oldVersion, newVersion] = match

return { [packageName]: { delete: semverCoerce(oldVersion).raw, insert: semverCoerce(newVersion).raw } }
}


/***/ }),

Expand Down Expand Up @@ -9569,7 +9584,7 @@ const getModuleVersionChanges = (prDiff) => {
const parsedDiffFiles = parse(prDiff)
const packageJsonChanges = parsedDiffFiles.find((file) => file.newPath === 'package.json')
if (!packageJsonChanges) {
return {}
return false
}

const moduleChanges = {}
Expand Down
17 changes: 16 additions & 1 deletion src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ module.exports = async function run() {
}

const prDiff = await client.getPullRequestDiff(pr.number)
const moduleChanges = getModuleVersionChanges(prDiff)

// Get changed modules from diff if available or from PR title as fallback
const moduleChanges = getModuleVersionChanges(prDiff) || parsePrTitle(pr)

if (TARGET !== targetOptions.any) {
logInfo(`Checking if the changes in the PR can be merged`)
Expand Down Expand Up @@ -98,3 +100,16 @@ function isAMajorReleaseBump(change) {
const diff = semverDiff(semverCoerce(from), semverCoerce(to))
return diff === targetOptions.major
}

function parsePrTitle(pullRequest) {
const expression = /bump (\S+) from (\S+) to (\S+)/i
const match = expression.exec(pullRequest.title)

if (!match) {
return {}
}

const [, packageName, oldVersion, newVersion] = match

return { [packageName]: { delete: semverCoerce(oldVersion).raw, insert: semverCoerce(newVersion).raw } }
}
2 changes: 1 addition & 1 deletion src/moduleVersionChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const getModuleVersionChanges = (prDiff) => {
const parsedDiffFiles = parse(prDiff)
const packageJsonChanges = parsedDiffFiles.find((file) => file.newPath === 'package.json')
if (!packageJsonChanges) {
return {}
return false
}

const moduleChanges = {}
Expand Down
59 changes: 57 additions & 2 deletions test/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,20 @@ tap.test('should check submodules semver when target is set', async () => {
sinon.assert.notCalled(stubs.mergeStub)
})

tap.test('should merge if no changes were made to package.json', async () => {
tap.test('should merge major bump using PR title', async () => {
const PR_NUMBER = Math.random()

const { action, stubs } = buildStubbedAction({
payload: {
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'build(deps): bump actions/checkout from 2 to 3'
}
},
inputs: {
PR_NUMBER,
TARGET: 'main',
TARGET: 'major',
EXCLUDE_PKGS: ['react'],
}
})
Expand All @@ -312,3 +314,56 @@ tap.test('should merge if no changes were made to package.json', async () => {
sinon.assert.called(stubs.approveStub)
sinon.assert.called(stubs.mergeStub)
})

tap.test('should forbid major bump using PR title', async () => {
const PR_NUMBER = Math.random()

const { action, stubs } = buildStubbedAction({
payload: {
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'build(deps): bump actions/checkout from 2 to 3'
}
},
inputs: {
PR_NUMBER,
TARGET: 'minor',
EXCLUDE_PKGS: ['react'],
}
})

stubs.prDiffStub.resolves(diffs.noPackageJsonChanges)

await action()

sinon.assert.notCalled(stubs.approveStub)
sinon.assert.notCalled(stubs.mergeStub)
})


tap.test('should not merge major bump if updating github-action-merge-dependabot', async () => {
const PR_NUMBER = Math.random()

const { action, stubs } = buildStubbedAction({
payload: {
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'build(deps): bump github-action-merge-dependabot from 2 to 3'
}
},
inputs: {
PR_NUMBER,
TARGET: 'any',
EXCLUDE_PKGS: ['react'],
}
})

stubs.prDiffStub.resolves(diffs.noPackageJsonChanges)

await action()

sinon.assert.notCalled(stubs.approveStub)
sinon.assert.notCalled(stubs.mergeStub)
})
25 changes: 13 additions & 12 deletions test/moduleChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,19 @@ index d3dfd3d..bd28161 100644
+ "github-action-merge-dependabot": "2.1.0",
`,
noPackageJsonChanges: `
diff --git a/test/action.test.js b/test/action.test.js
index e8c6572..751e69d 100644
--- a/test/action.test.js
+++ b/test/action.test.js
@@ -9,6 +9,7 @@ const core = require('@actions/core')
const github = require('@actions/github')
const toolkit = require('actions-toolkit')
+
const { diffs } = require('./moduleChanges')
const actionLog = require('../src/log')
const actionUtil = require('../src/util')
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index e790278..678e751 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -4,7 +4,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- - uses: actions/checkout@v2
+ - uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
`
}

Expand Down

0 comments on commit d2b39a9

Please sign in to comment.