Skip to content

Commit cee26fc

Browse files
committed
Handle branch names containing hyphen separators
1 parent 363356d commit cee26fc

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/dependabot/update_metadata.test.ts

+61
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,67 @@ test('it properly handles dependencies which contain slashes', async () => {
299299
expect(updatedDependencies[0].dependencyGroup).toEqual('')
300300
})
301301

302+
test('it handles branch names with hyphen separator', async () => {
303+
const commitMessage =
304+
'- [Release notes](https://github.com/fsevents/fsevents/releases)\n' +
305+
'- [Commits](fsevents/[email protected])\n' +
306+
'\n' +
307+
'---\n' +
308+
'updated-dependencies:\n' +
309+
'- dependency-name: fsevents\n' +
310+
' dependency-type: indirect\n' +
311+
'...\n' +
312+
'\n' +
313+
'Signed-off-by: dependabot[bot] <[email protected]>'
314+
315+
const getAlert = async () => Promise.resolve({ alertState: '', ghsaId: '', cvss: 0 })
316+
const getScore = async () => Promise.resolve(0)
317+
const updatedDependencies = await updateMetadata.parse(commitMessage, '', 'dependabot-npm_and_yarn-fsevents-1.2.13', 'master', getAlert, getScore)
318+
319+
expect(updatedDependencies[0].directory).toEqual('/')
320+
})
321+
322+
test('it handles branch names with hyphen separator and manifest files in nested directories', async () => {
323+
const commitMessage =
324+
'- [Release notes](https://github.com/fsevents/fsevents/releases)\n' +
325+
'- [Commits](fsevents/[email protected])\n' +
326+
'\n' +
327+
'---\n' +
328+
'updated-dependencies:\n' +
329+
'- dependency-name: fsevents\n' +
330+
' dependency-type: indirect\n' +
331+
'...\n' +
332+
'\n' +
333+
'Signed-off-by: dependabot[bot] <[email protected]>'
334+
335+
const getAlert = async () => Promise.resolve({ alertState: '', ghsaId: '', cvss: 0 })
336+
const getScore = async () => Promise.resolve(0)
337+
const updatedDependencies = await updateMetadata.parse(commitMessage, '', 'dependabot-npm_and_yarn-nested-nested-fsevents-1.2.13', 'master', getAlert, getScore)
338+
339+
expect(updatedDependencies[0].directory).toEqual('/nested/nested')
340+
})
341+
342+
test('it handles branch names with hyphen separator and dependency names with forward slashes', async () => {
343+
const commitMessage =
344+
'- [Release notes](https://github.com/composer/composer/releases)\n' +
345+
'- [Changelog](https://github.com/composer/composer/blob/main/CHANGELOG.md)\n' +
346+
'- [Commits](composer/[email protected])\n' +
347+
'\n' +
348+
'---\n' +
349+
'updated-dependencies:\n' +
350+
'- dependency-name: composer/composer\n' +
351+
' dependency-type: indirect\n' +
352+
'...\n' +
353+
'\n' +
354+
'Signed-off-by: dependabot[bot] <[email protected]>'
355+
356+
const getAlert = async () => Promise.resolve({ alertState: '', ghsaId: '', cvss: 0 })
357+
const getScore = async () => Promise.resolve(0)
358+
const updatedDependencies = await updateMetadata.parse(commitMessage, '', 'dependabot-composer-composer-composer-2.6.5', 'master', getAlert, getScore)
359+
360+
expect(updatedDependencies[0].directory).toEqual('/')
361+
})
362+
302363
test('calculateUpdateType should handle all paths', () => {
303364
expect(updateMetadata.calculateUpdateType('', '')).toEqual('')
304365
expect(updateMetadata.calculateUpdateType('', '1')).toEqual('')

src/dependabot/update_metadata.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ export async function parse (commitMessage: string, body: string, branchName: st
4949

5050
if (data['updated-dependencies']) {
5151
return await Promise.all(data['updated-dependencies'].map(async (dependency, index) => {
52-
const dirname = `/${chunks.slice(2, -1 * (1 + (dependency['dependency-name'].match(/\//g) || []).length)).join(delim) || ''}`
52+
// When a branch delimiter of "-" is used, we need to +1 to end of slice because there is always a hyphen
53+
// between dependency name and version at the end of the branch name, regardless of configured branch separator.
54+
// e.g. "fsevents-1.2.13".
55+
const baseSliceEnd = delim === '-' ? 2 : 1
56+
const dirname = `/${chunks.slice(2, -1 * (baseSliceEnd + (dependency['dependency-name'].match(/\//g) || []).length)).join('/') || ''}`
5357
const lastVersion = index === 0 ? prev : ''
5458
const nextVersion = index === 0 ? next : ''
5559
const updateType = dependency['update-type'] || calculateUpdateType(lastVersion, nextVersion)
@@ -64,7 +68,7 @@ export async function parse (commitMessage: string, body: string, branchName: st
6468
newVersion: nextVersion,
6569
compatScore: await scoreFn(dependency['dependency-name'], lastVersion, nextVersion, chunks[1]),
6670
maintainerChanges: newMaintainer,
67-
dependencyGroup: dependencyGroup,
71+
dependencyGroup,
6872
...await lookupFn(dependency['dependency-name'], lastVersion, dirname)
6973
}
7074
}))

0 commit comments

Comments
 (0)