Skip to content

Commit 919f913

Browse files
authored
Allow leading v on commit message versions (#338)
Previously, a leading `v` on the version in the commit message (eg, `Bumps org/repo from v1.3.0 to v1.3.2.`) did not populate the `previous-version` and `new-version`, so was also unable to calculate the proper `update-type`. This fixes that. Fix #244
1 parent 173b40e commit 919f913

File tree

4 files changed

+112
-8
lines changed

4 files changed

+112
-8
lines changed

dist/index.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dependabot/update_metadata.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,41 @@ test('it supports multiple dependencies within a single fragment', async () => {
119119
expect(updatedDependencies[1].cvss).toEqual(0)
120120
})
121121

122+
test('it returns the updated dependency information when there is a leading v in the commit message versions', async () => {
123+
const commitMessage =
124+
'Bumps [coffee-rails](https://github.com/rails/coffee-rails) from v4.0.1 to v4.2.2.\n' +
125+
'- [Release notes](https://github.com/rails/coffee-rails/releases)\n' +
126+
'- [Changelog](https://github.com/rails/coffee-rails/blob/master/CHANGELOG.md)\n' +
127+
'- [Commits](rails/[email protected])\n' +
128+
'\n' +
129+
'---\n' +
130+
'updated-dependencies:\n' +
131+
'- dependency-name: coffee-rails\n' +
132+
' dependency-type: direct:production\n' +
133+
'...\n' +
134+
'\n' +
135+
'Signed-off-by: dependabot[bot] <[email protected]>'
136+
137+
const getAlert = async () => Promise.resolve({ alertState: 'DISMISSED', ghsaId: 'GHSA-III-BBB', cvss: 4.6 })
138+
const getScore = async () => Promise.resolve(43)
139+
const updatedDependencies = await updateMetadata.parse(commitMessage, 'dependabot/nuget/coffee-rails', 'main', getAlert, getScore)
140+
141+
expect(updatedDependencies).toHaveLength(1)
142+
143+
expect(updatedDependencies[0].dependencyName).toEqual('coffee-rails')
144+
expect(updatedDependencies[0].dependencyType).toEqual('direct:production')
145+
expect(updatedDependencies[0].updateType).toEqual('version-update:semver-minor')
146+
expect(updatedDependencies[0].directory).toEqual('/')
147+
expect(updatedDependencies[0].packageEcosystem).toEqual('nuget')
148+
expect(updatedDependencies[0].targetBranch).toEqual('main')
149+
expect(updatedDependencies[0].prevVersion).toEqual('v4.0.1')
150+
expect(updatedDependencies[0].newVersion).toEqual('v4.2.2')
151+
expect(updatedDependencies[0].compatScore).toEqual(43)
152+
expect(updatedDependencies[0].alertState).toEqual('DISMISSED')
153+
expect(updatedDependencies[0].ghsaId).toEqual('GHSA-III-BBB')
154+
expect(updatedDependencies[0].cvss).toEqual(4.6)
155+
})
156+
122157
test('it only returns information within the first fragment if there are multiple yaml documents', async () => {
123158
const commitMessage =
124159
'- [Release notes](https://github.com/rails/coffee-rails/releases)\n' +

src/dependabot/update_metadata.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export interface scoreLookup {
2727
}
2828

2929
export async function parse (commitMessage: string, branchName: string, mainBranch: string, lookup?: alertLookup, getScore?: scoreLookup): Promise<Array<updatedDependency>> {
30-
const bumpFragment = commitMessage.match(/^Bumps .* from (?<from>\d[^ ]*) to (?<to>\d[^ ]*)\.$/m)
31-
const updateFragment = commitMessage.match(/^Update .* requirement from \S*? ?(?<from>\d[^ ]*) to \S*? ?(?<to>\d[^ ]*)$/m)
30+
const bumpFragment = commitMessage.match(/^Bumps .* from (?<from>v?\d[^ ]*) to (?<to>v?\d[^ ]*)\.$/m)
31+
const updateFragment = commitMessage.match(/^Update .* requirement from \S*? ?(?<from>v?\d[^ ]*) to \S*? ?(?<to>v?\d[^ ]*)$/m)
3232
const yamlFragment = commitMessage.match(/^-{3}\n(?<dependencies>[\S|\s]*?)\n^\.{3}\n/m)
3333
const lookupFn = lookup ?? (() => Promise.resolve({ alertState: '', ghsaId: '', cvss: 0 }))
3434
const scoreFn = getScore ?? (() => Promise.resolve(0))
@@ -72,8 +72,8 @@ export function calculateUpdateType (lastVersion: string, nextVersion: string) {
7272
return ''
7373
}
7474

75-
const lastParts = lastVersion.split('.')
76-
const nextParts = nextVersion.split('.')
75+
const lastParts = lastVersion.replace('v', '').split('.')
76+
const nextParts = nextVersion.replace('v', '').split('.')
7777

7878
if (lastParts[0] !== nextParts[0]) {
7979
return 'version-update:semver-major'

src/main.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,75 @@ test('it sets the updated dependency as an output for subsequent actions when gi
132132
expect(core.setOutput).toBeCalledWith('cvss', 0)
133133
})
134134

135+
test('it sets the updated dependency as an output for subsequent actions when there is a leading v in the commit message version', async () => {
136+
const mockCommitMessage =
137+
'Bumps [coffee-rails](https://github.com/rails/coffee-rails) from v4.0.1 to v4.2.2.\n' +
138+
'- [Release notes](https://github.com/rails/coffee-rails/releases)\n' +
139+
'- [Changelog](https://github.com/rails/coffee-rails/blob/master/CHANGELOG.md)\n' +
140+
'- [Commits](rails/[email protected])\n' +
141+
'\n' +
142+
'---\n' +
143+
'updated-dependencies:\n' +
144+
'- dependency-name: coffee-rails\n' +
145+
' dependency-type: direct:production\n' +
146+
'...\n' +
147+
'\n' +
148+
'Signed-off-by: dependabot[bot] <[email protected]>'
149+
const mockAlert = { alertState: 'FIXED', ghsaId: 'GSHA', cvss: 3.4 }
150+
151+
jest.spyOn(core, 'getInput').mockImplementation(jest.fn((name) => { return name === 'github-token' ? 'mock-token' : '' }))
152+
jest.spyOn(util, 'getBranchNames').mockReturnValue({ headName: 'dependabot|nuget|feature1', baseName: 'main' })
153+
jest.spyOn(dependabotCommits, 'getMessage').mockImplementation(jest.fn(
154+
() => Promise.resolve(mockCommitMessage)
155+
))
156+
jest.spyOn(dependabotCommits, 'getAlert').mockImplementation(jest.fn(
157+
() => Promise.resolve(mockAlert)
158+
))
159+
jest.spyOn(dependabotCommits, 'getCompatibility').mockImplementation(jest.fn(
160+
() => Promise.resolve(34)
161+
))
162+
jest.spyOn(core, 'setOutput').mockImplementation(jest.fn())
163+
164+
await run()
165+
166+
expect(core.startGroup).toHaveBeenCalledWith(
167+
expect.stringContaining('Outputting metadata for 1 updated dependency')
168+
)
169+
170+
expect(core.setOutput).toHaveBeenCalledWith(
171+
'updated-dependencies-json',
172+
[
173+
{
174+
dependencyName: 'coffee-rails',
175+
dependencyType: 'direct:production',
176+
updateType: 'version-update:semver-minor',
177+
directory: '/',
178+
packageEcosystem: 'nuget',
179+
targetBranch: 'main',
180+
prevVersion: 'v4.0.1',
181+
newVersion: 'v4.2.2',
182+
compatScore: 0,
183+
alertState: '',
184+
ghsaId: '',
185+
cvss: 0
186+
}
187+
]
188+
)
189+
190+
expect(core.setOutput).toBeCalledWith('dependency-names', 'coffee-rails')
191+
expect(core.setOutput).toBeCalledWith('dependency-type', 'direct:production')
192+
expect(core.setOutput).toBeCalledWith('update-type', 'version-update:semver-minor')
193+
expect(core.setOutput).toBeCalledWith('directory', '/')
194+
expect(core.setOutput).toBeCalledWith('package-ecosystem', 'nuget')
195+
expect(core.setOutput).toBeCalledWith('target-branch', 'main')
196+
expect(core.setOutput).toBeCalledWith('previous-version', 'v4.0.1')
197+
expect(core.setOutput).toBeCalledWith('new-version', 'v4.2.2')
198+
expect(core.setOutput).toBeCalledWith('compatibility-score', 0)
199+
expect(core.setOutput).toBeCalledWith('alert-state', '')
200+
expect(core.setOutput).toBeCalledWith('ghsa-id', '')
201+
expect(core.setOutput).toBeCalledWith('cvss', 0)
202+
})
203+
135204
test('it sets the updated dependency as an output for subsequent actions when given a commit message for library', async () => {
136205
const mockCommitMessage =
137206
'Update rubocop requirement from ~> 1.30.1 to ~> 1.31.0\n' +

0 commit comments

Comments
 (0)