Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiamalonni committed Jul 11, 2019
0 parents commit 6741f0d
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Gemfile.lock
pkg
tmp
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in capistrano-telegram.gemspec
gemspec

gem 'pry'
gem 'awesome_print'
gem 'telegram-bot-ruby'
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Mattia Malonni

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Capistrano - Telegram Notification

Send notifications to Telegram about Capistrano deployments.

## Installation

1. Add this line to your application's Gemfile:

```ruby
gem 'capistrano-telegram'
```

2. Execute:

```
$ bundle
```

3. Require the library in your application's Capfile:

```ruby
require 'capistrano/telegram'
```

### config/deploy.rb

```ruby
set :telegram_bot_key, 'YOUR_BOT_ID'
set :telegram_chat_id, 'YOUR_BOT_CHAT_ID'
```
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
24 changes: 24 additions & 0 deletions capistrano-telegram.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'capistrano/telegram/version'

Gem::Specification.new do |spec|
spec.name = "capistrano-telegram"
spec.version = Capistrano::TelegramNotification::VERSION
spec.authors = ["Mattia Malonni"]
spec.email = ["[email protected]"]
spec.summary = %q{Notify Capistrano deployment to Telegram.}
spec.description = %q{Notify Capistrano deployment to Telegram.}
spec.homepage = "https://github.com/MattiaMalonni/capistrano-telegram"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
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.add_dependency "telegram-bot-ruby", "~> 0.8"
spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
end
Empty file added lib/capistrano-telegram.rb
Empty file.
41 changes: 41 additions & 0 deletions lib/capistrano/tasks/telegram.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace :telegram do
namespace :deploy do

desc 'Notify about updating deploy'
task :updating do
Capistrano::Telegram.new(self).send(:updating)
end

desc 'Notify about reverting deploy'
task :reverting do
Capistrano::Telegram.new(self).send(:reverting)
end

desc 'Notify about updated deploy'
task :updated do
Capistrano::Telegram.new(self).send(:updated)
end

desc 'Notify about reverted deploy'
task :reverted do
Capistrano::Telegram.new(self).send(:reverted)
end

desc 'Notify about failed deploy'
task :failed do
Capistrano::Telegram.new(self).send(:failed)
end

desc 'Test Slack integration'
task :test => %i[updating updated reverting reverted failed] do
# all tasks run as dependencies
end

end
end

before 'deploy:updating', 'telegram:deploy:updating'
before 'deploy:reverting', 'telegram:deploy:reverting'
after 'deploy:finishing', 'telegram:deploy:updated'
after 'deploy:finishing_rollback', 'telegram:deploy:reverted'
after 'deploy:failed', 'telegram:deploy:failed'
29 changes: 29 additions & 0 deletions lib/capistrano/telegram.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative 'messaging/telegram/messaging/base'

require 'telegram/bot'

load File.expand_path('../tasks/telegram.rake', __FILE__)

module Capistrano
class Telegram

def initialize(env)
@telegram_bot_key = fetch(:telegram_bot_key, nil)
@telegram_chat_id = fetch(:telegram_chat_id, nil)
@message = Capistrano::Telegram::Messaging::Base.new
end

def send(action)
payload = @message.payload_for(action)
send_to_telegram(payload)
end

private
def send_to_telegram(message)
Telegram::Bot::Client.run(@telegram_bot_key) do |bot|
bot.api.send_message(chat_id: @telegram_chat_id, text: message)
end
end

end
end
47 changes: 47 additions & 0 deletions lib/capistrano/telegram/messaging/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_relative 'helpers'

module Capistrano
module Telegram
module Messaging
class Base
include Helpers

def payload_for_updating
{
text: "#{deployer} has started deploying branch #{branch} of #{application} to #{stage}"
}
end

def payload_for_reverting
{
text: "#{deployer} has started rolling back branch #{branch} of #{application} to #{stage}"
}
end

def payload_for_updated
{
text: "#{deployer} has finished deploying branch #{branch} of #{application} to #{stage}"
}
end

def payload_for_reverted
{
text: "#{deployer} has finished rolling back branch of #{application} to #{stage}"
}
end

def payload_for_failed
{
text: "#{deployer} has failed to #{deploying? ? 'deploy' : 'rollback'} branch #{branch} of #{application} to #{stage}"
}
end

def payload_for(action)
method = "payload_for_#{action}"
respond_to?(method) && send(method)
end

end
end
end
end
38 changes: 38 additions & 0 deletions lib/capistrano/telegram/messaging/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Capistrano
module Telegram
module Messaging
module Helpers
def icon_emoji
options.fetch(:icon_emoji, nil)
end

def deployer
default = ENV['USER'] || ENV['USERNAME']
fetch(:local_user, default)
end

def branch
fetch(:branch)
end

def application
fetch(:application)
end

def stage(default = 'an unknown stage')
fetch(:stage, default)
end

#
# Return the elapsed running time as a string.
#
# Examples: 21-18:26:30, 15:28:37, 01:14
#
def elapsed_time
`ps -p #{$$} -o etime=`.strip
end

end
end
end
end
5 changes: 5 additions & 0 deletions lib/capistrano/telegram/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Capistrano
module Telegram
VERSION = '0.1.1'
end
end
Binary file added misc/capistrano-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6741f0d

Please sign in to comment.