Skip to content

Commit

Permalink
feat(1082): Implement getBranchList function (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichi10 authored and tkyi committed Jun 8, 2018
1 parent b2e55cd commit 3e0da4f
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 30 deletions.
53 changes: 53 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const MATCH_COMPONENT_REPO_NAME = 3;
const MATCH_COMPONENT_USER_NAME = 2;
const MATCH_COMPONENT_HOST_NAME = 1;
const WEBHOOK_PAGE_SIZE = 30;
const BRANCH_PAGE_SIZE = 100;
const STATE_MAP = {
SUCCESS: 'success',
RUNNING: 'pending',
Expand Down Expand Up @@ -915,6 +916,58 @@ class GithubScm extends Scm {
return false;
}
}

/**
* Look up a branches from a repo
* @async _findBranches
* @param {Object} config
* @param {Object} config.scmInfo Data about repo
* @param {String} config.token Admin token for repo
* @param {Number} config.page Pagination: page number to search next
* @return {Promise} Resolves to a list of branches
*/
async _findBranches(config) {
let branches = await this.breaker.runCommand({
action: 'getBranches',
token: config.token,
params: {
owner: config.scmInfo.owner,
repo: config.scmInfo.repo,
page: config.page,
per_page: BRANCH_PAGE_SIZE
}
});

if (branches.length === BRANCH_PAGE_SIZE) {
config.page += 1;
const nextPageBranches = await this._findBranches(config);

branches = branches.concat(nextPageBranches);
}

return branches.map(branch => ({ name: hoek.reach(branch, 'name') }));
}

/**
* Get branch list from the Github repository
* @async _getBranchList
* @param {Object} config
* @param {String} config.scmUri The SCM URI to get branch list
* @param {String} config.token Service token to authenticate with Github
* @return {Promise} Resolves when complete
*/
async _getBranchList(config) {
const scmInfo = await this.lookupScmUri({
scmUri: config.scmUri,
token: config.token
});

return this._findBranches({
scmInfo,
page: 1,
token: config.token
});
}
}

module.exports = GithubScm;
56 changes: 28 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"hoek": "^5.0.3",
"joi": "^13.3.0",
"screwdriver-data-schema": "^18.20.0",
"screwdriver-scm-base": "^4.1.0"
"screwdriver-scm-base": "^4.2.0"
},
"release": {
"debug": false,
Expand Down
77 changes: 76 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ describe('index', function () {
getById: sinon.stub(),
getCommit: sinon.stub(),
getContent: sinon.stub(),
getHooks: sinon.stub()
getHooks: sinon.stub(),
getBranches: sinon.stub()
},
users: {
getForUser: sinon.stub()
Expand Down Expand Up @@ -1788,4 +1789,78 @@ jobs:
});
});
});

describe('getBranchList', () => {
const branchListConfig = {
scmUri: 'github.com:1289:branchName',
token: 'fakeToken'
};

beforeEach(() => {
githubMock.repos.getById.yieldsAsync(null, {
full_name: 'dolores/violentdelights'
});
githubMock.repos.getBranches.yieldsAsync(null, [{
name: 'master',
commit: {
sha: '6dcb09b5b57875f334f61aebed695e2e4193db5e',
url: 'https://api.github.com/repos/octocat/Hello-World/commits/c5b97'
},
protected: true,
protection_url: 'https://api.github.com/protect'
}]);
});

it('gets branches', (done) => {
scm.getBranchList(branchListConfig).then((b) => {
assert.calledWith(githubMock.authenticate, sinon.match({
token: 'fakeToken'
}));
assert.calledWith(githubMock.repos.getBranches, {
owner: 'dolores',
repo: 'violentdelights',
page: 1,
per_page: 100
});
assert.deepEqual(b, [{ name: 'master' }]);
done();
}).catch(done);
});

it('gets a lot of branches', (done) => {
const fakeBranches = [];

for (let i = 0; i < 300; i += 1) {
const bInfo = {
name: `master${i}`,
commit: {
sha: '6dcb09b5b57875f334f61aebed695e2e4193db5e',
url: 'https://api.github.com/repos/octocat/Hello-World/commits/c5b97'
},
protected: true,
protection_url: 'https://api.github.com/protect'
};

fakeBranches.push(bInfo);
}
githubMock.repos.getBranches.onCall(0).yieldsAsync(null, fakeBranches.slice(0, 100));
githubMock.repos.getBranches.onCall(1).yieldsAsync(null, fakeBranches.slice(100, 200));
githubMock.repos.getBranches.onCall(2).yieldsAsync(null, fakeBranches.slice(200, 300));
githubMock.repos.getBranches.onCall(3).yieldsAsync(null, []);
scm.getBranchList(branchListConfig).then((branches) => {
assert.equal(branches.length, 300);
done();
}).catch(done);
});

it('throws an error when failing to getBranches', () => {
const testError = new Error('getBranchesError');

githubMock.repos.getBranches.yieldsAsync(testError);

return scm.getBranchList(branchListConfig).then(assert.fail, (err) => {
assert.equal(err, testError);
});
});
});
});

0 comments on commit 3e0da4f

Please sign in to comment.