Skip to content

Commit

Permalink
If the update-type is missing for some reason, calculate it from th…
Browse files Browse the repository at this point in the history
…e versions
  • Loading branch information
mwaddell committed Mar 5, 2022
1 parent a96c30f commit 84741a1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
20 changes: 18 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/dependabot/update_metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ test('it returns the updated dependency information when there is a yaml fragmen
'updated-dependencies:\n' +
'- dependency-name: coffee-rails\n' +
' dependency-type: direct:production\n' +
' update-type: version-update:semver-minor\n' +
'...\n' +
'\n' +
'Signed-off-by: dependabot[bot] <[email protected]>'
Expand Down Expand Up @@ -194,3 +193,19 @@ test('it properly handles dependencies which contain slashes', async () => {
expect(updatedDependencies[0].ghsaId).toEqual('')
expect(updatedDependencies[0].cvss).toEqual(0)
})

test('calculateUpdateType should handle all paths', () => {
expect(updateMetadata.calculateUpdateType('', '')).toEqual('')
expect(updateMetadata.calculateUpdateType('', '1')).toEqual('')
expect(updateMetadata.calculateUpdateType('1', '')).toEqual('')
expect(updateMetadata.calculateUpdateType('1.1.1', '1.1.1')).toEqual('')
expect(updateMetadata.calculateUpdateType('1', '2')).toEqual('version-update:semver-major')
expect(updateMetadata.calculateUpdateType('1.2.2', '2.2.2')).toEqual('version-update:semver-major')
expect(updateMetadata.calculateUpdateType('1.1', '1')).toEqual('version-update:semver-minor')
expect(updateMetadata.calculateUpdateType('1', '1.1')).toEqual('version-update:semver-minor')
expect(updateMetadata.calculateUpdateType('1.2.1', '1.1.1')).toEqual('version-update:semver-minor')
expect(updateMetadata.calculateUpdateType('1.1.1', '1.1')).toEqual('version-update:semver-patch')
expect(updateMetadata.calculateUpdateType('1.1', '1.1.1')).toEqual('version-update:semver-patch')
expect(updateMetadata.calculateUpdateType('1.1.1', '1.1.2')).toEqual('version-update:semver-patch')
expect(updateMetadata.calculateUpdateType('1.1.1.1', '1.1.1.2')).toEqual('version-update:semver-patch')
})
22 changes: 21 additions & 1 deletion src/dependabot/update_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ export async function parse (commitMessage: string, branchName: string, mainBran
const dirname = `/${chunks.slice(2, -1 * (1 + (dependency['dependency-name'].match(/\//g) || []).length)).join(delim) || ''}`
const lastVersion = index === 0 ? prev : ''
const nextVersion = index === 0 ? next : ''
const updateType = dependency['update-type'] || calculateUpdateType(lastVersion, nextVersion)
return {
dependencyName: dependency['dependency-name'],
dependencyType: dependency['dependency-type'],
updateType: dependency['update-type'],
updateType: updateType,
directory: dirname,
packageEcosystem: chunks[1],
targetBranch: mainBranch,
Expand All @@ -64,3 +65,22 @@ export async function parse (commitMessage: string, branchName: string, mainBran

return Promise.resolve([])
}

export function calculateUpdateType (lastVersion: string, nextVersion: string) {
if (!lastVersion || !nextVersion || lastVersion === nextVersion) {
return ''
}

const lastParts = lastVersion.split('.')
const nextParts = nextVersion.split('.')

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

if (lastParts.length < 2 || nextParts.length < 2 || lastParts[1] !== nextParts[1]) {
return 'version-update:semver-minor'
}

return 'version-update:semver-patch'
}

0 comments on commit 84741a1

Please sign in to comment.