Skip to content

Commit d274b79

Browse files
committed
feat: Add _getOpenedPRs for BitBucket
1 parent c584bd6 commit d274b79

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,35 @@ class BitbucketScm extends Scm {
645645
return Promise.resolve({ name: 'sd-checkout-code', command: command.join(' && ') });
646646
}
647647

648+
/**
649+
* Get list of objects (each consists of opened PR name and ref (branch)) of a pipeline
650+
* @method getOpenedPRs
651+
* @param {Object} config Configuration
652+
* @param {String} config.scmUri The scmUri to get opened PRs
653+
* @param {String} config.token The token used to authenticate to the SCM
654+
* @return {Promise}
655+
*/
656+
_getOpenedPRs(config) {
657+
const repoId = getScmUriParts(config.scmUri)[1];
658+
659+
return this.breaker.runCommand({
660+
json: true,
661+
login_type: 'oauth2',
662+
method: 'GET',
663+
oauth_access_token: config.token,
664+
url: `${API_URL_V2}/repositories/${repoId}/pullrequests`
665+
}).then((response) => {
666+
checkResponseError(response);
667+
668+
const prList = response.body.values;
669+
670+
return prList.map(pr => ({
671+
name: `PR-${pr.id}`,
672+
ref: pr.source.branch.name
673+
}));
674+
});
675+
}
676+
648677
/**
649678
* Retrieve stats for the scm
650679
* @method stats
@@ -653,6 +682,7 @@ class BitbucketScm extends Scm {
653682
stats() {
654683
return this.breaker.stats();
655684
}
685+
656686
}
657687

658688
module.exports = BitbucketScm;

test/index.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,4 +1458,37 @@ describe('index', function () {
14581458
});
14591459
});
14601460
});
1461+
1462+
describe('_getOpenedPRs', () => {
1463+
const oauthToken = 'oauthToken';
1464+
const scmUri = 'hostName:repoId:branchName';
1465+
1466+
it('returns response of expected format from Bitbucket', () => {
1467+
requestMock.yieldsAsync(null, {
1468+
body: {
1469+
values: [{
1470+
id: 1,
1471+
source: {
1472+
branch: {
1473+
name: 'testbranch'
1474+
}
1475+
}
1476+
}]
1477+
},
1478+
statusCode: 200
1479+
});
1480+
1481+
// eslint-disable-next-line no-underscore-dangle
1482+
return scm._getOpenedPRs({
1483+
scmUri,
1484+
token: oauthToken
1485+
})
1486+
.then(response =>
1487+
assert.deepEqual(response, [{
1488+
name: 'PR-1',
1489+
ref: 'testbranch'
1490+
}])
1491+
);
1492+
});
1493+
});
14611494
});

0 commit comments

Comments
 (0)