diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/Action/ActionCommentCreationTest.php | 71 | ||||
-rw-r--r-- | app/Action/CommentCreation.php | 3 | ||||
-rw-r--r-- | app/Integration/GithubWebhook.php | 19 |
3 files changed, 84 insertions, 9 deletions
diff --git a/app/Action/ActionCommentCreationTest.php b/app/Action/ActionCommentCreationTest.php new file mode 100644 index 00000000..6531c5b3 --- /dev/null +++ b/app/Action/ActionCommentCreationTest.php @@ -0,0 +1,71 @@ +<?php + +require_once __DIR__.'/Base.php'; + +use Event\GenericEvent; +use Model\Task; +use Model\TaskCreation; +use Model\Comment; +use Model\Project; +use Integration\GithubWebhook; + +class ActionCommentCreationTest extends Base +{ + public function testWithUser() + { + $action = new Action\CommentCreation($this->container, 1, GithubWebhook::EVENT_ISSUE_COMMENT); + + // We create a task in the first column + $tc = new TaskCreation($this->container); + $p = new Project($this->container); + $c = new Comment($this->container); + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1))); + + // We create an event to move the task to the 2nd column + $event = array( + 'project_id' => 1, + 'task_id' => 1, + 'user_id' => 1, + 'comment' => 'youpi', + ); + + // Our event should be executed + $this->assertTrue($action->execute(new GenericEvent($event))); + + $comment = $c->getById(1); + $this->assertNotEmpty($comment); + $this->assertEquals(1, $comment['task_id']); + $this->assertEquals(1, $comment['user_id']); + $this->assertEquals('youpi', $comment['comment']); + } + + public function testWithNoUser() + { + $action = new Action\CommentCreation($this->container, 1, GithubWebhook::EVENT_ISSUE_COMMENT); + + // We create a task in the first column + $tc = new TaskCreation($this->container); + $p = new Project($this->container); + $c = new Comment($this->container); + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1))); + + // We create an event to move the task to the 2nd column + $event = array( + 'project_id' => 1, + 'task_id' => 1, + 'user_id' => 0, + 'comment' => 'youpi', + ); + + // Our event should be executed + $this->assertTrue($action->execute(new GenericEvent($event))); + + $comment = $c->getById(1); + $this->assertNotEmpty($comment); + $this->assertEquals(1, $comment['task_id']); + $this->assertEquals(0, $comment['user_id']); + $this->assertEquals('youpi', $comment['comment']); + } +} diff --git a/app/Action/CommentCreation.php b/app/Action/CommentCreation.php index 54d7be7d..44deb819 100644 --- a/app/Action/CommentCreation.php +++ b/app/Action/CommentCreation.php @@ -45,7 +45,6 @@ class CommentCreation extends Base public function getEventRequiredParameters() { return array( - 'reference', 'comment', 'user_id', 'task_id', @@ -62,7 +61,7 @@ class CommentCreation extends Base public function doAction(array $data) { return (bool) $this->comment->create(array( - 'reference' => $data['reference'], + 'reference' => isset($data['reference']) ? $data['reference'] : '', 'comment' => $data['comment'], 'task_id' => $data['task_id'], 'user_id' => $data['user_id'], diff --git a/app/Integration/GithubWebhook.php b/app/Integration/GithubWebhook.php index d95eba78..607bbc1e 100644 --- a/app/Integration/GithubWebhook.php +++ b/app/Integration/GithubWebhook.php @@ -80,7 +80,7 @@ class GithubWebhook extends \Core\Base $task_id = $this->task->getTaskIdFromText($commit['message']); - if (! $task_id) { + if (empty($task_id)) { continue; } @@ -90,7 +90,7 @@ class GithubWebhook extends \Core\Base continue; } - if ($task['is_active'] == Task::STATUS_OPEN && $task['project_id'] == $this->project_id) { + if ($task['project_id'] == $this->project_id) { $this->container['dispatcher']->dispatch( self::EVENT_COMMIT, new GenericEvent(array('task_id' => $task_id) + $task) @@ -140,15 +140,20 @@ class GithubWebhook extends \Core\Base public function parseCommentIssueEvent(array $payload) { $task = $this->taskFinder->getByReference($this->project_id, $payload['issue']['number']); - $user = $this->user->getByUsername($payload['comment']['user']['login']); - if (! empty($task) && ! empty($user)) { + if (! empty($task)) { + + $user = $this->user->getByUsername($payload['comment']['user']['login']); + + if (! empty($user) && ! $this->projectPermission->isMember($this->project_id, $user['id'])) { + $user = array(); + } $event = array( 'project_id' => $this->project_id, 'reference' => $payload['comment']['id'], - 'comment' => $payload['comment']['body'], - 'user_id' => $user['id'], + 'comment' => $payload['comment']['body']."\n\n[".t('By @%s on Github', $payload['comment']['user']['login']).']('.$payload['comment']['html_url'].')', + 'user_id' => ! empty($user) ? $user['id'] : 0, 'task_id' => $task['id'], ); @@ -257,7 +262,7 @@ class GithubWebhook extends \Core\Base $user = $this->user->getByUsername($issue['assignee']['login']); $task = $this->taskFinder->getByReference($this->project_id, $issue['number']); - if (! empty($user) && ! empty($task)) { + if (! empty($user) && ! empty($task) && $this->projectPermission->isMember($this->project_id, $user['id'])) { $event = array( 'project_id' => $this->project_id, |