Finish github webhooks integration

This commit is contained in:
Frédéric Guillot
2014-10-25 18:13:13 -04:00
parent 7af67934cf
commit c4e6d658e4
23 changed files with 348 additions and 15 deletions

View File

@@ -13,6 +13,7 @@ use Core\Tool;
* @author Frederic Guillot
*
* @property \Model\Acl $acl
* @property \Model\Comment $comment
* @property \Model\Task $task
* @property \Model\TaskFinder $taskFinder
*/

View File

@@ -0,0 +1,83 @@
<?php
namespace Action;
use Model\GithubWebhook;
/**
* Create automatically a comment from a webhook
*
* @package action
* @author Frederic Guillot
*/
class CommentCreation extends Base
{
/**
* Get the list of compatible events
*
* @access public
* @return array
*/
public function getCompatibleEvents()
{
return array(
GithubWebhook::EVENT_ISSUE_COMMENT,
);
}
/**
* Get the required parameter for the action (defined by the user)
*
* @access public
* @return array
*/
public function getActionRequiredParameters()
{
return array();
}
/**
* Get the required parameter for the event
*
* @access public
* @return array
*/
public function getEventRequiredParameters()
{
return array(
'reference',
'comment',
'user_id',
'task_id',
);
}
/**
* Execute the action (create a new comment)
*
* @access public
* @param array $data Event data dictionary
* @return bool True if the action was executed or false when not executed
*/
public function doAction(array $data)
{
return $this->comment->create(array(
'reference' => $data['reference'],
'comment' => $data['comment'],
'task_id' => $data['task_id'],
'user_id' => $data['user_id'],
));
}
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return true;
}
}

View File

@@ -54,6 +54,7 @@ class Project extends Base
$this->response->html($this->projectLayout('project_show', array(
'project' => $project,
'stats' => $this->project->getStats($project['id']),
'webhook_token' => $this->config->get('webhook_token'),
'title' => $project['name'],
)));
}

View File

@@ -55,9 +55,11 @@ class Webhook extends Base
$this->githubWebhook->setProjectId($this->request->getIntegerParam('project_id'));
$this->githubWebhook->parsePayload(
$result = $this->githubWebhook->parsePayload(
$this->request->getHeader('X-Github-Event'),
$this->request->getBody()
);
echo $result ? 'PARSED' : 'IGNORED';
}
}

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
'Everybody have access to this project.' => 'Tout le monde a acccès à ce projet.',
'Webhooks' => 'Webhooks',
'API' => 'API',
'Integration' => 'Intégration',
'Github webhook' => 'Webhook Github',
'Help on Github webhook' => 'Aide sur les webhooks Github',
'Create a comment from an external provider' => 'Créer un commentaire depuis un fournisseur externe',
'Github issue comment created' => 'Commentaire créé sur un ticket Github',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
'Everybody have access to this project.' => 'ทุกคนสามารถเข้าถึงโปรเจคนี้',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -553,4 +553,9 @@ return array(
// 'Everybody have access to this project.' => '',
// 'Webhooks' => '',
// 'API' => '',
// 'Integration' => '',
// 'Github webhook' => '',
// 'Help on Github webhook' => '',
// 'Create a comment from an external provider' => '',
// 'Github issue comment created' => '',
);

View File

@@ -46,6 +46,7 @@ class Action extends Base
'TaskAssignColorUser' => t('Assign a color to a specific user'),
'TaskAssignColorCategory' => t('Assign automatically a color based on a category'),
'TaskAssignCategoryColor' => t('Assign automatically a category based on a color'),
'CommentCreation' => t('Create a comment from an external provider'),
'TaskCreation' => t('Create a task from an external provider'),
'TaskAssignUser' => t('Change the assignee based on an external username'),
'TaskAssignCategoryLabel' => t('Change the category based on an external label'),
@@ -78,6 +79,7 @@ class Action extends Base
GithubWebhook::EVENT_ISSUE_REOPENED => t('Github issue reopened'),
GithubWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE => t('Github issue assignee change'),
GithubWebhook::EVENT_ISSUE_LABEL_CHANGE => t('Github issue label change'),
GithubWebhook::EVENT_ISSUE_COMMENT => t('Github issue comment created'),
);
asort($values);

View File

@@ -48,6 +48,7 @@ class GithubWebhook extends Base
* @access public
* @param string $type Github event type
* @param string $payload Raw Github event (JSON)
* @return boolean
*/
public function parsePayload($type, $payload)
{
@@ -58,7 +59,11 @@ class GithubWebhook extends Base
return $this->parsePushEvent($payload);
case 'issues':
return $this->parseIssueEvent($payload);
case 'issue_comment':
return $this->parseCommentIssueEvent($payload);
}
return false;
}
/**
@@ -66,6 +71,7 @@ class GithubWebhook extends Base
*
* @access public
* @param array $payload Event data
* @return boolean
*/
public function parsePushEvent(array $payload)
{
@@ -87,6 +93,8 @@ class GithubWebhook extends Base
$this->event->trigger(self::EVENT_COMMIT, array('task_id' => $task_id) + $task);
}
}
return true;
}
/**
@@ -94,32 +102,57 @@ class GithubWebhook extends Base
*
* @access public
* @param array $payload Event data
* @return boolean
*/
public function parseIssueEvent(array $payload)
{
switch ($payload['action']) {
case 'opened':
$this->handleIssueOpened($payload['issue']);
break;
return $this->handleIssueOpened($payload['issue']);
case 'closed':
$this->handleIssueClosed($payload['issue']);
break;
return $this->handleIssueClosed($payload['issue']);
case 'reopened':
$this->handleIssueReopened($payload['issue']);
break;
return $this->handleIssueReopened($payload['issue']);
case 'assigned':
$this->handleIssueAssigned($payload['issue']);
break;
return $this->handleIssueAssigned($payload['issue']);
case 'unassigned':
$this->handleIssueUnassigned($payload['issue']);
break;
return $this->handleIssueUnassigned($payload['issue']);
case 'labeled':
$this->handleIssueLabeled($payload['issue'], $payload['label']);
break;
return $this->handleIssueLabeled($payload['issue'], $payload['label']);
case 'unlabeled':
$this->handleIssueUnlabeled($payload['issue'], $payload['label']);
break;
return $this->handleIssueUnlabeled($payload['issue'], $payload['label']);
}
return false;
}
/**
* Parse comment issue events
*
* @access public
* @param array $payload Event data
* @return boolean
*/
public function parseCommentIssueEvent(array $payload)
{
$task = $this->taskFinder->getByReference($payload['issue']['number']);
$user = $this->user->getByUsername($payload['comment']['user']['login']);
if ($task && $user) {
$event = array(
'project_id' => $this->project_id,
'reference' => $payload['comment']['id'],
'comment' => $payload['comment']['body'],
'user_id' => $user['id'],
'task_id' => $task['id'],
);
$this->event->trigger(self::EVENT_ISSUE_COMMENT, $event);
return true;
}
return false;
}
/**
@@ -127,6 +160,7 @@ class GithubWebhook extends Base
*
* @access public
* @param array $issue Issue data
* @return boolean
*/
public function handleIssueOpened(array $issue)
{
@@ -138,6 +172,7 @@ class GithubWebhook extends Base
);
$this->event->trigger(self::EVENT_ISSUE_OPENED, $event);
return true;
}
/**
@@ -145,6 +180,7 @@ class GithubWebhook extends Base
*
* @access public
* @param array $issue Issue data
* @return boolean
*/
public function handleIssueClosed(array $issue)
{
@@ -158,7 +194,10 @@ class GithubWebhook extends Base
);
$this->event->trigger(self::EVENT_ISSUE_CLOSED, $event);
return true;
}
return false;
}
/**
@@ -166,6 +205,7 @@ class GithubWebhook extends Base
*
* @access public
* @param array $issue Issue data
* @return boolean
*/
public function handleIssueReopened(array $issue)
{
@@ -179,7 +219,10 @@ class GithubWebhook extends Base
);
$this->event->trigger(self::EVENT_ISSUE_REOPENED, $event);
return true;
}
return false;
}
/**
@@ -187,6 +230,7 @@ class GithubWebhook extends Base
*
* @access public
* @param array $issue Issue data
* @return boolean
*/
public function handleIssueAssigned(array $issue)
{
@@ -203,7 +247,10 @@ class GithubWebhook extends Base
);
$this->event->trigger(self::EVENT_ISSUE_ASSIGNEE_CHANGE, $event);
return true;
}
return false;
}
/**
@@ -211,6 +258,7 @@ class GithubWebhook extends Base
*
* @access public
* @param array $issue Issue data
* @return boolean
*/
public function handleIssueUnassigned(array $issue)
{
@@ -226,7 +274,10 @@ class GithubWebhook extends Base
);
$this->event->trigger(self::EVENT_ISSUE_ASSIGNEE_CHANGE, $event);
return true;
}
return false;
}
/**
@@ -235,6 +286,7 @@ class GithubWebhook extends Base
* @access public
* @param array $issue Issue data
* @param array $label Label data
* @return boolean
*/
public function handleIssueLabeled(array $issue, array $label)
{
@@ -250,7 +302,10 @@ class GithubWebhook extends Base
);
$this->event->trigger(self::EVENT_ISSUE_LABEL_CHANGE, $event);
return true;
}
return false;
}
/**
@@ -259,6 +314,7 @@ class GithubWebhook extends Base
* @access public
* @param array $issue Issue data
* @param array $label Label data
* @return boolean
*/
public function handleIssueUnlabeled(array $issue, array $label)
{
@@ -275,6 +331,9 @@ class GithubWebhook extends Base
);
$this->event->trigger(self::EVENT_ISSUE_LABEL_CHANGE, $event);
return true;
}
return false;
}
}

View File

@@ -53,3 +53,14 @@
</tr>
<?php endforeach ?>
</table>
<?php if (Helper\is_admin()): ?>
<div class="page-header">
<h2><?= t('Integration') ?></h2>
</div>
<h3><i class="fa fa-github fa-fw"></i><?= t('Github webhook') ?></h3>
<input type="text" readonly="readonly" value="<?= Helper\get_current_base_url().Helper\u('webhook', 'github', array('token' => $webhook_token, 'project_id' => $project['id'])) ?>"/><br/>
<p class="form-help"><a href="http://kanboard.net/documentation/github-webhooks" target="_blank"><?= t('Help on Github webhook') ?></a></p>
<?php endif ?>