Passed
Push — master ( 565c41...d8e277 )
by Frank
02:13
created

GerritHookController::buildFileAttachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 11
cp 0
rs 9.2
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 2
crap 2
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
        $json = json_decode(file_get_contents($input));
33
34
        if (
35 11
            $json->project !== 'Packages/TYPO3.CMS' ||
36 11
            $json->token !== $this->configuration['gerrit']['webhookToken']
37
        ) {
38 3
            return;
39
        }
40 8
        $patchId = (int) str_replace('https://review.typo3.org/', '', $json->{'change-url'});
41 8
        $patchSet = property_exists($json, 'patchset') ? (int) $json->patchset : 0;
42
43 8
        $item = $this->queryGerrit('change:' . $patchId);
44 8
        $item = $item[0];
45 8
        $created = substr($item->created, 0, 19);
46 8
        $text = "Branch: {$json->branch} | :calendar: {$created} | ID: {$item->_number}" . chr(10);
47 8
        $text .= ":link: <https://review.typo3.org/{$item->_number}|Goto Review>";
48 8
        if ($hook === 'patchset-created' && $patchSet === 1 && $json->branch === 'master') {
49 2
            $message = $this->buildMessage('[NEW] ' . $item->subject, $text);
50 2
            $this->sendMessageToChannel($hook, $message);
51 6
        } elseif ($hook === 'change-merged') {
52 5
            $message = $this->buildMessage(':white_check_mark: [MERGED] ' . $item->subject, $text, Message\Attachment::COLOR_GOOD);
53 5
            $this->sendMessageToChannel($hook, $message);
54 5
            $this->checkFiles($patchId, $json->commit);
55
        }
56 8
    }
57
58
    /**
59
     * @param string $title
60
     * @param string $text
61
     * @param string $color
62
     *
63
     * @return Message
64
     */
65 7
    protected function buildMessage(string $title, string $text, string $color = Message\Attachment::COLOR_NOTICE) : Message
66
    {
67 7
        $message = new Message();
68 7
        $message->setText(' ');
69 7
        $attachment = new Message\Attachment();
70
71 7
        $attachment->setColor($color);
72 7
        $attachment->setTitle($title);
73
74 7
        $attachment->setText($text);
75 7
        $attachment->setFallback($text);
76 7
        $message->addAttachment($attachment);
77 7
        return $message;
78
    }
79
80
    /**
81
     * @param int $patchId
82
     * @param int $commit
83
     *
84
     * @throws \Doctrine\DBAL\DBALException
85
     */
86 5
    protected function checkFiles($patchId, $commit)
87
    {
88 5
        $files = $this->getFilesForPatch($patchId, $commit);
89 5
        $rstFiles = [];
90 5
        if (is_array($files)) {
91
            foreach ($files as $fileName => $changeInfo) {
92
                if ($this->endsWith(strtolower($fileName), '.rst')) {
93
                    $rstFiles[$fileName] = $changeInfo;
94
                }
95
            }
96
        }
97 5
        if (!empty($rstFiles)) {
98
            $message = new Message();
99
            $message->setText(' ');
100
            foreach ($rstFiles as $fileName => $changeInfo) {
101
                $attachment = $this->buildFileAttachment($fileName, $changeInfo);
102
                $message->addAttachment($attachment);
103
            }
104
            $this->sendMessageToChannel('rst-merged', $message);
105
        }
106 5
    }
107
108
    /**
109
     * @param string $fileName
110
     * @param array $changeInfo
111
     *
112
     * @return Message\Attachment
113
     */
114
    protected function buildFileAttachment(string $fileName, array $changeInfo) : Message\Attachment
115
    {
116
        $attachment = new Message\Attachment();
117
        $status = $changeInfo['status'] ?? 'default';
118
        $color = [
119
            'A' => Message\Attachment::COLOR_GOOD,
120
            'D' => Message\Attachment::COLOR_WARNING,
121
            'default' => Message\Attachment::COLOR_WARNING,
122
        ];
123
        $text = [
124
            'A' => 'A new documentation file has been added',
125
            'D' => 'A documentation file has been removed',
126
            'default' => 'A documentation file has been updated',
127
        ];
128
        $attachment->setColor($color[$status]);
129
        $attachment->setTitle($text[$status]);
130
131
        $text = ':link: <https://git.typo3.org/Packages/TYPO3.CMS.git/blob/HEAD:/' . $fileName . '|' . $fileName . '>';
132
        $attachment->setText($text);
133
        $attachment->setFallback($text);
134
        return $attachment;
135
    }
136
137
    /**
138
     * @param string $hook
139
     * @param Message $message
140
     *
141
     * @throws \Doctrine\DBAL\DBALException
142
     */
143 7
    protected function sendMessageToChannel(string $hook, Message $message)
144
    {
145 7
        if (is_array($this->configuration['gerrit'][$hook]['channels'])) {
146 7
            foreach ($this->configuration['gerrit'][$hook]['channels'] as $channel) {
147 4
                $this->postToSlack($message, $channel);
148
            }
149
        }
150 7
    }
151
}
152