From b2d9dd3b5743c735e8ca1f28bc62f291ecad6e2a Mon Sep 17 00:00:00 2001
From: Moritz <15136847+Nemental@users.noreply.github.com>
Date: Tue, 10 Sep 2024 18:42:11 +0200
Subject: [PATCH] feat(docker-compose): yaml parser option `removeTemplates`
 (#31206)

---
 .../manager/docker-compose/extract.spec.ts    | 81 +++++++++++--------
 lib/modules/manager/docker-compose/extract.ts |  1 +
 2 files changed, 50 insertions(+), 32 deletions(-)

diff --git a/lib/modules/manager/docker-compose/extract.spec.ts b/lib/modules/manager/docker-compose/extract.spec.ts
index 01a4caf31c..b9436050d8 100644
--- a/lib/modules/manager/docker-compose/extract.spec.ts
+++ b/lib/modules/manager/docker-compose/extract.spec.ts
@@ -1,3 +1,4 @@
+import { codeBlock } from 'common-tags';
 import { Fixtures } from '../../../../test/fixtures';
 import { extractPackageFile } from '.';
 
@@ -56,20 +57,17 @@ describe('modules/manager/docker-compose/extract', () => {
     });
 
     it('extracts image and replaces registry', () => {
-      const res = extractPackageFile(
-        `
-    version: "3"
-    services:
-      nginx:
-        image: quay.io/nginx:0.0.1
-      `,
-        '',
-        {
-          registryAliases: {
-            'quay.io': 'my-quay-mirror.registry.com',
-          },
+      const compose = codeBlock`
+        version: "3"
+        services:
+          nginx:
+            image: quay.io/nginx:0.0.1
+      `;
+      const res = extractPackageFile(compose, '', {
+        registryAliases: {
+          'quay.io': 'my-quay-mirror.registry.com',
         },
-      );
+      });
       expect(res).toEqual({
         deps: [
           {
@@ -86,20 +84,17 @@ describe('modules/manager/docker-compose/extract', () => {
     });
 
     it('extracts image but no replacement', () => {
-      const res = extractPackageFile(
-        `
+      const compose = codeBlock`
         version: "3"
         services:
           nginx:
             image: quay.io/nginx:0.0.1
-        `,
-        '',
-        {
-          registryAliases: {
-            'index.docker.io': 'my-docker-mirror.registry.com',
-          },
+      `;
+      const res = extractPackageFile(compose, '', {
+        registryAliases: {
+          'index.docker.io': 'my-docker-mirror.registry.com',
         },
-      );
+      });
       expect(res).toEqual({
         deps: [
           {
@@ -116,21 +111,18 @@ describe('modules/manager/docker-compose/extract', () => {
     });
 
     it('extracts image and no double replacement', () => {
-      const res = extractPackageFile(
-        `
+      const compose = codeBlock`
         version: "3"
         services:
           nginx:
             image: quay.io/nginx:0.0.1
-        `,
-        '',
-        {
-          registryAliases: {
-            'quay.io': 'my-quay-mirror.registry.com',
-            'my-quay-mirror.registry.com': 'quay.io',
-          },
+      `;
+      const res = extractPackageFile(compose, '', {
+        registryAliases: {
+          'quay.io': 'my-quay-mirror.registry.com',
+          'my-quay-mirror.registry.com': 'quay.io',
         },
-      );
+      });
       expect(res).toEqual({
         deps: [
           {
@@ -145,5 +137,30 @@ describe('modules/manager/docker-compose/extract', () => {
         ],
       });
     });
+
+    it('extracts image of templated compose file', () => {
+      const compose = codeBlock`
+        version: "3"
+        services:
+          nginx:
+            image: quay.io/nginx:0.0.1
+            envrionment:
+              {{ services['nginx']['env'] }}
+      `;
+      const res = extractPackageFile(compose, '', {});
+      expect(res).toEqual({
+        deps: [
+          {
+            autoReplaceStringTemplate:
+              '{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}',
+            currentDigest: undefined,
+            currentValue: '0.0.1',
+            datasource: 'docker',
+            depName: 'quay.io/nginx',
+            replaceString: 'quay.io/nginx:0.0.1',
+          },
+        ],
+      });
+    });
   });
 });
diff --git a/lib/modules/manager/docker-compose/extract.ts b/lib/modules/manager/docker-compose/extract.ts
index 255d47ae7d..f685323a95 100644
--- a/lib/modules/manager/docker-compose/extract.ts
+++ b/lib/modules/manager/docker-compose/extract.ts
@@ -39,6 +39,7 @@ export function extractPackageFile(
     config = parseSingleYaml(content, {
       json: true,
       customSchema: DockerComposeFile,
+      removeTemplates: true,
     });
   } catch (err) {
     logger.debug(
-- 
GitLab