From 894d4d4b556bfb495cd48dd00f96fcc8376be90a Mon Sep 17 00:00:00 2001 From: Jordan Ryan Date: Thu, 24 Nov 2016 11:40:51 -0800 Subject: [PATCH 1/4] =?UTF-8?q?*=20Construct=20attachments=20array=20so=20?= =?UTF-8?q?that=20pretext=20can=20be=20added=20for=20each=20workflow=20cas?= =?UTF-8?q?e=20*=20Automatically=20check=20to=20see=20if=20the=20channel?= =?UTF-8?q?=20is=20private=20(no=20#=20in=20channel=20name)=20and=20if=20s?= =?UTF-8?q?o,=20don=E2=80=99t=20post=20to=20Slack=20with=20the=20username?= =?UTF-8?q?=20in=20the=20payload?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- slack_notification/slack_notification.php | 70 +++++++++++++++++------ 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/slack_notification/slack_notification.php b/slack_notification/slack_notification.php index 31bd4bd..da44430 100644 --- a/slack_notification/slack_notification.php +++ b/slack_notification/slack_notification.php @@ -46,6 +46,10 @@ ), ); +// Define Attachment array first so pretext can be added to it for each case +$attachment = array( + 'color' => $pantheon_yellow, // Can either be one of 'good', 'warning', 'danger', or any hex color code +); // Customize the message based on the workflow type. Note that slack_notification.php // must appear in your pantheon.yml for each workflow type you wish to send notifications on. switch($_POST['wf_type']) { @@ -68,6 +72,7 @@ 'value' => $text, 'short' => 'false' ); + $attachment += array('pretext' => 'Deployment complete :rocket:'); break; case 'sync_code': @@ -84,18 +89,35 @@ // Build an array of fields to be rendered with Slack Attachments as a table // attachment-style formatting: // https://api.slack.com/docs/attachments - $fields += array( - array( - 'title' => 'Commit', - 'value' => rtrim($hash), - 'short' => 'true' - ), - array( - 'title' => 'Commit Message', - 'value' => $message, - 'short' => 'false' - ) + $fields[] = array( + 'title' => 'Commit', + 'value' => rtrim($hash), + 'short' => 'true' + ); + $fields[] = array( + 'title' => 'Commit Message', + 'value' => $message, + 'short' => 'false' ); + $attachment += array('pretext' => 'Code syncing'); + break; + + case 'create_cloud_development_environment': + // Prepare the slack payload as per: + // https://api.slack.com/incoming-webhooks + $text = $_ENV['PANTHEON_ENVIRONMENT'] . ' spinning up for'; + $text .= $_ENV['PANTHEON_SITE_NAME'] .' by '. $_POST['user_email'] .' complete!'; + $text .= ' '; + $attachment += array('pretext' => 'New environment created'); + break; + + case 'cache_clear': + // Prepare the slack payload as per: + // https://api.slack.com/incoming-webhooks + $text = 'Cache clear for '. $_ENV['PANTHEON_ENVIRONMENT'] . ' on '; + $text .= $_ENV['PANTHEON_SITE_NAME'] .' by '. $_POST['user_email'] .' complete!'; + $text .= ' '; + $attachment += array('pretext' => 'Caches cleared'); break; default: @@ -103,13 +125,19 @@ break; } -$attachment = array( +// Add the prepared $text and $fields arrays +$attachment += array( 'fallback' => $text, - 'pretext' => 'Deploying :rocket:', - 'color' => $pantheon_yellow, // Can either be one of 'good', 'warning', 'danger', or any hex color code 'fields' => $fields ); +// Add pretext default if not already defined +if (empty($attachment->pretext)) { + $attachment += array( + 'pretext' => 'Pantheon system update :rocket:' + ); +} + _slack_notification($secrets['slack_url'], $secrets['slack_channel'], $secrets['slack_username'], $text, $attachment, $secrets['always_show_text']); @@ -143,10 +171,18 @@ function _get_secrets($requiredKeys, $defaults) function _slack_notification($slack_url, $channel, $username, $text, $attachment, $alwaysShowText = false) { $attachment['fallback'] = $text; - $post = array( - 'username' => $username, + if (substr($channel,1) == '#') { + // Post with the $username + $post = array( + 'username' => $username + ); + } else { + // Do not post with the username for private channels + $post = array(); + } + $post += array( 'channel' => $channel, - 'icon_emoji' => ':lightning_cloud:', + 'icon_emoji' => ':pantheon:', 'attachments' => array($attachment) ); if ($alwaysShowText) { From c475bd192158c824319d51e10972f743fb951449 Mon Sep 17 00:00:00 2001 From: Jordan Ryan Date: Thu, 24 Nov 2016 11:55:09 -0800 Subject: [PATCH 2/4] You need at least a username and a channel in order for the slack_notifications to work. --- slack_notification/slack_notification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slack_notification/slack_notification.php b/slack_notification/slack_notification.php index da44430..3eff904 100644 --- a/slack_notification/slack_notification.php +++ b/slack_notification/slack_notification.php @@ -13,7 +13,7 @@ // Load our hidden credentials. // See the README.md for instructions on storing secrets. -$secrets = _get_secrets(array('slack_url'), $defaults); +$secrets = _get_secrets(array('slack_url','slack_channel'), $defaults); // Build an array of fields to be rendered with Slack Attachments as a table // attachment-style formatting: From 1f695020a52d2539063542283a99dc4500547765 Mon Sep 17 00:00:00 2001 From: Jordan Ryan Date: Mon, 28 Nov 2016 22:41:57 -0800 Subject: [PATCH 3/4] Reverting errant emoji --- slack_notification/slack_notification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slack_notification/slack_notification.php b/slack_notification/slack_notification.php index 3eff904..db28e73 100644 --- a/slack_notification/slack_notification.php +++ b/slack_notification/slack_notification.php @@ -182,7 +182,7 @@ function _slack_notification($slack_url, $channel, $username, $text, $attachment } $post += array( 'channel' => $channel, - 'icon_emoji' => ':pantheon:', + 'icon_emoji' => ':lightning_cloud:', 'attachments' => array($attachment) ); if ($alwaysShowText) { From 43b083d4746d102e1f715eb868d195f7c152e88e Mon Sep 17 00:00:00 2001 From: Jordan Ryan Date: Mon, 28 Nov 2016 22:44:40 -0800 Subject: [PATCH 4/4] =?UTF-8?q?Don=E2=80=99t=20require=20the=20slack=5Fcha?= =?UTF-8?q?nnel=20in=20the=20secrets=20so=20that=20users=20can=20default?= =?UTF-8?q?=20the=20webhook=20to=20the=20config=20set=20in=20Slack=20Admin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- slack_notification/slack_notification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slack_notification/slack_notification.php b/slack_notification/slack_notification.php index db28e73..0ee2242 100644 --- a/slack_notification/slack_notification.php +++ b/slack_notification/slack_notification.php @@ -13,7 +13,7 @@ // Load our hidden credentials. // See the README.md for instructions on storing secrets. -$secrets = _get_secrets(array('slack_url','slack_channel'), $defaults); +$secrets = _get_secrets(array('slack_url'), $defaults); // Build an array of fields to be rendered with Slack Attachments as a table // attachment-style formatting: