refactor: use simpler api for branchExists (#1187)

simpler api + cacheable
This commit is contained in:
Rhys Arkins 2017-11-16 22:40:07 +01:00 committed by GitHub
parent 4c4d5cfdac
commit e27a1b486c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 260 deletions

View file

@ -205,34 +205,12 @@ async function getFileList(branchName = config.baseBranch) {
// Returns true if branch exists, otherwise false
async function branchExists(branchName) {
logger.debug(`Checking if branch exists: ${branchName}`);
try {
const res = await get(
`repos/${config.repoName}/git/refs/heads/${branchName}`
);
if (Array.isArray(res.body)) {
// This seems to happen if GitHub has partial matches, so we check ref
const matchedBranch = res.body.some(
branch => branch.ref === `refs/heads/${branchName}`
);
if (matchedBranch) {
logger.debug('Branch exists');
} else {
logger.debug('No matching branches');
}
return matchedBranch;
}
// This should happen if there's an exact match
return res.body.ref === `refs/heads/${branchName}`;
} catch (error) {
if (error.statusCode === 404) {
// If file not found, then return false
logger.debug("Branch doesn't exist");
return false;
}
// Propagate if it's any other error
throw error;
}
logger.debug(`branchExists(${branchName})`);
const branchList = (await get(
`repos/${config.repoName}/branches?per_page=100`,
{ paginate: true }
)).body.map(branch => branch.name);
return branchList.includes(branchName);
}
async function getAllRenovateBranches(branchPrefix) {

View file

@ -36,156 +36,6 @@ Array [
]
`;
exports[`platform/github branchExists(branchName) should propagate unknown errors 1`] = `
Array [
Array [
"repos/some/repo",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/pulls?per_page=100&state=all",
Object {
"paginate": true,
},
],
Array [
"repos/some/repo/git/trees/master?recursive=true",
],
Array [
"repos/some/repo/git/refs/heads/thebranchname",
],
]
`;
exports[`platform/github branchExists(branchName) should return false if a 404 is returned 1`] = `
Array [
Array [
"repos/some/repo",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/pulls?per_page=100&state=all",
Object {
"paginate": true,
},
],
Array [
"repos/some/repo/git/trees/master?recursive=true",
],
Array [
"repos/some/repo/git/refs/heads/thebranchname",
],
]
`;
exports[`platform/github branchExists(branchName) should return false if the branch does not exist (multiple results) 1`] = `
Array [
Array [
"repos/some/repo",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/pulls?per_page=100&state=all",
Object {
"paginate": true,
},
],
Array [
"repos/some/repo/git/trees/master?recursive=true",
],
Array [
"repos/some/repo/git/refs/heads/thebranchname",
],
]
`;
exports[`platform/github branchExists(branchName) should return false if the branch does not exist (one result) 1`] = `
Array [
Array [
"repos/some/repo",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/pulls?per_page=100&state=all",
Object {
"paginate": true,
},
],
Array [
"repos/some/repo/git/trees/master?recursive=true",
],
Array [
"repos/some/repo/git/refs/heads/thebranchname",
],
]
`;
exports[`platform/github branchExists(branchName) should return true if the branch exists (multiple results) 1`] = `
Array [
Array [
"repos/some/repo",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/pulls?per_page=100&state=all",
Object {
"paginate": true,
},
],
Array [
"repos/some/repo/git/trees/master?recursive=true",
],
Array [
"repos/some/repo/git/refs/heads/thebranchname",
],
]
`;
exports[`platform/github branchExists(branchName) should return true if the branch exists (one result) 1`] = `
Array [
Array [
"repos/some/repo",
Object {
"headers": Object {
"accept": "application/vnd.github.loki-preview+json",
},
},
],
Array [
"repos/some/repo/pulls?per_page=100&state=all",
Object {
"paginate": true,
},
],
Array [
"repos/some/repo/git/trees/master?recursive=true",
],
Array [
"repos/some/repo/git/refs/heads/thebranchname",
],
]
`;
exports[`platform/github commitFilesToBranch(branchName, files, message, parentBranch) should add a commit to a new branch if the branch does not already exist 1`] = `
Array [
Array [
@ -212,7 +62,10 @@ Array [
"repos/some/repo/git/commits/1111",
],
Array [
"repos/some/repo/git/refs/heads/package.json",
"repos/some/repo/branches?per_page=100",
Object {
"paginate": true,
},
],
]
`;
@ -260,7 +113,7 @@ Array [
"repos/some/repo/git/refs",
Object {
"body": Object {
"ref": "refs/heads/package.json",
"ref": "refs/heads/the-branch",
"sha": "5555",
},
},
@ -296,7 +149,10 @@ Array [
"repos/some/repo/git/commits/1111",
],
Array [
"repos/some/repo/git/refs/heads/package.json",
"repos/some/repo/branches?per_page=100",
Object {
"paginate": true,
},
],
]
`;
@ -346,7 +202,7 @@ Array [
exports[`platform/github commitFilesToBranch(branchName, files, message, parentBranch) should add a new commit to the branch 3`] = `
Array [
Array [
"repos/some/repo/git/refs/heads/package.json",
"repos/some/repo/git/refs/heads/the-branch",
Object {
"body": Object {
"force": true,

View file

@ -329,84 +329,17 @@ describe('platform/github', () => {
});
describe('branchExists(branchName)', () => {
it('should return true if the branch exists (one result)', async () => {
await initRepo('some/repo', 'token');
get.mockImplementationOnce(() => ({
body: {
ref: 'refs/heads/thebranchname',
},
}));
const exists = await github.branchExists('thebranchname');
expect(get.mock.calls).toMatchSnapshot();
expect(exists).toBe(true);
});
it('should return true if the branch exists (multiple results)', async () => {
await initRepo('some/repo', 'token');
get.mockImplementationOnce(() => ({
body: [
{
ref: 'refs/heads/notthebranchname',
},
{
ref: 'refs/heads/thebranchname',
name: 'thebranchname',
},
],
}));
const exists = await github.branchExists('thebranchname');
expect(get.mock.calls).toMatchSnapshot();
expect(exists).toBe(true);
});
it('should return false if the branch does not exist (one result)', async () => {
await initRepo('some/repo', 'token');
get.mockImplementationOnce(() => ({
body: {
ref: 'refs/heads/notthebranchname',
},
}));
const exists = await github.branchExists('thebranchname');
expect(get.mock.calls).toMatchSnapshot();
expect(exists).toBe(false);
});
it('should return false if the branch does not exist (multiple results)', async () => {
await initRepo('some/repo', 'token');
get.mockImplementationOnce(() => ({
body: [
{
ref: 'refs/heads/notthebranchname',
},
{
ref: 'refs/heads/alsonotthebranchname',
},
],
}));
const exists = await github.branchExists('thebranchname');
expect(get.mock.calls).toMatchSnapshot();
expect(exists).toBe(false);
});
it('should return false if a 404 is returned', async () => {
await initRepo('some/repo', 'token');
get.mockImplementationOnce(() =>
Promise.reject({
statusCode: 404,
})
);
const exists = await github.branchExists('thebranchname');
expect(get.mock.calls).toMatchSnapshot();
expect(exists).toBe(false);
});
it('should propagate unknown errors', async () => {
await initRepo('some/repo', 'token');
get.mockImplementationOnce(() =>
Promise.reject(new Error('Something went wrong'))
);
let err;
try {
await github.branchExists('thebranchname');
} catch (e) {
err = e;
}
expect(get.mock.calls).toMatchSnapshot();
expect(err.message).toBe('Something went wrong');
});
});
describe('getAllRenovateBranches()', () => {
it('should return all renovate branches', async () => {
@ -1313,9 +1246,14 @@ describe('platform/github', () => {
it('should add a new commit to the branch', async () => {
// branchExists
get.mockImplementationOnce(() => ({
body: {
ref: 'refs/heads/package.json',
body: [
{
name: 'master',
},
{
name: 'the-branch',
},
],
}));
const files = [
{
@ -1324,7 +1262,7 @@ describe('platform/github', () => {
},
];
await github.commitFilesToBranch(
'package.json',
'the-branch',
files,
'my commit message'
);
@ -1334,11 +1272,13 @@ describe('platform/github', () => {
});
it('should add a commit to a new branch if the branch does not already exist', async () => {
// branchExists
get.mockImplementationOnce(() =>
Promise.reject({
statusCode: 404,
})
);
get.mockImplementationOnce(() => ({
body: [
{
name: 'master',
},
],
}));
const files = [
{
name: 'package.json',
@ -1346,7 +1286,7 @@ describe('platform/github', () => {
},
];
await github.commitFilesToBranch(
'package.json',
'the-branch',
files,
'my other commit message'
);