Skip to content

Commit f2ab9eb

Browse files
authored
feat(911): Add getChangedFiles method. BREAKING CHANGE: new scm base (#46)
* feat(911): Add getChangedFiles method * fix: Fix spacing
1 parent f8c1a05 commit f2ab9eb

File tree

3 files changed

+142
-118
lines changed

3 files changed

+142
-118
lines changed

index.js

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint no-underscore-dangle: ["error", { "allowAfterThis": true }] */
1+
/* eslint-disable no-underscore-dangle */
22

33
'use strict';
44

@@ -200,7 +200,7 @@ class BitbucketScm extends Scm {
200200
}
201201

202202
return this.breaker.runCommand(params)
203-
.then(checkResponseError);
203+
.then(checkResponseError);
204204
}
205205

206206
/**
@@ -224,14 +224,14 @@ class BitbucketScm extends Scm {
224224
token: config.token,
225225
url: config.url
226226
})
227-
.then(hookInfo =>
228-
this._createWebhook({
229-
hookInfo,
230-
repoId: repoInfo.repoId,
231-
token: config.token,
232-
url: config.url
233-
})
234-
);
227+
.then(hookInfo =>
228+
this._createWebhook({
229+
hookInfo,
230+
repoId: repoInfo.repoId,
231+
token: config.token,
232+
url: config.url
233+
})
234+
);
235235
}
236236

237237
/**
@@ -376,16 +376,16 @@ class BitbucketScm extends Scm {
376376
});
377377
}
378378

379-
/**
380-
* Decorate a given SCM URI with additional data to better display
381-
* related information. If a branch suffix is not provided, it will default
382-
* to the master branch
383-
* @method decorateUrl
384-
* @param {Config} config Configuration object
385-
* @param {String} config.scmUri The scmUri
386-
* @param {String} config.token Service token to authenticate with Bitbucket
387-
* @return {Object} Resolves to a decoratedUrl with url, name, and branch
388-
*/
379+
/**
380+
* Decorate a given SCM URI with additional data to better display
381+
* related information. If a branch suffix is not provided, it will default
382+
* to the master branch
383+
* @method decorateUrl
384+
* @param {Config} config Configuration object
385+
* @param {String} config.scmUri The scmUri
386+
* @param {String} config.token Service token to authenticate with Bitbucket
387+
* @return {Object} Resolves to a decoratedUrl with url, name, and branch
388+
*/
389389
_decorateUrl(config) {
390390
const scm = getScmUriParts(config.scmUri);
391391
const options = {
@@ -491,6 +491,21 @@ class BitbucketScm extends Scm {
491491
});
492492
}
493493

494+
/**
495+
* Bitbucket doesn't have an equivalent endpoint to get the changed files,
496+
* so returning null for now
497+
* @method getFile
498+
* @param {Object} config Configuration
499+
* @param {String} config.type Can be 'pr' or 'repo'
500+
* @param {Object} config.webhookPayload The webhook payload received from the
501+
* SCM service.
502+
* @param {String} config.token Service token to authenticate with Github
503+
* @return {Promise} Resolves to the content of the file
504+
*/
505+
_getChangedFiles() {
506+
return Promise.resolve(null);
507+
}
508+
494509
/**
495510
* Fetch content of a file from Bitbucket
496511
* @method getFile
@@ -621,11 +636,11 @@ class BitbucketScm extends Scm {
621636
});
622637
}
623638

624-
/**
625-
* Return a valid Bell configuration (for OAuth)
626-
* @method getBellConfiguration
627-
* @return {Promise}
628-
*/
639+
/**
640+
* Return a valid Bell configuration (for OAuth)
641+
* @method getBellConfiguration
642+
* @return {Promise}
643+
*/
629644
_getBellConfiguration() {
630645
const scmContexts = this._getScmContexts();
631646
const scmContext = scmContexts[0];
@@ -643,18 +658,18 @@ class BitbucketScm extends Scm {
643658
});
644659
}
645660

646-
/**
647-
* Checkout the source code from a repository; resolves as an object with checkout commands
648-
* @method getCheckoutCommand
649-
* @param {Object} config
650-
* @param {String} config.branch Pipeline branch
651-
* @param {String} config.host Scm host to checkout source code from
652-
* @param {String} config.org Scm org name
653-
* @param {String} config.repo Scm repo name
654-
* @param {String} config.sha Commit sha
655-
* @param {String} [config.prRef] PR reference (can be a PR branch or reference)
656-
* @return {Promise}
657-
*/
661+
/**
662+
* Checkout the source code from a repository; resolves as an object with checkout commands
663+
* @method getCheckoutCommand
664+
* @param {Object} config
665+
* @param {String} config.branch Pipeline branch
666+
* @param {String} config.host Scm host to checkout source code from
667+
* @param {String} config.org Scm org name
668+
* @param {String} config.repo Scm repo name
669+
* @param {String} config.sha Commit sha
670+
* @param {String} [config.prRef] PR reference (can be a PR branch or reference)
671+
* @return {Promise}
672+
*/
658673
_getCheckoutCommand(config) {
659674
const checkoutUrl = `${config.host}/${config.org}/${config.repo}`;
660675
const sshCheckoutUrl = `git@${config.host}:${config.org}/${config.repo}`;
@@ -695,13 +710,13 @@ class BitbucketScm extends Scm {
695710
}
696711

697712
/**
698-
* Get list of objects (each consists of opened PR name and ref (branch)) of a pipeline
699-
* @method getOpenedPRs
700-
* @param {Object} config Configuration
701-
* @param {String} config.scmUri The scmUri to get opened PRs
702-
* @param {String} config.token The token used to authenticate to the SCM
703-
* @return {Promise}
704-
*/
713+
* Get list of objects (each consists of opened PR name and ref (branch)) of a pipeline
714+
* @method getOpenedPRs
715+
* @param {Object} config Configuration
716+
* @param {String} config.scmUri The scmUri to get opened PRs
717+
* @param {String} config.token The token used to authenticate to the SCM
718+
* @return {Promise}
719+
*/
705720
_getOpenedPRs(config) {
706721
const repoId = getScmUriParts(config.scmUri).repoId;
707722

@@ -725,14 +740,14 @@ class BitbucketScm extends Scm {
725740
}
726741

727742
/**
728-
* Resolve a pull request object based on the config
729-
* @method getPrRef
730-
* @param {Object} config Configuration
731-
* @param {String} config.scmUri The scmUri to get PR info of
732-
* @param {String} config.token The token used to authenticate to the SCM
733-
* @param {Integer} config.prNum The PR number used to fetch the PR
734-
* @return {Promise}
735-
*/
743+
* Resolve a pull request object based on the config
744+
* @method getPrRef
745+
* @param {Object} config Configuration
746+
* @param {String} config.scmUri The scmUri to get PR info of
747+
* @param {String} config.token The token used to authenticate to the SCM
748+
* @param {Integer} config.prNum The PR number used to fetch the PR
749+
* @return {Promise}
750+
*/
736751
_getPrInfo(config) {
737752
const repoId = getScmUriParts(config.scmUri).repoId;
738753

@@ -769,24 +784,24 @@ class BitbucketScm extends Scm {
769784
return { [scmContext]: stats };
770785
}
771786

772-
/**
773-
* Get an array of scm context (e.g. bitbucket:bitbucket.org)
774-
* @method getScmContexts
775-
* @return {Array}
776-
*/
787+
/**
788+
* Get an array of scm context (e.g. bitbucket:bitbucket.org)
789+
* @method getScmContexts
790+
* @return {Array}
791+
*/
777792
_getScmContexts() {
778793
const contextName = [`bitbucket:${this.hostname}`];
779794

780795
return contextName;
781796
}
782797

783-
/**
784-
* Determine if a scm module can handle the received webhook
785-
* @method canHandleWebhook
786-
* @param {Object} headers The request headers associated with the webhook payload
787-
* @param {Object} payload The webhook payload received from the SCM service
788-
* @return {Promise}
789-
* */
798+
/**
799+
* Determine if a scm module can handle the received webhook
800+
* @method canHandleWebhook
801+
* @param {Object} headers The request headers associated with the webhook payload
802+
* @param {Object} payload The webhook payload received from the SCM service
803+
* @return {Promise}
804+
*/
790805
_canHandleWebhook(headers, payload) {
791806
return this._parseHook(headers, payload).then((parseResult) => {
792807
if (parseResult === null) {

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
},
3838
"devDependencies": {
3939
"chai": "^3.5.0",
40-
"eslint": "^3.2.2",
41-
"eslint-config-screwdriver": "^2.0.0",
40+
"eslint": "^4.6.0",
41+
"eslint-config-screwdriver": "^3.0.0",
4242
"jenkins-mocha": "^4.0.0",
4343
"mockery": "^2.0.0",
4444
"sinon": "^1.17.6",
@@ -49,7 +49,7 @@
4949
"hoek": "^5.0.3",
5050
"joi": "^13.0.0",
5151
"request": "^2.75.0",
52-
"screwdriver-data-schema": "^18.11.3",
53-
"screwdriver-scm-base": "^3.0.0"
52+
"screwdriver-data-schema": "^18.13.4",
53+
"screwdriver-scm-base": "^4.0.1"
5454
}
5555
}

test/index.test.js

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,21 @@ describe('index', function () {
228228
checkoutUrl: 'git@bitbucket.corp.jp:batman/test.git#master',
229229
token
230230
})
231-
.then(() => assert.fail('Should not get here'))
232-
.catch((error) => {
233-
assert.match(error.message, expectedError.message);
234-
});
231+
.then(() => assert.fail('Should not get here'))
232+
.catch((error) => {
233+
assert.match(error.message, expectedError.message);
234+
});
235+
});
236+
});
237+
238+
describe('getChangedFiles', () => {
239+
it('resolves null', () => {
240+
scm.getChangedFiles({
241+
type: 'pr',
242+
payload: testPayloadOpen,
243+
token: 'thisisatoken'
244+
})
245+
.then(result => assert.isNull(result));
235246
});
236247
});
237248

@@ -1072,7 +1083,7 @@ describe('index', function () {
10721083
description: 'Screwdriver/123/main'
10731084
},
10741085
auth: {
1075-
bearer: 'bearerToken' // Decoded access token
1086+
bearer: 'bearerToken' // Decoded access token
10761087
}
10771088
};
10781089
requestMock.yieldsAsync(null, fakeResponse);
@@ -1228,36 +1239,36 @@ describe('index', function () {
12281239
token: oauthToken,
12291240
url: 'url'
12301241
})
1231-
.then(() => {
1232-
assert.calledWith(requestMock, {
1233-
json: true,
1234-
method: 'GET',
1235-
auth: {
1236-
bearer: oauthToken
1237-
},
1238-
url: `${API_URL_V2}/repositories/repoId/hooks?pagelen=30&page=1`
1239-
});
1240-
assert.calledWith(requestMock, {
1241-
body: {
1242-
description: 'Screwdriver-CD build trigger',
1243-
url: 'url',
1244-
active: true,
1245-
events: [
1246-
'repo:push',
1247-
'pullrequest:created',
1248-
'pullrequest:fulfilled',
1249-
'pullrequest:rejected',
1250-
'pullrequest:updated'
1251-
]
1252-
},
1253-
json: true,
1254-
method: 'POST',
1255-
auth: {
1256-
bearer: oauthToken
1257-
},
1258-
url: `${API_URL_V2}/repositories/repoId/hooks`
1242+
.then(() => {
1243+
assert.calledWith(requestMock, {
1244+
json: true,
1245+
method: 'GET',
1246+
auth: {
1247+
bearer: oauthToken
1248+
},
1249+
url: `${API_URL_V2}/repositories/repoId/hooks?pagelen=30&page=1`
1250+
});
1251+
assert.calledWith(requestMock, {
1252+
body: {
1253+
description: 'Screwdriver-CD build trigger',
1254+
url: 'url',
1255+
active: true,
1256+
events: [
1257+
'repo:push',
1258+
'pullrequest:created',
1259+
'pullrequest:fulfilled',
1260+
'pullrequest:rejected',
1261+
'pullrequest:updated'
1262+
]
1263+
},
1264+
json: true,
1265+
method: 'POST',
1266+
auth: {
1267+
bearer: oauthToken
1268+
},
1269+
url: `${API_URL_V2}/repositories/repoId/hooks`
1270+
});
12591271
});
1260-
});
12611272
});
12621273

12631274
it('updates a pre-existing webhook', () => {
@@ -1577,14 +1588,13 @@ describe('index', function () {
15771588
scmUri,
15781589
token: oauthToken
15791590
})
1580-
.then((response) => {
1581-
assert.calledWith(requestMock, expectedOptions);
1582-
assert.deepEqual(response, [{
1583-
name: 'PR-1',
1584-
ref: 'testbranch'
1585-
}]);
1586-
}
1587-
);
1591+
.then((response) => {
1592+
assert.calledWith(requestMock, expectedOptions);
1593+
assert.deepEqual(response, [{
1594+
name: 'PR-1',
1595+
ref: 'testbranch'
1596+
}]);
1597+
});
15881598
});
15891599
});
15901600

@@ -1623,15 +1633,14 @@ describe('index', function () {
16231633
token: oauthToken,
16241634
prNum
16251635
})
1626-
.then((response) => {
1627-
assert.calledWith(requestMock, expectedOptions);
1628-
assert.deepEqual(response, {
1629-
name: 'PR-1',
1630-
ref: 'testbranch',
1631-
sha: 'hashValue'
1636+
.then((response) => {
1637+
assert.calledWith(requestMock, expectedOptions);
1638+
assert.deepEqual(response, {
1639+
name: 'PR-1',
1640+
ref: 'testbranch',
1641+
sha: 'hashValue'
1642+
});
16321643
});
1633-
}
1634-
);
16351644
});
16361645
});
16371646

0 commit comments

Comments
 (0)