From 4ead106f81accce473c0bc73b05c1c4dc738a127 Mon Sep 17 00:00:00 2001 From: Hasan Awad <90554456+hasanwhitesource@users.noreply.github.com> Date: Mon, 8 Aug 2022 10:50:36 +0300 Subject: [PATCH] feat(util/template): add prettyNewMajor and prettyNewVersion field (#16831) * initial * expose prettyNewVersion and prettyNewMajor * tests * revert change * simplify * test naming * documentation * documentation * remove wrong documentation * expose template finction * tests naming * combine condition * Update docs/usage/templates.md Co-authored-by: Michael Kriese <michael.kriese@visualon.de> * remove unecessary nullish op * Update docs/usage/templates.md Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> * Expose prettyVersion and prettyMajor * documentation * remove function and use regex * lexographic order * tests need fixing * add tests * Captial letter * renaming and prettyNewMajor * space * move to upgrade interface * use new fields where v is prepended manually * move to interface Co-authored-by: Michael Kriese <michael.kriese@visualon.de> Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> Co-authored-by: Rhys Arkins <rhys@arkins.net> --- lib/config/options/index.ts | 4 +- lib/modules/datasource/docker/index.ts | 2 +- lib/modules/manager/buildkite/index.ts | 2 +- lib/util/template/index.ts | 2 + .../__snapshots__/generate.spec.ts.snap | 3 + .../repository/updates/generate.spec.ts | 121 +++++++++++++++--- lib/workers/repository/updates/generate.ts | 9 ++ lib/workers/types.ts | 2 + 8 files changed, 126 insertions(+), 19 deletions(-) diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts index b2bd8c206a..4a0439c853 100644 --- a/lib/config/options/index.ts +++ b/lib/config/options/index.ts @@ -1358,7 +1358,7 @@ const options: RenovateOptions[] = [ branchTopic: '{{{depNameSanitized}}}-replacement', commitMessageAction: 'Replace', commitMessageExtra: - 'with {{newName}} {{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{newVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}', + 'with {{newName}} {{#if isMajor}}{{{prettyNewMajor}}}{{else}}{{#if isSingleVersion}}{{{prettyNewVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}', prBodyNotes: [ 'This is a special PR that replaces `{{{depNameSanitized}}}` with the community suggested minimal stable replacement version.', ], @@ -1639,7 +1639,7 @@ const options: RenovateOptions[] = [ 'Extra description used after the commit message topic - typically the version.', type: 'string', default: - 'to {{#if isPinDigest}}{{{newDigestShort}}}{{else}}{{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{newVersion}}}{{else}}{{#if newValue}}{{{newValue}}}{{else}}{{{newDigestShort}}}{{/if}}{{/if}}{{/if}}{{/if}}', + 'to {{#if isPinDigest}}{{{newDigestShort}}}{{else}}{{#if isMajor}}{{prettyNewMajor}}{{else}}{{#if isSingleVersion}}{{prettyNewVersion}}{{else}}{{#if newValue}}{{{newValue}}}{{else}}{{{newDigestShort}}}{{/if}}{{/if}}{{/if}}{{/if}}', cli: false, }, { diff --git a/lib/modules/datasource/docker/index.ts b/lib/modules/datasource/docker/index.ts index 3d02ed2856..82645d7a6c 100644 --- a/lib/modules/datasource/docker/index.ts +++ b/lib/modules/datasource/docker/index.ts @@ -338,7 +338,7 @@ export function isECRMaxResultsError(err: HttpError): boolean { export const defaultConfig = { commitMessageTopic: '{{{depName}}} Docker tag', commitMessageExtra: - 'to v{{#if isMajor}}{{{newMajor}}}{{else}}{{{newVersion}}}{{/if}}', + 'to {{#if isMajor}}{{{prettyNewMajor}}}{{else}}{{{prettyNewVersion}}}{{/if}}', digest: { branchTopic: '{{{depNameSanitized}}}-{{{currentValue}}}', commitMessageExtra: 'to {{newDigestShort}}', diff --git a/lib/modules/manager/buildkite/index.ts b/lib/modules/manager/buildkite/index.ts index 028d54c729..d50a6e18b8 100644 --- a/lib/modules/manager/buildkite/index.ts +++ b/lib/modules/manager/buildkite/index.ts @@ -7,7 +7,7 @@ export const defaultConfig = { fileMatch: ['buildkite\\.ya?ml', '\\.buildkite/.+\\.ya?ml$'], commitMessageTopic: 'buildkite plugin {{depName}}', commitMessageExtra: - 'to {{#if isMajor}}v{{{newMajor}}}{{else}}{{{newValue}}}{{/if}}', + 'to {{#if isMajor}}{{{prettyNewMajor}}}{{else}}{{{newValue}}}{{/if}}', }; export const supportedDatasources = [GithubTagsDatasource.id]; diff --git a/lib/util/template/index.ts b/lib/util/template/index.ts index 6fedb36172..f6dfd30ae6 100644 --- a/lib/util/template/index.ts +++ b/lib/util/template/index.ts @@ -117,6 +117,8 @@ export const allowedFields = { 'The name of the directory that the dependency was found in, without full path', platform: 'VCS platform in use, e.g. "github", "gitlab", etc.', prettyDepType: 'Massaged depType', + prettyNewMajor: 'The new major value with v prepended to it.', + prettyNewVersion: 'The new version value with v prepended to it.', project: 'ChangeLogProject object', recreateClosed: 'If true, this PR will be recreated if closed', references: 'A list of references for the upgrade', diff --git a/lib/workers/repository/updates/__snapshots__/generate.spec.ts.snap b/lib/workers/repository/updates/__snapshots__/generate.spec.ts.snap index eb890680e9..59e1f8cdc8 100644 --- a/lib/workers/repository/updates/__snapshots__/generate.spec.ts.snap +++ b/lib/workers/repository/updates/__snapshots__/generate.spec.ts.snap @@ -156,6 +156,7 @@ Object { "newVersion": "0.5.8", "prTitle": "some-title", "prettyDepType": "dependency", + "prettyNewVersion": "v0.5.8", }, ], } @@ -221,6 +222,7 @@ Object { "prBodyColumns": Array [], "prTitle": "some-title", "prettyDepType": "dependency", + "prettyNewVersion": "v1.0.1", "releaseTimestamp": undefined, "reuseLockFiles": true, "upgrades": Array [ @@ -240,6 +242,7 @@ Object { "newVersion": "1.0.1", "prTitle": "some-title", "prettyDepType": "dependency", + "prettyNewVersion": "v1.0.1", }, ], } diff --git a/lib/workers/repository/updates/generate.spec.ts b/lib/workers/repository/updates/generate.spec.ts index fdab3f042d..de9aae117a 100644 --- a/lib/workers/repository/updates/generate.spec.ts +++ b/lib/workers/repository/updates/generate.spec.ts @@ -80,6 +80,7 @@ describe('workers/repository/updates/generate', () => { newValue: '^1.0.0', newVersion: '1.0.1', reuseLockFiles: true, + prettyNewVersion: 'v1.0.1', upgrades: [ { branchName: 'some-branch', @@ -90,6 +91,7 @@ describe('workers/repository/updates/generate', () => { lockedVersion: '1.0.0', newValue: '^1.0.0', newVersion: '1.0.1', + prettyNewVersion: 'v1.0.1', }, ], }); @@ -134,7 +136,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '5.1.2', newVersion: '5.1.2', @@ -154,7 +156,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '5.1.2', newVersion: '5.1.2', @@ -175,7 +177,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '5.1.2', newVersion: '5.1.2', @@ -206,7 +208,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '5.1.2', newVersion: '5.1.2', @@ -220,7 +222,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '5.2.0', newVersion: '5.2.0', @@ -242,7 +244,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '5.1.2', newVersion: '5.1.2', @@ -258,7 +260,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '1.1.0', newVersion: '1.1.0', @@ -285,7 +287,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '^6.0', newVersion: '6.0.3', @@ -301,7 +303,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '^6.0', newVersion: '6.0.1', @@ -328,7 +330,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '^6.0.1', newVersion: '6.0.2', @@ -344,7 +346,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '^6.0.0', newVersion: '6.0.2', @@ -371,7 +373,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', isDigest: true, currentDigest: 'abcdefghijklmnopqrstuvwxyz', newDigest: '123abcdefghijklmnopqrstuvwxyz', @@ -387,7 +389,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: 'zzzzzzzzzz', group: { @@ -428,7 +430,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '>= 5.1.2', newVersion: '5.1.2', @@ -444,7 +446,7 @@ describe('workers/repository/updates/generate', () => { branchName: 'some-branch', prTitle: 'some-title', commitMessageExtra: - 'to {{#if isMajor}}v{{newMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', + 'to {{#if isMajor}}{{prettyNewMajor}}{{else}}{{#unless isRange}}v{{/unless}}{{newValue}}{{/if}}', foo: 1, newValue: '^5,1,2', newVersion: '5.1.2', @@ -564,6 +566,95 @@ describe('workers/repository/updates/generate', () => { ); }); + it('use prettyVersion in pr title when there is a v', () => { + const branch: BranchUpgradeConfig[] = [ + { + ...defaultConfig, + manager: 'some-manager', + depName: 'some-dep', + packageFile: 'foo/bar/package.json', + packageFileDir: 'foo/bar', + semanticCommits: 'enabled', + semanticCommitType: 'chore', + semanticCommitScope: '{{packageFileDir}}', + commitMessageExtra: '{{prettyNewVersion}}', + newValue: 'v1.2.0', + isSingleVersion: true, + newVersion: 'v1.2.0', + } as BranchUpgradeConfig, + ]; + const res = generateBranchConfig(branch); + expect(res.prTitle).toBe( + 'chore(foo/bar): update dependency some-dep v1.2.0' + ); + }); + + it('use prettyVersion in pr title there is no v', () => { + const branch: BranchUpgradeConfig[] = [ + { + ...defaultConfig, + manager: 'some-manager', + depName: 'some-dep', + packageFile: 'foo/bar/package.json', + packageFileDir: 'foo/bar', + semanticCommits: 'enabled', + semanticCommitType: 'chore', + semanticCommitScope: '{{packageFileDir}}', + commitMessageExtra: '{{prettyNewVersion}}', + newValue: '3.2.0', + newVersion: '3.2.0', + newMajor: 3, + } as BranchUpgradeConfig, + ]; + const res = generateBranchConfig(branch); + expect(res.prTitle).toBe( + 'chore(foo/bar): update dependency some-dep v3.2.0' + ); + }); + + it('use newMajor in pr title with v', () => { + const branch: BranchUpgradeConfig[] = [ + { + ...defaultConfig, + manager: 'some-manager', + depName: 'some-dep', + packageFile: 'foo/bar/package.json', + packageFileDir: 'foo/bar', + semanticCommits: 'enabled', + semanticCommitType: 'chore', + semanticCommitScope: '{{packageFileDir}}', + commitMessageExtra: '{{prettyNewMajor}}', + newValue: '3.2.0', + newVersion: '3.2.0', + newMajor: 3, + } as BranchUpgradeConfig, + ]; + const res = generateBranchConfig(branch); + expect(res.prTitle).toBe('chore(foo/bar): update dependency some-dep v3'); + }); + + it('Default commitMessageExtra pr title', () => { + const branch: BranchUpgradeConfig[] = [ + { + ...defaultConfig, + manager: 'some-manager', + depName: 'some-dep', + packageFile: 'foo/bar/package.json', + packageFileDir: 'foo/bar', + semanticCommits: 'enabled', + semanticCommitType: 'chore', + semanticCommitScope: '{{packageFileDir}}', + newValue: 'v1.2.0', + isSingleVersion: true, + newVersion: 'v1.2.0', + } as BranchUpgradeConfig, + ]; + const res = generateBranchConfig(branch); + expect(res.prTitle).toBe( + 'chore(foo/bar): update dependency some-dep to v1.2.0' + ); + }); + it('adds commit message body', () => { // TODO #7154 incompatible types const branch: BranchUpgradeConfig[] = [ diff --git a/lib/workers/repository/updates/generate.ts b/lib/workers/repository/updates/generate.ts index 2bbad40db5..9ffec7ff5f 100644 --- a/lib/workers/repository/updates/generate.ts +++ b/lib/workers/repository/updates/generate.ts @@ -182,6 +182,15 @@ export function generateBranchConfig( regEx(/[A-Z]/).exec(upgrade.semanticCommitType!) === null && !upgrade.semanticCommitType!.startsWith(':'); } + // prettify newVersion and newMajor for printing + if (upgrade.newVersion) { + upgrade.prettyNewVersion = upgrade.newVersion.startsWith('v') + ? upgrade.newVersion + : `v${upgrade.newVersion}`; + } + if (upgrade.newMajor) { + upgrade.prettyNewMajor = `v${upgrade.newMajor}`; + } // Compile a few times in case there are nested templates upgrade.commitMessage = template.compile( upgrade.commitMessage ?? '', diff --git a/lib/workers/types.ts b/lib/workers/types.ts index 1159400107..87f3cc36b9 100644 --- a/lib/workers/types.ts +++ b/lib/workers/types.ts @@ -57,6 +57,8 @@ export interface BranchUpgradeConfig prBodyTemplate?: string; prPriority?: number; prTitle?: string; + prettyNewMajor?: string; + prettyNewVersion?: string; releases?: ReleaseWithNotes[]; releaseTimestamp?: string; repoName?: string; -- GitLab