diff --git a/lib/worker.js b/lib/worker.js index 0d703886ba3ec15bc64dd9210620f63828e38ebf..991839e9585b496ad31d185d988aefb7052b27e7 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -119,7 +119,7 @@ function updateDependency(upgrade) { const commitMessage = handlebars.transform(config.commitMessage, params); const prTitle = handlebars.transform(config.prTitle, params); - return checkForUnmergeablePr() + return checkForClosedPr() .then(ensureBranch) .then(getChangelog) .then(log => ensurePr(log)) @@ -161,33 +161,6 @@ function updateDependency(upgrade) { }); } - function deleteUnmergeablePr(pr) { - return github.updatePr(pr.number, `${pr.title} (unmergeable)`) - .then(() => github.deleteBranch(branchName)); - } - - function checkForUnmergeablePr() { - return getBranchPr() - .then((pr) => { - if (pr) { - logger.debug(`checkForUnmergeablePr found PR #${pr.number}`); - if (pr.mergeable_state === 'dirty') { - logger.debug(`Existing PR ${pr.number} is not mergeable`); - if (pr.additions * pr.deletions === 1) { - // No other changes except ours - logger.verbose(`Deleting branch ${branchName}`); - // Rename PR and then delete the branch - return deleteUnmergeablePr(pr); - } - logger.verbose(`Not deleting branch ${branchName} because it has additional changes`); - } - return Promise.resolve(); - } - // If no open PR exists then check for any closed one - return checkForClosedPr(); - }); - } - function ensureBranch() { return github.branchExists(branchName) .then((branchExists) => { @@ -207,16 +180,33 @@ function updateDependency(upgrade) { } function updateExistingBranch() { + // By default, we'll add a commit to the existing branch if necessary + let parentBranch = branchName; logger.debug(`Checking if branch ${branchName} needs updating`); - return github.getFileContent(config.packageFile, branchName) + return getBranchPr() + .then((pr) => { + if (pr.mergeable_state === 'dirty') { + logger.verbose(`Existing PR #${pr.number} is not mergeable`); + if (pr.additions * pr.deletions === 1) { + // No other changes except ours + logger.verbose(`Rebasing ${branchName}`); + // Setting parentBranch to undefined means that we'll use the default branch + parentBranch = undefined; + } else { + logger.warning(`Cannot rebase ${branchName} as it has been modified`); + } + } + return Promise.resolve(); + }) + .then(() => github.getFileContent(config.packageFile, parentBranch)) .then((existingContent) => { const newContent = getNewPackageJson(existingContent); if (newContent === existingContent) { - logger.debug(`Branch ${branchName} is already up-to-date`); + logger.debug(`Branch ${parentBranch} is already up-to-date`); return Promise.resolve(); } logger.verbose(`Adding commit '${commitMessage}' to branch ${branchName}`); - return github.commitFile(config.packageFile, newContent, commitMessage, branchName); + return github.commitFile(config.packageFile, newContent, commitMessage, parentBranch); }) .then((commit) => { if (!commit) {