From 31c57a82b9e53b9973471c4df4d3840620e67c5d Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Fri, 17 Jul 2015 19:12:08 -0400 Subject: Update Gitlab webhook --- app/Action/CommentCreation.php | 1 + app/Integration/GitlabWebhook.php | 67 ++++++++++-- app/Locale/da_DK/translations.php | 2 + app/Locale/de_DE/translations.php | 2 + app/Locale/es_ES/translations.php | 2 + app/Locale/fi_FI/translations.php | 2 + app/Locale/fr_FR/translations.php | 2 + app/Locale/hu_HU/translations.php | 2 + app/Locale/it_IT/translations.php | 2 + app/Locale/ja_JP/translations.php | 2 + app/Locale/nl_NL/translations.php | 2 + app/Locale/pl_PL/translations.php | 2 + app/Locale/pt_BR/translations.php | 2 + app/Locale/ru_RU/translations.php | 2 + app/Locale/sr_Latn_RS/translations.php | 2 + app/Locale/sv_SE/translations.php | 2 + app/Locale/th_TH/translations.php | 2 + app/Locale/tr_TR/translations.php | 2 + app/Locale/zh_CN/translations.php | 2 + app/Model/Action.php | 1 + docs/gitlab-webhooks.markdown | 10 ++ tests/units/GitlabWebhookTest.php | 131 +++++++++++++++++++---- tests/units/fixtures/gitlab_comment_created.json | 46 ++++++++ tests/units/fixtures/gitlab_issue_closed.json | 25 +++++ tests/units/fixtures/gitlab_issue_opened.json | 25 +++++ tests/units/fixtures/gitlab_push.json | 44 ++++++++ 26 files changed, 356 insertions(+), 28 deletions(-) create mode 100644 tests/units/fixtures/gitlab_comment_created.json create mode 100644 tests/units/fixtures/gitlab_issue_closed.json create mode 100644 tests/units/fixtures/gitlab_issue_opened.json create mode 100644 tests/units/fixtures/gitlab_push.json diff --git a/app/Action/CommentCreation.php b/app/Action/CommentCreation.php index 5dfd1c89..4029c875 100644 --- a/app/Action/CommentCreation.php +++ b/app/Action/CommentCreation.php @@ -28,6 +28,7 @@ class CommentCreation extends Base BitbucketWebhook::EVENT_ISSUE_COMMENT, BitbucketWebhook::EVENT_COMMIT, GitlabWebhook::EVENT_COMMIT, + GitlabWebhook::EVENT_ISSUE_COMMENT, ); } diff --git a/app/Integration/GitlabWebhook.php b/app/Integration/GitlabWebhook.php index dce7413a..b8925daf 100644 --- a/app/Integration/GitlabWebhook.php +++ b/app/Integration/GitlabWebhook.php @@ -21,14 +21,16 @@ class GitlabWebhook extends \Core\Base const EVENT_ISSUE_OPENED = 'gitlab.webhook.issue.opened'; const EVENT_ISSUE_CLOSED = 'gitlab.webhook.issue.closed'; const EVENT_COMMIT = 'gitlab.webhook.commit'; + const EVENT_ISSUE_COMMENT = 'gitlab.webhook.issue.commented'; /** * Supported webhook events * * @var string */ - const TYPE_PUSH = 'push'; - const TYPE_ISSUE = 'issue'; + const TYPE_PUSH = 'push'; + const TYPE_ISSUE = 'issue'; + const TYPE_COMMENT = 'comment'; /** * Project id @@ -63,6 +65,8 @@ class GitlabWebhook extends \Core\Base return $this->handlePushEvent($payload); case self::TYPE_ISSUE; return $this->handleIssueEvent($payload); + case self::TYPE_COMMENT; + return $this->handleCommentEvent($payload); } return false; @@ -77,15 +81,20 @@ class GitlabWebhook extends \Core\Base */ public function getType(array $payload) { - if (isset($payload['object_kind']) && $payload['object_kind'] === 'issue') { - return self::TYPE_ISSUE; + if (empty($payload['object_kind'])) { + return ''; } - if (isset($payload['commits'])) { - return self::TYPE_PUSH; + switch ($payload['object_kind']) { + case 'issue': + return self::TYPE_ISSUE; + case 'note': + return self::TYPE_COMMENT; + case 'push': + return self::TYPE_PUSH; + default: + return ''; } - - return ''; } /** @@ -213,4 +222,46 @@ class GitlabWebhook extends \Core\Base return false; } + + /** + * Parse comment issue events + * + * @access public + * @param array $payload Event data + * @return boolean + */ + public function handleCommentEvent(array $payload) + { + if (! isset($payload['issue'])) { + return false; + } + + $task = $this->taskFinder->getByReference($this->project_id, $payload['issue']['id']); + + if (! empty($task)) { + + $user = $this->user->getByUsername($payload['user']['username']); + + if (! empty($user) && ! $this->projectPermission->isMember($this->project_id, $user['id'])) { + $user = array(); + } + + $event = array( + 'project_id' => $this->project_id, + 'reference' => $payload['object_attributes']['id'], + 'comment' => $payload['object_attributes']['note']."\n\n[".t('By @%s on Gitlab', $payload['user']['username']).']('.$payload['object_attributes']['url'].')', + 'user_id' => ! empty($user) ? $user['id'] : 0, + 'task_id' => $task['id'], + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_COMMENT, + new GenericEvent($event) + ); + + return true; + } + + return false; + } } diff --git a/app/Locale/da_DK/translations.php b/app/Locale/da_DK/translations.php index a03dcf80..c40574d9 100644 --- a/app/Locale/da_DK/translations.php +++ b/app/Locale/da_DK/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/de_DE/translations.php b/app/Locale/de_DE/translations.php index d91b6cce..37f759b2 100644 --- a/app/Locale/de_DE/translations.php +++ b/app/Locale/de_DE/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/es_ES/translations.php b/app/Locale/es_ES/translations.php index c8a002b1..7d5a3c56 100644 --- a/app/Locale/es_ES/translations.php +++ b/app/Locale/es_ES/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/fi_FI/translations.php b/app/Locale/fi_FI/translations.php index 4cc28369..4093947f 100644 --- a/app/Locale/fi_FI/translations.php +++ b/app/Locale/fi_FI/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/fr_FR/translations.php b/app/Locale/fr_FR/translations.php index d06ee84c..558c4f4d 100644 --- a/app/Locale/fr_FR/translations.php +++ b/app/Locale/fr_FR/translations.php @@ -996,4 +996,6 @@ return array( 'Remote user' => 'Utilisateur distant', 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => 'Les utilisateurs distants ne stockent pas leur mot de passe dans la base de données de Kanboard, exemples : comptes LDAP, Github ou Google.', 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => 'Si vous cochez la case « Interdir le formulaire d\'authentification », les identifiants entrés dans le formulaire d\'authentification seront ignorés.', + 'By @%s on Gitlab' => 'Par @%s sur Gitlab', + 'Gitlab issue comment created' => 'Commentaire créé sur un ticket Gitlab', ); diff --git a/app/Locale/hu_HU/translations.php b/app/Locale/hu_HU/translations.php index f5c234f7..5d82f3e1 100644 --- a/app/Locale/hu_HU/translations.php +++ b/app/Locale/hu_HU/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/it_IT/translations.php b/app/Locale/it_IT/translations.php index 16970d30..5493d218 100644 --- a/app/Locale/it_IT/translations.php +++ b/app/Locale/it_IT/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/ja_JP/translations.php b/app/Locale/ja_JP/translations.php index 3656e432..3ced5ea1 100644 --- a/app/Locale/ja_JP/translations.php +++ b/app/Locale/ja_JP/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/nl_NL/translations.php b/app/Locale/nl_NL/translations.php index 59bba39a..688f988d 100644 --- a/app/Locale/nl_NL/translations.php +++ b/app/Locale/nl_NL/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/pl_PL/translations.php b/app/Locale/pl_PL/translations.php index 1e825837..c61ec77c 100644 --- a/app/Locale/pl_PL/translations.php +++ b/app/Locale/pl_PL/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/pt_BR/translations.php b/app/Locale/pt_BR/translations.php index 2979953f..b5f4d0e0 100644 --- a/app/Locale/pt_BR/translations.php +++ b/app/Locale/pt_BR/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/ru_RU/translations.php b/app/Locale/ru_RU/translations.php index fdeb5536..40e49952 100644 --- a/app/Locale/ru_RU/translations.php +++ b/app/Locale/ru_RU/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/sr_Latn_RS/translations.php b/app/Locale/sr_Latn_RS/translations.php index 2019beed..7a731c15 100644 --- a/app/Locale/sr_Latn_RS/translations.php +++ b/app/Locale/sr_Latn_RS/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/sv_SE/translations.php b/app/Locale/sv_SE/translations.php index 5c1319eb..73c94287 100644 --- a/app/Locale/sv_SE/translations.php +++ b/app/Locale/sv_SE/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/th_TH/translations.php b/app/Locale/th_TH/translations.php index 6755c772..ae81e16a 100644 --- a/app/Locale/th_TH/translations.php +++ b/app/Locale/th_TH/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/tr_TR/translations.php b/app/Locale/tr_TR/translations.php index 49e356cc..61210ddd 100644 --- a/app/Locale/tr_TR/translations.php +++ b/app/Locale/tr_TR/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Locale/zh_CN/translations.php b/app/Locale/zh_CN/translations.php index 97b15504..37bbda95 100644 --- a/app/Locale/zh_CN/translations.php +++ b/app/Locale/zh_CN/translations.php @@ -994,4 +994,6 @@ return array( // 'Remote user' => '', // 'Remote users do not store their password in Kanboard database, examples: LDAP, Google and Github accounts.' => '', // 'If you check the box "Disallow login form", credentials entered in the login form will be ignored.' => '', + // 'By @%s on Gitlab' => '', + // 'Gitlab issue comment created' => '', ); diff --git a/app/Model/Action.php b/app/Model/Action.php index d0607794..5e994c99 100644 --- a/app/Model/Action.php +++ b/app/Model/Action.php @@ -92,6 +92,7 @@ class Action extends Base GitlabWebhook::EVENT_COMMIT => t('Gitlab commit received'), GitlabWebhook::EVENT_ISSUE_OPENED => t('Gitlab issue opened'), GitlabWebhook::EVENT_ISSUE_CLOSED => t('Gitlab issue closed'), + GitlabWebhook::EVENT_ISSUE_COMMENT => t('Gitlab issue comment created'), BitbucketWebhook::EVENT_COMMIT => t('Bitbucket commit received'), BitbucketWebhook::EVENT_ISSUE_OPENED => t('Bitbucket issue opened'), BitbucketWebhook::EVENT_ISSUE_CLOSED => t('Bitbucket issue closed'), diff --git a/docs/gitlab-webhooks.markdown b/docs/gitlab-webhooks.markdown index 9ef73f97..bd1852ac 100644 --- a/docs/gitlab-webhooks.markdown +++ b/docs/gitlab-webhooks.markdown @@ -9,12 +9,14 @@ List of supported events - Gitlab commit received - Gitlab issue opened - Gitlab issue closed +- Gitlab issue comment created List of supported actions ------------------------- - Create a task from an external provider - Close a task +- Create a comment from an external provider Configuration ------------- @@ -53,3 +55,11 @@ When a task is created from a Gitlab issue, the link to the issue is added to th - Choose the event: **Gitlab issue closed** - Choose the action: **Close the task** + +### Create a comment on Kanboard when an issue is commented on Gitlab + +- Choose the event: **Gitlab issue comment created** +- Choose the action: **Create a comment from an external provider** + +If the username is the same between Github and Kanboard the comment author will be assigned, otherwise there is no author. +The user also have to be member of the project in Kanboard. \ No newline at end of file diff --git a/tests/units/GitlabWebhookTest.php b/tests/units/GitlabWebhookTest.php index b69f7431..a2dc0d3a 100644 --- a/tests/units/GitlabWebhookTest.php +++ b/tests/units/GitlabWebhookTest.php @@ -6,20 +6,18 @@ use Integration\GitlabWebhook; use Model\TaskCreation; use Model\TaskFinder; use Model\Project; +use Model\ProjectPermission; +use Model\User; class GitlabWebhookTest extends Base { - private $push_payload = '{"before":"9187f41ba34a2b40d41c50ed4b624ce374c5e583","after":"b3caaee62ad27dc31497946065ac18299784aee4","ref":"refs/heads/master","user_id":74067,"user_name":"Fred","project_id":124474,"repository":{"name":"kanboard","url":"git@gitlab.com:minicoders/kanboard.git","description":"Test repo","homepage":"https://gitlab.com/minicoders/kanboard"},"commits":[{"id":"b3caaee62ad27dc31497946065ac18299784aee4","message":"Fix bug #2\n","timestamp":"2014-12-28T20:31:48-05:00","url":"https://gitlab.com/minicoders/kanboard/commit/b3caaee62ad27dc31497946065ac18299784aee4","author":{"name":"Frédéric Guillot","email":"git@localhost"}}],"total_commits_count":1}'; - private $issue_open_payload = '{"object_kind":"issue","user":{"name":"Fred","username":"minicoders","avatar_url":"https://secure.gravatar.com/avatar/3c44936e5a56f80711bff14987d2733f?s=40\u0026d=identicon"},"object_attributes":{"id":103356,"title":"Test Webhook","assignee_id":null,"author_id":74067,"project_id":124474,"created_at":"2014-12-29 01:24:24 UTC","updated_at":"2014-12-29 01:24:24 UTC","position":0,"branch_name":null,"description":"- test1\r\n- test2","milestone_id":null,"state":"opened","iid":1,"url":"https://gitlab.com/minicoders/kanboard/issues/1","action":"open"}}'; - private $issue_closed_payload = '{"object_kind":"issue","user":{"name":"Fred","username":"minicoders","avatar_url":"https://secure.gravatar.com/avatar/3c44936e5a56f80711bff14987d2733f?s=40\u0026d=identicon"},"object_attributes":{"id":103361,"title":"uu","assignee_id":null,"author_id":74067,"project_id":124474,"created_at":"2014-12-29 01:28:44 UTC","updated_at":"2014-12-29 01:34:47 UTC","position":0,"branch_name":null,"description":"","milestone_id":null,"state":"closed","iid":4,"url":"https://gitlab.com/minicoders/kanboard/issues/4","action":"update"}}'; - public function testGetEventType() { $g = new GitlabWebhook($this->container); - $this->assertEquals(GitlabWebhook::TYPE_PUSH, $g->getType(json_decode($this->push_payload, true))); - $this->assertEquals(GitlabWebhook::TYPE_ISSUE, $g->getType(json_decode($this->issue_open_payload, true))); - $this->assertEquals(GitlabWebhook::TYPE_ISSUE, $g->getType(json_decode($this->issue_closed_payload, true))); + $this->assertEquals(GitlabWebhook::TYPE_PUSH, $g->getType(json_decode(file_get_contents(__DIR__.'/fixtures/gitlab_push.json'), true))); + $this->assertEquals(GitlabWebhook::TYPE_ISSUE, $g->getType(json_decode(file_get_contents(__DIR__.'/fixtures/gitlab_issue_opened.json'), true))); + $this->assertEquals(GitlabWebhook::TYPE_COMMENT, $g->getType(json_decode(file_get_contents(__DIR__.'/fixtures/gitlab_comment_created.json'), true))); $this->assertEquals('', $g->getType(array())); } @@ -35,7 +33,7 @@ class GitlabWebhookTest extends Base $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_COMMIT, array($this, 'onCommit')); - $event = json_decode($this->push_payload, true); + $event = json_decode(file_get_contents(__DIR__.'/fixtures/gitlab_push.json'), true); // No task $this->assertFalse($g->handleCommit($event['commits'][0])); @@ -59,7 +57,7 @@ class GitlabWebhookTest extends Base $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_OPENED, array($this, 'onOpen')); - $event = json_decode($this->issue_open_payload, true); + $event = json_decode(file_get_contents(__DIR__.'/fixtures/gitlab_issue_opened.json'), true); $this->assertTrue($g->handleIssueOpened($event['object_attributes'])); $called = $this->container['dispatcher']->getCalledListeners(); @@ -78,7 +76,7 @@ class GitlabWebhookTest extends Base $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_CLOSED, array($this, 'onClose')); - $event = json_decode($this->issue_closed_payload, true); + $event = json_decode(file_get_contents(__DIR__.'/fixtures/gitlab_issue_closed.json'), true); // Issue not there $this->assertFalse($g->handleIssueClosed($event['object_attributes'])); @@ -87,11 +85,11 @@ class GitlabWebhookTest extends Base $this->assertEmpty($called); // Create a task with the issue reference - $this->assertEquals(1, $tc->create(array('title' => 'A', 'project_id' => 1, 'reference' => 103361))); - $task = $tf->getByReference(1, 103361); + $this->assertEquals(1, $tc->create(array('title' => 'A', 'project_id' => 1, 'reference' => 355691))); + $task = $tf->getByReference(1, 355691); $this->assertNotEmpty($task); - $task = $tf->getByReference(2, 103361); + $task = $tf->getByReference(2, 355691); $this->assertEmpty($task); $this->assertTrue($g->handleIssueClosed($event['object_attributes'])); @@ -100,13 +98,76 @@ class GitlabWebhookTest extends Base $this->assertArrayHasKey(GitlabWebhook::EVENT_ISSUE_CLOSED.'.GitlabWebhookTest::onClose', $called); } + public function testCommentCreatedWithNoUser() + { + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNoUser')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 355691, 'project_id' => 1))); + + $g = new GitlabWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + json_decode(file_get_contents(__DIR__.'/fixtures/gitlab_comment_created.json'), true) + )); + } + + public function testCommentCreatedWithNotMember() + { + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNotMember')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 355691, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'minicoders'))); + + $g = new GitlabWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + json_decode(file_get_contents(__DIR__.'/fixtures/gitlab_comment_created.json'), true) + )); + } + + public function testCommentCreatedWithUser() + { + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithUser')); + + $p = new Project($this->container); + $this->assertEquals(1, $p->create(array('name' => 'foobar'))); + + $tc = new TaskCreation($this->container); + $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 355691, 'project_id' => 1))); + + $u = new User($this->container); + $this->assertEquals(2, $u->create(array('username' => 'minicoders'))); + + $pp = new ProjectPermission($this->container); + $this->assertTrue($pp->addMember(1, 2)); + + $g = new GitlabWebhook($this->container); + $g->setProjectId(1); + + $this->assertNotFalse($g->parsePayload( + json_decode(file_get_contents(__DIR__.'/fixtures/gitlab_comment_created.json'), true) + )); + } + public function onOpen($event) { $data = $event->getAll(); $this->assertEquals(1, $data['project_id']); - $this->assertEquals(103356, $data['reference']); - $this->assertEquals('Test Webhook', $data['title']); - $this->assertEquals("- test1\r\n- test2\n\n[Gitlab Issue](https://gitlab.com/minicoders/kanboard/issues/1)", $data['description']); + $this->assertEquals(355691, $data['reference']); + $this->assertEquals('Bug', $data['title']); + $this->assertEquals("There is a bug somewhere.\r\n\r\nBye\n\n[Gitlab Issue](https://gitlab.com/minicoders/test-webhook/issues/1)", $data['description']); } public function onClose($event) @@ -114,7 +175,7 @@ class GitlabWebhookTest extends Base $data = $event->getAll(); $this->assertEquals(1, $data['project_id']); $this->assertEquals(1, $data['task_id']); - $this->assertEquals(103361, $data['reference']); + $this->assertEquals(355691, $data['reference']); } public function onCommit($event) @@ -123,8 +184,38 @@ class GitlabWebhookTest extends Base $this->assertEquals(1, $data['project_id']); $this->assertEquals(2, $data['task_id']); $this->assertEquals('test2', $data['title']); - $this->assertEquals("Fix bug #2\n\n\n[Commit made by @Frédéric Guillot on Gitlab](https://gitlab.com/minicoders/kanboard/commit/b3caaee62ad27dc31497946065ac18299784aee4)", $data['commit_comment']); - $this->assertEquals("Fix bug #2\n", $data['commit_message']); - $this->assertEquals('https://gitlab.com/minicoders/kanboard/commit/b3caaee62ad27dc31497946065ac18299784aee4', $data['commit_url']); + $this->assertEquals("Fix bug #2\n\n[Commit made by @Fred on Gitlab](https://gitlab.com/minicoders/test-webhook/commit/48aafa75eef9ad253aa254b0c82c987a52ebea78)", $data['commit_comment']); + $this->assertEquals("Fix bug #2", $data['commit_message']); + $this->assertEquals('https://gitlab.com/minicoders/test-webhook/commit/48aafa75eef9ad253aa254b0c82c987a52ebea78', $data['commit_url']); + } + + public function onCommentCreatedWithNoUser($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(0, $data['user_id']); + $this->assertEquals(1642761, $data['reference']); + $this->assertEquals("Super comment!\n\n[By @minicoders on Gitlab](https://gitlab.com/minicoders/test-webhook/issues/1#note_1642761)", $data['comment']); + } + + public function onCommentCreatedWithNotMember($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(0, $data['user_id']); + $this->assertEquals(1642761, $data['reference']); + $this->assertEquals("Super comment!\n\n[By @minicoders on Gitlab](https://gitlab.com/minicoders/test-webhook/issues/1#note_1642761)", $data['comment']); + } + + public function onCommentCreatedWithUser($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(2, $data['user_id']); + $this->assertEquals(1642761, $data['reference']); + $this->assertEquals("Super comment!\n\n[By @minicoders on Gitlab](https://gitlab.com/minicoders/test-webhook/issues/1#note_1642761)", $data['comment']); } } diff --git a/tests/units/fixtures/gitlab_comment_created.json b/tests/units/fixtures/gitlab_comment_created.json new file mode 100644 index 00000000..b6599419 --- /dev/null +++ b/tests/units/fixtures/gitlab_comment_created.json @@ -0,0 +1,46 @@ +{ + "object_kind": "note", + "user": { + "name": "Fred", + "username": "minicoders", + "avatar_url": "https://secure.gravatar.com/avatar/3c44936e5a56f80711bff14987d2733f?s=40&d=identicon" + }, + "project_id": 320820, + "repository": { + "name": "test-webhook", + "url": "git@gitlab.com:minicoders/test-webhook.git", + "description": "", + "homepage": "https://gitlab.com/minicoders/test-webhook" + }, + "object_attributes": { + "id": 1642761, + "note": "Super comment!", + "noteable_type": "Issue", + "author_id": 74067, + "created_at": "2015-07-17 21:37:48 UTC", + "updated_at": "2015-07-17 21:37:48 UTC", + "project_id": 320820, + "attachment": null, + "line_code": null, + "commit_id": "", + "noteable_id": 355691, + "st_diff": null, + "system": false, + "url": "https://gitlab.com/minicoders/test-webhook/issues/1#note_1642761" + }, + "issue": { + "id": 355691, + "title": "Bug", + "assignee_id": null, + "author_id": 74067, + "project_id": 320820, + "created_at": "2015-07-17 21:31:47 UTC", + "updated_at": "2015-07-17 21:37:48 UTC", + "position": 0, + "branch_name": null, + "description": "There is a bug somewhere.\r\n\r\nBye", + "milestone_id": null, + "state": "opened", + "iid": 1 + } +} \ No newline at end of file diff --git a/tests/units/fixtures/gitlab_issue_closed.json b/tests/units/fixtures/gitlab_issue_closed.json new file mode 100644 index 00000000..82500b3c --- /dev/null +++ b/tests/units/fixtures/gitlab_issue_closed.json @@ -0,0 +1,25 @@ +{ + "object_kind": "issue", + "user": { + "name": "Fred", + "username": "minicoders", + "avatar_url": "https://secure.gravatar.com/avatar/3c44936e5a56f80711bff14987d2733f?s=40&d=identicon" + }, + "object_attributes": { + "id": 355691, + "title": "Bug", + "assignee_id": null, + "author_id": 74067, + "project_id": 320820, + "created_at": "2015-07-17 21:31:47 UTC", + "updated_at": "2015-07-17 22:10:17 UTC", + "position": 0, + "branch_name": null, + "description": "There is a bug somewhere.\r\n\r\nBye", + "milestone_id": null, + "state": "closed", + "iid": 1, + "url": "https://gitlab.com/minicoders/test-webhook/issues/1", + "action": "close" + } +} \ No newline at end of file diff --git a/tests/units/fixtures/gitlab_issue_opened.json b/tests/units/fixtures/gitlab_issue_opened.json new file mode 100644 index 00000000..3e75c138 --- /dev/null +++ b/tests/units/fixtures/gitlab_issue_opened.json @@ -0,0 +1,25 @@ +{ + "object_kind": "issue", + "user": { + "name": "Fred", + "username": "minicoders", + "avatar_url": "https://secure.gravatar.com/avatar/3c44936e5a56f80711bff14987d2733f?s=40&d=identicon" + }, + "object_attributes": { + "id": 355691, + "title": "Bug", + "assignee_id": null, + "author_id": 74067, + "project_id": 320820, + "created_at": "2015-07-17 21:31:47 UTC", + "updated_at": "2015-07-17 21:31:47 UTC", + "position": 0, + "branch_name": null, + "description": "There is a bug somewhere.\r\n\r\nBye", + "milestone_id": null, + "state": "opened", + "iid": 1, + "url": "https://gitlab.com/minicoders/test-webhook/issues/1", + "action": "open" + } +} \ No newline at end of file diff --git a/tests/units/fixtures/gitlab_push.json b/tests/units/fixtures/gitlab_push.json new file mode 100644 index 00000000..ed77f041 --- /dev/null +++ b/tests/units/fixtures/gitlab_push.json @@ -0,0 +1,44 @@ +{ + "object_kind": "push", + "before": "e4ec6156d208a45fc546fae73c28300b5af1692a", + "after": "48aafa75eef9ad253aa254b0c82c987a52ebea78", + "ref": "refs/heads/master", + "checkout_sha": "48aafa75eef9ad253aa254b0c82c987a52ebea78", + "message": null, + "user_id": 74067, + "user_name": "Fred", + "user_email": "f+gitlab@minicoders.com", + "project_id": 320820, + "repository": { + "name": "test-webhook", + "url": "git@gitlab.com:minicoders/test-webhook.git", + "description": "", + "homepage": "https://gitlab.com/minicoders/test-webhook", + "git_http_url": "https://gitlab.com/minicoders/test-webhook.git", + "git_ssh_url": "git@gitlab.com:minicoders/test-webhook.git", + "visibility_level": 0 + }, + "commits": [ + { + "id": "48aafa75eef9ad253aa254b0c82c987a52ebea78", + "message": "Fix bug #2", + "timestamp": "2015-06-21T00:41:41+00:00", + "url": "https://gitlab.com/minicoders/test-webhook/commit/48aafa75eef9ad253aa254b0c82c987a52ebea78", + "author": { + "name": "Fred", + "email": "me@localhost" + } + }, + { + "id": "e4ec6156d208a45fc546fae73c28300b5af1692a", + "message": "test", + "timestamp": "2015-06-21T00:35:55+00:00", + "url": "https://gitlab.com/localhost/test-webhook/commit/e4ec6156d208a45fc546fae73c28300b5af1692a", + "author": { + "name": "Fred", + "email": "me@localhost" + } + } + ], + "total_commits_count": 2 +} \ No newline at end of file -- cgit v1.2.3