Skip to content

Commit 7ed3389

Browse files
yuichi10tkyi
authored andcommitted
feat(1082): Implement getBranchList function (#49)
1 parent 1a5a96b commit 7ed3389

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const MATCH_COMPONENT_HOSTNAME = 1;
1717
const MATCH_COMPONENT_USER = 2;
1818
const MATCH_COMPONENT_REPO = 3;
1919
const MATCH_COMPONENT_BRANCH = 4;
20+
const BRANCH_PAGE_SIZE = 100;
2021
const STATE_MAP = {
2122
SUCCESS: 'SUCCESSFUL',
2223
RUNNING: 'INPROGRESS',
@@ -816,6 +817,56 @@ class BitbucketScm extends Scm {
816817
Promise.resolve(false)
817818
));
818819
}
820+
821+
/**
822+
* Look up a branches from a repo
823+
* @async _findBranches
824+
* @param {Object} config
825+
* @param {String} config.repoId The bitbucket repo ID (e.g., "username/repoSlug")
826+
* @param {String} config.token Admin Oauth2 token for the repo
827+
* @param {Number} config.page pagination: page number to search next. 1-index.
828+
* @return {Promise} Resolves to a list of branches
829+
*/
830+
async _findBranches(config) {
831+
const response = await this.breaker.runCommand({
832+
json: true,
833+
method: 'GET',
834+
auth: {
835+
bearer: config.token
836+
},
837+
url: `${API_URL_V2}/repositories/${config.repoId}`
838+
+ `/refs/branches?pagelen=${BRANCH_PAGE_SIZE}&page=${config.page}`
839+
});
840+
841+
let branches = hoek.reach(response, 'body.values');
842+
843+
if (branches.length === BRANCH_PAGE_SIZE) {
844+
config.page += 1;
845+
const nextPageBranches = await this._findBranches(config);
846+
847+
branches = branches.concat(nextPageBranches);
848+
}
849+
850+
return branches.map(branch => ({ name: hoek.reach(branch, 'name') }));
851+
}
852+
853+
/**
854+
* Get branch list from the Github repository
855+
* @async _getBranchList
856+
* @param {Object} config
857+
* @param {String} config.scmUri The SCM URI to get branch list
858+
* @param {String} config.token Service token to authenticate with Github
859+
* @return {Promise} Resolves when complete
860+
*/
861+
async _getBranchList(config) {
862+
const repoInfo = getScmUriParts(config.scmUri);
863+
864+
return this._findBranches({
865+
repoId: repoInfo.repoId,
866+
page: 1,
867+
token: config.token
868+
});
869+
}
819870
}
820871

821872
module.exports = BitbucketScm;

test/index.test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,4 +1739,85 @@ describe('index', function () {
17391739
});
17401740
});
17411741
});
1742+
1743+
describe('getBranchList', () => {
1744+
const branchListConfig = {
1745+
scmUri: 'hostName:repoId:branchName',
1746+
token: 'oauthToken'
1747+
};
1748+
1749+
beforeEach(() => {
1750+
requestMock.yieldsAsync(null, {
1751+
statusCode: 200,
1752+
body: {
1753+
values: []
1754+
}
1755+
});
1756+
});
1757+
1758+
it('gets branches', (done) => {
1759+
requestMock.onFirstCall().yieldsAsync(null, {
1760+
body: {
1761+
values: [{ name: 'master' }],
1762+
size: 1
1763+
},
1764+
statusCode: 200
1765+
});
1766+
scm.getBranchList(branchListConfig).then((b) => {
1767+
assert.calledWith(requestMock, {
1768+
json: true,
1769+
method: 'GET',
1770+
auth: {
1771+
bearer: branchListConfig.token
1772+
},
1773+
url: `${API_URL_V2}/repositories/repoId/refs/branches?pagelen=100&page=1`
1774+
});
1775+
assert.deepEqual(b, [{ name: 'master' }]);
1776+
done();
1777+
}).catch(done);
1778+
});
1779+
1780+
it('gets a lot of branches', (done) => {
1781+
const fakeBranches = [];
1782+
1783+
for (let i = 0; i < 100; i += 1) {
1784+
fakeBranches.push({
1785+
name: `master${i}`
1786+
});
1787+
}
1788+
1789+
const fakeResponse = {
1790+
statusCode: 200,
1791+
body: {
1792+
values: fakeBranches
1793+
}
1794+
};
1795+
1796+
const fakeResponseEmpty = {
1797+
statusCode: 200,
1798+
body: {
1799+
values: []
1800+
}
1801+
};
1802+
1803+
requestMock.onCall(0).yieldsAsync(null, fakeResponse, fakeResponse.body);
1804+
requestMock.onCall(1).yieldsAsync(null, fakeResponse, fakeResponse.body);
1805+
requestMock.onCall(2).yieldsAsync(null, fakeResponse, fakeResponse.body);
1806+
requestMock.onCall(3).yieldsAsync(null, fakeResponseEmpty, fakeResponseEmpty.body);
1807+
scm.getBranchList(branchListConfig).then((branches) => {
1808+
assert.equal(branches.length, 300);
1809+
done();
1810+
}).catch(done);
1811+
});
1812+
1813+
it('throws an error when failing to getBranches', () => {
1814+
const testError = new Error('getBranchesError');
1815+
1816+
requestMock.yieldsAsync(testError);
1817+
1818+
return scm.getBranchList(branchListConfig).then(assert.fail, (err) => {
1819+
assert.equal(err, testError);
1820+
});
1821+
});
1822+
});
17421823
});

0 commit comments

Comments
 (0)