diff --git a/recipes/slack.php b/recipes/slack.php index d379265..3d8449d 100644 --- a/recipes/slack.php +++ b/recipes/slack.php @@ -11,48 +11,43 @@ task('deploy:slack', function () { global $php_errormsg; - $config = get('slack', []); - - if (!isset($config['message'])) { - $config['message'] = "Deployment to `{{host}}` on *{{stage}}* was successful\n({{release_path}})"; - } + $defaultConfig = [ + 'channel' => '#general', + 'icon' => ':sunny:', + 'username' => 'Deploy', + 'message' => "Deployment to `{{host}}` on *{{stage}}* was successful\n({{release_path}})", + 'app' => 'app-name', + ]; + $config = array_merge($defaultConfig, (array) get('slack', [])); + $server = \Deployer\Task\Context::get()->getServer()->getConfiguration(); - $host = $server->getHost(); $user = !$server->getUser() ? null : $server->getUser(); $messagePlaceHolders = [ '{{release_path}}' => env('release_path'), - '{{host}}' => env('server.host'), - '{{stage}}' => env('stages')[0], - '{{user}}' => $user, - '{{branch}}' => env('branch'), - '{{app_name}}' => isset($config['app']) ? ['app'] : 'app-name', + '{{host}}' => env('server.host'), + '{{stage}}' => env('stages')[0], + '{{user}}' => $user, + '{{branch}}' => env('branch'), + '{{app_name}}' => isset($config['app']) ? $config['app'] : 'app-name', ]; $config['message'] = strtr($config['message'], $messagePlaceHolders); - - $defaultConfig = [ - 'channel' => '#general', - 'icon' => ':sunny:', - 'username' => 'Deploy', - 'message' => "Deployment to `{{host}}` on *{{stage}}* was successful\n({{release_path}})", - ]; - - $config = array_merge($defaultConfig, $config); + if (!is_array($config) || !isset($config['token']) || !isset($config['team']) || !isset($config['channel'])) { - throw new \RuntimeException("Please configure new slack: set('slack', array('token' => 'xoxp...', 'team' => 'team', 'channel' => '#channel', 'messsage' => 'message to send'));"); + throw new \RuntimeException("Please configure new slack: set('slack', ['token' => 'xoxp...', 'team' => 'team', 'channel' => '#channel', 'messsage' => 'message to send']);"); } $urlParams = [ - 'channel' => $config['channel'], - 'token' => $config['token'], - 'text' => $config['message'], - 'username' => $config['username'], + 'channel' => $config['channel'], + 'token' => $config['token'], + 'text' => $config['message'], + 'username' => $config['username'], 'icon_emoji' => $config['icon'], - 'pretty' => true + 'pretty' => true ]; if (isset($config['icon_url'])) {