|
| 1 | +<?php |
| 2 | +/* (c) Markus Bachmann <markus.bachmann@bachi.biz> |
| 3 | + * |
| 4 | + * For the full copyright and license information, please view the LICENSE |
| 5 | + * file that was distributed with this source code. |
| 6 | + */ |
| 7 | +namespace Deployer; |
| 8 | + |
| 9 | +use Deployer\Utility\Httpie; |
| 10 | + |
| 11 | +set('rockchat_title', function() { |
| 12 | + return get('application', 'Project'); |
| 13 | +}); |
| 14 | + |
| 15 | +set('rocketchat_icon_emoji', ':robot:'); |
| 16 | +set('rocketchat_icon_url', null); |
| 17 | + |
| 18 | +set('rocketchat_channel', null); |
| 19 | +set('rocketchat_room_id', null); |
| 20 | +set('rocketchat_username', null); |
| 21 | +set('rocketchat_webhook', null); |
| 22 | + |
| 23 | +set('rocketchat_color', '#000000'); |
| 24 | +set('rocketchat_success_color', '#00c100'); |
| 25 | +set('rocketchat_failure_color', '#ff0909'); |
| 26 | + |
| 27 | +set('rocketchat_text', '_{{user}}_ deploying `{{branch}}` to *{{target}}*'); |
| 28 | +set('rocketchat_success_text', 'Deploy to *{{target}}* successful'); |
| 29 | +set('rocketchat_failure_text', 'Deploy to *{{target}}* failed'); |
| 30 | + |
| 31 | +desc('Notify RocketChat'); |
| 32 | +task('rocketchat:notify', function() { |
| 33 | + if (null === get('rocketchat_webhook')) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $body = [ |
| 38 | + 'text' => get('rockchat_title'), |
| 39 | + 'username' => get('rocketchat_username'), |
| 40 | + 'attachments' => [[ |
| 41 | + 'text' => get('rocketchat_text'), |
| 42 | + 'color' => get('rocketchat_color'), |
| 43 | + ]] |
| 44 | + ]; |
| 45 | + |
| 46 | + if (get('rocketchat_channel')) { |
| 47 | + $body['channel'] = get('rocketchat_channel'); |
| 48 | + } |
| 49 | + if (get('rocketchat_room_id')) { |
| 50 | + $body['roomId'] = get('rocketchat_room_id'); |
| 51 | + } |
| 52 | + if (get('rocketchat_icon_url')) { |
| 53 | + $body['avatar'] = get('rocketchat_icon_url'); |
| 54 | + } elseif (get('rocketchat_icon_emoji')) { |
| 55 | + $body['emoji'] = get('rocketchat_icon_emoji'); |
| 56 | + } |
| 57 | + |
| 58 | + Httpie::post(get('rocketchat_webhook'))->body($body)->send(); |
| 59 | +}); |
| 60 | + |
| 61 | +desc('Notifying RocketChat about deploy finish'); |
| 62 | +task('rocketchat:notify:success', function() { |
| 63 | + if (null === get('rocketchat_webhook')) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $body = [ |
| 68 | + 'text' => get('rockchat_title'), |
| 69 | + 'username' => get('rocketchat_username'), |
| 70 | + 'attachments' => [[ |
| 71 | + 'text' => get('rocketchat_success_text'), |
| 72 | + 'color' => get('rocketchat_success_color'), |
| 73 | + ]] |
| 74 | + ]; |
| 75 | + |
| 76 | + if (get('rocketchat_channel')) { |
| 77 | + $body['channel'] = get('rocketchat_channel'); |
| 78 | + } |
| 79 | + if (get('rocketchat_room_id')) { |
| 80 | + $body['roomId'] = get('rocketchat_room_id'); |
| 81 | + } |
| 82 | + if (get('rocketchat_icon_url')) { |
| 83 | + $body['avatar'] = get('rocketchat_icon_url'); |
| 84 | + } elseif (get('rocketchat_icon_emoji')) { |
| 85 | + $body['emoji'] = get('rocketchat_icon_emoji'); |
| 86 | + } |
| 87 | + |
| 88 | + Httpie::post(get('rocketchat_webhook'))->body($body)->send(); |
| 89 | +}); |
| 90 | + |
| 91 | +desc('Notifying RocketChat about deploy failure'); |
| 92 | +task('rocketchat:notify:failure', function() { |
| 93 | + if (null === get('rocketchat_webhook')) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + $body = [ |
| 98 | + 'text' => get('rockchat_title'), |
| 99 | + 'username' => get('rocketchat_username'), |
| 100 | + 'attachments' => [[ |
| 101 | + 'color' => get('rocketchat_failure_color'), |
| 102 | + 'text' => get('rocketchat_failure_text') |
| 103 | + ]] |
| 104 | + ]; |
| 105 | + |
| 106 | + if (get('rocketchat_channel')) { |
| 107 | + $body['channel'] = get('rocketchat_channel'); |
| 108 | + } |
| 109 | + if (get('rocketchat_room_id')) { |
| 110 | + $body['roomId'] = get('rocketchat_room_id'); |
| 111 | + } |
| 112 | + if (get('rocketchat_icon_url')) { |
| 113 | + $body['avatar'] = get('rocketchat_icon_url'); |
| 114 | + } elseif (get('rocketchat_icon_emoji')) { |
| 115 | + $body['emoji'] = get('rocketchat_icon_emoji'); |
| 116 | + } |
| 117 | + |
| 118 | + Httpie::post(get('rocketchat_webhook'))->body($body)->send(); |
| 119 | +}); |
| 120 | + |
0 commit comments