diff options
-rw-r--r-- | app/Action/TaskOpen.php | 2 | ||||
-rw-r--r-- | app/Integration/GitlabWebhook.php | 33 | ||||
-rw-r--r-- | app/Model/Action.php | 1 | ||||
-rw-r--r-- | doc/gitlab-webhooks.markdown | 7 | ||||
-rw-r--r-- | tests/units/Integration/GitlabWebhookTest.php | 36 | ||||
-rw-r--r-- | tests/units/fixtures/gitlab_issue_reopened.json | 25 |
6 files changed, 103 insertions, 1 deletions
diff --git a/app/Action/TaskOpen.php b/app/Action/TaskOpen.php index 2e53efae..2e84c695 100644 --- a/app/Action/TaskOpen.php +++ b/app/Action/TaskOpen.php @@ -3,6 +3,7 @@ namespace Kanboard\Action; use Kanboard\Integration\GithubWebhook; +use Kanboard\Integration\GitlabWebhook; use Kanboard\Integration\BitbucketWebhook; /** @@ -23,6 +24,7 @@ class TaskOpen extends Base { return array( GithubWebhook::EVENT_ISSUE_REOPENED, + GitlabWebhook::EVENT_ISSUE_REOPENED, BitbucketWebhook::EVENT_ISSUE_REOPENED, ); } diff --git a/app/Integration/GitlabWebhook.php b/app/Integration/GitlabWebhook.php index b3f9b0b5..17b6da70 100644 --- a/app/Integration/GitlabWebhook.php +++ b/app/Integration/GitlabWebhook.php @@ -19,6 +19,7 @@ class GitlabWebhook extends \Kanboard\Core\Base */ const EVENT_ISSUE_OPENED = 'gitlab.webhook.issue.opened'; const EVENT_ISSUE_CLOSED = 'gitlab.webhook.issue.closed'; + const EVENT_ISSUE_REOPENED = 'gitlab.webhook.issue.reopened'; const EVENT_COMMIT = 'gitlab.webhook.commit'; const EVENT_ISSUE_COMMENT = 'gitlab.webhook.issue.commented'; @@ -164,6 +165,8 @@ class GitlabWebhook extends \Kanboard\Core\Base return $this->handleIssueOpened($payload['object_attributes']); case 'close': return $this->handleIssueClosed($payload['object_attributes']); + case 'reopen': + return $this->handleIssueReopened($payload['object_attributes']); } return false; @@ -194,6 +197,36 @@ class GitlabWebhook extends \Kanboard\Core\Base } /** + * Handle issue reopening + * + * @access public + * @param array $issue Issue data + * @return boolean + */ + public function handleIssueReopened(array $issue) + { + $task = $this->taskFinder->getByReference($this->project_id, $issue['id']); + + if (! empty($task)) { + $event = array( + 'project_id' => $this->project_id, + 'task_id' => $task['id'], + 'reference' => $issue['id'], + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_REOPENED, + new GenericEvent($event) + ); + + return true; + } + + return false; + } + + + /** * Handle issue closing * * @access public diff --git a/app/Model/Action.php b/app/Model/Action.php index dbf17e49..289471f4 100644 --- a/app/Model/Action.php +++ b/app/Model/Action.php @@ -118,6 +118,7 @@ class Action extends Base GithubWebhook::EVENT_ISSUE_COMMENT => t('Github issue comment created'), GitlabWebhook::EVENT_COMMIT => t('Gitlab commit received'), GitlabWebhook::EVENT_ISSUE_OPENED => t('Gitlab issue opened'), + GitlabWebhook::EVENT_ISSUE_REOPENED => t('Gitlab issue reopened'), GitlabWebhook::EVENT_ISSUE_CLOSED => t('Gitlab issue closed'), GitlabWebhook::EVENT_ISSUE_COMMENT => t('Gitlab issue comment created'), BitbucketWebhook::EVENT_COMMIT => t('Bitbucket commit received'), diff --git a/doc/gitlab-webhooks.markdown b/doc/gitlab-webhooks.markdown index 9d9ecaf5..fec47c19 100644 --- a/doc/gitlab-webhooks.markdown +++ b/doc/gitlab-webhooks.markdown @@ -56,10 +56,15 @@ 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** +### Reopen a Kanboard task when a closed issue is reopened on Gitlab + +- Choose the event: **Gitlab issue reopened** +- Choose the action: **Open a 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 Gitlab 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 +The user also have to be member of the project in Kanboard. diff --git a/tests/units/Integration/GitlabWebhookTest.php b/tests/units/Integration/GitlabWebhookTest.php index 6d37819b..fa05f292 100644 --- a/tests/units/Integration/GitlabWebhookTest.php +++ b/tests/units/Integration/GitlabWebhookTest.php @@ -64,6 +64,35 @@ class GitlabWebhookTest extends Base $this->assertArrayHasKey(GitlabWebhook::EVENT_ISSUE_OPENED.'.GitlabWebhookTest::onOpen', $called); } + public function testHandleIssueReopened() + { + $g = new GitlabWebhook($this->container); + $p = new Project($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $g->setProjectId(1); + + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_REOPENED, array($this, 'onReopen')); + + $event = json_decode(file_get_contents(__DIR__.'/../fixtures/gitlab_issue_reopened.json'), true); + + // Issue not there + $this->assertFalse($g->handleIssueReopened($event['object_attributes'])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertEmpty($called); + + $this->assertEquals(1, $tc->create(array('title' => 'A', 'project_id' => 1, 'reference' => 355691))); + $task = $tf->getByReference(1, 355691); + $this->assertTrue($g->handleIssueReopened($event['object_attributes'])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(GitlabWebhook::EVENT_ISSUE_REOPENED.'.GitlabWebhookTest::onReopen', $called); + } + + public function testHandleIssueClosed() { $g = new GitlabWebhook($this->container); @@ -170,6 +199,13 @@ class GitlabWebhookTest extends Base $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 onReopen($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(355691, $data['reference']); + } public function onClose($event) { $data = $event->getAll(); diff --git a/tests/units/fixtures/gitlab_issue_reopened.json b/tests/units/fixtures/gitlab_issue_reopened.json new file mode 100644 index 00000000..bf76262d --- /dev/null +++ b/tests/units/fixtures/gitlab_issue_reopened.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": "reopen" + } +} |