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