Skip to content

Commit

Permalink
Merge pull request #4 from marcosgz/master
Browse files Browse the repository at this point in the history
Mina 1.x compatibility
  • Loading branch information
TAKAyukiatkwsk authored Jan 19, 2019
2 parents 8c1d7dc + de79dea commit be691ba
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 78 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in mina-slack.gemspec
Expand Down
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Add this line to your application's Gemfile:

gem 'mina-slack'
gem 'mina-slack', require: false

And then execute:

Expand All @@ -16,15 +16,37 @@ Or install it yourself as:

$ gem install mina-slack

mina 0.3.x:

gem 'mina-slack', '~> 0.3.0', require: false

In your slack settings, create new Incomming WebHooks and get WebHooks URL.

## Usage

### Load the recipe
Include the recipe in your deploy.rb

# config/deploy.rb
require 'mina/slack'
```ruby
# config/deploy.rb
require 'mina/slack'

task :deploy do
deploy do
invoke :'slack:starting'
...

on :launch do
...
end
end

run(:local) do
invoke :'slack:finished'
end
end
```


### Setup Slack Details
You'll need to setup your slack details with an API key, room and subdomain. You can add these as ENV variables or in the config/deploy.rb
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
require "bundler/gem_tasks"
# frozen_string_literal: true

require 'bundler/gem_tasks'
8 changes: 8 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'mina/slack'
require 'irb'

IRB.start
11 changes: 2 additions & 9 deletions lib/mina/slack.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
require "mina/slack/version"
# frozen_string_literal: true

require 'mina/slack/defaults'
require 'mina/slack/tasks'


module Mina
module Slack
end
end
load File.expand_path('./slack/tasks.rb', __dir__)
12 changes: 0 additions & 12 deletions lib/mina/slack/defaults.rb

This file was deleted.

69 changes: 33 additions & 36 deletions lib/mina/slack/tasks.rb
Original file line number Diff line number Diff line change
@@ -1,79 +1,76 @@
require 'mina/hooks'
# frozen_string_literal: true

require 'json'
require 'net/http'

# Before and after hooks for mina deploy
before_mina :deploy, :'slack:starting'
after_mina :deploy, :'slack:finished'

require 'mina'

# Slack tasks
namespace :slack do
# Required
set :slack_token, -> { ENV['SLACK_TOKEN'] }
set :slack_room, -> { ENV['SLACK_ROOM'] }
set :slack_subdomain, -> { ENV['SLACK_SUBDOMAIN'] }
# Optional
set :slack_stage, -> { ENV['SLACK_STAGE'] || fetch(:rails_env, 'production') }
set :slack_application, -> { ENV['SLACK_APPLICATION'] || fetch(:application_name) }
set :slack_username, -> { ENV['SLACK_USERNAME'] || 'deploybot' }
set :slack_emoji, -> { ENV['SLACK_EMOJI'] || ':cloud:' }
# Git
set :deployer, -> { ENV['GIT_AUTHOR_NAME'] || `git config user.name`.chomp }
set :deployed_revision, -> { ENV['GIT_COMMIT'] || `git rev-parse #{fetch(:branch)} | cut -c 1-7`.strip }

task :starting do
if slack_token and slack_room and slack_subdomain
announcement = "#{announced_deployer} is deploying #{announced_application_name} to #{announced_stage}"
if fetch(:slack_token) && fetch(:slack_room) && fetch(:slack_subdomain)
announcement = "#{fetch(:deployer)} is deploying #{announced_application_name} to #{fetch(:slack_stage)}"

post_slack_message(announcement)
set(:start_time, Time.now)
else
print_local_status "Unable to create Slack Announcement, no slack details provided."
print_error 'Unable to create Slack Announcement, no slack details provided.'
end
end

task :finished do
if slack_token and slack_room and slack_subdomain
if fetch(:slack_token) && fetch(:slack_room) && fetch(:slack_subdomain)
end_time = Time.now
start_time = fetch(:start_time)
elapsed = end_time.to_i - start_time.to_i

announcement = "#{announced_deployer} successfully deployed #{announced_application_name} in #{elapsed} seconds."
announcement = "#{fetch(:deployer)} successfully deployed #{announced_application_name} in #{elapsed} seconds."

post_slack_message(announcement)
else
print_local_status "Unable to create Slack Announcement, no slack details provided."
print_error 'Unable to create Slack Announcement, no slack details provided.'
end
end


def announced_stage
ENV['to'] || rails_env || 'production'
end

def announced_deployer
deployer
end

def short_revision
deployed_revision[0..7] if deployed_revision
end

def announced_application_name
"".tap do |output|
output << slack_application if slack_application
output << " #{branch}" if branch
output << " (#{short_revision})" if short_revision
(+'').tap do |output|
output << fetch(:slack_application)
output << " #{fetch(:branch)}" if fetch(:branch)
output << " (#{fetch(:deployed_revision)})" if fetch(:deployed_revision)
end
end

def post_slack_message(message)
return if fetch(:simulate)
# Parse the URI and handle the https connection
uri = URI.parse("https://#{slack_subdomain}.slack.com/services/hooks/incoming-webhook?token=#{slack_token}")
uri = URI.parse("https://#{fetch(:slack_subdomain)}.slack.com/services/hooks/incoming-webhook?token=#{fetch(:slack_token)}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

payload = {
"parse" => "full",
"channel" => slack_room,
"username" => slack_username,
"text" => message,
"icon_emoji" => slack_emoji
'parse' => 'full',
'channel' => fetch(:slack_room),
'username' => fetch(:slack_username),
'text' => message,
'icon_emoji' => fetch(:slack_emoji)
}

# Create the post request and setup the form data
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(:payload => payload.to_json)
request.set_form_data(payload: payload.to_json)

# Make the actual request to the API
http.request(request)
Expand Down
4 changes: 3 additions & 1 deletion lib/mina/slack/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module Mina
module Slack
VERSION = "0.1.0"
VERSION = '1.0.0'
end
end
32 changes: 16 additions & 16 deletions mina-slack.gemspec
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mina/slack/version'

Gem::Specification.new do |spec|
spec.name = "mina-slack"
spec.version = Mina::Slack::VERSION
spec.authors = ["TAKAyuki_atkwsk", "Marcos G. Zimmermann"]
spec.email = ["[email protected]", "[email protected]"]
spec.description = %q{Slack web hook from mina}
spec.summary = %q{Slack web hook from mina}
spec.homepage = "https://github.com/TAKAyukiatkwsk/mina-slack"
spec.license = "MIT"
spec.name = 'mina-slack'
spec.version = Mina::Slack::VERSION
spec.authors = ['TAKAyuki_atkwsk', 'Marcos G. Zimmermann']
spec.email = ['[email protected]', '[email protected]']
spec.description = 'Slack web hook from mina'
spec.summary = 'Slack web hook from mina'
spec.homepage = 'https://github.com/TAKAyukiatkwsk/mina-slack'
spec.license = 'MIT'

spec.files = `git ls-files`.split($/)
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake'

spec.add_dependency "mina"
spec.add_dependency "mina-hooks"
spec.add_dependency 'mina', '>= 1.0'
end

0 comments on commit be691ba

Please sign in to comment.