From b3676ac8ec29ad157029e55f72e837669903a39e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20CROCQUESEL?=
 <88554524+scrocquesel@users.noreply.github.com>
Date: Mon, 25 Oct 2021 07:25:35 +0200
Subject: [PATCH] feat(regex): depType can be captured and templated (#12287)

---
 docs/usage/configuration-options.md                | 5 +++++
 lib/config/options/index.ts                        | 9 +++++++++
 lib/config/validation.spec.ts                      | 2 ++
 lib/config/validation.ts                           | 1 +
 lib/manager/regex/__snapshots__/index.spec.ts.snap | 9 +++++++++
 lib/manager/regex/index.spec.ts                    | 2 ++
 lib/manager/regex/index.ts                         | 1 +
 lib/manager/regex/readme.md                        | 1 +
 lib/manager/types.ts                               | 1 +
 9 files changed, 31 insertions(+)

diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md
index 4cf1206535..4b83c5809c 100644
--- a/docs/usage/configuration-options.md
+++ b/docs/usage/configuration-options.md
@@ -2154,6 +2154,11 @@ It will be compiled using Handlebars and the regex `groups` result.
 If the `datasource` for a dependency is not captured with a named group then it can be defined in config using this field.
 It will be compiled using Handlebars and the regex `groups` result.
 
+### depTypeTemplate
+
+If `depType` cannot be captured with a named capture group in `matchString` then it can be defined manually using this field.
+It will be compiled using Handlebars and the regex `groups` result.
+
 ### versioningTemplate
 
 If the `versioning` for a dependency is not captured with a named group then it can be defined in config using this field.
diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts
index e12d34a3ad..d043910c68 100644
--- a/lib/config/options/index.ts
+++ b/lib/config/options/index.ts
@@ -1967,6 +1967,15 @@ const options: RenovateOptions[] = [
     cli: false,
     env: false,
   },
+  {
+    name: 'depTypeTemplate',
+    description:
+      'Optional depType for extracted dependencies. Valid only within a `regexManagers` object.',
+    type: 'string',
+    parent: 'regexManagers',
+    cli: false,
+    env: false,
+  },
   {
     name: 'currentValueTemplate',
     description:
diff --git a/lib/config/validation.spec.ts b/lib/config/validation.spec.ts
index 7699913c18..711db99719 100644
--- a/lib/config/validation.spec.ts
+++ b/lib/config/validation.spec.ts
@@ -352,6 +352,7 @@ describe('config/validation', () => {
             datasourceTemplate: 'bar',
             registryUrlTemplate: 'foobar',
             extractVersionTemplate: '^(?<version>v\\d+\\.\\d+)',
+            depTypeTemplate: 'apple',
           },
         ],
       };
@@ -370,6 +371,7 @@ describe('config/validation', () => {
             matchStrings: ['ENV (?<currentValue>.*?)\\s'],
             depNameTemplate: 'foo',
             datasourceTemplate: 'bar',
+            depTypeTemplate: 'apple',
             automerge: true,
           },
         ],
diff --git a/lib/config/validation.ts b/lib/config/validation.ts
index efe4447047..3faa3dbd77 100644
--- a/lib/config/validation.ts
+++ b/lib/config/validation.ts
@@ -389,6 +389,7 @@ export async function validateConfig(
                 'currentValueTemplate',
                 'extractVersionTemplate',
                 'autoReplaceStringTemplate',
+                'depTypeTemplate',
               ];
               // TODO: fix types
               for (const regexManager of val as any[]) {
diff --git a/lib/manager/regex/__snapshots__/index.spec.ts.snap b/lib/manager/regex/__snapshots__/index.spec.ts.snap
index 6071a39e35..bcc3505484 100644
--- a/lib/manager/regex/__snapshots__/index.spec.ts.snap
+++ b/lib/manager/regex/__snapshots__/index.spec.ts.snap
@@ -81,11 +81,13 @@ Object {
 
 exports[`manager/regex/index extracts multiple dependencies 1`] = `
 Object {
+  "depTypeTemplate": "final",
   "deps": Array [
     Object {
       "currentValue": "6.2",
       "datasource": "gradle-version",
       "depName": "gradle",
+      "depType": "final",
       "replaceString": "ENV GRADLE_VERSION=6.2 # gradle-version/gradle&versioning=maven
 ",
       "versioning": "maven",
@@ -94,6 +96,7 @@ Object {
       "currentValue": "10.19.0",
       "datasource": "github-tags",
       "depName": "nodejs/node",
+      "depType": "final",
       "replaceString": "ENV NODE_VERSION=10.19.0 # github-tags/nodejs/node&versioning=node
 ",
       "versioning": "node",
@@ -102,6 +105,7 @@ Object {
       "currentValue": "1.9.3",
       "datasource": "github-releases",
       "depName": "composer/composer",
+      "depType": "final",
       "replaceString": "ENV COMPOSER_VERSION=1.9.3 # github-releases/composer/composer
 ",
       "versioning": "semver",
@@ -110,6 +114,7 @@ Object {
       "currentValue": "1.9.0",
       "datasource": "rubygems",
       "depName": "cocoapods",
+      "depType": "final",
       "replaceString": "ENV COCOAPODS_VERSION=1.9.0 # rubygems/cocoapods&versioning=ruby
 ",
       "versioning": "ruby",
@@ -118,6 +123,7 @@ Object {
       "currentValue": "19.03.1",
       "datasource": "github-releases",
       "depName": "docker/docker-ce",
+      "depType": "final",
       "replaceString": "ENV DOCKER_VERSION=19.03.1 # github-releases/docker/docker-ce&versioning=docker
 ",
       "versioning": "docker",
@@ -126,6 +132,7 @@ Object {
       "currentValue": "1.0.0",
       "datasource": "github-releases",
       "depName": "python-poetry/poetry",
+      "depType": "final",
       "replaceString": "ENV POETRY_VERSION=1.0.0 # github-releases/python-poetry/poetry
 ",
       "versioning": "semver",
@@ -134,6 +141,7 @@ Object {
       "currentValue": "6.10.2",
       "datasource": "npm",
       "depName": "npm",
+      "depType": "final",
       "replaceString": "ENV NPM_VERSION=6.10.2 # npm/npm
 ",
       "versioning": "semver",
@@ -142,6 +150,7 @@ Object {
       "currentValue": "1.19.1",
       "datasource": "npm",
       "depName": "yarn",
+      "depType": "final",
       "replaceString": "ENV YARN_VERSION=1.19.1 # npm/yarn
 ",
       "versioning": "semver",
diff --git a/lib/manager/regex/index.spec.ts b/lib/manager/regex/index.spec.ts
index 7f3d9bdfc6..13258f85f2 100644
--- a/lib/manager/regex/index.spec.ts
+++ b/lib/manager/regex/index.spec.ts
@@ -21,6 +21,7 @@ describe('manager/regex/index', () => {
       ],
       versioningTemplate:
         '{{#if versioning}}{{versioning}}{{else}}semver{{/if}}',
+      depTypeTemplate: 'final',
     };
     const res = await extractPackageFile(
       dockerfileContent,
@@ -35,6 +36,7 @@ describe('manager/regex/index', () => {
     expect(res.deps.find((dep) => dep.depName === 'gradle').versioning).toEqual(
       'maven'
     );
+    expect(res.deps.filter((dep) => dep.depType === 'final')).toHaveLength(8);
   });
   it('returns null if no dependencies found', async () => {
     const config = {
diff --git a/lib/manager/regex/index.ts b/lib/manager/regex/index.ts
index 70f9c181ef..7a866a6f70 100644
--- a/lib/manager/regex/index.ts
+++ b/lib/manager/regex/index.ts
@@ -22,6 +22,7 @@ const validMatchFields = [
   'versioning',
   'extractVersion',
   'registryUrl',
+  'depType',
 ];
 
 const mergeFields = ['registryUrls', ...validMatchFields];
diff --git a/lib/manager/regex/readme.md b/lib/manager/regex/readme.md
index c7289ff64a..71d5337bb0 100644
--- a/lib/manager/regex/readme.md
+++ b/lib/manager/regex/readme.md
@@ -21,6 +21,7 @@ Configuration-wise, it works like this:
 - You must have either a `depName` capture group or a `depNameTemplate` config field
 - You can optionally have a `lookupName` capture group or a `lookupNameTemplate` if it differs from `depName`
 - You must have either a `datasource` capture group or a `datasourceTemplate` config field
+- You can optionally have a `depType` capture group or a `depTypeTemplate` config field
 - You can optionally have a `versioning` capture group or a `versioningTemplate` config field. If neither are present, `semver` will be used as the default
 - You can optionally have an `extractVersion` capture group or an `extractVersionTemplate` config field
 - You can optionally have a `currentDigest` capture group.
diff --git a/lib/manager/types.ts b/lib/manager/types.ts
index 64f6e496af..311ad5e492 100644
--- a/lib/manager/types.ts
+++ b/lib/manager/types.ts
@@ -35,6 +35,7 @@ export interface CustomExtractConfig extends ExtractConfig {
   lookupNameTemplate?: string;
   datasourceTemplate?: string;
   versioningTemplate?: string;
+  depTypeTemplate?: string;
 }
 
 export interface UpdateArtifactsConfig {
-- 
GitLab