Skip to content

Commit 548e460

Browse files
authored
feat(1415): Add custom actions to addWebhook (#71)
* feat(1415): Add custom actions to addWebhook * feat(1415): Add events mapping * feat(1415): Add action mapper function
1 parent 9c2b21c commit 548e460

File tree

2 files changed

+90
-14
lines changed

2 files changed

+90
-14
lines changed

index.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ class BitbucketScm extends Scm {
131131
this.expiresIn = 0;
132132
}
133133

134+
/**
135+
* Get the webhook events mapping of screwdriver events and scm events
136+
* @async _getWebhookEventsMapping
137+
* @return {Object} Returns a mapping of the events
138+
*/
139+
async _getWebhookEventsMapping() {
140+
return {
141+
'~pr': [
142+
'pullrequest:created',
143+
'pullrequest:fulfilled',
144+
'pullrequest:rejected',
145+
'pullrequest:updated'
146+
],
147+
'~commit': 'push'
148+
};
149+
}
150+
134151
/**
135152
* Look for a specific webhook that is attached to a repo.
136153
*
@@ -185,6 +202,7 @@ class BitbucketScm extends Scm {
185202
* @param {String} config.repoId Bitbucket repo ID (e.g., "username/repoSlug")
186203
* @param {String} config.token Admin Oauth2 token for the repo
187204
* @param {String} config.url url to create for webhook notifications
205+
* @param {String} config.actions Actions for the webhook events
188206
* @return {Promise} Resolves when complete
189207
*/
190208
_createWebhook(config) {
@@ -193,13 +211,13 @@ class BitbucketScm extends Scm {
193211
description: 'Screwdriver-CD build trigger',
194212
url: config.url,
195213
active: true,
196-
events: [
214+
events: config.actions.length === 0 ? [
197215
'repo:push',
198216
'pullrequest:created',
199217
'pullrequest:fulfilled',
200218
'pullrequest:rejected',
201219
'pullrequest:updated'
202-
]
220+
] : config.actions
203221
},
204222
json: true,
205223
method: 'POST',
@@ -225,10 +243,11 @@ class BitbucketScm extends Scm {
225243
* is instead updated.
226244
* @method _addWebhook
227245
* @param {Object} config
228-
* @param {String} config.scmUri The SCM URI to add the webhook to
229-
* @param {String} config.token Oauth2 token to authenticate with Bitbucket
230-
@param {String} config.webhookUrl The URL to use for the webhook notifications
231-
* @return {Promise} Resolves upon success
246+
* @param {String} config.scmUri The SCM URI to add the webhook to
247+
* @param {String} config.token Oauth2 token to authenticate with Bitbucket
248+
* @param {String} config.webhookUrl The URL to use for the webhook notifications
249+
* @param {String} config.actions Actions for the webhook events
250+
* @return {Promise} Resolves upon success
232251
*/
233252
_addWebhook(config) {
234253
const repoInfo = getScmUriParts(config.scmUri);
@@ -243,6 +262,7 @@ class BitbucketScm extends Scm {
243262
this._createWebhook({
244263
hookInfo,
245264
repoId: repoInfo.repoId,
265+
actions: config.actions,
246266
token: config.token,
247267
url: config.webhookUrl
248268
})

test/index.test.js

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,14 @@ describe('index', function () {
15581558
/* eslint-enable no-underscore-dangle */
15591559
scmUri,
15601560
token: oauthToken,
1561-
webhookUrl: 'url'
1561+
webhookUrl: 'url',
1562+
actions: [
1563+
'repo:push',
1564+
'pullrequest:created',
1565+
'pullrequest:fulfilled',
1566+
'pullrequest:rejected',
1567+
'pullrequest:updated'
1568+
]
15621569
})
15631570
.then(() => {
15641571
assert.calledWith(requestMock, {
@@ -1613,7 +1620,14 @@ describe('index', function () {
16131620
/* eslint-enable no-underscore-dangle */
16141621
scmUri,
16151622
token: oauthToken,
1616-
webhookUrl: 'url'
1623+
webhookUrl: 'url',
1624+
actions: [
1625+
'repo:push',
1626+
'pullrequest:created',
1627+
'pullrequest:fulfilled',
1628+
'pullrequest:rejected',
1629+
'pullrequest:updated'
1630+
]
16171631
}).then(() => {
16181632
assert.calledWith(requestMock, {
16191633
json: true,
@@ -1681,7 +1695,14 @@ describe('index', function () {
16811695
/* eslint-enable no-underscore-dangle */
16821696
scmUri,
16831697
token: oauthToken,
1684-
webhookUrl: 'url'
1698+
webhookUrl: 'url',
1699+
actions: [
1700+
'repo:push',
1701+
'pullrequest:created',
1702+
'pullrequest:fulfilled',
1703+
'pullrequest:rejected',
1704+
'pullrequest:updated'
1705+
]
16851706
}).then(() => {
16861707
assert.calledWith(requestMock, {
16871708
json: true,
@@ -1740,7 +1761,14 @@ describe('index', function () {
17401761
/* eslint-enable no-underscore-dangle */
17411762
scmUri,
17421763
token,
1743-
webhookUrl: 'url'
1764+
webhookUrl: 'url',
1765+
actions: [
1766+
'repo:push',
1767+
'pullrequest:created',
1768+
'pullrequest:fulfilled',
1769+
'pullrequest:rejected',
1770+
'pullrequest:updated'
1771+
]
17441772
}).then(assert.fail, (err) => {
17451773
assert.strictEqual(err.message, expectedMessage);
17461774
});
@@ -1760,7 +1788,14 @@ describe('index', function () {
17601788
/* eslint-enable no-underscore-dangle */
17611789
scmUri,
17621790
token,
1763-
webhookUrl: 'url'
1791+
webhookUrl: 'url',
1792+
actions: [
1793+
'repo:push',
1794+
'pullrequest:created',
1795+
'pullrequest:fulfilled',
1796+
'pullrequest:rejected',
1797+
'pullrequest:updated'
1798+
]
17641799
}).then(assert.fail, (err) => {
17651800
assert.strictEqual(err.message, expectedMessage);
17661801
});
@@ -1795,7 +1830,14 @@ describe('index', function () {
17951830
/* eslint-enable no-underscore-dangle */
17961831
scmUri,
17971832
token,
1798-
webhookUrl: 'url'
1833+
webhookUrl: 'url',
1834+
actions: [
1835+
'repo:push',
1836+
'pullrequest:created',
1837+
'pullrequest:fulfilled',
1838+
'pullrequest:rejected',
1839+
'pullrequest:updated'
1840+
]
17991841
}).then(assert.fail, (err) => {
18001842
assert.strictEqual(err.message, [
18011843
'Your credentials lack one or more required privilege scopes.',
@@ -1840,7 +1882,14 @@ describe('index', function () {
18401882
/* eslint-enable no-underscore-dangle */
18411883
scmUri,
18421884
token,
1843-
webhookUrl: 'url'
1885+
webhookUrl: 'url',
1886+
actions: [
1887+
'repo:push',
1888+
'pullrequest:created',
1889+
'pullrequest:fulfilled',
1890+
'pullrequest:rejected',
1891+
'pullrequest:updated'
1892+
]
18441893
}).then(assert.fail, (err) => {
18451894
assert.strictEqual(err.message, expectedMessage);
18461895
});
@@ -1870,7 +1919,14 @@ describe('index', function () {
18701919
/* eslint-enable no-underscore-dangle */
18711920
scmUri,
18721921
token,
1873-
webhookUrl: 'url'
1922+
webhookUrl: 'url',
1923+
actions: [
1924+
'repo:push',
1925+
'pullrequest:created',
1926+
'pullrequest:fulfilled',
1927+
'pullrequest:rejected',
1928+
'pullrequest:updated'
1929+
]
18741930
}).then(assert.fail, (err) => {
18751931
assert.strictEqual(err.message, expectedMessage);
18761932
});

0 commit comments

Comments
 (0)