Skip to content

Commit d48092e

Browse files
committed
feat: implement getOpenedPrs
1 parent 6f79ed5 commit d48092e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,37 @@ class GithubScm extends Scm {
302302
});
303303
}
304304

305+
/**
306+
* Get a list of objects which consist of opened PR names and refs
307+
* @method _getOpenedPRs
308+
* @param {Object} config
309+
* @param {String} config.scmUri The scmUri to get opened PRs from
310+
* @param {String} config.token The token used to authenticate with the SCM
311+
* @return {Promise}
312+
*/
313+
_getOpenedPRs(config) {
314+
return this.lookupScmUri({
315+
scmUri: config.scmUri,
316+
token: config.token
317+
}).then(scmInfo =>
318+
this.breaker.runCommand({
319+
action: 'getAll',
320+
scopeType: 'pullRequests',
321+
token: config.token,
322+
params: {
323+
owner: scmInfo.owner,
324+
repo: scmInfo.repo,
325+
state: 'open'
326+
}
327+
})
328+
).then(pullRequests =>
329+
pullRequests.map(pullRequestInfo => ({
330+
name: `PR-${pullRequestInfo.number}`,
331+
ref: `pull/${pullRequestInfo.number}/merge`
332+
}))
333+
);
334+
}
335+
305336
/**
306337
* Get a owners permissions on a repository
307338
* @method _getPermissions

test/.eslintrc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ extends: screwdriver
22
rules:
33
func-names: off
44
prefer-arrow-callback: off
5+
no-underscore-dangle: off

test/index.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ describe('index', function () {
3636
beforeEach(() => {
3737
githubMock = {
3838
authenticate: sinon.stub(),
39+
pullRequests: {
40+
getAll: sinon.stub()
41+
},
3942
repos: {
4043
createHook: sinon.stub(),
4144
createStatus: sinon.stub(),
@@ -1319,4 +1322,67 @@ jobs:
13191322
});
13201323
});
13211324
});
1325+
1326+
describe('_getOpenedPRs', () => {
1327+
const scmUri = 'github.com:111:branchName';
1328+
const config = {
1329+
scmUri,
1330+
token: 'token'
1331+
};
1332+
1333+
beforeEach(() => {
1334+
githubMock.repos.getById.yieldsAsync(null, {
1335+
full_name: 'repoOwner/repoName'
1336+
});
1337+
});
1338+
1339+
it('returns a list of opened pull requests', () => {
1340+
githubMock.pullRequests.getAll.yieldsAsync(null, [
1341+
{ number: 1 },
1342+
{ number: 2 }
1343+
]);
1344+
1345+
return scm._getOpenedPRs(config).then((data) => {
1346+
assert.deepEqual(data, [
1347+
{
1348+
name: 'PR-1',
1349+
ref: 'pull/1/merge'
1350+
},
1351+
{
1352+
name: 'PR-2',
1353+
ref: 'pull/2/merge'
1354+
}
1355+
]);
1356+
1357+
assert.calledWith(githubMock.repos.getById, { id: '111' });
1358+
assert.calledWith(githubMock.pullRequests.getAll, {
1359+
owner: 'repoOwner',
1360+
repo: 'repoName',
1361+
state: 'open'
1362+
});
1363+
});
1364+
});
1365+
1366+
it('rejects when failing to lookup the SCM URI information', () => {
1367+
const testError = new Error('testError');
1368+
1369+
githubMock.repos.getById.yieldsAsync(testError);
1370+
1371+
return scm._getOpenedPRs(config).then(assert.fail, (err) => {
1372+
assert.instanceOf(err, Error);
1373+
assert.strictEqual(testError.message, err.message);
1374+
});
1375+
});
1376+
1377+
it('rejects when failing to fetch opened pull requests', () => {
1378+
const testError = new Error('testError');
1379+
1380+
githubMock.pullRequests.getAll.yieldsAsync(testError);
1381+
1382+
return scm._getOpenedPRs(config).then(assert.fail, (err) => {
1383+
assert.instanceOf(err, Error);
1384+
assert.strictEqual(testError.message, err.message);
1385+
});
1386+
});
1387+
});
13221388
});

0 commit comments

Comments
 (0)