From 8f79b660b01504a778933d315438f3788a6630bc Mon Sep 17 00:00:00 2001
From: Paul Tyng <paul@paultyng.net>
Date: Wed, 13 Feb 2019 13:05:52 -0500
Subject: [PATCH] feat(gomod): Run `go mod tidy` if configured (#3201)

Adds a configurable option to run `go mod tidy` on a repo between the `go get...` and the `go mod vendor`.

Closes #2594
---
 lib/config/definitions.js                     |  6 +++++
 lib/manager/gomod/artifacts.js                | 22 +++++++++++++++++++
 .../__snapshots__/flatten.spec.js.snap        |  8 +++++++
 website/docs/configuration-options.md         |  2 ++
 4 files changed, 38 insertions(+)

diff --git a/lib/config/definitions.js b/lib/config/definitions.js
index b37632b498..e21b310b5d 100644
--- a/lib/config/definitions.js
+++ b/lib/config/definitions.js
@@ -1064,6 +1064,12 @@ const options = [
     },
     mergeable: true,
   },
+  {
+    name: 'gomodTidy',
+    description: 'Enable to run `go mod tidy` after Go module updates',
+    type: 'boolean',
+    default: false,
+  },
   {
     name: 'ruby',
     releaseStatus: 'alpha',
diff --git a/lib/manager/gomod/artifacts.js b/lib/manager/gomod/artifacts.js
index e51935d31d..1827baebce 100644
--- a/lib/manager/gomod/artifacts.js
+++ b/lib/manager/gomod/artifacts.js
@@ -78,6 +78,28 @@ async function getArtifacts(
       { seconds, type: 'go.sum', stdout, stderr },
       'Generated lockfile'
     );
+    // istanbul ignore if
+    if (config.gomodTidy) {
+      if (config.gitFs) {
+        args = 'mod tidy';
+        logger.debug({ cmd, args }, 'go mod tidy 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, type: 'go.sum', stdout, stderr },
+          'Tidied lockfile'
+        );
+      } else {
+        logger.warn(
+          'Renovate administrator should enable gitFs in order to support tidying go modules'
+        );
+      }
+    }
     const res = [];
     // istanbul ignore if
     if (config.gitFs) {
diff --git a/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap b/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap
index e50697a4d8..d5161e4c73 100644
--- a/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap
+++ b/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap
@@ -27,6 +27,7 @@ Array [
     "gitAuthor": null,
     "gitFs": null,
     "gitPrivateKey": null,
+    "gomodTidy": false,
     "group": Object {
       "branchTopic": "{{{groupSlug}}}",
       "commitMessageTopic": "{{{groupName}}}",
@@ -124,6 +125,7 @@ Array [
     "gitAuthor": null,
     "gitFs": null,
     "gitPrivateKey": null,
+    "gomodTidy": false,
     "group": Object {
       "branchTopic": "{{{groupSlug}}}",
       "commitMessageTopic": "{{{groupName}}}",
@@ -219,6 +221,7 @@ Array [
     "gitAuthor": null,
     "gitFs": null,
     "gitPrivateKey": null,
+    "gomodTidy": false,
     "group": Object {
       "branchTopic": "{{{groupSlug}}}",
       "commitMessageTopic": "{{{groupName}}}",
@@ -318,6 +321,7 @@ Array [
     "gitAuthor": null,
     "gitFs": null,
     "gitPrivateKey": null,
+    "gomodTidy": false,
     "group": Object {
       "branchTopic": "{{{groupSlug}}}",
       "commitMessageTopic": "{{{groupName}}}",
@@ -413,6 +417,7 @@ Array [
     "gitAuthor": null,
     "gitFs": null,
     "gitPrivateKey": null,
+    "gomodTidy": false,
     "group": Object {
       "branchTopic": "{{{groupSlug}}}",
       "commitMessageTopic": "{{{groupName}}}",
@@ -512,6 +517,7 @@ Array [
     "gitAuthor": null,
     "gitFs": null,
     "gitPrivateKey": null,
+    "gomodTidy": false,
     "group": Object {
       "branchTopic": "{{{groupSlug}}}",
       "commitMessageTopic": "{{{groupName}}}",
@@ -609,6 +615,7 @@ Array [
     "gitAuthor": null,
     "gitFs": null,
     "gitPrivateKey": null,
+    "gomodTidy": false,
     "group": Object {
       "branchTopic": "{{{groupSlug}}}",
       "commitMessageTopic": "{{{groupName}}} Docker tags",
@@ -706,6 +713,7 @@ Array [
     "gitAuthor": null,
     "gitFs": null,
     "gitPrivateKey": null,
+    "gomodTidy": false,
     "group": Object {
       "branchTopic": "{{{groupSlug}}}",
       "commitMessageTopic": "{{{groupName}}} Docker tags",
diff --git a/website/docs/configuration-options.md b/website/docs/configuration-options.md
index 672988d933..fc6a7bbbd4 100644
--- a/website/docs/configuration-options.md
+++ b/website/docs/configuration-options.md
@@ -247,6 +247,8 @@ Configuration added here applies for all Go-related updates, however currently t
 
 Configuration for Go Modules (`go mod`). Supersedes anything in the `go` config object.
 
+## gomodTidy
+
 ## gradle
 
 Configuration for Java gradle projects
-- 
GitLab