| 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 |  |                 $this->checkFiles($patchId, $commit); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 5 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string $title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param string $color | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return Message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 7 |  |     protected function buildMessage(string $title, string $text, string $color = Message\Attachment::COLOR_NOTICE) : Message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 7 |  |         $message = new Message(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 7 |  |         $message->setText(' '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 7 |  |         $attachment = new Message\Attachment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 7 |  |         $attachment->setColor($color); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 7 |  |         $attachment->setTitle($title); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 7 |  |         $attachment->setText($text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 7 |  |         $attachment->setFallback($text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 7 |  |         $message->addAttachment($attachment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 7 |  |         return $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param int $patchId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param int $commit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 5 |  |     protected function checkFiles($patchId, $commit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 5 |  |         $files = $this->getFilesForPatch($patchId, $commit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 5 |  |         $rstFiles = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 5 |  |         if (is_array($files)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             foreach ($files as $fileName => $changeInfo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 if ($this->endsWith(strtolower($fileName), '.rst')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     $rstFiles[$fileName] = $changeInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 5 |  |         if (count($rstFiles) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $message = new Message(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $message->setText(' '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             foreach ($rstFiles as $fileName => $changeInfo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $attachment = new Message\Attachment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 $status = !empty($changeInfo['status']) ? $changeInfo['status'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 switch ($status) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     case 'A': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                         $attachment->setColor(Message\Attachment::COLOR_GOOD); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                         $attachment->setTitle('A new documentation file has been added'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     case 'D': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                         $attachment->setColor(Message\Attachment::COLOR_WARNING); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                         $attachment->setTitle('A documentation file has been removed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                         $attachment->setColor(Message\Attachment::COLOR_WARNING); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                         $attachment->setTitle('A documentation file has been updated'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 $text = ':link: <https://git.typo3.org/Packages/TYPO3.CMS.git/blob/HEAD:/' . $fileName . '|' . $fileName . '>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 $attachment->setText($text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 $attachment->setFallback($text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 $message->addAttachment($attachment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $this->sendMessageToChannel('rst-merged', $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param string $hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param Message $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 7 |  |     protected function sendMessageToChannel(string $hook, Message $message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 7 |  |         if (is_array($this->configuration['gerrit'][$hook]['channels'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 7 |  |             foreach ($this->configuration['gerrit'][$hook]['channels'] as $channel) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 4 |  |                 $this->postToSlack($message, $channel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 7 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 159 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |  |