From 699da47083dddb66e5f5d2d48825b2e375a1d6c3 Mon Sep 17 00:00:00 2001
From: adrian-gomez <adrian_g171@hotmail.com>
Date: Mon, 10 Feb 2020 16:23:41 -0300
Subject: [PATCH] Add ability to ignore merge conflicts (#17)

---
 README.md            |  4 ++++
 src/autoupdater.js   | 10 ++++++++++
 src/config-loader.js |  5 +++++
 3 files changed, 19 insertions(+)

diff --git a/README.md b/README.md
index 4546a374..a79e8b05 100644
--- a/README.md
+++ b/README.md
@@ -54,6 +54,10 @@ All configuration values, except `GITHUB_TOKEN`, are optional.
 
 * `RETRY_SLEEP`: The amount of time (in milliseconds) that _autoupdate_ should wait between branch update attempts (default: `"300"`).
 
+* `MERGE_CONFLICT_ACTION`: Controls how _autoupdate_ handles a merge conflict when updating a PR. Possible values are:
+  * `"fail"` (default): _autoupdate_ will report a failure on each PR that has a merge conflict.
+  * `"ignore"`: _autoupdate_ will silently ignore merge conflicts.
+
 Here's an example workflow file with all of the above options specified:
 
 ```yaml
diff --git a/src/autoupdater.js b/src/autoupdater.js
index adde3a23..aa0118f7 100644
--- a/src/autoupdater.js
+++ b/src/autoupdater.js
@@ -224,6 +224,8 @@ class AutoUpdater {
 
     const retryCount = this.config.retryCount();
     const retrySleep = this.config.retrySleep();
+    const mergeConflictAction = this.config.mergeConflictAction();
+    
     let retries = 0;
 
     while (true) {
@@ -232,6 +234,14 @@ class AutoUpdater {
         await doMerge();
         break;
       } catch (e) {
+        if (e.message === "Merge conflict" && mergeConflictAction === "ignore") {
+          ghCore.info('Merge conflict detected, skipping update.');
+          return;
+        } else if (e.message === "Merge conflict") {
+          ghCore.error("Merge conflict error trying to update branch");
+          throw e;
+        }
+
         ghCore.error(`Caught error trying to update branch: ${e.message}`);
 
         if (retries < retryCount) {
diff --git a/src/config-loader.js b/src/config-loader.js
index f4486d33..18c56dbf 100644
--- a/src/config-loader.js
+++ b/src/config-loader.js
@@ -60,6 +60,11 @@ class ConfigLoader {
     ), 10);
   }
 
+  mergeConflictAction() {
+    // one of 'fail' or 'ignore'.
+    return this.getValue('MERGE_CONFLICT_ACTION', false,  'fail');
+  }
+
   getValue(key, required = false, defaulVal = null) {
     if (key in this.env
       && this.env[key] !== null