summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-06-20 20:55:50 -0400
committerFrederic Guillot <fred@kanboard.net>2015-06-20 20:55:50 -0400
commit98fd34bfe340fae6d0fd3b7333b6f9a6647cbae2 (patch)
treed22b7bff634a05e73f89d423938776df43554f52
parent7b947ebdbd3b6bcd9de10ea4255bfa11fc88695c (diff)
Improve automatic action to create comments based on commit messages
-rw-r--r--app/Action/ActionCommentCreationTest.php53
-rw-r--r--app/Action/CommentCreation.php13
-rw-r--r--app/Integration/BitbucketWebhook.php26
-rw-r--r--app/Integration/GithubWebhook.php17
-rw-r--r--app/Integration/GitlabWebhook.php25
-rw-r--r--app/Template/board/comments.php5
-rw-r--r--tests/units/BitbucketWebhookTest.php19
-rw-r--r--tests/units/GithubWebhookTest.php3
-rw-r--r--tests/units/GitlabWebhookTest.php19
9 files changed, 139 insertions, 41 deletions
diff --git a/app/Action/ActionCommentCreationTest.php b/app/Action/ActionCommentCreationTest.php
index 6531c5b3..a4ca284e 100644
--- a/app/Action/ActionCommentCreationTest.php
+++ b/app/Action/ActionCommentCreationTest.php
@@ -11,6 +11,59 @@ use Integration\GithubWebhook;
class ActionCommentCreationTest extends Base
{
+ public function testWithoutRequiredParams()
+ {
+ $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,
+ );
+
+ // Our event should be executed
+ $this->assertTrue($action->execute(new GenericEvent($event)));
+
+ $comment = $c->getById(1);
+ $this->assertEmpty($comment);
+ }
+
+ public function testWithCommitMessage()
+ {
+ $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,
+ 'commit_comment' => 'plop',
+ );
+
+ // 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('plop', $comment['comment']);
+ }
+
public function testWithUser()
{
$action = new Action\CommentCreation($this->container, 1, GithubWebhook::EVENT_ISSUE_COMMENT);
diff --git a/app/Action/CommentCreation.php b/app/Action/CommentCreation.php
index 44deb819..fb9f1172 100644
--- a/app/Action/CommentCreation.php
+++ b/app/Action/CommentCreation.php
@@ -2,7 +2,9 @@
namespace Action;
+use Integration\BitbucketWebhook;
use Integration\GithubWebhook;
+use Integration\GitlabWebhook;
/**
* Create automatically a comment from a webhook
@@ -22,6 +24,9 @@ class CommentCreation extends Base
{
return array(
GithubWebhook::EVENT_ISSUE_COMMENT,
+ GithubWebhook::EVENT_COMMIT,
+ BitbucketWebhook::EVENT_COMMIT,
+ GitlabWebhook::EVENT_COMMIT,
);
}
@@ -45,8 +50,6 @@ class CommentCreation extends Base
public function getEventRequiredParameters()
{
return array(
- 'comment',
- 'user_id',
'task_id',
);
}
@@ -62,9 +65,9 @@ class CommentCreation extends Base
{
return (bool) $this->comment->create(array(
'reference' => isset($data['reference']) ? $data['reference'] : '',
- 'comment' => $data['comment'],
+ 'comment' => empty($data['comment']) ? $data['commit_comment'] : $data['comment'],
'task_id' => $data['task_id'],
- 'user_id' => $data['user_id'],
+ 'user_id' => empty($data['user_id']) ? 0 : $data['user_id'],
));
}
@@ -77,6 +80,6 @@ class CommentCreation extends Base
*/
public function hasRequiredCondition(array $data)
{
- return true;
+ return ! empty($data['comment']) || ! empty($data['commit_comment']);
}
}
diff --git a/app/Integration/BitbucketWebhook.php b/app/Integration/BitbucketWebhook.php
index 75fc1c81..eced5596 100644
--- a/app/Integration/BitbucketWebhook.php
+++ b/app/Integration/BitbucketWebhook.php
@@ -2,7 +2,7 @@
namespace Integration;
-use Event\TaskEvent;
+use Event\GenericEvent;
use Model\Task;
/**
@@ -72,7 +72,7 @@ class BitbucketWebhook extends \Core\Base
{
$task_id = $this->task->getTaskIdFromText($commit['message']);
- if (! $task_id) {
+ if (empty($task_id)) {
return false;
}
@@ -82,16 +82,20 @@ class BitbucketWebhook extends \Core\Base
return false;
}
- if ($task['is_active'] == Task::STATUS_OPEN && $task['project_id'] == $this->project_id) {
-
- $this->container['dispatcher']->dispatch(
- self::EVENT_COMMIT,
- new TaskEvent(array('task_id' => $task_id) + $task)
- );
-
- return true;
+ if ($task['project_id'] != $this->project_id) {
+ return false;
}
- return false;
+ $this->container['dispatcher']->dispatch(
+ self::EVENT_COMMIT,
+ new GenericEvent(array(
+ 'task_id' => $task_id,
+ 'commit_message' => $commit['message'],
+ 'commit_url' => '',
+ 'commit_comment' => $commit['message']."\n\n".t('Commit made by @%s on Bitbucket', $commit['author'])
+ ) + $task)
+ );
+
+ return true;
}
}
diff --git a/app/Integration/GithubWebhook.php b/app/Integration/GithubWebhook.php
index 607bbc1e..a725f5b9 100644
--- a/app/Integration/GithubWebhook.php
+++ b/app/Integration/GithubWebhook.php
@@ -90,12 +90,19 @@ class GithubWebhook extends \Core\Base
continue;
}
- if ($task['project_id'] == $this->project_id) {
- $this->container['dispatcher']->dispatch(
- self::EVENT_COMMIT,
- new GenericEvent(array('task_id' => $task_id) + $task)
- );
+ if ($task['project_id'] != $this->project_id) {
+ continue;
}
+
+ $this->container['dispatcher']->dispatch(
+ self::EVENT_COMMIT,
+ new GenericEvent(array(
+ 'task_id' => $task_id,
+ 'commit_message' => $commit['message'],
+ 'commit_url' => $commit['url'],
+ 'commit_comment' => $commit['message']."\n\n[".t('Commit made by @%s on Github', $commit['author']['username']).']('.$commit['url'].')'
+ ) + $task)
+ );
}
return true;
diff --git a/app/Integration/GitlabWebhook.php b/app/Integration/GitlabWebhook.php
index 8a11f5c6..dce7413a 100644
--- a/app/Integration/GitlabWebhook.php
+++ b/app/Integration/GitlabWebhook.php
@@ -3,7 +3,6 @@
namespace Integration;
use Event\GenericEvent;
-use Event\TaskEvent;
use Model\Task;
/**
@@ -116,7 +115,7 @@ class GitlabWebhook extends \Core\Base
{
$task_id = $this->task->getTaskIdFromText($commit['message']);
- if (! $task_id) {
+ if (empty($task_id)) {
return false;
}
@@ -126,17 +125,21 @@ class GitlabWebhook extends \Core\Base
return false;
}
- if ($task['is_active'] == Task::STATUS_OPEN && $task['project_id'] == $this->project_id) {
-
- $this->container['dispatcher']->dispatch(
- self::EVENT_COMMIT,
- new TaskEvent(array('task_id' => $task_id) + $task)
- );
-
- return true;
+ if ($task['project_id'] != $this->project_id) {
+ return false;
}
- return false;
+ $this->container['dispatcher']->dispatch(
+ self::EVENT_COMMIT,
+ new GenericEvent(array(
+ 'task_id' => $task_id,
+ 'commit_message' => $commit['message'],
+ 'commit_url' => $commit['url'],
+ 'commit_comment' => $commit['message']."\n\n[".t('Commit made by @%s on Gitlab', $commit['author']['name']).']('.$commit['url'].')'
+ ) + $task)
+ );
+
+ return true;
}
/**
diff --git a/app/Template/board/comments.php b/app/Template/board/comments.php
index 75816af6..2e2c0c1e 100644
--- a/app/Template/board/comments.php
+++ b/app/Template/board/comments.php
@@ -1,7 +1,10 @@
<section>
<?php foreach ($comments as $comment): ?>
<p class="comment-title">
- <span class="comment-username"><?= $this->e($comment['name'] ?: $comment['username']) ?></span> @ <span class="comment-date"><?= dt('%b %e, %Y, %k:%M %p', $comment['date']) ?></span>
+ <?php if (! empty($comment['username'])): ?>
+ <span class="comment-username"><?= $this->e($comment['name'] ?: $comment['username']) ?></span> @
+ <?php endif ?>
+ <span class="comment-date"><?= dt('%b %e, %Y, %k:%M %p', $comment['date_creation']) ?></span>
</p>
<div class="comment-inner">
diff --git a/tests/units/BitbucketWebhookTest.php b/tests/units/BitbucketWebhookTest.php
index cb33b595..f1735d3d 100644
--- a/tests/units/BitbucketWebhookTest.php
+++ b/tests/units/BitbucketWebhookTest.php
@@ -21,7 +21,7 @@ class BitbucketWebhookTest extends Base
$this->assertEquals(1, $p->create(array('name' => 'test')));
$g->setProjectId(1);
- $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_COMMIT, function() {});
+ $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_COMMIT, array($this, 'onCommit'));
$event = json_decode($this->post_payload, true);
@@ -29,15 +29,15 @@ class BitbucketWebhookTest extends Base
$this->assertFalse($g->handleCommit($event['commits'][0]));
// Create task with the wrong id
- $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
+ $this->assertEquals(1, $tc->create(array('title' => 'test1', 'project_id' => 1)));
$this->assertFalse($g->handleCommit($event['commits'][1]));
// Create task with the right id
- $this->assertEquals(2, $tc->create(array('title' => 'test', 'project_id' => 1)));
+ $this->assertEquals(2, $tc->create(array('title' => 'test2', 'project_id' => 1)));
$this->assertTrue($g->handleCommit($event['commits'][1]));
$called = $this->container['dispatcher']->getCalledListeners();
- $this->assertArrayHasKey(BitbucketWebhook::EVENT_COMMIT.'.closure', $called);
+ $this->assertArrayHasKey(BitbucketWebhook::EVENT_COMMIT.'.BitbucketWebhookTest::onCommit', $called);
}
public function testParsePayload()
@@ -62,4 +62,15 @@ class BitbucketWebhookTest extends Base
$called = $this->container['dispatcher']->getCalledListeners();
$this->assertArrayHasKey(BitbucketWebhook::EVENT_COMMIT.'.closure', $called);
}
+
+ public function onCommit($event)
+ {
+ $data = $event->getAll();
+ $this->assertEquals(1, $data['project_id']);
+ $this->assertEquals(2, $data['task_id']);
+ $this->assertEquals('test2', $data['title']);
+ $this->assertEquals("Fix #2\n\n\nCommit made by @Frederic Guillot on Bitbucket", $data['commit_comment']);
+ $this->assertEquals("Fix #2\n", $data['commit_message']);
+ $this->assertEquals('', $data['commit_url']);
+ }
}
diff --git a/tests/units/GithubWebhookTest.php b/tests/units/GithubWebhookTest.php
index 65aa045f..8d9d7144 100644
--- a/tests/units/GithubWebhookTest.php
+++ b/tests/units/GithubWebhookTest.php
@@ -451,5 +451,8 @@ class GithubWebhookTest extends Base
$this->assertEquals(1, $data['project_id']);
$this->assertEquals(1, $data['task_id']);
$this->assertEquals('boo', $data['title']);
+ $this->assertEquals("Update README to fix #1\n\n[Commit made by @fguillot on Github](https://github.com/kanboardapp/webhook/commit/98dee3e49ee7aa66ffec1f761af93da5ffd711f6)", $data['commit_comment']);
+ $this->assertEquals('Update README to fix #1', $data['commit_message']);
+ $this->assertEquals('https://github.com/kanboardapp/webhook/commit/98dee3e49ee7aa66ffec1f761af93da5ffd711f6', $data['commit_url']);
}
}
diff --git a/tests/units/GitlabWebhookTest.php b/tests/units/GitlabWebhookTest.php
index cea4e839..b69f7431 100644
--- a/tests/units/GitlabWebhookTest.php
+++ b/tests/units/GitlabWebhookTest.php
@@ -33,7 +33,7 @@ class GitlabWebhookTest extends Base
$this->assertEquals(1, $p->create(array('name' => 'test')));
$g->setProjectId(1);
- $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_COMMIT, function() {});
+ $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_COMMIT, array($this, 'onCommit'));
$event = json_decode($this->push_payload, true);
@@ -41,15 +41,15 @@ class GitlabWebhookTest extends Base
$this->assertFalse($g->handleCommit($event['commits'][0]));
// Create task with the wrong id
- $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
+ $this->assertEquals(1, $tc->create(array('title' => 'test1', 'project_id' => 1)));
$this->assertFalse($g->handleCommit($event['commits'][0]));
// Create task with the right id
- $this->assertEquals(2, $tc->create(array('title' => 'test', 'project_id' => 1)));
+ $this->assertEquals(2, $tc->create(array('title' => 'test2', 'project_id' => 1)));
$this->assertTrue($g->handleCommit($event['commits'][0]));
$called = $this->container['dispatcher']->getCalledListeners();
- $this->assertArrayHasKey(GitlabWebhook::EVENT_COMMIT.'.closure', $called);
+ $this->assertArrayHasKey(GitlabWebhook::EVENT_COMMIT.'.GitlabWebhookTest::onCommit', $called);
}
public function testHandleIssueOpened()
@@ -116,4 +116,15 @@ class GitlabWebhookTest extends Base
$this->assertEquals(1, $data['task_id']);
$this->assertEquals(103361, $data['reference']);
}
+
+ public function onCommit($event)
+ {
+ $data = $event->getAll();
+ $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']);
+ }
}