|
4 | 4 | var Promise = require('ember-cli/lib/Ext/promise');
|
5 | 5 | var SilentError = require('ember-cli/lib/errors/silent');
|
6 | 6 | var SlackNotifier = require('./lib/slack-notifier');
|
7 |
| -var defaults = require('./lib/default-config'); |
8 |
| -var chalk = require('chalk'); |
9 |
| - |
10 |
| -var blue = chalk.blue; |
11 |
| -var yellow = chalk.yellow; |
12 |
| - |
13 |
| -function applyDefaultConfigIfNecessary(config, prop, defaultConfig, ui){ |
14 |
| - if (!config[prop]) { |
15 |
| - var value = defaultConfig[prop] || function() { return; }; |
16 |
| - config[prop] = value; |
17 |
| - if (defaultConfig[prop]) { |
18 |
| - ui.write(blue('| ')); |
19 |
| - ui.writeLine(yellow('- Missing config: `' + prop + '`, using default: `' + value + '`')); |
20 |
| - } |
21 |
| - } |
22 |
| -} |
23 |
| - |
24 |
| -function initSlackNotifier(config, context) { |
25 |
| - var webhookURL = config.webhookURL; |
26 |
| - |
27 |
| - if (!webhookURL) { |
28 |
| - var message = 'Ember-CLI-Deploy: You have to pass a `webhookURL` config-option to ember-cli-deploy-slack'; |
29 |
| - context.slack = context.slack || {}; |
30 |
| - |
31 |
| - context.slack.error = message; |
32 |
| - |
33 |
| - throw new SilentError(message); |
34 |
| - } |
35 |
| - |
36 |
| - var channel = config.channel; |
37 |
| - var username = config.username; |
| 7 | +var DeployPluginBase = require('ember-cli-deploy-plugin'); |
38 | 8 |
|
39 |
| - return new SlackNotifier({ |
40 |
| - webhookURL: webhookURL, |
41 |
| - channel: channel, |
42 |
| - username: username |
43 |
| - }); |
44 |
| -} |
45 |
| - |
46 |
| -function executeSlackNotificationHook(context, hookName) { |
47 |
| - var deployment = context.deployment; |
48 |
| - var ui = deployment.ui; |
49 |
| - var config = deployment.config[this.name] = deployment.config[this.name] || {}; |
50 |
| - var slack = initSlackNotifier(config, context); |
51 |
| - |
52 |
| - applyDefaultConfigIfNecessary(config, hookName, defaults, ui); |
53 |
| - |
54 |
| - return config[hookName](context, slack); |
55 |
| -} |
| 9 | +var moment = require('moment'); |
| 10 | +require('moment-duration-format'); |
56 | 11 |
|
57 | 12 | module.exports = {
|
58 | 13 | name: 'ember-cli-deploy-slack',
|
59 | 14 |
|
60 | 15 | createDeployPlugin: function(options) {
|
61 |
| - return { |
| 16 | + var DeployPlugin = DeployPluginBase.extend({ |
62 | 17 | name: options.name,
|
| 18 | + requiredConfig: ['webhookURL'], |
| 19 | + defaultConfig: { |
| 20 | + willDeploy: function(context) { |
| 21 | + return function(slack){ |
| 22 | + return { |
| 23 | + slackStartDeployDate: new Date() |
| 24 | + }; |
| 25 | + } |
| 26 | + }, |
| 27 | + |
| 28 | + didDeploy: function(context) { |
| 29 | + return function(slack){ |
| 30 | + var startDeployDate = this.context.slackStartDeployDate; |
| 31 | + var endDeployDate = new Date(); |
| 32 | + var duration = moment.duration(endDeployDate - startDeployDate); |
| 33 | + |
| 34 | + debugger; |
| 35 | + return slack.notify({ |
| 36 | + attachments: [{ |
| 37 | + "fallback":"Deployment finished! New revision was successfully uploaded.", |
| 38 | + "pretext":"Deployment finished! New revision was successfully uploaded.", |
| 39 | + "color":"good", |
| 40 | + "fields":[ |
| 41 | + { |
| 42 | + "title":"Stats", |
| 43 | + "value":"Deploying revision took "+duration.format('m [min], s [s], S [ms]')+'.', |
| 44 | + "short":false |
| 45 | + } |
| 46 | + ] |
| 47 | + }] |
| 48 | + }); |
| 49 | + }; |
| 50 | + }, |
| 51 | + |
| 52 | + didFail: function(slack) { |
| 53 | + return function(context){ |
| 54 | + var message = "Ember-cli-deploy tried to deploy a revision but failed."; |
| 55 | + |
| 56 | + return slack.notify({ |
| 57 | + attachments: [{ |
| 58 | + "fallback": "Deployment failed!", |
| 59 | + "pretext": "Deployment failed!", |
| 60 | + "color": "danger", |
| 61 | + "fields":[ |
| 62 | + { |
| 63 | + "title": "Failure", |
| 64 | + "value": message, |
| 65 | + "short": false |
| 66 | + } |
| 67 | + ] |
| 68 | + }] |
| 69 | + }); |
| 70 | + }; |
| 71 | + } |
| 72 | + }, |
63 | 73 |
|
64 |
| - willDeploy: function(context) { |
65 |
| - return executeSlackNotificationHook.bind(this)(context, 'willDeploy'); |
| 74 | + willDeploy: function(/* context */) { |
| 75 | + return this._executeSlackNotificationHook('willDeploy'); |
66 | 76 | },
|
67 | 77 |
|
68 |
| - willBuild: function(context) { |
69 |
| - return executeSlackNotificationHook.bind(this)(context, 'willBuild'); |
| 78 | + willBuild: function(/* context */) { |
| 79 | + return this._executeSlackNotificationHook('willBuild'); |
70 | 80 | },
|
71 | 81 |
|
72 |
| - build: function(context) { |
73 |
| - return executeSlackNotificationHook.bind(this)(context, 'build'); |
| 82 | + build: function(/* context */) { |
| 83 | + return this._executeSlackNotificationHook('build'); |
74 | 84 | },
|
75 | 85 |
|
76 |
| - didBuild: function(context) { |
77 |
| - return executeSlackNotificationHook.bind(this)(context, 'didBuild'); |
| 86 | + didBuild: function(/* context */) { |
| 87 | + return this._executeSlackNotificationHook('didBuild'); |
78 | 88 | },
|
79 | 89 |
|
80 |
| - willUpload: function(context) { |
81 |
| - return executeSlackNotificationHook.bind(this)(context, 'willUpload'); |
| 90 | + willUpload: function(/* context */) { |
| 91 | + return this._executeSlackNotificationHook('willUpload'); |
82 | 92 | },
|
83 | 93 |
|
84 |
| - upload: function(context) { |
85 |
| - return executeSlackNotificationHook.bind(this)(context, 'upload'); |
| 94 | + upload: function(/* context */) { |
| 95 | + return this._executeSlackNotificationHook('upload'); |
86 | 96 | },
|
87 | 97 |
|
88 |
| - didUpload: function(context) { |
89 |
| - return executeSlackNotificationHook.bind(this)(context, 'didUpload'); |
| 98 | + didUpload: function(/* context */) { |
| 99 | + return this._executeSlackNotificationHook('didUpload'); |
90 | 100 | },
|
91 | 101 |
|
92 |
| - willActivate: function(context) { |
93 |
| - return executeSlackNotificationHook.bind(this)(context, 'willActivate'); |
| 102 | + willActivate: function(/* context */) { |
| 103 | + return this._executeSlackNotificationHook('willActivate'); |
94 | 104 | },
|
95 | 105 |
|
96 |
| - activate: function(context) { |
97 |
| - return executeSlackNotificationHook.bind(this)(context, 'activate'); |
| 106 | + activate: function(/* context */) { |
| 107 | + return this._executeSlackNotificationHook('activate'); |
98 | 108 | },
|
99 | 109 |
|
100 |
| - didActivate: function(context) { |
101 |
| - return executeSlackNotificationHook.bind(this)(context, 'didActivate'); |
| 110 | + didActivate: function(/* context */) { |
| 111 | + return this._executeSlackNotificationHook('didActivate'); |
102 | 112 | },
|
103 | 113 |
|
104 |
| - didDeploy: function(context) { |
105 |
| - return executeSlackNotificationHook.bind(this)(context, 'didDeploy'); |
| 114 | + didDeploy: function(/* context */) { |
| 115 | + return this._executeSlackNotificationHook('didDeploy'); |
106 | 116 | },
|
107 | 117 |
|
108 |
| - didFail: function(context) { |
109 |
| - return executeSlackNotificationHook.bind(this)(context, 'didFail'); |
| 118 | + didFail: function(/* context */) { |
| 119 | + return this._executeSlackNotificationHook('didFail'); |
| 120 | + }, |
| 121 | + _executeSlackNotificationHook: function(hookName) { |
| 122 | + var slack = this._initSlackNotifier(); |
| 123 | + var slackHook = this.readConfig(hookName); |
| 124 | + if (slackHook) { |
| 125 | + return slackHook.call(this, slack); |
| 126 | + } |
| 127 | + }, |
| 128 | + _initSlackNotifier: function() { |
| 129 | + var webhookURL = this.readConfig('webhookURL'); |
| 130 | + |
| 131 | + if (!webhookURL) { |
| 132 | + var message = 'Ember-CLI-Deploy: You have to pass a `webhookURL` config-option to ember-cli-deploy-slack'; |
| 133 | + throw new Error(message); |
| 134 | + } |
| 135 | + |
| 136 | + var channel = this.readConfig('channel'); |
| 137 | + var username = this.readConfig('username'); |
| 138 | + |
| 139 | + return this.readConfig('slackNotifier') || new SlackNotifier({ |
| 140 | + webhookURL: webhookURL, |
| 141 | + channel: channel, |
| 142 | + username: username |
| 143 | + }); |
110 | 144 | }
|
111 |
| - } |
| 145 | + }); |
| 146 | + return new DeployPlugin(); |
112 | 147 | }
|
113 | 148 | };
|
0 commit comments