Skip to content
Snippets Groups Projects
Commit 8e81a647 authored by Rhys Arkins's avatar Rhys Arkins
Browse files

refactor: lazy load branch status to reduce requests

parent 80463651
No related branches found
No related tags found
No related merge requests found
...@@ -19,16 +19,23 @@ async function ensurePr(prConfig) { ...@@ -19,16 +19,23 @@ async function ensurePr(prConfig) {
logger.debug('Found existing PR'); logger.debug('Found existing PR');
} }
config.upgrades = []; config.upgrades = [];
const branchStatus = await platform.getBranchStatus(
branchName,
config.requiredStatusChecks
);
if (config.lockFileErrors && config.lockFileErrors.length) { if (config.lockFileErrors && config.lockFileErrors.length) {
logger.debug('Forcing PR because of lock file errors'); logger.debug('Forcing PR because of lock file errors');
config.forcePr = true; config.forcePr = true;
} }
let branchStatus;
async function getBranchStatus() {
if (!branchStatus) {
branchStatus = await platform.getBranchStatus(
branchName,
config.requiredStatusChecks
);
}
return branchStatus;
}
// Only create a PR if a branch automerge has failed // Only create a PR if a branch automerge has failed
if ( if (
config.automerge === true && config.automerge === true &&
...@@ -36,9 +43,12 @@ async function ensurePr(prConfig) { ...@@ -36,9 +43,12 @@ async function ensurePr(prConfig) {
!config.forcePr !config.forcePr
) { ) {
logger.debug( logger.debug(
`Branch is configured for branch automerge, branchStatus is: ${branchStatus}` `Branch is configured for branch automerge, branch status) is: ${await getBranchStatus()}`
); );
if (branchStatus === 'pending' || branchStatus === 'running') { if (
(await getBranchStatus()) === 'pending' ||
(await getBranchStatus()) === 'running'
) {
logger.debug('Checking how long this branch has been pending'); logger.debug('Checking how long this branch has been pending');
const lastCommitTime = await platform.getBranchLastCommitTime(branchName); const lastCommitTime = await platform.getBranchLastCommitTime(branchName);
const currentTime = new Date(); const currentTime = new Date();
...@@ -51,7 +61,7 @@ async function ensurePr(prConfig) { ...@@ -51,7 +61,7 @@ async function ensurePr(prConfig) {
config.forcePr = true; config.forcePr = true;
} }
} }
if (config.forcePr || branchStatus === 'failure') { if (config.forcePr || (await getBranchStatus()) === 'failure') {
logger.debug(`Branch tests failed, so will create PR`); logger.debug(`Branch tests failed, so will create PR`);
} else { } else {
return null; return null;
...@@ -59,8 +69,10 @@ async function ensurePr(prConfig) { ...@@ -59,8 +69,10 @@ async function ensurePr(prConfig) {
} }
if (config.prCreation === 'status-success') { if (config.prCreation === 'status-success') {
logger.debug('Checking branch combined status'); logger.debug('Checking branch combined status');
if (branchStatus !== 'success') { if ((await getBranchStatus()) !== 'success') {
logger.debug(`Branch status is "${branchStatus}" - not creating PR`); logger.debug(
`Branch status is "${await getBranchStatus()}" - not creating PR`
);
return null; return null;
} }
logger.debug('Branch status success'); logger.debug('Branch status success');
...@@ -70,8 +82,13 @@ async function ensurePr(prConfig) { ...@@ -70,8 +82,13 @@ async function ensurePr(prConfig) {
!config.forcePr !config.forcePr
) { ) {
logger.debug('Checking branch combined status'); logger.debug('Checking branch combined status');
if (branchStatus === 'pending' || branchStatus === 'running') { if (
logger.debug(`Branch status is "${branchStatus}" - checking timeout`); (await getBranchStatus()) === 'pending' ||
(await getBranchStatus()) === 'running'
) {
logger.debug(
`Branch status is "${await getBranchStatus()}" - checking timeout`
);
const lastCommitTime = await platform.getBranchLastCommitTime(branchName); const lastCommitTime = await platform.getBranchLastCommitTime(branchName);
const currentTime = new Date(); const currentTime = new Date();
const millisecondsPerHour = 1000 * 60 * 60; const millisecondsPerHour = 1000 * 60 * 60;
...@@ -205,7 +222,7 @@ async function ensurePr(prConfig) { ...@@ -205,7 +222,7 @@ async function ensurePr(prConfig) {
try { try {
if (existingPr) { if (existingPr) {
logger.debug('Processing existing PR'); logger.debug('Processing existing PR');
if (config.automerge && branchStatus === 'failure') { if (config.automerge && (await getBranchStatus()) === 'failure') {
logger.debug(`Setting assignees and reviewers as status checks failed`); logger.debug(`Setting assignees and reviewers as status checks failed`);
await addAssigneesReviewers(config, existingPr); await addAssigneesReviewers(config, existingPr);
} }
...@@ -291,7 +308,7 @@ async function ensurePr(prConfig) { ...@@ -291,7 +308,7 @@ async function ensurePr(prConfig) {
platform.ensureComment(pr.number, subject, content); platform.ensureComment(pr.number, subject, content);
} }
// Skip assign and review if automerging PR // Skip assign and review if automerging PR
if (config.automerge && branchStatus !== 'failure') { if (config.automerge && (await getBranchStatus()) !== 'failure') {
logger.debug( logger.debug(
`Skipping assignees and reviewers as automerge=${config.automerge}` `Skipping assignees and reviewers as automerge=${config.automerge}`
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment