| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Deployer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | function sendHttpRequest($url, $formParams) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     $formParams = json_encode($formParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     $ch = curl_init($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     curl_setopt($ch, CURLOPT_POSTFIELDS, $formParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     curl_setopt( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $ch, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         CURLOPT_HTTPHEADER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             'Content-Type: application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             'Content-Length: '.strlen($formParams), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     return curl_exec($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | function sendDeployNotification($subject) | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     $url = get('notify_channel_url'); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     if (! $url) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         throw new \UnexpectedValueException('[Laravel-Deployer] Not found webhook url!'); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     $notifyBy = get('notify_by', 'webhook'); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     switch ($notifyBy) { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         case 'wechat_bot': | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $content = '在 '.get('environment').' 环境更新 '.get('branch').' 分支 '; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             $formParams = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                 'msgtype' => 'news', | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 'news' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                     'articles' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                         [ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |                             'title' => get('user').' '.$subject, | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                             'description' => $content, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                             'url' => get('app_repo_url', 'https://github.com'), | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                             'picurl' => get('pic_url', 'https://picsum.photos/id/'.rand(1, 1000).'/800/600'), | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                         ], | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             break; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         default: | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             $content = implode("\n", [ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 $subject, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 '应用名称: '.get('application'), | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 '发布者: '.get('user'), | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 '分支名: '.get('branch'), | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 '环境: '.get('environment'), | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $formParams = ['text' => $content]; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             break; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     if (get('group_notify')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         sendHttpRequest($url, $formParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     $deployedWebookUrl = get('deployed_webhook_url'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     if ($deployedWebookUrl) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $deployedData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'application' => get('application'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             'user' => get('user'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             'branch' => get('branch'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             'environment' => get('environment'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'app_repo_url' =>  get('app_repo_url', 'https://github.com'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         sendHttpRequest($deployedWebookUrl, $deployedData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     return writeln($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | task('success:notify', function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     return sendDeployNotification('成功发布新版本!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | })->local(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | task('failed:notify', function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     return sendDeployNotification('发布新版本失败!'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | })->local(); | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |