From 1914a2eaf7e51e362cc97598c7c96378ef203537 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Wed, 3 Feb 2021 15:02:36 +0100
Subject: [PATCH] fix: expose more fields to templates

---
 .../__snapshots__/migration.spec.ts.snap      | 91 ++++++++++---------
 lib/config/migration.spec.ts                  |  2 +
 lib/config/migration.ts                       | 13 ++-
 lib/util/template/index.ts                    | 20 ++--
 4 files changed, 72 insertions(+), 54 deletions(-)

diff --git a/lib/config/__snapshots__/migration.spec.ts.snap b/lib/config/__snapshots__/migration.spec.ts.snap
index 35860a317c..5f31362308 100644
--- a/lib/config/__snapshots__/migration.spec.ts.snap
+++ b/lib/config/__snapshots__/migration.spec.ts.snap
@@ -6,6 +6,51 @@ Object {
 }
 `;
 
+exports[`config/migration migrateConfig(config, parentConfig) it migrates packageRules 1`] = `
+Object {
+  "packageRules": Array [
+    Object {
+      "excludePackageNames": Array [
+        "baz",
+      ],
+      "excludePackagePatterns": Array [
+        "^baz",
+      ],
+      "matchBaseBranches": Array [
+        "master",
+      ],
+      "matchDatasources": Array [
+        "orb",
+      ],
+      "matchDepTypes": Array [
+        "peerDependencies",
+      ],
+      "matchLanguages": Array [
+        "python",
+      ],
+      "matchManagers": Array [
+        "dockerfile",
+      ],
+      "matchPackageNames": Array [
+        "foo",
+      ],
+      "matchPackagePatterns": Array [
+        "^bar",
+      ],
+      "matchPaths": Array [
+        "package.json",
+      ],
+      "matchSourceUrlPrefixes": Array [
+        "https://github.com/vuejs/vue",
+      ],
+      "matchUpdateTypes": Array [
+        "major",
+      ],
+    },
+  ],
+}
+`;
+
 exports[`config/migration migrateConfig(config, parentConfig) migrates before and after schedules 1`] = `
 Object {
   "major": Object {
@@ -39,6 +84,7 @@ Object {
   "baseBranches": Array [
     "next",
   ],
+  "branchName": "{{{branchPrefix}}}{{{additionalBranchPrefix}}}{{{branchTopic}}}",
   "branchPrefix": "renovate/",
   "commitMessage": "{{#if semanticCommitType}}{{semanticCommitType}}{{#if semanticCommitScope}}({{semanticCommitScope}}){{/if}}: {{/if}}some commit message",
   "commitMessageExtra": "{{currentValue}} something",
@@ -276,51 +322,6 @@ Object {
 }
 `;
 
-exports[`config/migration migrateConfig(config, parentConfig) it migrates packageRules 1`] = `
-Object {
-  "packageRules": Array [
-    Object {
-      "excludePackageNames": Array [
-        "baz",
-      ],
-      "excludePackagePatterns": Array [
-        "^baz",
-      ],
-      "matchBaseBranches": Array [
-        "master",
-      ],
-      "matchDatasources": Array [
-        "orb",
-      ],
-      "matchDepTypes": Array [
-        "peerDependencies",
-      ],
-      "matchLanguages": Array [
-        "python",
-      ],
-      "matchManagers": Array [
-        "dockerfile",
-      ],
-      "matchPackageNames": Array [
-        "foo",
-      ],
-      "matchPackagePatterns": Array [
-        "^bar",
-      ],
-      "matchPaths": Array [
-        "package.json",
-      ],
-      "matchSourceUrlPrefixes": Array [
-        "https://github.com/vuejs/vue",
-      ],
-      "matchUpdateTypes": Array [
-        "major",
-      ],
-    },
-  ],
-}
-`;
-
 exports[`config/migration migrateConfig(config, parentConfig) migrates packages 1`] = `
 Object {
   "packageRules": Array [
diff --git a/lib/config/migration.spec.ts b/lib/config/migration.spec.ts
index 4e6787d04c..7c1d16e4df 100644
--- a/lib/config/migration.spec.ts
+++ b/lib/config/migration.spec.ts
@@ -46,6 +46,8 @@ describe('config/migration', () => {
         gomodTidy: true,
         upgradeInRange: true,
         automergeType: 'branch-push',
+        branchName:
+          '{{{branchPrefix}}}{{{managerBranchPrefix}}}{{{branchTopic}}}',
         baseBranch: 'next',
         managerBranchPrefix: 'foo',
         branchPrefix: 'renovate/{{parentDir}}-',
diff --git a/lib/config/migration.ts b/lib/config/migration.ts
index 16e15c7cec..5b586159a3 100644
--- a/lib/config/migration.ts
+++ b/lib/config/migration.ts
@@ -191,6 +191,15 @@ export function migrateConfig(
         } else if (val === false) {
           migratedConfig.trustLevel = 'low';
         }
+      } else if (
+        key === 'branchName' &&
+        is.string(val) &&
+        val?.includes('{{managerBranchPrefix}}')
+      ) {
+        migratedConfig.branchName = val.replace(
+          '{{managerBranchPrefix}}',
+          '{{additionalBranchPrefix}}'
+        );
       } else if (key === 'managerBranchPrefix') {
         delete migratedConfig.managerBranchPrefix;
         migratedConfig.additionalBranchPrefix = val;
@@ -491,7 +500,9 @@ export function migrateConfig(
           .replace(/currentVersion/g, 'currentValue')
           .replace(/newVersion/g, 'newValue')
           .replace(/newValueMajor/g, 'newMajor')
-          .replace(/newValueMinor/g, 'newMinor');
+          .replace(/newValueMinor/g, 'newMinor')
+          .replace(/newVersionMajor/g, 'newMajor')
+          .replace(/newVersionMinor/g, 'newMinor');
       } else if (key === 'raiseDeprecationWarnings') {
         delete migratedConfig.raiseDeprecationWarnings;
         if (val === false) {
diff --git a/lib/util/template/index.ts b/lib/util/template/index.ts
index 50b5ed6f00..612b9f279c 100644
--- a/lib/util/template/index.ts
+++ b/lib/util/template/index.ts
@@ -85,6 +85,8 @@ export const allowedFields = {
   releases: 'An array of releases for an upgrade',
   releaseNotes: 'A ChangeLogNotes object for the release',
   repository: 'The current repository',
+  semanticPrefix: 'The fully generated semantic prefix for commit messages',
+  sourceUrl: 'The source URL for the package',
   toVersion: 'The new version in the upgrade, e.g. "3.1.0"',
   updateType: 'One of digest, pin, rollback, patch, minor, major',
   upgrades: 'An array of upgrade objects in the branch',
@@ -144,14 +146,16 @@ export function compile(
 ): string {
   const filteredInput = filterFields ? getFilteredObject(input) : input;
   logger.trace({ template, filteredInput }, 'Compiling template');
-  const matches = template.matchAll(templateRegex);
-  for (const match of matches) {
-    const varName = match[3];
-    if (!allowedFieldsList.includes(varName)) {
-      logger.info(
-        { varName, template },
-        'Disallowed variable name in template'
-      );
+  if (filterFields) {
+    const matches = template.matchAll(templateRegex);
+    for (const match of matches) {
+      const varName = match[3];
+      if (!allowedFieldsList.includes(varName)) {
+        logger.info(
+          { varName, template },
+          'Disallowed variable name in template'
+        );
+      }
     }
   }
   return handlebars.compile(template)(filteredInput);
-- 
GitLab