Skip to content

Commit 51fcf74

Browse files
authored
fix(3225): organize the return values of _parseHook (#91)
1 parent 19b23cc commit 51fcf74

File tree

5 files changed

+395
-26
lines changed

5 files changed

+395
-26
lines changed

index.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -365,31 +365,41 @@ class BitbucketScm extends Scm {
365365
* @param {Object} payload The webhook payload received from the SCM service.
366366
* @return {Object} A key-map of data related to the received payload
367367
*/
368-
_parseHook(headers, payload) {
368+
async _parseHook(headers, payload) {
369369
const [typeHeader, actionHeader] = headers['x-event-key'].split(':');
370370
const parsed = {};
371371
const scmContexts = this._getScmContexts();
372372

373373
parsed.hookId = headers['x-request-uuid'];
374374
parsed.scmContext = scmContexts[0];
375375

376+
if (hoek.reach(payload, 'repository.links.html.href') === undefined) {
377+
throwError(`Invalid webhook payload`, 400);
378+
}
379+
380+
const link = Url.parse(hoek.reach(payload, 'repository.links.html.href'));
381+
const checkoutUrl = `${link.protocol}//${link.hostname}${link.pathname}.git`;
382+
383+
if (!`${link.hostname}${link.pathname}.git`.startsWith(this.hostname)) {
384+
throwError(`Incorrect checkout SshHost: ${checkoutUrl}`, 400);
385+
}
386+
376387
switch (typeHeader) {
377388
case 'repo': {
378389
if (actionHeader !== 'push') {
379-
return Promise.resolve(null);
390+
return null;
380391
}
381392
const changes = hoek.reach(payload, 'push.changes');
382-
const link = Url.parse(hoek.reach(payload, 'repository.links.html.href'));
383393

384394
parsed.type = 'repo';
385395
parsed.action = 'push';
386396
parsed.username = hoek.reach(payload, 'actor.uuid');
387-
parsed.checkoutUrl = `${link.protocol}//${link.hostname}${link.pathname}.git`;
397+
parsed.checkoutUrl = checkoutUrl;
388398
parsed.branch = hoek.reach(changes[0], 'new.name');
389399
parsed.sha = hoek.reach(changes[0], 'new.target.hash');
390400
parsed.lastCommitMessage = hoek.reach(changes[0], 'new.target.message', { default: '' });
391401

392-
return Promise.resolve(parsed);
402+
return parsed;
393403
}
394404
case 'pullrequest': {
395405
if (actionHeader === 'created') {
@@ -399,23 +409,21 @@ class BitbucketScm extends Scm {
399409
} else if (actionHeader === 'fullfilled' || actionHeader === 'rejected') {
400410
parsed.action = 'closed';
401411
} else {
402-
return Promise.resolve(null);
412+
return null;
403413
}
404414

405-
const link = Url.parse(hoek.reach(payload, 'repository.links.html.href'));
406-
407415
parsed.type = 'pr';
408416
parsed.username = hoek.reach(payload, 'actor.uuid');
409-
parsed.checkoutUrl = `${link.protocol}//${link.hostname}${link.pathname}.git`;
417+
parsed.checkoutUrl = checkoutUrl;
410418
parsed.branch = hoek.reach(payload, 'pullrequest.destination.branch.name');
411419
parsed.sha = hoek.reach(payload, 'pullrequest.source.commit.hash');
412420
parsed.prNum = hoek.reach(payload, 'pullrequest.id');
413421
parsed.prRef = hoek.reach(payload, 'pullrequest.source.branch.name');
414422

415-
return Promise.resolve(parsed);
423+
return parsed;
416424
}
417425
default:
418-
return Promise.resolve(null);
426+
return null;
419427
}
420428
}
421429

@@ -991,16 +999,12 @@ class BitbucketScm extends Scm {
991999
*/
9921000
_canHandleWebhook(headers, payload) {
9931001
return this._parseHook(headers, payload)
994-
.then(parseResult => {
995-
if (parseResult === null) {
996-
return Promise.resolve(false);
997-
}
998-
999-
const [, checkoutUrlHost] = parseResult.checkoutUrl.split('//');
1002+
.then(() => Promise.resolve(true))
1003+
.catch(err => {
1004+
logger.error('Failed to run canHandleWebhook', err);
10001005

1001-
return Promise.resolve(checkoutUrlHost.startsWith(this.hostname));
1002-
})
1003-
.catch(() => Promise.resolve(false));
1006+
return Promise.resolve(false);
1007+
});
10041008
}
10051009

10061010
/**

test/data/issue.create.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"issue": {
3+
"id": 1,
4+
"component": "component",
5+
"title": "Issue title",
6+
"content": {
7+
"raw": "Issue description",
8+
"html": "<p>Issue description</p>",
9+
"markup": "markdown"
10+
},
11+
"priority": "trivial|minor|major|critical|blocker",
12+
"state": "submitted|new|open|on hold|resolved|duplicate|invalid|wontfix|closed",
13+
"type": "bug|enhancement|proposal|task",
14+
"milestone": {
15+
"name": "milestone 1"
16+
},
17+
"version": {
18+
"name": "version 1"
19+
},
20+
"created_on": "2015-04-06T15:23:38.179678+00:00",
21+
"updated_on": "2015-04-06T15:23:38.179678+00:00",
22+
"links": {
23+
"self": {
24+
"href": "https://api.bitbucket.org/api/2.0/issues/issue_id"
25+
},
26+
"html": {
27+
"href": "https://api.bitbucket.org/issue_id"
28+
}
29+
}
30+
},
31+
"repository": {
32+
"scm": "git",
33+
"website": "",
34+
"name": "test",
35+
"links": {
36+
"self": {
37+
"href": "https://api.bitbucket.org/2.0/repositories/batman/test"
38+
},
39+
"html": {
40+
"href": "https://bitbucket.org/batman/test"
41+
},
42+
"avatar": {
43+
"href": "https://bitbucket.org/batman/test/avatar/32/"
44+
}
45+
},
46+
"full_name": "batman/test",
47+
"owner": {
48+
"username": "batman",
49+
"display_name": "Batman",
50+
"type": "user",
51+
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
52+
"links": {
53+
"self": {
54+
"href": "https://api.bitbucket.org/2.0/users/batman"
55+
},
56+
"html": {
57+
"href": "https://bitbucket.org/batman/"
58+
},
59+
"avatar": {
60+
"href": "https://bitbucket.org/account/batman/avatar/32/"
61+
}
62+
}
63+
},
64+
"type": "repository",
65+
"is_private": true,
66+
"uuid": "{de7d7695-1196-46a1-b87d-371b7b2945ab}"
67+
},
68+
"actor": {
69+
"username": "robin",
70+
"display_name": "Robin",
71+
"type": "user",
72+
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
73+
"links": {
74+
"self": {
75+
"href": "https://api.bitbucket.org/2.0/users/robin"
76+
},
77+
"html": {
78+
"href": "https://bitbucket.org/robin/"
79+
},
80+
"avatar": {
81+
"href": "https://bitbucket.org/account/robin/avatar/32/"
82+
}
83+
}
84+
}
85+
}

test/data/pr.commentCreate.json

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
{
2+
"pullrequest": {
3+
"type": "pullrequest",
4+
"description": "* stuff\r\n\r\n* testpayload\r\n\r\n* testing",
5+
"links": {
6+
"self": {
7+
"href": "https://api.bitbucket.org/2.0/repositories/batman/test/pullrequests/3"
8+
},
9+
"html": {
10+
"href": "https://bitbucket.org/batman/test/pull-requests/3"
11+
}
12+
},
13+
"title": "Mynewbranch",
14+
"close_source_branch": false,
15+
"reviewers": [],
16+
"destination": {
17+
"commit": {
18+
"hash": "4bcb8de69a58",
19+
"links": {
20+
"self": {
21+
"href": "https://api.bitbucket.org/2.0/repositories/batman/test/commit/4bcb8de69a58"
22+
}
23+
}
24+
},
25+
"branch": {
26+
"name": "master"
27+
},
28+
"repository": {
29+
"links": {
30+
"self": {
31+
"href": "https://api.bitbucket.org/2.0/repositories/batman/test"
32+
},
33+
"html": {
34+
"href": "https://bitbucket.org/batman/test"
35+
},
36+
"avatar": {
37+
"href": "https://bitbucket.org/batman/test/avatar/32/"
38+
}
39+
},
40+
"type": "repository",
41+
"uuid": "{de7d7695-1196-46a1-b87d-371b7b2945ab}",
42+
"full_name": "batman/test",
43+
"name": "test"
44+
}
45+
},
46+
"reason": "",
47+
"closed_by": null,
48+
"source": {
49+
"commit": {
50+
"hash": "40171b678527",
51+
"links": {
52+
"self": {
53+
"href": "https://api.bitbucket.org/2.0/repositories/batman/test/commit/40171b678527"
54+
}
55+
}
56+
},
57+
"branch": {
58+
"name": "mynewbranch"
59+
},
60+
"repository": {
61+
"links": {
62+
"self": {
63+
"href": "https://api.bitbucket.org/2.0/repositories/batman/test"
64+
},
65+
"html": {
66+
"href": "https://bitbucket.org/batman/test"
67+
},
68+
"avatar": {
69+
"href": "https://bitbucket.org/batman/test/avatar/32/"
70+
}
71+
},
72+
"type": "repository",
73+
"uuid": "{de7d7695-1196-46a1-b87d-371b7b2945ab}",
74+
"full_name": "batman/test",
75+
"name": "test"
76+
}
77+
},
78+
"comment_count": 0,
79+
"state": "OPEN",
80+
"author": {
81+
"username": "batman",
82+
"display_name": "Batman",
83+
"type": "user",
84+
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
85+
"links": {
86+
"self": {
87+
"href": "https://api.bitbucket.org/2.0/users/batman"
88+
},
89+
"html": {
90+
"href": "https://bitbucket.org/batman/"
91+
},
92+
"avatar": {
93+
"href": "https://bitbucket.org/account/batman/avatar/32/"
94+
}
95+
}
96+
},
97+
"created_on": "2016-10-11T00:05:17.748641+00:00",
98+
"participants": [],
99+
"updated_on": "2016-10-11T00:05:17.763900+00:00",
100+
"merge_commit": null,
101+
"id": 3,
102+
"task_count": 0
103+
},
104+
"actor": {
105+
"username": "robin",
106+
"display_name": "Robin",
107+
"type": "user",
108+
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
109+
"links": {
110+
"self": {
111+
"href": "https://api.bitbucket.org/2.0/users/robin"
112+
},
113+
"html": {
114+
"href": "https://bitbucket.org/robin/"
115+
},
116+
"avatar": {
117+
"href": "https://bitbucket.org/account/robin/avatar/32/"
118+
}
119+
}
120+
},
121+
"repository": {
122+
"scm": "git",
123+
"website": "",
124+
"uuid": "{de7d7695-1196-46a1-b87d-371b7b2945ab}",
125+
"links": {
126+
"self": {
127+
"href": "https://api.bitbucket.org/2.0/repositories/batman/test"
128+
},
129+
"html": {
130+
"href": "https://bitbucket.org/batman/test"
131+
},
132+
"avatar": {
133+
"href": "https://bitbucket.org/batman/test/avatar/32/"
134+
}
135+
},
136+
"full_name": "batman/test",
137+
"owner": {
138+
"username": "batman",
139+
"display_name": "Batman",
140+
"type": "user",
141+
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
142+
"links": {
143+
"self": {
144+
"href": "https://api.bitbucket.org/2.0/users/batman"
145+
},
146+
"html": {
147+
"href": "https://bitbucket.org/batman/"
148+
},
149+
"avatar": {
150+
"href": "https://bitbucket.org/account/batman/avatar/32/"
151+
}
152+
}
153+
},
154+
"type": "repository",
155+
"is_private": true,
156+
"name": "test"
157+
},
158+
"comment": {
159+
"id": 17,
160+
"parent": {
161+
"id": 16
162+
},
163+
"content": {
164+
"raw": "Comment text",
165+
"html": "<p>Comment text</p>",
166+
"markup": "markdown"
167+
},
168+
"inline": {
169+
"path": "path/to/file",
170+
"from": null,
171+
"to": 10
172+
},
173+
"created_on": "2015-04-06T16:52:29.982346+00:00",
174+
"updated_on": "2015-04-06T16:52:29.983730+00:00",
175+
"links": {
176+
"self": {
177+
"href": "https://api.bitbucket.org/api/2.0/comments/comment_id"
178+
},
179+
"html": {
180+
"href": "https://api.bitbucket.org/comment_id"
181+
}
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)