Skip to content

Commit

Permalink
Refactor shared base class, spec cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
justincampbell committed Apr 30, 2012
1 parent 5fffc51 commit 5142d1a
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 135 deletions.
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end

1 change: 0 additions & 1 deletion capistrano-notifier.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Gem::Specification.new do |gem|
gem.add_dependency 'actionmailer'
gem.add_dependency 'activesupport'
gem.add_dependency 'capistrano', '>= 2'
gem.add_dependency 'ruby-git'

gem.add_development_dependency 'guard-rspec'
gem.add_development_dependency 'rspec'
Expand Down
5 changes: 5 additions & 0 deletions lib/capistrano/notifier.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module Capistrano
module Notifier
end
end

require 'active_support'
require 'capistrano'
require 'yaml'
Expand Down
96 changes: 46 additions & 50 deletions lib/capistrano/notifier/base.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,49 @@
module Capistrano
module Notifier
class Base
def initialize(capistrano)
@cap = capistrano
end

private

def application
cap.application.titleize
end

def branch
cap.branch
end

def cap
@cap
end

def current_revision
cap.current_revision[0,7]
end

def git_log
`git log #{git_range} --no-merges --format=format:"%h %s (%an)"`
end

def git_range
"#{previous_revision}..#{current_revision}"
end

def previous_revision
cap.previous_revision[0,7]
end

def now
@now ||= Time.now
end

def stage
cap.stage
end

def user
user = ENV['DEPLOYER']
user = `git config --get user.name`.strip if user.nil?
end
end
class Capistrano::Notifier::Base
def initialize(capistrano)
@cap = capistrano
end

private

def application
cap.application.titleize
end

def branch
cap.branch
end

def cap
@cap
end

def git_current_revision
cap.current_revision[0,7]
end

def git_log
`git log #{git_range} --no-merges --format=format:"%h %s (%an)"`
end

def git_previous_revision
cap.previous_revision[0,7]
end

def git_range
"#{git_previous_revision}..#{git_current_revision}"
end

def now
@now ||= Time.now
end

def stage
cap.stage
end

def user_name
user = ENV['DEPLOYER']
user = `git config --get user.name`.strip if user.nil?
end
end

Expand Down
152 changes: 74 additions & 78 deletions lib/capistrano/notifier/mail.rb
Original file line number Diff line number Diff line change
@@ -1,82 +1,78 @@
require 'action_mailer'

module Capistrano
module Notifier
class Mail < Base
def perform
mail = ActionMailer::Base.mail({
:body => text,
:delivery_method => notify_method,
:from => from,
:subject => subject,
:to => to
})

mail.deliver

puts ActionMailer::Base.deliveries if notify_method == :test
end

private

def body
<<-BODY.gsub(/^ {10}/, '')
#{user} deployed
#{application} branch
#{branch} to
#{stage} on
#{now.strftime("%m/%d/%Y")} at
#{now.strftime("%I:%M %p %Z")}
#{git_range}
#{git_log}
BODY
end

def from
cap.notifier_mail_options[:from]
end

def github_commit_prefix
"#{github_prefix}/commit"
end

def github_compare_prefix
"#{github_prefix}/compare"
end

def github_prefix
"https://github.com/#{github}"
end

def github
cap.notifier_mail_options[:github]
end

def html
body.gsub(
/([0-9a-f]{7})\.\.([0-9a-f]{7})/, "<a href=\"#{github_compare_prefix}/\\1...\\2\">\\1..\\2</a>"
).gsub(
/^([0-9a-f]{7})/, "<a href=\"#{github_commit_prefix}/\\0\">\\0</a>"
)
end

def notify_method
cap.notifier_mail_options[:method]
end


def subject
"#{user} deployed #{application}@#{branch} to #{stage}"
end

def text
body.gsub(/([0-9a-f]{7})\.\.([0-9a-f]{7})/, "#{github_compare_prefix}/\\1...\\2")
end

def to
cap.notifier_mail_options[:to]
end
end
class Capistrano::Notifier::Mail < Capistrano::Notifier::Base
def perform
mail = ActionMailer::Base.mail({
:body => text,
:delivery_method => notify_method,
:from => from,
:subject => subject,
:to => to
})

mail.deliver

puts ActionMailer::Base.deliveries if notify_method == :test
end

private

def body
<<-BODY.gsub(/^ {6}/, '')
#{user_name} deployed
#{application} branch
#{branch} to
#{stage} on
#{now.strftime("%m/%d/%Y")} at
#{now.strftime("%I:%M %p %Z")}
#{git_range}
#{git_log}
BODY
end

def from
cap.notifier_mail_options[:from]
end

def github_commit_prefix
"#{github_prefix}/commit"
end

def github_compare_prefix
"#{github_prefix}/compare"
end

def github_prefix
"https://github.com/#{github}"
end

def github
cap.notifier_mail_options[:github]
end

def html
body.gsub(
/([0-9a-f]{7})\.\.([0-9a-f]{7})/, "<a href=\"#{github_compare_prefix}/\\1...\\2\">\\1..\\2</a>"
).gsub(
/^([0-9a-f]{7})/, "<a href=\"#{github_commit_prefix}/\\0\">\\0</a>"
)
end

def notify_method
cap.notifier_mail_options[:method]
end


def subject
"#{application} branch #{branch} deployed to #{stage}"
end

def text
body.gsub(/([0-9a-f]{7})\.\.([0-9a-f]{7})/, "#{github_compare_prefix}/\\1...\\2")
end

def to
cap.notifier_mail_options[:to]
end
end
Empty file.
Empty file.
Empty file.
Empty file.
17 changes: 12 additions & 5 deletions spec/capistrano/notifier/mail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'capistrano/notifier/mail'

describe Capistrano::Notifier::Mail do

let(:configuration) { Capistrano::Configuration.new }
subject { described_class.new configuration }

Expand All @@ -18,10 +17,16 @@
set :application, 'example'
set :branch, 'master'
set :stage, 'test'
set :current_revision, '1234567'
set :previous_revision, '890abcd'

set :current_revision, '12345670000000000000000000000000'
set :previous_revision, '890abcd0000000000000000000000000'
end

subject.stub(:git_log).and_return <<-LOG.gsub /^ {6}/, ''
1234567 This is the current commit (John Doe)
890abcd This is the previous commit (John Doe)
LOG
subject.stub(:user_name).and_return "John Doe"
end

it { subject.send(:github).should == 'example/example' }
Expand All @@ -31,14 +36,16 @@

it "renders a plaintext email" do
subject.send(:body).should == <<-BODY.gsub(/^ {6}/, '')
Justin Campbell deployed
John Doe deployed
Example branch
master to
test on
01/01/2012 at
12:00 AM EST
890abcd..1234567
1234567 This is the current commit (John Doe)
890abcd This is the previous commit (John Doe)
BODY
end
Expand Down

0 comments on commit 5142d1a

Please sign in to comment.