Skip to content

Commit

Permalink
Merge pull request #2 from alejdg/slack-reminder
Browse files Browse the repository at this point in the history
Slack reminder
  • Loading branch information
alejdg authored Aug 26, 2016
2 parents 7793c7c + ec1ce74 commit aa10f4d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source "https://rubygems.org"
gem "net-ssh"
gem 'slack-api'

gemspec
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

[![Gem Version](https://badge.fury.io/rb/lita-capistrano.png)](http://badge.fury.io/rb/lita-capistrano)

**lita-capistrano** is a handler for [Lita](https://www.lita.io/) that allows you to use make deploys through your robot.
**lita-capistrano** is a handler for [Lita](https://www.lita.io/) that allows you to make deploys through your robot.

## Features

* Deploy and rollback.

* Permission groups and allowed rooms for deploy based on apps.

* Set reminders to deploy to environments after a sucessful deploy.

## Requirements

In order to **lita-capistrano** to identify a good deploy from a failed one, you capistrano script should always end with a message.
In order to **lita-capistrano** to identify a good deploy from a failed one, your capistrano script should always end with a message.

## Installation

Expand All @@ -28,13 +36,19 @@ gem "lita-capistrano"

* `deploy_tree` (String) – A json configuration of how deploys work.

### Optional attributes

* `slack_api_token` (String) – The slack api token. Only necessary if using Slack reminders

### Example

``` ruby
Lita.configure do |config|
config.handlers.capistrano.server = "capistrano-deploy.com"
config.handlers.capistrano.server_user = "lita"
config.handlers.capistrano.server_password = "secret"
config.handlers.capistrano.slack_api_token = "super-long-token-for-slack-api" # not required, if not using Slack reminders

end

config.handlers.capistrano.deploy_tree = {
Expand Down Expand Up @@ -66,7 +80,12 @@ config.handlers.capistrano.deploy_tree = {
envs: [
"dc1",
"dc2"
]
],
reminders: {
dc1: {
dc2: "2 hours" # Set a reminder to deploy to dc2 after 2 hours from the dc1 deploy success.
}
}
}
}
```
Expand Down
51 changes: 45 additions & 6 deletions lib/lita/handlers/capistrano.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'net/ssh'
require 'slack'

module Lita
module Handlers
Expand All @@ -8,11 +9,13 @@ class Capistrano < Handler
config :server_user, type: String, required: true
config :server_password, type: String, required: true
config :deploy_tree, type: Hash, required: true
config :slack_api_token, type: String, required: false

on :loaded, :define_routes

on :deploy_checked, :deploy_exec
on :deploy_aborted, :deploy_abort
on :deploy_finished, :remind_next_deploy

def define_routes(payload)
define_static_routes
Expand Down Expand Up @@ -55,10 +58,10 @@ def deploy_request(response)
return response.reply("Deploy da app #{app} #{area} permitido somente no canal ##{allowed_channel}")
end

unless area_exists?(area)
unless area_exists?(app, area)
return response.reply("A área informada é inválida.")
end
unless env_exists?(area, env)
unless env_exists?(app, area, env)
return response.reply("O ambiente informado é inválido.")
end

Expand All @@ -67,12 +70,12 @@ def deploy_request(response)
deploy_in_progress?(app, area, env, tag, response)
end

def area_exists?(area)
config.deploy_tree[:commerce].include?(area.to_sym)
def area_exists?(app, area)
config.deploy_tree[app.to_sym].include?(area.to_sym)
end

def env_exists?(area, env)
config.deploy_tree[:commerce][area.to_sym][:envs].include?(env)
def env_exists?(app, area, env)
config.deploy_tree[app.to_sym][area.to_sym][:envs].include?(env)
end

def get_app_tree(config_tree)
Expand Down Expand Up @@ -218,6 +221,23 @@ def deploy_exec(payload)
end
end

def remind_next_deploy(payload)
deploy_status = payload[:status]
if slack_api_configured? && deploy_status == "success"
app = payload[:app]
area = payload[:area]
env = payload[:env]
tag = payload[:tag]
responsible = payload[:responsible]
reminders = get_reminders(app, area, env)
if reminders
reminders.each do |env, time|
set_deploy_reminder(responsible, app, area, env, tag, time)
end
end
end
end

private

def define_static_routes
Expand Down Expand Up @@ -317,6 +337,25 @@ def allowed_room?(room_id, allowed_channel)
return true if room.metadata["name"] == allowed_channel
end

def get_reminders(app, area, env)
config.deploy_tree[app.to_sym][area.to_sym][:reminders][env.to_sym]
rescue
false
end

def set_deploy_reminder(target_user, app, area, env, tag, time)
slack = Slack::API.new(token: config.slack_api_token)
target = Lita::User.find_by_mention_name(target_user).id
todo_action = "Fazer deploy da app #{app} #{env} tag:#{tag}"
slack.reminders_add(text: todo_action, time: time, user: target)
end

def slack_api_configured?
config.slack_api_token
rescue
config.slack_api_token ||= false
end

end

Lita.register_handler(Capistrano)
Expand Down

0 comments on commit aa10f4d

Please sign in to comment.