From 84741a1a63cbf5bb002b8808f6afc0b15ea1c4b3 Mon Sep 17 00:00:00 2001 From: Michael Waddell Date: Fri, 4 Mar 2022 22:40:59 -0600 Subject: [PATCH] If the `update-type` is missing for some reason, calculate it from the versions --- dist/index.js | 20 ++++++++++++++++++-- src/dependabot/update_metadata.test.ts | 17 ++++++++++++++++- src/dependabot/update_metadata.ts | 22 +++++++++++++++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 986ad148..0673d59d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9043,7 +9043,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.parse = void 0; +exports.calculateUpdateType = exports.parse = void 0; const YAML = __importStar(__nccwpck_require__(4603)); function parse(commitMessage, branchName, mainBranch, lookup, getScore) { var _a, _b, _c, _d; @@ -9064,7 +9064,8 @@ function parse(commitMessage, branchName, mainBranch, lookup, getScore) { 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 : ''; - return Object.assign({ dependencyName: dependency['dependency-name'], dependencyType: dependency['dependency-type'], updateType: dependency['update-type'], directory: dirname, packageEcosystem: chunks[1], targetBranch: mainBranch, prevVersion: lastVersion, newVersion: nextVersion, compatScore: yield scoreFn(dependency['dependency-name'], lastVersion, nextVersion, chunks[1]) }, yield lookupFn(dependency['dependency-name'], lastVersion, dirname)); + const updateType = dependency['update-type'] || calculateUpdateType(lastVersion, nextVersion); + return Object.assign({ dependencyName: dependency['dependency-name'], dependencyType: dependency['dependency-type'], updateType: updateType, directory: dirname, packageEcosystem: chunks[1], targetBranch: mainBranch, prevVersion: lastVersion, newVersion: nextVersion, compatScore: yield scoreFn(dependency['dependency-name'], lastVersion, nextVersion, chunks[1]) }, yield lookupFn(dependency['dependency-name'], lastVersion, dirname)); }))); } } @@ -9072,6 +9073,21 @@ function parse(commitMessage, branchName, mainBranch, lookup, getScore) { }); } exports.parse = parse; +function calculateUpdateType(lastVersion, nextVersion) { + 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'; +} +exports.calculateUpdateType = calculateUpdateType; /***/ }), diff --git a/src/dependabot/update_metadata.test.ts b/src/dependabot/update_metadata.test.ts index b2af9d2d..18e225ab 100644 --- a/src/dependabot/update_metadata.test.ts +++ b/src/dependabot/update_metadata.test.ts @@ -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] ' @@ -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') +}) diff --git a/src/dependabot/update_metadata.ts b/src/dependabot/update_metadata.ts index 014f4dca..21fb7851 100644 --- a/src/dependabot/update_metadata.ts +++ b/src/dependabot/update_metadata.ts @@ -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, @@ -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' +}