Skip to content

Commit

Permalink
fix: listBranches fails if repo has many branches (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Sep 8, 2020
1 parent 1a25661 commit eda2336
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 118 deletions.
24 changes: 13 additions & 11 deletions src/github-handler/branch-handler.ts
Expand Up @@ -63,17 +63,19 @@ export async function existsBranchWithName(
remote: RepoDomain,
name: string
): Promise<boolean> {
const branches = (
await octokit.repos.listBranches({
owner: remote.owner,
repo: remote.repo,
})
).data;
const match = branches.some(branch => branch.name === name);
logger.info(
`Existing remote branch ${name} found on ${remote.owner}/${remote.repo}`
);
return match;
try {
const data = (
await octokit.git.getRef({
owner: remote.owner,
repo: remote.repo,
ref: `heads/${name}`,
})
).data;
return data.ref ? true : false;
} catch (err) {
if (err.status === 404) return false;
else throw err;
}
}

/**
Expand Down
123 changes: 16 additions & 107 deletions test/branch.ts
Expand Up @@ -71,34 +71,6 @@ describe('Branch', () => {
url: 'http://fake-url.com',
data: branchResponseBody,
};

const listResponseBody = [
{
name: 'master',
commit: {
sha: 'c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc',
url:
'https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc',
},
protected: true,
protection: {
enabled: true,
required_status_checks: {
enforcement_level: 'non_admins',
contexts: ['ci-test', 'linter'],
},
},
protection_url:
'https://api.github.com/repos/octocat/hello-world/branches/master/protection',
},
];
const listBranchResponse = {
headers: {},
status: 200,
url: 'http://fake-url.com',
data: listResponseBody,
};

const createRefResponse = {
headers: {},
status: 200,
Expand All @@ -119,9 +91,11 @@ describe('Branch', () => {
const getBranchStub = sandbox
.stub(octokit.repos, 'getBranch')
.resolves(branchResponse);
const getRefError = Error('Not Found');
Object.assign(getRefError, {status: 404});
const listBranchStub = sandbox
.stub(octokit.repos, 'listBranches')
.resolves(listBranchResponse);
.stub(octokit.git, 'getRef')
.rejects(getRefError);
const createRefStub = sandbox
.stub(octokit.git, 'createRef')
.resolves(createRefResponse);
Expand All @@ -136,6 +110,7 @@ describe('Branch', () => {
sandbox.assert.calledOnceWithExactly(listBranchStub, {
owner: origin.owner,
repo: origin.repo,
ref: `heads/${branchName}`,
});
sandbox.assert.calledOnceWithExactly(createRefStub, {
owner: origin.owner,
Expand All @@ -156,58 +131,19 @@ describe('Branch', () => {
url: 'http://fake-url.com',
data: branchResponseBody,
};

const listResponseBody = [
{
name: 'master',
commit: {
sha: 'c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc',
url:
'https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc',
},
protected: true,
protection: {
enabled: true,
required_status_checks: {
enforcement_level: 'non_admins',
contexts: ['ci-test', 'linter'],
},
},
protection_url:
'https://api.github.com/repos/octocat/hello-world/branches/master/protection',
},
{
name: 'existing-branch',
commit: {
sha: 'c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc',
url:
'https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc',
},
protected: true,
protection: {
enabled: true,
required_status_checks: {
enforcement_level: 'non_admins',
contexts: ['ci-test', 'linter'],
},
},
protection_url:
'https://api.github.com/repos/octocat/hello-world/existing-branch/master/protection',
},
];
const listBranchResponse = {
const getRefResponseBody = await import('./fixtures/get-ref-response.json');
const getRefResponse = {
headers: {},
status: 200,
status: 404,
url: 'http://fake-url.com',
data: listResponseBody,
data: getRefResponseBody,
};

const getBranchStub = sandbox
.stub(octokit.repos, 'getBranch')
.resolves(branchResponse);
const listBranchStub = sandbox
.stub(octokit.repos, 'listBranches')
.resolves(listBranchResponse);
.stub(octokit.git, 'getRef')
.resolves(getRefResponse);
const createRefStub = sandbox.stub(octokit.git, 'createRef');
// tests
const sha = await branch(
Expand All @@ -226,6 +162,7 @@ describe('Branch', () => {
sandbox.assert.calledOnceWithExactly(listBranchStub, {
owner: origin.owner,
repo: origin.repo,
ref: 'heads/existing-branch',
});
sandbox.assert.notCalled(createRefStub);
});
Expand All @@ -250,9 +187,7 @@ describe('Branch', () => {
data: branchResponseBody,
};
sandbox.stub(octokit.repos, 'getBranch').resolves(branchResponse);
sandbox
.stub(octokit.repos, 'listBranches')
.rejects(Error(testErrorMessage));
sandbox.stub(octokit.git, 'getRef').rejects(Error(testErrorMessage));
try {
await branch(octokit, origin, upstream, branchName, 'master');
assert.fail();
Expand All @@ -270,36 +205,10 @@ describe('Branch', () => {
url: 'http://fake-url.com',
data: branchResponseBody,
};

const listResponseBody = [
{
name: 'master',
commit: {
sha: 'c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc',
url:
'https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc',
},
protected: true,
protection: {
enabled: true,
required_status_checks: {
enforcement_level: 'non_admins',
contexts: ['ci-test', 'linter'],
},
},
protection_url:
'https://api.github.com/repos/octocat/hello-world/branches/master/protection',
},
];
const listBranchResponse = {
headers: {},
status: 200,
url: 'http://fake-url.com',
data: listResponseBody,
};

sandbox.stub(octokit.repos, 'getBranch').resolves(branchResponse);
sandbox.stub(octokit.repos, 'listBranches').resolves(listBranchResponse);
const getRefError = Error('Not Found');
Object.assign(getRefError, {status: 404});
sandbox.stub(octokit.git, 'getRef').rejects(getRefError);
sandbox.stub(octokit.git, 'createRef').rejects(Error(testErrorMessage));
try {
await branch(octokit, origin, upstream, branchName, 'master');
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/get-ref-response.json
@@ -0,0 +1,10 @@
{
"ref": "refs/heads/release-v5.0.0",
"node_id": "abc123=",
"url": "https://api.github.com/repos/bcoe/test-release-please/git/refs/heads/release-v5.0.0",
"object": {
"sha": "7fe1012cf4a9f9ada0fde9e6a6111ae2bf390c31",
"type": "commit",
"url": "https://api.github.com/repos/bcoe/test-release-please/git/commits/7fe1012cf4a9f9ada0fde9e6a6111ae2bf390c31"
}
}

0 comments on commit eda2336

Please sign in to comment.