From c87a153823292919c97308e0205ed52cc8f6ee16 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 11 Feb 2019 10:19:25 +0100
Subject: [PATCH] feat: go module vendoring support (#3191)

Detects if a `vendor/modules.txt` is present and runs `go mod vendor` if so. Requires gitFs to work.

Closes #2580
---
 lib/manager/gomod/artifacts.js | 53 +++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 10 deletions(-)

diff --git a/lib/manager/gomod/artifacts.js b/lib/manager/gomod/artifacts.js
index 3437d3d0cb..e51935d31d 100644
--- a/lib/manager/gomod/artifacts.js
+++ b/lib/manager/gomod/artifacts.js
@@ -65,19 +65,20 @@ async function getArtifacts(
       logger.info('Running go via global command');
       cmd = 'go';
     }
-    const args = 'get';
+    let args = 'get';
     logger.debug({ cmd, args }, 'go get command');
     ({ stdout, stderr } = await exec(`${cmd} ${args}`, {
       cwd,
       shell: true,
       env,
     }));
-    const duration = process.hrtime(startTime);
-    const seconds = Math.round(duration[0] + duration[1] / 1e9);
+    let duration = process.hrtime(startTime);
+    let seconds = Math.round(duration[0] + duration[1] / 1e9);
     logger.info(
       { seconds, type: 'go.sum', stdout, stderr },
       'Generated lockfile'
     );
+    const res = [];
     // istanbul ignore if
     if (config.gitFs) {
       const status = await platform.getRepoStatus();
@@ -93,14 +94,46 @@ async function getArtifacts(
       }
     }
     logger.debug('Returning updated go.sum');
-    return [
-      {
-        file: {
-          name: sumFileName,
-          contents: await fs.readFile(localGoSumFileName, 'utf8'),
-        },
+    res.push({
+      file: {
+        name: sumFileName,
+        contents: await fs.readFile(localGoSumFileName, 'utf8'),
       },
-    ];
+    });
+    const vendorDir = upath.join(upath.dirname(goModFileName), 'vendor/');
+    const vendorModulesFileName = upath.join(vendorDir, 'modules.txt');
+    // istanbul ignore if
+    if (platform.getFile(vendorModulesFileName)) {
+      if (config.gitFs) {
+        args = 'mod vendor';
+        logger.debug({ cmd, args }, 'go mod vendor command');
+        ({ stdout, stderr } = await exec(`${cmd} ${args}`, {
+          cwd,
+          shell: true,
+          env,
+        }));
+        duration = process.hrtime(startTime);
+        seconds = Math.round(duration[0] + duration[1] / 1e9);
+        logger.info({ seconds, stdout, stderr }, 'Vendored modules');
+        const status = await platform.getRepoStatus();
+        for (const modified of status.modified) {
+          if (modified.startsWith(vendorDir)) {
+            const localModified = upath.join(config.localDir, modified);
+            res.push({
+              file: {
+                name: modified,
+                contents: await fs.readFile(localModified, 'utf8'),
+              },
+            });
+          }
+        }
+      } else {
+        logger.warn(
+          'Vendor modules found - Renovate administrator should enable gitFs in order to update them'
+        );
+      }
+    }
+    return res;
   } catch (err) {
     logger.warn({ err, message: err.message }, 'Failed to update go.sum');
     return [
-- 
GitLab