Completed
Branch master (51b807)
by Frank
04:17 queued 02:02
created

GerritHookController::sendMessageToChannel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * T3Bot.
4
 *
5
 * @author Frank Nägler <[email protected]>
6
 *
7
 * @link http://www.t3bot.de
8
 * @link http://wiki.typo3.org/T3Bot
9
 */
10
namespace T3Bot\Controller;
11
12
use T3Bot\Slack\Message;
13
use T3Bot\Traits\GerritTrait;
14
15
/**
16
 * Class GerritHookController.
17
 */
18
class GerritHookController extends AbstractHookController
19
{
20
    use GerritTrait;
21
22
    /**
23
     * public method to start processing the request.
24
     *
25
     * @param string $hook
26
     * @param string $input
27
     *
28
     * @throws \Doctrine\DBAL\DBALException
29
     */
30 11
    public function process($hook, $input = 'php://input')
31
    {
32 11
        $entityBody = file_get_contents($input);
33 11
        $json = json_decode($entityBody);
34
35 11
        if ($this->configuration['gerrit']['webhookToken'] !== $json->token) {
36 2
            return;
37
        }
38 9
        if ($json->project !== 'Packages/TYPO3.CMS') {
39
            // only core patches please...
40 1
            return;
41
        }
42 8
        $patchId = (int) str_replace('https://review.typo3.org/', '', $json->{'change-url'});
43 8
        $patchSet = property_exists($json, 'patchset') ? (int) $json->patchset : 0;
44 8
        $commit = $json->commit;
45 8
        $branch = $json->branch;
46
47
        switch ($hook) {
48 8
            case 'patchset-created':
49 3
                if ($patchSet === 1 && $branch === 'master') {
50 2
                    $item = $this->queryGerrit('change:' . $patchId);
51 2
                    $item = $item[0];
52 2
                    $created = substr($item->created, 0, 19);
53
54 2
                    $message = new Message();
55 2
                    $message->setText(' ');
56 2
                    $attachment = new Message\Attachment();
57
58 2
                    $attachment->setColor(Message\Attachment::COLOR_NOTICE);
59 2
                    $attachment->setTitle('[NEW] ' . $item->subject);
60
61 2
                    $text = "Branch: *{$branch}* | :calendar: _{$created}_ | ID: {$item->_number}\n";
62 2
                    $text .= ":link: <https://review.typo3.org/{$item->_number}|Review now>";
63 2
                    $attachment->setText($text);
64 2
                    $attachment->setFallback($text);
65 2
                    $message->addAttachment($attachment);
66
67 2
                    $this->sendMessageToChannel($hook, $message);
68
                }
69 3
                break;
70 5
            case 'change-merged':
71
                /** @var array $item */
72 5
                $item = $this->queryGerrit('change:' . $patchId);
73 5
                $item = $item[0];
74
                /* @var \stdClass $item */
75 5
                $created = substr($item->created, 0, 19);
76
77 5
                $message = new Message();
78 5
                $message->setText(' ');
79 5
                $attachment = new Message\Attachment();
80
81 5
                $attachment->setColor(Message\Attachment::COLOR_GOOD);
82 5
                $attachment->setTitle(':white_check_mark: [MERGED] ' . $item->subject);
83
84 5
                $text = "Branch: {$branch} | :calendar: {$created} | ID: {$item->_number}\n";
85 5
                $text .= ":link: <https://review.typo3.org/{$item->_number}|Goto Review>";
86 5
                $attachment->setText($text);
87 5
                $attachment->setFallback($text);
88 5
                $message->addAttachment($attachment);
89
90 5
                $this->sendMessageToChannel($hook, $message);
91
92 5
                $files = $this->getFilesForPatch($patchId, $commit);
93 5
                $rstFiles = [];
94 5
                if (is_array($files)) {
95
                    foreach ($files as $fileName => $changeInfo) {
96
                        if ($this->endsWith(strtolower($fileName), '.rst')) {
97
                            $rstFiles[$fileName] = $changeInfo;
98
                        }
99
                    }
100
                }
101 5
                if (count($rstFiles) > 0) {
102
                    $message = new Message();
103
                    $message->setText(' ');
104
                    foreach ($rstFiles as $fileName => $changeInfo) {
105
                        $attachment = new Message\Attachment();
106
                        $status = !empty($changeInfo['status']) ? $changeInfo['status'] : null;
107
                        switch ($status) {
108
                            case 'A':
109
                                $attachment->setColor(Message\Attachment::COLOR_GOOD);
110
                                $attachment->setTitle('A new documentation file has been added');
111
                                break;
112
                            case 'D':
113
                                $attachment->setColor(Message\Attachment::COLOR_WARNING);
114
                                $attachment->setTitle('A documentation file has been removed');
115
                                break;
116
                            default:
117
                                $attachment->setColor(Message\Attachment::COLOR_WARNING);
118
                                $attachment->setTitle('A documentation file has been updated');
119
                                break;
120
                        }
121
                        $text = ':link: <https://git.typo3.org/Packages/TYPO3.CMS.git/blob/HEAD:/' . $fileName
122
                            . '|' . $fileName . '>';
123
                        $attachment->setText($text);
124
                        $attachment->setFallback($text);
125
                        $message->addAttachment($attachment);
126
                    }
127
                    $this->sendMessageToChannel('rst-merged', $message);
128
                }
129 5
                break;
130
        }
131 8
    }
132
133
    /**
134
     * @param string $hook
135
     * @param Message $message
136
     *
137
     * @throws \Doctrine\DBAL\DBALException
138
     */
139 7
    protected function sendMessageToChannel(string $hook, Message $message)
140
    {
141 7
        if (is_array($this->configuration['gerrit'][$hook]['channels'])) {
142 7
            foreach ($this->configuration['gerrit'][$hook]['channels'] as $channel) {
143 4
                $this->postToSlack($message, $channel);
144
            }
145
        }
146 7
    }
147
}
148