curl -d '{"color":"green","message":"My first notification (yey)","notify":false,"message_format":"text"}' -H 'Content-Type: application/json' https://youcompany.hipchat.com/v2/room/{roomId}/notification?auth_token={token}
<?php Route::group(['prefix' => LaravelLocalization::setLocale()], function () { Route::group( [ 'prefix' => 'development', ], function () { Route::group(['prefix' => 'bitbucket', 'namespace' => '\BE\Dev\Backend\Http\Controllers'], function () { Route::post('/', [ 'as' => 'bitbucket.push_event', 'uses' => 'BitbucketEventsController@pushEvent' ]); }); } ); } );
<?php namespace BE\Dev\Backend\Http\Controllers; use App\Http\Controllers\Controller; use BE\Dev\Services\Bitbucket\BitbucketPushEventService; use BE\Dev\Services\HipChat\HipChatService; use Illuminate\Http\Request; class BitbucketEventsController extends Controller { <?php namespace BE\Dev\Backend\Http\Controllers; use App\Http\Controllers\Controller; use BE\Dev\Services\Bitbucket\BitbucketPushEventService; use BE\Dev\Services\HipChat\HipChatService; use Illuminate\Http\Request; class BitbucketEventsController extends Controller { /** * @var $hipchatService HipChatService */ protected $hipchatService; /** * @var $pushService BitbucketPushEventService */ protected $pushService; protected $config; public function __construct(Request $request) { $this->config = config('be_dev'); $this->hipchatService = app(HipChatService::class); $this->pushService = app(BitbucketPushEventService::class, [$request->all()]); } public function pushEvent(Request $request) { $data = $request->all(); // staging - if ($this->pushService->getBranch() != 'staging') { return false; } // $comment = strtolower($data['push']['changes'][0]['commits'][0]['message']); // $author = $data['actor']['display_name']; $data = [ 'color' => 'green', 'message' => "<strong>{$author}</strong> BE \"{$comment}\"", 'notify' => true, 'message_format' => 'html', ]; // $this->hipchatService->sendNotification($data); // no tests - if (strpos($comment, 'no tests')) { return response()->json([ 'success' => true, 'message' => 'tests was not executed' ])->setStatusCode(200); } // git pull + + $service = new \BE\Dev\Services\RuntTestsInQueueAndNotifyHipChatRoom(); $service->handle(); return response()->json([ 'success' => true ])->setStatusCode(200); } }
<?php namespace BE\Dev\Services; use BE\Dev\Services\HipChat\HipChatService; use Illuminate\Bus\Queueable; use BE\Jobs\Job; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class RuntTestsInQueueAndNotifyHipChatRoom extends Job implements ShouldQueue { use Queueable, SerializesModels; /** * @var $deployService DeployService */ protected $deployService; /** * @var $tests RunTests */ protected $tests; /** * @var $hipchatService HipChatService */ protected $hipchatService; public function __construct() { $this->deployService = app(DeployService::class); $this->tests = app(RunTests::class); $this->tests = app(HipChatService::class); } /** * Execute the job. * * @return void */ public function handle() { try { // git pull $this->deployService->pullPackagesChanges(); // $outputTests = $this->tests->run(); if ($outputTests === true) { $outputTests = ' '; $colorTests = 'green'; } else { $colorTests = 'red'; } // $this->hipchatService->sendNotification([ 'color' => $colorTests, 'message' => $outputTests, 'notify' => true, 'message_format' => 'html', ]); } catch (\Exception $exception) { \Log::error($exception->getMessage()); } } }
<?php namespace BE\Dev\Services; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; class DeployService { public function pullPackagesChanges() { // cd ../ , index.php public, // $process = new Process('cd ../ && git pull'); $process->run(); // executes after the command finishes if (!$process->isSuccessful()) { throw new ProcessFailedException($process); } return $process->getOutput(); } }
<?php namespace BE\Dev\Services; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; class RunTests { /** * @return string */ public function run() { $process = new Process('cd ../ && vendor/bin/phpunit --tap'); $process->run(); // executes after the command finishes if (!$process->isSuccessful()) { return $process->getIncrementalOutput(); } return true; } }
<?php namespace BE\Dev\Services\HipChat; class HipChatService { /** * @var $config array Service config */ protected $config; public function __construct() { $this->config = config('be_dev.hipchat'); } public function sendNotification($data) { // create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, $this->config['url']); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // $output contains the output string $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); return $output; } }
Log::getMonoLog()->pushHandler(new \Monolog\Handler\HipChatHandler( 'AUTH_TOKEN', 'ROOM_ID', 'hipchat-app', true, \Monolog\Logger::CRITICAL, true, true, 'text', 'COMPANY_NAME'.hipchat.com, 'v2' ));
Source: https://habr.com/ru/post/341724/
All Articles