fix: abort renovation if repository has changed during run

If attempting to create a branch and it already exists, or attempting to update a branch and it no longer exists, then we abort.
This commit is contained in:
Rhys Arkins 2018-02-03 15:45:43 +01:00
parent d186633866
commit e306f707db
4 changed files with 32 additions and 10 deletions

View file

@ -1009,13 +1009,14 @@ async function createBranch(branchName, sha) {
config.branchList.push(branchName);
await get.post(`repos/${config.repository}/git/refs`, options);
} catch (err) /* istanbul ignore next */ {
if (err.statusCode === 422) {
logger.warn('Branch already existed? Attempting update instead');
await updateBranch(branchName, sha);
} else {
logger.debug('Unknown error creating branch');
throw err;
if (
err.statusCode === 422 &&
err.message.startsWith('Reference already exists')
) {
logger.info({ err }, 'Branch already exists - exiting');
throw new Error('repository-changed');
}
throw err;
}
}
@ -1032,10 +1033,21 @@ async function updateBranch(branchName, commit) {
if (config.forkToken) {
options.token = config.forkToken;
}
try {
await get.patch(
`repos/${config.repository}/git/refs/heads/${branchName}`,
options
);
} catch (err) /* istanbul ignore next */ {
if (
err.statusCode === 422 &&
err.message.startsWith('Reference does not exist')
) {
logger.info({ err }, 'Branch no longer exists - exiting');
throw new Error('repository-changed');
}
throw err;
}
}
// Low-level commit operations

View file

@ -158,6 +158,11 @@ async function processBranch(branchConfig) {
config.forcePr = true;
}
} catch (err) {
// istanbul ignore if
if (err.message === 'repository-changed') {
logger.debug('Passing repository-changed error up');
throw err;
}
logger.error({ err }, `Error updating branch: ${err.message}`);
// Don't throw here - we don't want to stop the other renovations
return 'error';

View file

@ -30,6 +30,10 @@ async function handleError(config, err) {
} else if (err.message === 'loops>5') {
logger.warn('Repository has looped 5 times already');
return err.message;
} else if (err.message === 'repository-changed') {
logger.warn('Repository has changed during renovation - aborting');
delete config.branchList; // eslint-disable-line no-param-reassign
return err.message;
} else if (err.message === 'config-validation') {
delete config.branchList; // eslint-disable-line no-param-reassign
logger.info({ error: err }, 'Repository has invalid config');

View file

@ -13,6 +13,7 @@ describe('workers/repository/error', () => {
const errors = [
'uninitiated',
'disabled',
'repository-changed',
'fork',
'no-package-files',
'loops>5',