summaryrefslogtreecommitdiff
path: root/app/Model/GithubWebhook.php
blob: 688ab60116d45c525fd7688cf0f2000ff7324c5e (plain)
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
<?php

namespace Model;

/**
 * Github Webhook model
 *
 * @package  model
 * @author   Frederic Guillot
 */
class GithubWebhook extends Base
{
    /**
     * Events
     *
     * @var string
     */
    const EVENT_ISSUE_OPENED  = 'github.webhook.issue.opened';
    const EVENT_ISSUE_CLOSED  = 'github.webhook.issue.closed';
    const EVENT_ISSUE_LABELED = 'github.webhook.issue.labeled';
    const EVENT_ISSUE_COMMENT = 'github.webhook.issue.commented';
    const EVENT_COMMIT        = 'github.webhook.commit';

    /**
     * Parse Github events
     *
     * @access public
     * @param  string  $type      Github event type
     * @param  string  $payload   Raw Github event (JSON)
     */
    public function parsePayload($type, $payload)
    {
        $payload = json_decode($payload, true);

        switch ($type) {
            case 'push':
                return $this->parsePushEvent($payload);
            case 'issues':
                return $this->parseIssueEvent($payload);
        }
    }

    /**
     * Parse Push events (list of commits)
     *
     * @access public
     * @param  array   $payload   Event data
     */
    public function parsePushEvent(array $payload)
    {
        foreach ($payload['commits'] as $commit) {

            $task_id = $this->task->getTaskIdFromText($commit['message']);

            if (! $task_id) {
                continue;
            }

            $task = $this->task->getById($task_id);

            if (! $task) {
                continue;
            }

            if ($task['is_active'] == Task::STATUS_OPEN) {
                $this->event->trigger(self::EVENT_COMMIT, array('task_id' => $task_id) + $task);
            }
        }
    }

    /**
     * Parse issue events
     *
     * @access public
     * @param  array   $payload   Event data
     */
    public function parseIssueEvent(array $payload)
    {

    }
}