diff --git a/packages/auto-approve/README.md b/packages/auto-approve/README.md index 9df9505a14e..aa4d431bc08 100644 --- a/packages/auto-approve/README.md +++ b/packages/auto-approve/README.md @@ -54,12 +54,12 @@ Below is what each process checks for: * PythonDependency: - Checks that the author is 'renovate-bot' - Checks that the title of the PR matches the regexp: /^(fix|chore)\(deps\): update dependency (@?\S*) to v(\S*)$/ - - Max 3 files changed in the PR - Each file path must match one of these regexps: - /requirements.txt$/ + - /^samples/**/requirements*.txt$/ - All files must: - - Match this regexp: /^samples\/snippets\/requirements.txt$/ - - Increase the non-major package version of a dependency + - Match this regexp: /requirements.txt$/ + - Increase the package version of a dependency (major or nonmajor) - Only change one dependency - Change the dependency that was there previously, and that is on the title of the PR * PythonSampleDependency: diff --git a/packages/auto-approve/package-lock.json b/packages/auto-approve/package-lock.json index df0c4ecbd07..85f4e99eb00 100644 --- a/packages/auto-approve/package-lock.json +++ b/packages/auto-approve/package-lock.json @@ -13,7 +13,8 @@ "ajv": "^8.11.0", "dayjs": "^1.11.5", "gcf-utils": "^14.2.0", - "jsonwebtoken": "^9.0.0" + "jsonwebtoken": "^9.0.0", + "semver": "^7.3.8" }, "devDependencies": { "@octokit/rest": "^19.0.4", diff --git a/packages/auto-approve/package.json b/packages/auto-approve/package.json index 2a0d93b08a6..cbc30547792 100644 --- a/packages/auto-approve/package.json +++ b/packages/auto-approve/package.json @@ -31,7 +31,8 @@ "ajv": "^8.11.0", "dayjs": "^1.11.5", "gcf-utils": "^14.2.0", - "jsonwebtoken": "^9.0.0" + "jsonwebtoken": "^9.0.0", + "semver": "^7.3.8" }, "devDependencies": { "@octokit/rest": "^19.0.4", diff --git a/packages/auto-approve/src/process-checks/python/dependency.ts b/packages/auto-approve/src/process-checks/python/dependency.ts index 02ace963de0..e418f7f65bf 100644 --- a/packages/auto-approve/src/process-checks/python/dependency.ts +++ b/packages/auto-approve/src/process-checks/python/dependency.ts @@ -16,21 +16,18 @@ import {LanguageRule, File, FileRule, Process} from '../../interfaces'; import { checkAuthor, checkTitleOrBody, - checkFileCount, checkFilePathsMatch, doesDependencyChangeMatchPRTitleV2, getVersionsV2, - runVersioningValidation, isOneDependencyChanged, reportIndividualChecks, + isVersionBumped, } from '../../utils-for-pr-checking'; import {Octokit} from '@octokit/rest'; - export class PythonDependency extends Process implements LanguageRule { classRule: { author: string; titleRegex?: RegExp; - maxFiles: number; fileNameRegex?: RegExp[]; fileRules?: { oldVersion?: RegExp; @@ -66,11 +63,13 @@ export class PythonDependency extends Process implements LanguageRule { author: 'renovate-bot', titleRegex: /^(fix|chore)\(deps\): update dependency (@?\S*) to v(\S*)$/, - maxFiles: 3, - fileNameRegex: [/requirements.txt$/], + fileNameRegex: [ + /^samples\/.*?\/.*?requirements.*?\.txt$/, + /requirements\.txt$/, + ], fileRules: [ { - targetFileToCheck: /^samples\/snippets\/requirements.txt$/, + targetFileToCheck: /requirements.txt$/, // This would match: fix(deps): update dependency @octokit to v1 dependencyTitle: new RegExp( /^(fix|chore)\(deps\): update dependency (@?\S*) to v(\S*)$/ @@ -99,11 +98,6 @@ export class PythonDependency extends Process implements LanguageRule { this.classRule.titleRegex ); - const fileCountMatch = checkFileCount( - this.incomingPR.fileCount, - this.classRule.maxFiles - ); - const filePatternsMatch = checkFilePathsMatch( this.incomingPR.changedFiles.map(x => x.filename), this.classRule.fileNameRegex @@ -135,8 +129,7 @@ export class PythonDependency extends Process implements LanguageRule { this.incomingPR.title ); - const isVersionValid = runVersioningValidation(versions); - + const isVersionValid = isVersionBumped(versions); const oneDependencyChanged = isOneDependencyChanged(file); if (!(doesDependencyMatch && isVersionValid && oneDependencyChanged)) { @@ -153,19 +146,12 @@ export class PythonDependency extends Process implements LanguageRule { } reportIndividualChecks( - [ - 'authorshipMatches', - 'titleMatches', - 'fileCountMatches', - 'filePatternsMatch', - ], - [authorshipMatches, titleMatches, fileCountMatch, filePatternsMatch], + ['authorshipMatches', 'titleMatches', 'filePatternsMatch'], + [authorshipMatches, titleMatches, filePatternsMatch], this.incomingPR.repoOwner, this.incomingPR.repoName, this.incomingPR.prNumber ); - return ( - authorshipMatches && titleMatches && fileCountMatch && filePatternsMatch - ); + return authorshipMatches && titleMatches && filePatternsMatch; } } diff --git a/packages/auto-approve/src/utils-for-pr-checking.ts b/packages/auto-approve/src/utils-for-pr-checking.ts index ee0164626ab..716f3be43fd 100644 --- a/packages/auto-approve/src/utils-for-pr-checking.ts +++ b/packages/auto-approve/src/utils-for-pr-checking.ts @@ -18,6 +18,8 @@ import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; import {logger} from 'gcf-utils'; import {Octokit} from '@octokit/rest'; +import * as semver from 'semver'; + dayjs.extend(utc); dayjs.extend(timezone); @@ -309,6 +311,19 @@ export function isMinorVersionUpgraded(versions: Versions): boolean { return Number(versions.newMinorVersion) > Number(versions.oldMinorVersion); } +/** + * This function determines whether a package was upgraded, regardless of whether or not it was a major bump. + * + * @param versions an object containing the previous and newer versions of the package being updated + * @returns whether the minor version was upgraded. + */ +export function isVersionBumped(versions: Versions): boolean { + return semver.lt( + versions.oldMajorVersion + '.' + versions.oldMinorVersion, + versions.newMajorVersion + '.' + versions.newMinorVersion + ); +} + /** * This function determines whether there was at most one change in the given file. * diff --git a/packages/auto-approve/test/python-dependency.test.ts b/packages/auto-approve/test/python-dependency.test.ts index 5569df42639..6d3bfd783b7 100644 --- a/packages/auto-approve/test/python-dependency.test.ts +++ b/packages/auto-approve/test/python-dependency.test.ts @@ -51,11 +51,13 @@ describe('behavior of Python Dependency process', () => { author: 'renovate-bot', titleRegex: /^(fix|chore)\(deps\): update dependency (@?\S*) to v(\S*)$/, - maxFiles: 3, - fileNameRegex: [/requirements.txt$/], + fileNameRegex: [ + /^samples\/.*?\/.*?requirements.*?\.txt$/, + /requirements\.txt$/, + ], fileRules: [ { - targetFileToCheck: /^samples\/snippets\/requirements.txt$/, + targetFileToCheck: /requirements.txt$/, // This would match: fix(deps): update dependency @octokit to v1 dependencyTitle: new RegExp( /^(fix|chore)\(deps\): update dependency (@?\S*) to v(\S*)$/ @@ -154,7 +156,7 @@ describe('behavior of Python Dependency process', () => { '@@ -1,2 +1,2 @@\n' + ' google-cloud-videointelligence==2.5.1\n' + '-google-cloud-storage==1.42.3\n' + - '+google-cloud-storage==1.43.0', + '+google-cloud-storage==2.0.0', }, ], 'testRepoName',