summaryrefslogtreecommitdiff
path: root/app/Controller/Webhook.php
blob: 667c5087943e4573cd4df510a9e709b0aef068ab (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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php

namespace Controller;

/**
 * Webhook controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class Webhook extends Base
{
    /**
     * Webhook to create a task
     *
     * @access public
     */
    public function task()
    {
        if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) {
            $this->response->text('Not Authorized', 401);
        }

        $defaultProject = $this->project->getFirst();

        $values = array(
            'title' => $this->request->getStringParam('title'),
            'description' => $this->request->getStringParam('description'),
            'color_id' => $this->request->getStringParam('color_id'),
            'project_id' => $this->request->getIntegerParam('project_id', $defaultProject['id']),
            'owner_id' => $this->request->getIntegerParam('owner_id'),
            'column_id' => $this->request->getIntegerParam('column_id'),
            'category_id' => $this->request->getIntegerParam('category_id'),
        );

        list($valid,) = $this->taskValidator->validateCreation($values);

        if ($valid && $this->taskCreation->create($values)) {
            $this->response->text('OK');
        }

        $this->response->text('FAILED');
    }

    /**
     * Handle Github webhooks
     *
     * @access public
     */
    public function github()
    {
        if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) {
            $this->response->text('Not Authorized', 401);
        }

        $this->githubWebhook->setProjectId($this->request->getIntegerParam('project_id'));

        $result = $this->githubWebhook->parsePayload(
            $this->request->getHeader('X-Github-Event'),
            $this->request->getJson() ?: array()
        );

        echo $result ? 'PARSED' : 'IGNORED';
    }

    /**
     * Handle Gitlab webhooks
     *
     * @access public
     */
    public function gitlab()
    {
        if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) {
            $this->response->text('Not Authorized', 401);
        }

        $this->gitlabWebhook->setProjectId($this->request->getIntegerParam('project_id'));

        $result = $this->gitlabWebhook->parsePayload(
            $this->request->getJson() ?: array()
        );

        echo $result ? 'PARSED' : 'IGNORED';
    }

    /**
     * Handle Bitbucket webhooks
     *
     * @access public
     */
    public function bitbucket()
    {
        if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) {
            $this->response->text('Not Authorized', 401);
        }

        $this->bitbucketWebhook->setProjectId($this->request->getIntegerParam('project_id'));

        $result = $this->bitbucketWebhook->parsePayload(json_decode(@$_POST['payload'], true));

        echo $result ? 'PARSED' : 'IGNORED';
    }

    /**
     * Handle Postmark webhooks
     *
     * @access public
     */
    public function postmark()
    {
        if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) {
            $this->response->text('Not Authorized', 401);
        }

        echo $this->postmarkWebhook->parsePayload($this->request->getJson() ?: array()) ? 'PARSED' : 'IGNORED';
    }

    /**
     * Handle Mailgun webhooks
     *
     * @access public
     */
    public function mailgun()
    {
        if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) {
            $this->response->text('Not Authorized', 401);
        }

        echo $this->mailgunWebhook->parsePayload($_POST) ? 'PARSED' : 'IGNORED';
    }

    /**
     * Handle Sendgrid webhooks
     *
     * @access public
     */
    public function sendgrid()
    {
        if ($this->config->get('webhook_token') !== $this->request->getStringParam('token')) {
            $this->response->text('Not Authorized', 401);
        }

        echo $this->sendgridWebhook->parsePayload($_POST) ? 'PARSED' : 'IGNORED';
    }
}