From 8dfad00414ed3d5a839117900d265d0e8c92bced Mon Sep 17 00:00:00 2001
From: Hasan Awad <90554456+hasanwhitesource@users.noreply.github.com>
Date: Wed, 11 May 2022 16:57:14 +0300
Subject: [PATCH] fix(github-actions): handle quotes (#15502)

---
 .../__snapshots__/extract.spec.ts.snap        |  4 +-
 .../manager/github-actions/extract.spec.ts    | 82 +++++++++++++++++++
 lib/modules/manager/github-actions/extract.ts |  9 +-
 3 files changed, 92 insertions(+), 3 deletions(-)

diff --git a/lib/modules/manager/github-actions/__snapshots__/extract.spec.ts.snap b/lib/modules/manager/github-actions/__snapshots__/extract.spec.ts.snap
index a4b68ba2b5..721889f778 100644
--- a/lib/modules/manager/github-actions/__snapshots__/extract.spec.ts.snap
+++ b/lib/modules/manager/github-actions/__snapshots__/extract.spec.ts.snap
@@ -93,7 +93,7 @@ Array [
 exports[`modules/manager/github-actions/extract extractPackageFile() extracts multiple action tag lines with double quotes and comments 1`] = `
 Array [
   Object {
-    "autoReplaceStringTemplate": "{{depName}}@{{#if newDigest}}{{newDigest}}{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}{{/unless}}",
+    "autoReplaceStringTemplate": "\\"{{depName}}@{{#if newDigest}}{{newDigest}}\\"{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}\\"{{/unless}}",
     "commitMessageTopic": "{{{depName}}} action",
     "currentValue": "v0.13.1",
     "datasource": "github-tags",
@@ -124,7 +124,7 @@ Array [
     "versioning": "docker",
   },
   Object {
-    "autoReplaceStringTemplate": "{{depName}}@{{#if newDigest}}{{newDigest}}{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}{{/unless}}",
+    "autoReplaceStringTemplate": "\\"{{depName}}@{{#if newDigest}}{{newDigest}}\\"{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}\\"{{/unless}}",
     "commitMessageTopic": "{{{depName}}} action",
     "currentValue": "v1.1.2",
     "datasource": "github-tags",
diff --git a/lib/modules/manager/github-actions/extract.spec.ts b/lib/modules/manager/github-actions/extract.spec.ts
index a3f129c54b..3dde393331 100644
--- a/lib/modules/manager/github-actions/extract.spec.ts
+++ b/lib/modules/manager/github-actions/extract.spec.ts
@@ -59,5 +59,87 @@ describe('modules/manager/github-actions/extract', () => {
         },
       ]);
     });
+
+    it('maintains quotes', () => {
+      const yamlContent = `
+      jobs:
+        build:
+          steps:
+            - name: "test1"
+              uses: actions/setup-node@56337c425554a6be30cdef71bf441f15be286854 # tag=v3.1.1
+            - name: "test2"
+              uses: 'actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561' # tag=v3.1.1
+            - name: "test3"
+              uses: "actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561" # tag=v2.5.1
+            - name: "checkout repository"
+              uses: "actions/checkout@v2" # comment after
+            - name: "quoted, no comment, outdated"
+              uses: "actions/setup-java@v2"`;
+
+      const res = extractPackageFile(yamlContent);
+      expect(res.deps).toMatchObject([
+        {
+          depName: 'actions/setup-node',
+          commitMessageTopic: '{{{depName}}} action',
+          datasource: 'github-tags',
+          versioning: 'docker',
+          depType: 'action',
+          replaceString:
+            'actions/setup-node@56337c425554a6be30cdef71bf441f15be286854 # tag=v3.1.1',
+          autoReplaceStringTemplate:
+            '{{depName}}@{{#if newDigest}}{{newDigest}}{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}{{/unless}}',
+          currentValue: 'v3.1.1',
+          currentDigest: '56337c425554a6be30cdef71bf441f15be286854',
+        },
+        {
+          depName: 'actions/setup-node',
+          commitMessageTopic: '{{{depName}}} action',
+          datasource: 'github-tags',
+          versioning: 'docker',
+          depType: 'action',
+          replaceString:
+            "'actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561' # tag=v3.1.1",
+          autoReplaceStringTemplate:
+            "'{{depName}}@{{#if newDigest}}{{newDigest}}'{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}'{{/unless}}",
+          currentValue: 'v3.1.1',
+          currentDigest: '1f8c6b94b26d0feae1e387ca63ccbdc44d27b561',
+        },
+        {
+          depName: 'actions/setup-node',
+          commitMessageTopic: '{{{depName}}} action',
+          datasource: 'github-tags',
+          versioning: 'docker',
+          depType: 'action',
+          replaceString:
+            '"actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561" # tag=v2.5.1',
+          autoReplaceStringTemplate:
+            '"{{depName}}@{{#if newDigest}}{{newDigest}}"{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}"{{/unless}}',
+          currentValue: 'v2.5.1',
+          currentDigest: '1f8c6b94b26d0feae1e387ca63ccbdc44d27b561',
+        },
+        {
+          depName: 'actions/checkout',
+          commitMessageTopic: '{{{depName}}} action',
+          datasource: 'github-tags',
+          versioning: 'docker',
+          depType: 'action',
+          replaceString: '"actions/checkout@v2"',
+          autoReplaceStringTemplate:
+            '"{{depName}}@{{#if newDigest}}{{newDigest}}"{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}"{{/unless}}',
+          currentValue: 'v2',
+        },
+        {
+          depName: 'actions/setup-java',
+          commitMessageTopic: '{{{depName}}} action',
+          datasource: 'github-tags',
+          versioning: 'docker',
+          depType: 'action',
+          replaceString: '"actions/setup-java@v2"',
+          autoReplaceStringTemplate:
+            '"{{depName}}@{{#if newDigest}}{{newDigest}}"{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}"{{/unless}}',
+          currentValue: 'v2',
+        },
+      ]);
+    });
   });
 });
diff --git a/lib/modules/manager/github-actions/extract.ts b/lib/modules/manager/github-actions/extract.ts
index 222263fc40..c5162e583a 100644
--- a/lib/modules/manager/github-actions/extract.ts
+++ b/lib/modules/manager/github-actions/extract.ts
@@ -40,6 +40,13 @@ export function extractPackageFile(content: string): PackageFile | null {
         tag,
         replaceString,
       } = tagMatch.groups;
+      let quotes = '';
+      if (replaceString.indexOf("'") >= 0) {
+        quotes = "'";
+      }
+      if (replaceString.indexOf('"') >= 0) {
+        quotes = '"';
+      }
       const dep: PackageDependency = {
         depName,
         commitMessageTopic: '{{{depName}}} action',
@@ -47,7 +54,7 @@ export function extractPackageFile(content: string): PackageFile | null {
         versioning: dockerVersioning.id,
         depType: 'action',
         replaceString,
-        autoReplaceStringTemplate: `{{depName}}${path}@{{#if newDigest}}{{newDigest}}{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}{{/unless}}`,
+        autoReplaceStringTemplate: `${quotes}{{depName}}${path}@{{#if newDigest}}{{newDigest}}${quotes}{{#if newValue}} # tag={{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}${quotes}{{/unless}}`,
       };
       if (shaRe.test(currentValue)) {
         dep.currentValue = tag;
-- 
GitLab