Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretext per Workflow Case, Auto-Check for Private Channel, and Bug Fix #101

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions slack_notification/slack_notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand All @@ -68,6 +72,7 @@
'value' => $text,
'short' => 'false'
);
$attachment += array('pretext' => 'Deployment complete :rocket:');
break;

case 'sync_code':
Expand All @@ -84,32 +89,55 @@
// 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 .= ' <https://dashboard.pantheon.io/sites/'. PANTHEON_SITE .'#'. PANTHEON_ENVIRONMENT .'/deploys|View Dashboard>';
$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 .= ' <https://dashboard.pantheon.io/sites/'. PANTHEON_SITE .'#'. PANTHEON_ENVIRONMENT .'/deploys|View Dashboard>';
$attachment += array('pretext' => 'Caches cleared');
break;

default:
$text = $_POST['qs_description'];
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']);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshkoenig - You'll see here that someone has already configured the _slack_notification to look for slack_channel in the secrets. Perhaps you want to check git blame here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at:

jordan8037310@c475bd1

We removed the channel from the code intentionally because if left blank the default channel the webhook is configured to post into should be used, which is what developers were expecting. Unless something's changed, you don't need to specify a channel in order for it to work.

27a3251

However, we clearly missed taking it end-to-end, hence the confusion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned elsewhere, I used secrets for all sorts of configuration early on. This is probably just one place that was missed when we took the non-secret configuration information out of secrets.json in all of the examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think that even if it wasn't reading from the secrets file, I would probably just rework into a local config.json file that I would store parallel to the /private/scripts.

Much easier for us to keep scripts unified across all our clients with consistent inputs & outputs that way. This is especially attractive to us as we work with multiple agencies at times and will be doing some work to make slack notifications loop through multiple slack configs and channels.

Either way if you want to take the quicksilver-examples in a different direction that is cool, I just wanted to contribute where I saw this discrepancy!



Expand Down Expand Up @@ -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:',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is (yet) a standard emoji. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! We should probably convert this to a full url to a 128x128 Pantheon Emoji. Slack accepts full image urls for this field.

'attachments' => array($attachment)
);
if ($alwaysShowText) {
Expand Down