Skip to content

Commit

Permalink
Merge pull request #173 from pangaeatech/update_type_fix
Browse files Browse the repository at this point in the history
If the `update-type` is missing for some reason, calculate it
  • Loading branch information
brrygrdn committed Mar 22, 2022
2 parents a96c30f + 84741a1 commit d960673
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
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] <support@github.com>'
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
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 d960673

Please sign in to comment.