Skip to content

Commit c181fe0

Browse files
committed
feat: add _getPrInfo function
1 parent 2f2e193 commit c181fe0

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,14 @@ class BitbucketScm extends Scm {
447447
* @param {Object} config Configuration
448448
* @param {String} config.scmUri The scmUri
449449
* @param {String} config.token The token used to authenticate to the SCM
450+
* @param {Integer} [config.prNum] The PR number used to fetch the PR
450451
* @return {Promise} Resolves to the sha for the scmUri
451452
*/
452453
_getCommitSha(config) {
454+
if (config.prNum) {
455+
return this._getPrInfo(config).then(pr => pr.sha);
456+
}
457+
453458
const scm = getScmUriParts(config.scmUri);
454459
const branchUrl =
455460
`${REPO_URL}/${scm.repoId}/refs/branches/${scm.branch}`;
@@ -688,6 +693,38 @@ class BitbucketScm extends Scm {
688693
});
689694
}
690695

696+
/**
697+
* Resolve a pull request object based on the config
698+
* @method getPrRef
699+
* @param {Object} config Configuration
700+
* @param {String} config.scmUri The scmUri to get PR info of
701+
* @param {String} config.token The token used to authenticate to the SCM
702+
* @param {Integer} config.prNum The PR number used to fetch the PR
703+
* @return {Promise}
704+
*/
705+
_getPrInfo(config) {
706+
const repoId = getScmUriParts(config.scmUri).repoId;
707+
708+
return this.breaker.runCommand({
709+
url: `${API_URL_V2}/repositories/${repoId}/pullrequests/${config.prNum}`,
710+
method: 'GET',
711+
json: true,
712+
auth: {
713+
bearer: config.token
714+
}
715+
}).then((response) => {
716+
checkResponseError(response);
717+
718+
const pr = response.body;
719+
720+
return {
721+
name: `PR-${pr.id}`,
722+
ref: pr.source.branch.name,
723+
sha: pr.source.commit.hash
724+
};
725+
});
726+
}
727+
691728
/**
692729
* Retrieve stats for the scm
693730
* @method stats

test/index.test.js

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ describe('index', function () {
693693
requestMock.yieldsAsync(null, fakeResponse, fakeResponse.body);
694694
});
695695

696-
it('resolves to correct commit sha', () =>
696+
it('resolves to correct commit sha without prNum', () =>
697697
scm.getCommitSha({
698698
scmUri,
699699
token
@@ -703,6 +703,42 @@ describe('index', function () {
703703
})
704704
);
705705

706+
it('resolves to correct commit sha with prNum', () => {
707+
const prNum = 1;
708+
const prExpectedOptions = {
709+
url: `${API_URL_V2}/repositories/repoId/pullrequests/${prNum}`,
710+
method: 'GET',
711+
json: true,
712+
auth: {
713+
bearer: token
714+
}
715+
};
716+
717+
requestMock.yieldsAsync(null, {
718+
body: {
719+
id: 1,
720+
source: {
721+
branch: {
722+
name: 'testbranch'
723+
},
724+
commit: {
725+
hash: 'hashValue'
726+
}
727+
}
728+
},
729+
statusCode: 200
730+
});
731+
732+
return scm.getCommitSha({
733+
scmUri,
734+
token,
735+
prNum: 1
736+
}).then((sha) => {
737+
assert.calledWith(requestMock, prExpectedOptions);
738+
assert.deepEqual(sha, 'hashValue');
739+
});
740+
});
741+
706742
it('rejects if status code is not 200', () => {
707743
fakeResponse = {
708744
statusCode: 404,
@@ -1528,4 +1564,51 @@ describe('index', function () {
15281564
);
15291565
});
15301566
});
1567+
1568+
describe('_getPrInfo', () => {
1569+
const oauthToken = 'oauthToken';
1570+
const scmUri = 'hostName:repoId:branchName';
1571+
const prNum = 1;
1572+
const expectedOptions = {
1573+
url: `${API_URL_V2}/repositories/repoId/pullrequests/${prNum}`,
1574+
method: 'GET',
1575+
json: true,
1576+
auth: {
1577+
bearer: oauthToken
1578+
}
1579+
};
1580+
1581+
it('returns response of expected format from Bitbucket', () => {
1582+
requestMock.yieldsAsync(null, {
1583+
body: {
1584+
id: 1,
1585+
source: {
1586+
branch: {
1587+
name: 'testbranch'
1588+
},
1589+
commit: {
1590+
hash: 'hashValue'
1591+
}
1592+
}
1593+
},
1594+
statusCode: 200
1595+
});
1596+
1597+
// eslint-disable-next-line no-underscore-dangle
1598+
return scm._getPrInfo({
1599+
scmUri,
1600+
token: oauthToken,
1601+
prNum
1602+
})
1603+
.then((response) => {
1604+
assert.calledWith(requestMock, expectedOptions);
1605+
assert.deepEqual(response, {
1606+
name: 'PR-1',
1607+
ref: 'testbranch',
1608+
sha: 'hashValue'
1609+
});
1610+
}
1611+
);
1612+
});
1613+
});
15311614
});

0 commit comments

Comments
 (0)