Passed
Push — master ( 51b807...3f897c )
by Frank
02:13
created

GerritHookController::buildMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 3
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 1
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
                    $text = "Branch: *{$branch}* | :calendar: _{$created}_ | ID: {$item->_number}\n";
55 2
                    $text .= ":link: <https://review.typo3.org/{$item->_number}|Review now>";
56
57 2
                    $message = $this->buildMessage('[NEW] ' . $item->subject, $text);
58 2
                    $this->sendMessageToChannel($hook, $message);
59
                }
60 3
                break;
61 5
            case 'change-merged':
62 5
                $item = $this->queryGerrit('change:' . $patchId);
63 5
                $item = $item[0];
64 5
                $created = substr($item->created, 0, 19);
65
66 5
                $text = "Branch: {$branch} | :calendar: {$created} | ID: {$item->_number}\n";
67 5
                $text .= ":link: <https://review.typo3.org/{$item->_number}|Goto Review>";
68
69 5
                $message = $this->buildMessage(':white_check_mark: [MERGED] ' . $item->subject, $text, Message\Attachment::COLOR_GOOD);
70 5
                $this->sendMessageToChannel($hook, $message);
71
72 5
                $files = $this->getFilesForPatch($patchId, $commit);
73 5
                $rstFiles = [];
74 5
                if (is_array($files)) {
75
                    foreach ($files as $fileName => $changeInfo) {
76
                        if ($this->endsWith(strtolower($fileName), '.rst')) {
77
                            $rstFiles[$fileName] = $changeInfo;
78
                        }
79
                    }
80
                }
81 5
                if (count($rstFiles) > 0) {
82
                    $message = new Message();
83
                    $message->setText(' ');
84
                    foreach ($rstFiles as $fileName => $changeInfo) {
85
                        $attachment = new Message\Attachment();
86
                        $status = !empty($changeInfo['status']) ? $changeInfo['status'] : null;
87
                        switch ($status) {
88
                            case 'A':
89
                                $attachment->setColor(Message\Attachment::COLOR_GOOD);
90
                                $attachment->setTitle('A new documentation file has been added');
91
                                break;
92
                            case 'D':
93
                                $attachment->setColor(Message\Attachment::COLOR_WARNING);
94
                                $attachment->setTitle('A documentation file has been removed');
95
                                break;
96
                            default:
97
                                $attachment->setColor(Message\Attachment::COLOR_WARNING);
98
                                $attachment->setTitle('A documentation file has been updated');
99
                                break;
100
                        }
101
                        $text = ':link: <https://git.typo3.org/Packages/TYPO3.CMS.git/blob/HEAD:/' . $fileName . '|' . $fileName . '>';
102
                        $attachment->setText($text);
103
                        $attachment->setFallback($text);
104
                        $message->addAttachment($attachment);
105
                    }
106
                    $this->sendMessageToChannel('rst-merged', $message);
107
                }
108 5
                break;
109
        }
110 8
    }
111
112
    /**
113
     * @param string $title
114
     * @param string $text
115
     * @param string $color
116
     *
117
     * @return Message
118
     */
119 7
    protected function buildMessage(string $title, string $text, string $color = Message\Attachment::COLOR_NOTICE) : Message
120
    {
121 7
        $message = new Message();
122 7
        $message->setText(' ');
123 7
        $attachment = new Message\Attachment();
124
125 7
        $attachment->setColor($color);
126 7
        $attachment->setTitle($title);
127
128 7
        $attachment->setText($text);
129 7
        $attachment->setFallback($text);
130 7
        $message->addAttachment($attachment);
131 7
        return $message;
132
    }
133
134
    /**
135
     * @param string $hook
136
     * @param Message $message
137
     *
138
     * @throws \Doctrine\DBAL\DBALException
139
     */
140 7
    protected function sendMessageToChannel(string $hook, Message $message)
141
    {
142 7
        if (is_array($this->configuration['gerrit'][$hook]['channels'])) {
143 7
            foreach ($this->configuration['gerrit'][$hook]['channels'] as $channel) {
144 4
                $this->postToSlack($message, $channel);
145
            }
146
        }
147 7
    }
148
}
149