mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-12 06:56:24 +00:00
refactor: use simpler api for branchExists (#1187)
simpler api + cacheable
This commit is contained in:
parent
4c4d5cfdac
commit
e27a1b486c
3 changed files with 34 additions and 260 deletions
|
@ -205,34 +205,12 @@ async function getFileList(branchName = config.baseBranch) {
|
||||||
|
|
||||||
// Returns true if branch exists, otherwise false
|
// Returns true if branch exists, otherwise false
|
||||||
async function branchExists(branchName) {
|
async function branchExists(branchName) {
|
||||||
logger.debug(`Checking if branch exists: ${branchName}`);
|
logger.debug(`branchExists(${branchName})`);
|
||||||
try {
|
const branchList = (await get(
|
||||||
const res = await get(
|
`repos/${config.repoName}/branches?per_page=100`,
|
||||||
`repos/${config.repoName}/git/refs/heads/${branchName}`
|
{ paginate: true }
|
||||||
);
|
)).body.map(branch => branch.name);
|
||||||
if (Array.isArray(res.body)) {
|
return branchList.includes(branchName);
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getAllRenovateBranches(branchPrefix) {
|
async function getAllRenovateBranches(branchPrefix) {
|
||||||
|
|
|
@ -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`] = `
|
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 [
|
||||||
Array [
|
Array [
|
||||||
|
@ -212,7 +62,10 @@ Array [
|
||||||
"repos/some/repo/git/commits/1111",
|
"repos/some/repo/git/commits/1111",
|
||||||
],
|
],
|
||||||
Array [
|
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",
|
"repos/some/repo/git/refs",
|
||||||
Object {
|
Object {
|
||||||
"body": Object {
|
"body": Object {
|
||||||
"ref": "refs/heads/package.json",
|
"ref": "refs/heads/the-branch",
|
||||||
"sha": "5555",
|
"sha": "5555",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -296,7 +149,10 @@ Array [
|
||||||
"repos/some/repo/git/commits/1111",
|
"repos/some/repo/git/commits/1111",
|
||||||
],
|
],
|
||||||
Array [
|
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`] = `
|
exports[`platform/github commitFilesToBranch(branchName, files, message, parentBranch) should add a new commit to the branch 3`] = `
|
||||||
Array [
|
Array [
|
||||||
Array [
|
Array [
|
||||||
"repos/some/repo/git/refs/heads/package.json",
|
"repos/some/repo/git/refs/heads/the-branch",
|
||||||
Object {
|
Object {
|
||||||
"body": Object {
|
"body": Object {
|
||||||
"force": true,
|
"force": true,
|
||||||
|
|
|
@ -329,84 +329,17 @@ describe('platform/github', () => {
|
||||||
});
|
});
|
||||||
describe('branchExists(branchName)', () => {
|
describe('branchExists(branchName)', () => {
|
||||||
it('should return true if the branch exists (one result)', async () => {
|
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');
|
await initRepo('some/repo', 'token');
|
||||||
get.mockImplementationOnce(() => ({
|
get.mockImplementationOnce(() => ({
|
||||||
body: [
|
body: [
|
||||||
{
|
{
|
||||||
ref: 'refs/heads/notthebranchname',
|
name: 'thebranchname',
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: 'refs/heads/thebranchname',
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}));
|
}));
|
||||||
const exists = await github.branchExists('thebranchname');
|
const exists = await github.branchExists('thebranchname');
|
||||||
expect(get.mock.calls).toMatchSnapshot();
|
|
||||||
expect(exists).toBe(true);
|
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()', () => {
|
describe('getAllRenovateBranches()', () => {
|
||||||
it('should return all renovate branches', async () => {
|
it('should return all renovate branches', async () => {
|
||||||
|
@ -1313,9 +1246,14 @@ describe('platform/github', () => {
|
||||||
it('should add a new commit to the branch', async () => {
|
it('should add a new commit to the branch', async () => {
|
||||||
// branchExists
|
// branchExists
|
||||||
get.mockImplementationOnce(() => ({
|
get.mockImplementationOnce(() => ({
|
||||||
body: {
|
body: [
|
||||||
ref: 'refs/heads/package.json',
|
{
|
||||||
},
|
name: 'master',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'the-branch',
|
||||||
|
},
|
||||||
|
],
|
||||||
}));
|
}));
|
||||||
const files = [
|
const files = [
|
||||||
{
|
{
|
||||||
|
@ -1324,7 +1262,7 @@ describe('platform/github', () => {
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
await github.commitFilesToBranch(
|
await github.commitFilesToBranch(
|
||||||
'package.json',
|
'the-branch',
|
||||||
files,
|
files,
|
||||||
'my commit message'
|
'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 () => {
|
it('should add a commit to a new branch if the branch does not already exist', async () => {
|
||||||
// branchExists
|
// branchExists
|
||||||
get.mockImplementationOnce(() =>
|
get.mockImplementationOnce(() => ({
|
||||||
Promise.reject({
|
body: [
|
||||||
statusCode: 404,
|
{
|
||||||
})
|
name: 'master',
|
||||||
);
|
},
|
||||||
|
],
|
||||||
|
}));
|
||||||
const files = [
|
const files = [
|
||||||
{
|
{
|
||||||
name: 'package.json',
|
name: 'package.json',
|
||||||
|
@ -1346,7 +1286,7 @@ describe('platform/github', () => {
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
await github.commitFilesToBranch(
|
await github.commitFilesToBranch(
|
||||||
'package.json',
|
'the-branch',
|
||||||
files,
|
files,
|
||||||
'my other commit message'
|
'my other commit message'
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue