Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
make unsets when other fieldParams are set logic more compact
Browse files Browse the repository at this point in the history
  • Loading branch information
snowiow committed Mar 14, 2017
1 parent 3964147 commit 93b6fb2
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

namespace Deployer;

/**
/*
* Get local username
*/
set('local_user', function () {
return trim(run("whoami"));
return trim(run('whoami'));
});

// Do not skip slack notifications by default
Expand Down Expand Up @@ -76,8 +76,8 @@
'short' => true,
],
],
]
]
],
],
];

$newConfig = get('slack');
Expand All @@ -96,7 +96,7 @@
if ($server instanceof \Deployer\Server\Local) {
$user = get('local_user');
} else {
$user = $server->getConfiguration()->getUser() ? : null;
$user = $server->getConfiguration()->getUser() ?: null;
}

$messagePlaceHolders = [
Expand All @@ -105,7 +105,7 @@
'{{stage}}' => $stage,
'{{user}}' => $user,
'{{branch}}' => $branch,
'{{app_name}}' => isset($config['app']) ? $config['app'] : 'app-name',
'{{app_name}}' => $config['app'] ?? 'app-name',
];
$config['message'] = strtr($config['message'], $messagePlaceHolders);

Expand All @@ -115,11 +115,13 @@
'text' => $config['message'],
'username' => $config['username'],
'icon_emoji' => $config['icon'],
'pretty' => true
'pretty' => true,
];

if ($config['unset_text']) {
unset($urlParams['text']);
foreach (['unset_text' => 'text', 'icon_url' => 'icon_emoji'] as $set => $unset) {

This comment has been minimized.

Copy link
@karakum

karakum Nov 17, 2017

Hi.
This commit broke some logic - it is impossible to disable unsetting text now.
Because $defaultConfig have unset_text key implicitly. It is need to remove this key(from line 45), or modify your next condition :

if (isset($config[$set]) && $config[$set]) {

This comment has been minimized.

Copy link
@antonmedv

antonmedv Nov 18, 2017

Member

Now refactored.

This comment has been minimized.

Copy link
@karakum

karakum Nov 19, 2017

Это здорово! Но как насчет v4?

This comment has been minimized.

Copy link
@antonmedv

antonmedv Nov 20, 2017

Member

Yes, it wasn’t backported to v4. If you can do it, please send PR.

if (isset($config[$set])) {
unset($urlParams[$unset]);
}
}

foreach (['parse', 'link_names', 'icon_url', 'unfurl_links', 'unfurl_media', 'as_user'] as $option) {
Expand All @@ -132,10 +134,6 @@
$urlParams['attachments'] = json_encode($config['attachments']);
}

if (isset($config['icon_url'])) {
unset($urlParams['icon_emoji']);
}

$url = 'https://slack.com/api/chat.postMessage?' . http_build_query($urlParams);
$result = @file_get_contents($url);

Expand Down

0 comments on commit 93b6fb2

Please sign in to comment.