diff --git a/src/github.js b/src/github.js index 109c9858d3859e47c9001606803433e7e23a12c0..795344db2e5ebfccd6aff0cb588891ba25b7fb65 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 2fd43142cfd6d012f7513f0badf638340d32ed1d..0b3b6db0df3bf6264fbb06e0c3429499743d87a5 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 {