From 0cf8cc0cc561d3f599596c5567cc5b7d289b00c3 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Sat, 7 Jan 2017 21:19:15 +0100
Subject: [PATCH] Simplify check for closed pr

---
 src/github.js | 38 +++++++++++---------------------------
 src/index.js  |  2 +-
 2 files changed, 12 insertions(+), 28 deletions(-)

diff --git a/src/github.js b/src/github.js
index 109c9858d3..795344db2e 100644
--- a/src/github.js
+++ b/src/github.js
@@ -45,32 +45,16 @@ module.exports = {
       }
     });
   },
-  checkPrExists(branchName, prTitle) {
-    if (config.verbose) {
-      console.log(`Checking if branch/PR exists: ${branchName} / ${prTitle}`);
-    }
-    return ghGot(`repos/${config.repoName}/pulls?state=closed&head=${config.userName}:${branchName}`, { token: config.token })
-      .then(res => {
-        if (config.verbose) {
-          console.log(`Got ${res.body.length} results for ${branchName}`);
-        }
-        let prAlreadyExists = false;
-        res.body.forEach(function(result) {
-          if (result.title === prTitle && result.head.label === `${config.userName}:${branchName}`) {
-            prAlreadyExists = true;
-          }
-        });
-        if (config.verbose) {
-          if (prAlreadyExists) {
-            console.log(`PR already exists for ${branchName}`);
-          } else {
-            console.log(`PR doesn't exist for ${branchName}`);
-          }
-        }
-        return prAlreadyExists;
-      }).catch((err) => {
-        console.error('Error checking if PR already existed');
+  checkForClosedPr(branchName, prTitle) {
+    return ghGot(`repos/${config.repoName}/pulls?state=closed&head=${config.userName}:${branchName}`, {
+      token: config.token
+    }).then(res => {
+      return res.body.some((pr) => {
+        return pr.title === prTitle && pr.head.label === `${config.userName}:${branchName}`;
       });
+    }).catch((err) => {
+      console.error('Error checking if PR already existed');
+    });
   },
   getFile: function(filePath, branchName) {
     branchName = branchName || config.baseBranch;
@@ -83,8 +67,8 @@ module.exports = {
       return JSON.parse(new Buffer(res.body.content, 'base64').toString());
     });
   },
-  getPrNo: function(branchName) {
-    return ghGot(`repos/${config.repoName}/pulls?base=${config.baseBranch}&head=${config.userName}:${branchName}`, {
+  getPrNo: function(branchName, state = 'open') {
+    return ghGot(`repos/${config.repoName}/pulls?state=${state}&base=${config.baseBranch}&head=${config.userName}:${branchName}`, {
       token: config.token,
     }).then(res => {
       let prNo = 0;
diff --git a/src/index.js b/src/index.js
index 2fd43142cf..0b3b6db0df 100644
--- a/src/index.js
+++ b/src/index.js
@@ -60,7 +60,7 @@ function updateDependency(depType, depName, currentVersion, nextVersion) {
     prTitle = config.templates.prTitleMinor({ depType, depName, currentVersion, nextVersion, nextVersionMajor });
   }
   // Check if same PR already exists or existed
-  return github.checkPrExists(branchName, prTitle).then((prExisted) => {
+  return github.checkForClosedPr(branchName, prTitle).then((prExisted) => {
     if (!prExisted) {
       return writeUpdates(depType, depName, branchName, prTitle, currentVersion, nextVersion);
     } else {
-- 
GitLab