Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve PR Title parsing, handle invalid inputs #186

Merged
merged 3 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9384,23 +9384,23 @@ function isAMajorReleaseBump(change) {
const from = change.delete
const to = change.insert

if (!from || !to) {
return false
}

const diff = semverDiff(semverCoerce(from), semverCoerce(to))
return diff === targetOptions.major
}

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

if (!match) {
return {}
throw new Error('Error while parsing PR title, expected: `bump <package> from <old-version> to <new-version>`')
}
const [, oldVersion, newVersion] = match

const [, packageName, oldVersion, newVersion] = match
// Get package name from branch
// dependabot branch names are in format "dependabot/npm_and_yarn/pkg-0.0.1"
// or "dependabot/github_actions/fastify/github-action-merge-dependabot-2.6.0"
const packageName = pullRequest.head.ref.split('/').pop().split('-').slice(0, -1).join('-')

return { [packageName]: { delete: semverCoerce(oldVersion).raw, insert: semverCoerce(newVersion).raw } }
}
Expand Down
14 changes: 7 additions & 7 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,23 @@ function isAMajorReleaseBump(change) {
const from = change.delete
const to = change.insert

if (!from || !to) {
return false
}

const diff = semverDiff(semverCoerce(from), semverCoerce(to))
return diff === targetOptions.major
}

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

if (!match) {
return {}
throw new Error('Error while parsing PR title, expected: `bump <package> from <old-version> to <new-version>`')
}
const [, oldVersion, newVersion] = match

const [, packageName, oldVersion, newVersion] = match
// Get package name from branch
// dependabot branch names are in format "dependabot/npm_and_yarn/pkg-0.0.1"
// or "dependabot/github_actions/fastify/github-action-merge-dependabot-2.6.0"
const packageName = pullRequest.head.ref.split('/').pop().split('-').slice(0, -1).join('-')
guilhermelimak marked this conversation as resolved.
Show resolved Hide resolved

return { [packageName]: { delete: semverCoerce(oldVersion).raw, insert: semverCoerce(newVersion).raw } }
}
50 changes: 19 additions & 31 deletions test/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ tap.test('should merge major bump using PR title', async () => {
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'build(deps): bump actions/checkout from 2 to 3'
title: 'build(deps): bump actions/checkout from 2 to 3',
head: {
ref: 'dependabot/github_actions/actions/checkout-3'
}
}
},
inputs: {
Expand All @@ -323,7 +326,10 @@ tap.test('should forbid major bump using PR title', async () => {
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'build(deps): bump actions/checkout from 2 to 3'
title: 'build(deps): bump actions/checkout from 2 to 3',
head: {
ref: 'dependabot/github_actions/actions/cache-3'
}
}
},
inputs: {
Expand All @@ -350,7 +356,10 @@ tap.test('should not merge major bump if updating github-action-merge-dependabot
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'build(deps): bump github-action-merge-dependabot from 2 to 3'
title: 'build(deps): bump github-action-merge-dependabot from 2 to 3 zzz',
head: {
ref: 'dependabot/github_actions/fastify/github-action-merge-dependabot-3'
}
}
},
inputs: {
Expand All @@ -376,7 +385,10 @@ tap.test('should throw if the PR title is not valid', async () => {
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'Invalid PR title'
title: 'Invalid PR title',
head: {
ref: 'dependabot/github_actions/fastify/github-action-merge-dependabot-2.6.0'
}
}
},
inputs: {
Expand All @@ -389,32 +401,8 @@ tap.test('should throw if the PR title is not valid', async () => {
stubs.prDiffStub.resolves(diffs.noPackageJsonChanges)

await action()
sinon.assert.called(stubs.approveStub)
sinon.assert.called(stubs.mergeStub)
})

tap.test('should handle a newly added package', async () => {
const PR_NUMBER = Math.random()

const { action, stubs } = buildStubbedAction({
payload: {
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'Invalid PR title'
}
},
inputs: {
PR_NUMBER,
TARGET: 'any',
EXCLUDE_PKGS: ['react'],
}
})

stubs.prDiffStub.resolves(diffs.thisModuleAdded)

await action()

sinon.assert.called(stubs.approveStub)
sinon.assert.called(stubs.mergeStub)
sinon.assert.calledWith(stubs.coreStub.setFailed, ("Error while parsing PR title, expected: `bump <package> from <old-version> to <new-version>`"))
sinon.assert.notCalled(stubs.approveStub)
sinon.assert.notCalled(stubs.mergeStub)
})