This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathWebhookHandler.php
115 lines (98 loc) · 2.43 KB
/
WebhookHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Kanboard\Plugin\GogsWebhook;
use Kanboard\Core\Base;
use Kanboard\Event\GenericEvent;
/**
* Gogs Webhook
*
* @author Frederic Guillot
*/
class WebhookHandler extends Base
{
/**
* Events
*
* @var string
*/
const EVENT_COMMIT = 'gogs.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 incoming events
*
* @access public
* @param string $type Gogs event type
* @param array $payload Gogs event
* @return boolean
*/
public function parsePayload($type, array $payload)
{
if ($type === 'push') {
return $this->handlePush($payload);
}
return false;
}
/**
* Parse push events
*
* @access public
* @param array $payload
* @return boolean
*/
public function handlePush(array $payload)
{
$results = array();
if (isset($payload['commits'])) {
foreach ($payload['commits'] as $commit) {
$results[] = $this->handleCommit($commit);
}
}
return in_array(true, $results, true);
}
/**
* Parse commit
*
* @access public
* @param array $commit Gogs commit
* @return boolean
*/
public function handleCommit(array $commit)
{
$task_id = $this->taskModel->getTaskIdFromText($commit['message']);
if (empty($task_id)) {
return false;
}
$task = $this->taskFinderModel->getById($task_id);
if (empty($task)) {
return false;
}
if ($task['project_id'] != $this->project_id) {
return false;
}
$this->dispatcher->dispatch(
self::EVENT_COMMIT,
new GenericEvent(array(
'task_id' => $task_id,
'commit_message' => $commit['message'],
'commit_url' => $commit['url'],
'comment' => $commit['message']."\n\n[".t('Commit made by @%s on Gogs', $commit['author']['name'] ?: $commit['author']['username']).']('.$commit['url'].')',
) + $task)
);
return true;
}
}