Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(3225): organize the return values of _parseHook [3] #91

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,31 +365,41 @@ class BitbucketScm extends Scm {
* @param {Object} payload The webhook payload received from the SCM service.
* @return {Object} A key-map of data related to the received payload
*/
_parseHook(headers, payload) {
async _parseHook(headers, payload) {
const [typeHeader, actionHeader] = headers['x-event-key'].split(':');
const parsed = {};
const scmContexts = this._getScmContexts();

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

if (hoek.reach(payload, 'repository.links.html.href') === undefined) {
throwError(`Invalid webhook payload`, 400);
}

const link = Url.parse(hoek.reach(payload, 'repository.links.html.href'));
const checkoutUrl = `${link.protocol}//${link.hostname}${link.pathname}.git`;

if (!`${link.hostname}${link.pathname}.git`.startsWith(this.hostname)) {
throwError(`Incorrect checkout SshHost: ${checkoutUrl}`, 400);
}

switch (typeHeader) {
case 'repo': {
if (actionHeader !== 'push') {
return Promise.resolve(null);
return null;
}
const changes = hoek.reach(payload, 'push.changes');
const link = Url.parse(hoek.reach(payload, 'repository.links.html.href'));

parsed.type = 'repo';
parsed.action = 'push';
parsed.username = hoek.reach(payload, 'actor.uuid');
parsed.checkoutUrl = `${link.protocol}//${link.hostname}${link.pathname}.git`;
parsed.checkoutUrl = checkoutUrl;
parsed.branch = hoek.reach(changes[0], 'new.name');
parsed.sha = hoek.reach(changes[0], 'new.target.hash');
parsed.lastCommitMessage = hoek.reach(changes[0], 'new.target.message', { default: '' });

return Promise.resolve(parsed);
return parsed;
}
case 'pullrequest': {
if (actionHeader === 'created') {
Expand All @@ -399,23 +409,21 @@ class BitbucketScm extends Scm {
} else if (actionHeader === 'fullfilled' || actionHeader === 'rejected') {
parsed.action = 'closed';
} else {
return Promise.resolve(null);
return null;
}

const link = Url.parse(hoek.reach(payload, 'repository.links.html.href'));

parsed.type = 'pr';
parsed.username = hoek.reach(payload, 'actor.uuid');
parsed.checkoutUrl = `${link.protocol}//${link.hostname}${link.pathname}.git`;
parsed.checkoutUrl = checkoutUrl;
parsed.branch = hoek.reach(payload, 'pullrequest.destination.branch.name');
parsed.sha = hoek.reach(payload, 'pullrequest.source.commit.hash');
parsed.prNum = hoek.reach(payload, 'pullrequest.id');
parsed.prRef = hoek.reach(payload, 'pullrequest.source.branch.name');

return Promise.resolve(parsed);
return parsed;
}
default:
return Promise.resolve(null);
return null;
}
}

Expand Down Expand Up @@ -991,16 +999,12 @@ class BitbucketScm extends Scm {
*/
_canHandleWebhook(headers, payload) {
return this._parseHook(headers, payload)
.then(parseResult => {
if (parseResult === null) {
return Promise.resolve(false);
}

const [, checkoutUrlHost] = parseResult.checkoutUrl.split('//');
.then(() => Promise.resolve(true))
.catch(err => {
logger.error('Failed to run canHandleWebhook', err);

return Promise.resolve(checkoutUrlHost.startsWith(this.hostname));
})
.catch(() => Promise.resolve(false));
return Promise.resolve(false);
});
}

/**
Expand Down
85 changes: 85 additions & 0 deletions test/data/issue.create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"issue": {
"id": 1,
"component": "component",
"title": "Issue title",
"content": {
"raw": "Issue description",
"html": "<p>Issue description</p>",
"markup": "markdown"
},
"priority": "trivial|minor|major|critical|blocker",
"state": "submitted|new|open|on hold|resolved|duplicate|invalid|wontfix|closed",
"type": "bug|enhancement|proposal|task",
"milestone": {
"name": "milestone 1"
},
"version": {
"name": "version 1"
},
"created_on": "2015-04-06T15:23:38.179678+00:00",
"updated_on": "2015-04-06T15:23:38.179678+00:00",
"links": {
"self": {
"href": "https://api.bitbucket.org/api/2.0/issues/issue_id"
},
"html": {
"href": "https://api.bitbucket.org/issue_id"
}
}
},
"repository": {
"scm": "git",
"website": "",
"name": "test",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/batman/test"
},
"html": {
"href": "https://bitbucket.org/batman/test"
},
"avatar": {
"href": "https://bitbucket.org/batman/test/avatar/32/"
}
},
"full_name": "batman/test",
"owner": {
"username": "batman",
"display_name": "Batman",
"type": "user",
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/users/batman"
},
"html": {
"href": "https://bitbucket.org/batman/"
},
"avatar": {
"href": "https://bitbucket.org/account/batman/avatar/32/"
}
}
},
"type": "repository",
"is_private": true,
"uuid": "{de7d7695-1196-46a1-b87d-371b7b2945ab}"
},
"actor": {
"username": "robin",
"display_name": "Robin",
"type": "user",
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/users/robin"
},
"html": {
"href": "https://bitbucket.org/robin/"
},
"avatar": {
"href": "https://bitbucket.org/account/robin/avatar/32/"
}
}
}
}
184 changes: 184 additions & 0 deletions test/data/pr.commentCreate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
{
"pullrequest": {
"type": "pullrequest",
"description": "* stuff\r\n\r\n* testpayload\r\n\r\n* testing",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/batman/test/pullrequests/3"
},
"html": {
"href": "https://bitbucket.org/batman/test/pull-requests/3"
}
},
"title": "Mynewbranch",
"close_source_branch": false,
"reviewers": [],
"destination": {
"commit": {
"hash": "4bcb8de69a58",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/batman/test/commit/4bcb8de69a58"
}
}
},
"branch": {
"name": "master"
},
"repository": {
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/batman/test"
},
"html": {
"href": "https://bitbucket.org/batman/test"
},
"avatar": {
"href": "https://bitbucket.org/batman/test/avatar/32/"
}
},
"type": "repository",
"uuid": "{de7d7695-1196-46a1-b87d-371b7b2945ab}",
"full_name": "batman/test",
"name": "test"
}
},
"reason": "",
"closed_by": null,
"source": {
"commit": {
"hash": "40171b678527",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/batman/test/commit/40171b678527"
}
}
},
"branch": {
"name": "mynewbranch"
},
"repository": {
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/batman/test"
},
"html": {
"href": "https://bitbucket.org/batman/test"
},
"avatar": {
"href": "https://bitbucket.org/batman/test/avatar/32/"
}
},
"type": "repository",
"uuid": "{de7d7695-1196-46a1-b87d-371b7b2945ab}",
"full_name": "batman/test",
"name": "test"
}
},
"comment_count": 0,
"state": "OPEN",
"author": {
"username": "batman",
"display_name": "Batman",
"type": "user",
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/users/batman"
},
"html": {
"href": "https://bitbucket.org/batman/"
},
"avatar": {
"href": "https://bitbucket.org/account/batman/avatar/32/"
}
}
},
"created_on": "2016-10-11T00:05:17.748641+00:00",
"participants": [],
"updated_on": "2016-10-11T00:05:17.763900+00:00",
"merge_commit": null,
"id": 3,
"task_count": 0
},
"actor": {
"username": "robin",
"display_name": "Robin",
"type": "user",
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/users/robin"
},
"html": {
"href": "https://bitbucket.org/robin/"
},
"avatar": {
"href": "https://bitbucket.org/account/robin/avatar/32/"
}
}
},
"repository": {
"scm": "git",
"website": "",
"uuid": "{de7d7695-1196-46a1-b87d-371b7b2945ab}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/repositories/batman/test"
},
"html": {
"href": "https://bitbucket.org/batman/test"
},
"avatar": {
"href": "https://bitbucket.org/batman/test/avatar/32/"
}
},
"full_name": "batman/test",
"owner": {
"username": "batman",
"display_name": "Batman",
"type": "user",
"uuid": "{2dca4f54-ab3f-400c-a777-c059e1ac0394}",
"links": {
"self": {
"href": "https://api.bitbucket.org/2.0/users/batman"
},
"html": {
"href": "https://bitbucket.org/batman/"
},
"avatar": {
"href": "https://bitbucket.org/account/batman/avatar/32/"
}
}
},
"type": "repository",
"is_private": true,
"name": "test"
},
"comment": {
"id": 17,
"parent": {
"id": 16
},
"content": {
"raw": "Comment text",
"html": "<p>Comment text</p>",
"markup": "markdown"
},
"inline": {
"path": "path/to/file",
"from": null,
"to": 10
},
"created_on": "2015-04-06T16:52:29.982346+00:00",
"updated_on": "2015-04-06T16:52:29.983730+00:00",
"links": {
"self": {
"href": "https://api.bitbucket.org/api/2.0/comments/comment_id"
},
"html": {
"href": "https://api.bitbucket.org/comment_id"
}
}
}
}
Loading