2014-11-23 20:13:38 +01:00
|
|
|
<?php
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
namespace Integration;
|
|
|
|
|
|
|
|
use Event\GenericEvent;
|
|
|
|
use Model\Task;
|
2014-11-23 20:13:38 +01:00
|
|
|
|
|
|
|
/**
|
2015-01-16 14:23:05 +01:00
|
|
|
* Github Webhook
|
2014-11-23 20:13:38 +01:00
|
|
|
*
|
2015-01-16 14:23:05 +01:00
|
|
|
* @package integration
|
2014-11-23 20:13:38 +01:00
|
|
|
* @author Frederic Guillot
|
|
|
|
*/
|
2015-08-16 17:04:56 +02:00
|
|
|
class GithubWebhook extends \Core\Base
|
2014-11-23 20:13:38 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Events
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const EVENT_ISSUE_OPENED = 'github.webhook.issue.opened';
|
|
|
|
const EVENT_ISSUE_CLOSED = 'github.webhook.issue.closed';
|
|
|
|
const EVENT_ISSUE_REOPENED = 'github.webhook.issue.reopened';
|
|
|
|
const EVENT_ISSUE_ASSIGNEE_CHANGE = 'github.webhook.issue.assignee';
|
|
|
|
const EVENT_ISSUE_LABEL_CHANGE = 'github.webhook.issue.label';
|
|
|
|
const EVENT_ISSUE_COMMENT = 'github.webhook.issue.commented';
|
|
|
|
const EVENT_COMMIT = 'github.webhook.commit';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project id
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
private $project_id = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the project id
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param integer $project_id Project id
|
|
|
|
*/
|
|
|
|
public function setProjectId($project_id)
|
|
|
|
{
|
|
|
|
$this->project_id = $project_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse Github events
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $type Github event type
|
2014-12-22 19:15:38 +01:00
|
|
|
* @param array $payload Github event
|
2014-11-23 20:13:38 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-12-22 19:15:38 +01:00
|
|
|
public function parsePayload($type, array $payload)
|
2014-11-23 20:13:38 +01:00
|
|
|
{
|
|
|
|
switch ($type) {
|
|
|
|
case 'push':
|
|
|
|
return $this->parsePushEvent($payload);
|
|
|
|
case 'issues':
|
|
|
|
return $this->parseIssueEvent($payload);
|
|
|
|
case 'issue_comment':
|
|
|
|
return $this->parseCommentIssueEvent($payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse Push events (list of commits)
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $payload Event data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function parsePushEvent(array $payload)
|
|
|
|
{
|
|
|
|
foreach ($payload['commits'] as $commit) {
|
|
|
|
|
|
|
|
$task_id = $this->task->getTaskIdFromText($commit['message']);
|
|
|
|
|
2015-08-16 17:04:56 +02:00
|
|
|
if (empty($task_id)) {
|
2014-11-23 20:13:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$task = $this->taskFinder->getById($task_id);
|
|
|
|
|
2015-04-21 17:56:16 +02:00
|
|
|
if (empty($task)) {
|
2014-11-23 20:13:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-16 17:04:56 +02:00
|
|
|
if ($task['project_id'] != $this->project_id) {
|
|
|
|
continue;
|
2014-11-23 20:13:38 +01:00
|
|
|
}
|
2015-08-16 17:04:56 +02:00
|
|
|
|
|
|
|
$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)
|
|
|
|
);
|
2014-11-23 20:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse issue events
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $payload Event data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function parseIssueEvent(array $payload)
|
|
|
|
{
|
|
|
|
switch ($payload['action']) {
|
|
|
|
case 'opened':
|
|
|
|
return $this->handleIssueOpened($payload['issue']);
|
|
|
|
case 'closed':
|
|
|
|
return $this->handleIssueClosed($payload['issue']);
|
|
|
|
case 'reopened':
|
|
|
|
return $this->handleIssueReopened($payload['issue']);
|
|
|
|
case 'assigned':
|
|
|
|
return $this->handleIssueAssigned($payload['issue']);
|
|
|
|
case 'unassigned':
|
|
|
|
return $this->handleIssueUnassigned($payload['issue']);
|
|
|
|
case 'labeled':
|
|
|
|
return $this->handleIssueLabeled($payload['issue'], $payload['label']);
|
|
|
|
case 'unlabeled':
|
|
|
|
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)
|
|
|
|
{
|
2015-08-16 17:04:56 +02:00
|
|
|
$task = $this->taskFinder->getByReference($this->project_id, $payload['issue']['number']);
|
2014-11-23 20:13:38 +01:00
|
|
|
|
2015-08-16 17:04:56 +02:00
|
|
|
if (! empty($task)) {
|
|
|
|
|
|
|
|
$user = $this->user->getByUsername($payload['comment']['user']['login']);
|
|
|
|
|
|
|
|
if (! empty($user) && ! $this->projectPermission->isMember($this->project_id, $user['id'])) {
|
|
|
|
$user = array();
|
|
|
|
}
|
2014-11-23 20:13:38 +01:00
|
|
|
|
|
|
|
$event = array(
|
|
|
|
'project_id' => $this->project_id,
|
|
|
|
'reference' => $payload['comment']['id'],
|
2015-08-16 17:04:56 +02:00
|
|
|
'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,
|
2014-11-23 20:13:38 +01:00
|
|
|
'task_id' => $task['id'],
|
|
|
|
);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['dispatcher']->dispatch(
|
|
|
|
self::EVENT_ISSUE_COMMENT,
|
|
|
|
new GenericEvent($event)
|
|
|
|
);
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle new issues
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $issue Issue data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function handleIssueOpened(array $issue)
|
|
|
|
{
|
|
|
|
$event = array(
|
|
|
|
'project_id' => $this->project_id,
|
|
|
|
'reference' => $issue['number'],
|
|
|
|
'title' => $issue['title'],
|
|
|
|
'description' => $issue['body']."\n\n[".t('Github Issue').']('.$issue['html_url'].')',
|
|
|
|
);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['dispatcher']->dispatch(
|
|
|
|
self::EVENT_ISSUE_OPENED,
|
|
|
|
new GenericEvent($event)
|
|
|
|
);
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle issue closing
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $issue Issue data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function handleIssueClosed(array $issue)
|
|
|
|
{
|
2015-08-16 17:04:56 +02:00
|
|
|
$task = $this->taskFinder->getByReference($this->project_id, $issue['number']);
|
2014-11-23 20:13:38 +01:00
|
|
|
|
2015-04-21 17:56:16 +02:00
|
|
|
if (! empty($task)) {
|
2014-11-23 20:13:38 +01:00
|
|
|
$event = array(
|
|
|
|
'project_id' => $this->project_id,
|
|
|
|
'task_id' => $task['id'],
|
|
|
|
'reference' => $issue['number'],
|
|
|
|
);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['dispatcher']->dispatch(
|
|
|
|
self::EVENT_ISSUE_CLOSED,
|
|
|
|
new GenericEvent($event)
|
|
|
|
);
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle issue reopened
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $issue Issue data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function handleIssueReopened(array $issue)
|
|
|
|
{
|
2015-08-16 17:04:56 +02:00
|
|
|
$task = $this->taskFinder->getByReference($this->project_id, $issue['number']);
|
2014-11-23 20:13:38 +01:00
|
|
|
|
2015-04-21 17:56:16 +02:00
|
|
|
if (! empty($task)) {
|
2014-11-23 20:13:38 +01:00
|
|
|
$event = array(
|
|
|
|
'project_id' => $this->project_id,
|
|
|
|
'task_id' => $task['id'],
|
|
|
|
'reference' => $issue['number'],
|
|
|
|
);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['dispatcher']->dispatch(
|
|
|
|
self::EVENT_ISSUE_REOPENED,
|
|
|
|
new GenericEvent($event)
|
|
|
|
);
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle issue assignee change
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $issue Issue data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function handleIssueAssigned(array $issue)
|
|
|
|
{
|
|
|
|
$user = $this->user->getByUsername($issue['assignee']['login']);
|
2015-08-16 17:04:56 +02:00
|
|
|
$task = $this->taskFinder->getByReference($this->project_id, $issue['number']);
|
2014-11-23 20:13:38 +01:00
|
|
|
|
2015-08-16 17:04:56 +02:00
|
|
|
if (! empty($user) && ! empty($task) && $this->projectPermission->isMember($this->project_id, $user['id'])) {
|
2014-11-23 20:13:38 +01:00
|
|
|
|
|
|
|
$event = array(
|
|
|
|
'project_id' => $this->project_id,
|
|
|
|
'task_id' => $task['id'],
|
|
|
|
'owner_id' => $user['id'],
|
|
|
|
'reference' => $issue['number'],
|
|
|
|
);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['dispatcher']->dispatch(
|
|
|
|
self::EVENT_ISSUE_ASSIGNEE_CHANGE,
|
|
|
|
new GenericEvent($event)
|
|
|
|
);
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle unassigned issue
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $issue Issue data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function handleIssueUnassigned(array $issue)
|
|
|
|
{
|
2015-08-16 17:04:56 +02:00
|
|
|
$task = $this->taskFinder->getByReference($this->project_id, $issue['number']);
|
2014-11-23 20:13:38 +01:00
|
|
|
|
2015-04-21 17:56:16 +02:00
|
|
|
if (! empty($task)) {
|
2014-11-23 20:13:38 +01:00
|
|
|
|
|
|
|
$event = array(
|
|
|
|
'project_id' => $this->project_id,
|
|
|
|
'task_id' => $task['id'],
|
|
|
|
'owner_id' => 0,
|
|
|
|
'reference' => $issue['number'],
|
|
|
|
);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['dispatcher']->dispatch(
|
|
|
|
self::EVENT_ISSUE_ASSIGNEE_CHANGE,
|
|
|
|
new GenericEvent($event)
|
|
|
|
);
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle labeled issue
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $issue Issue data
|
|
|
|
* @param array $label Label data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function handleIssueLabeled(array $issue, array $label)
|
|
|
|
{
|
2015-08-16 17:04:56 +02:00
|
|
|
$task = $this->taskFinder->getByReference($this->project_id, $issue['number']);
|
2014-11-23 20:13:38 +01:00
|
|
|
|
2015-04-21 17:56:16 +02:00
|
|
|
if (! empty($task)) {
|
2014-11-23 20:13:38 +01:00
|
|
|
|
|
|
|
$event = array(
|
|
|
|
'project_id' => $this->project_id,
|
|
|
|
'task_id' => $task['id'],
|
|
|
|
'reference' => $issue['number'],
|
|
|
|
'label' => $label['name'],
|
|
|
|
);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['dispatcher']->dispatch(
|
|
|
|
self::EVENT_ISSUE_LABEL_CHANGE,
|
|
|
|
new GenericEvent($event)
|
|
|
|
);
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle unlabeled issue
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param array $issue Issue data
|
|
|
|
* @param array $label Label data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function handleIssueUnlabeled(array $issue, array $label)
|
|
|
|
{
|
2015-08-16 17:04:56 +02:00
|
|
|
$task = $this->taskFinder->getByReference($this->project_id, $issue['number']);
|
2014-11-23 20:13:38 +01:00
|
|
|
|
2015-04-21 17:56:16 +02:00
|
|
|
if (! empty($task)) {
|
2014-11-23 20:13:38 +01:00
|
|
|
|
|
|
|
$event = array(
|
|
|
|
'project_id' => $this->project_id,
|
|
|
|
'task_id' => $task['id'],
|
|
|
|
'reference' => $issue['number'],
|
|
|
|
'label' => $label['name'],
|
|
|
|
'category_id' => 0,
|
|
|
|
);
|
|
|
|
|
2015-01-16 14:23:05 +01:00
|
|
|
$this->container['dispatcher']->dispatch(
|
|
|
|
self::EVENT_ISSUE_LABEL_CHANGE,
|
|
|
|
new GenericEvent($event)
|
|
|
|
);
|
|
|
|
|
2014-11-23 20:13:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|