diff --git a/lib/workers/repository/update/pr/index.spec.ts b/lib/workers/repository/update/pr/index.spec.ts index a0bbcb6c6522858723a13f2a4dec568f85766577..ba55693a69640d8b5f74003dec14e7343f4c9d7b 100644 --- a/lib/workers/repository/update/pr/index.spec.ts +++ b/lib/workers/repository/update/pr/index.spec.ts @@ -341,8 +341,6 @@ describe('workers/repository/update/pr/index', () => { describe('Automerge', () => { it('handles branch automerge', async () => { - platform.getBranchPr.mockResolvedValueOnce(pr); - const res = await ensurePr({ ...config, automerge: true, diff --git a/lib/workers/repository/update/pr/index.ts b/lib/workers/repository/update/pr/index.ts index dfdc6974891f6e85896ea3825e9dffc3a1e1477f..f63fff0a9cd3affc6287e4c8cc5c20723ba8515e 100644 --- a/lib/workers/repository/update/pr/index.ts +++ b/lib/workers/repository/update/pr/index.ts @@ -115,75 +115,74 @@ export async function ensurePr( config.forcePr = true; } - // Only create a PR if a branch automerge has failed - if ( - config.automerge === true && - config.automergeType?.startsWith('branch') && - !config.forcePr - ) { - logger.debug(`Branch automerge is enabled`); + if (!existingPr) { + // Only create a PR if a branch automerge has failed if ( - config.stabilityStatus !== 'yellow' && - (await getBranchStatus()) === 'yellow' && - is.number(config.prNotPendingHours) + config.automerge === true && + config.automergeType?.startsWith('branch') && + !config.forcePr ) { - logger.debug('Checking how long this branch has been pending'); - const lastCommitTime = await getBranchLastCommitTime(branchName); - if (getElapsedHours(lastCommitTime) >= config.prNotPendingHours) { - logger.debug('Branch exceeds prNotPending hours - forcing PR creation'); - config.forcePr = true; - } - } - if (config.forcePr || (await getBranchStatus()) === 'red') { - logger.debug(`Branch tests failed, so will create PR`); - } else { - // Branch should be automerged, so we don't want to create a PR - return { type: 'without-pr', prBlockedBy: 'BranchAutomerge' }; - } - } - if (config.prCreation === 'status-success') { - logger.debug('Checking branch combined status'); - if ((await getBranchStatus()) !== 'green') { - logger.debug(`Branch status isn't green - not creating PR`); - return { type: 'without-pr', prBlockedBy: 'AwaitingTests' }; - } - logger.debug('Branch status success'); - } else if ( - config.prCreation === 'approval' && - !existingPr && - dependencyDashboardCheck !== 'approvePr' - ) { - return { type: 'without-pr', prBlockedBy: 'NeedsApproval' }; - } else if ( - config.prCreation === 'not-pending' && - !existingPr && - !config.forcePr - ) { - logger.debug('Checking branch combined status'); - if ((await getBranchStatus()) === 'yellow') { - logger.debug(`Branch status is yellow - checking timeout`); - const lastCommitTime = await getBranchLastCommitTime(branchName); - const elapsedHours = getElapsedHours(lastCommitTime); + logger.debug(`Branch automerge is enabled`); if ( - !dependencyDashboardCheck && - ((config.stabilityStatus && config.stabilityStatus !== 'yellow') || - (is.number(config.prNotPendingHours) && - elapsedHours < config.prNotPendingHours)) + config.stabilityStatus !== 'yellow' && + (await getBranchStatus()) === 'yellow' && + is.number(config.prNotPendingHours) ) { + logger.debug('Checking how long this branch has been pending'); + const lastCommitTime = await getBranchLastCommitTime(branchName); + if (getElapsedHours(lastCommitTime) >= config.prNotPendingHours) { + logger.debug( + 'Branch exceeds prNotPending hours - forcing PR creation' + ); + config.forcePr = true; + } + } + if (config.forcePr || (await getBranchStatus()) === 'red') { + logger.debug(`Branch tests failed, so will create PR`); + } else { + // Branch should be automerged, so we don't want to create a PR + return { type: 'without-pr', prBlockedBy: 'BranchAutomerge' }; + } + } + if (!existingPr && config.prCreation === 'status-success') { + logger.debug('Checking branch combined status'); + if ((await getBranchStatus()) !== 'green') { + logger.debug(`Branch status isn't green - not creating PR`); + return { type: 'without-pr', prBlockedBy: 'AwaitingTests' }; + } + logger.debug('Branch status success'); + } else if ( + config.prCreation === 'approval' && + dependencyDashboardCheck !== 'approvePr' + ) { + return { type: 'without-pr', prBlockedBy: 'NeedsApproval' }; + } else if (config.prCreation === 'not-pending' && !config.forcePr) { + logger.debug('Checking branch combined status'); + if ((await getBranchStatus()) === 'yellow') { + logger.debug(`Branch status is yellow - checking timeout`); + const lastCommitTime = await getBranchLastCommitTime(branchName); + const elapsedHours = getElapsedHours(lastCommitTime); + if ( + !dependencyDashboardCheck && + ((config.stabilityStatus && config.stabilityStatus !== 'yellow') || + (is.number(config.prNotPendingHours) && + elapsedHours < config.prNotPendingHours)) + ) { + logger.debug( + `Branch is ${elapsedHours} hours old - skipping PR creation` + ); + return { + type: 'without-pr', + prBlockedBy: 'AwaitingTests', + }; + } + const prNotPendingHours = String(config.prNotPendingHours); logger.debug( - `Branch is ${elapsedHours} hours old - skipping PR creation` + `prNotPendingHours=${prNotPendingHours} threshold hit - creating PR` ); - return { - type: 'without-pr', - prBlockedBy: 'AwaitingTests', - }; } - const prNotPendingHours = String(config.prNotPendingHours); - logger.debug( - `prNotPendingHours=${prNotPendingHours} threshold hit - creating PR` - ); + logger.debug('Branch status success'); } - logger.debug('Branch status success'); } const processedUpgrades: string[] = [];