Skip to content

Commit

Permalink
Success email code and DB changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Eversmann committed Jun 24, 2014
1 parent 6ad14b5 commit 56fd48a
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 15 deletions.
15 changes: 12 additions & 3 deletions app/mailers/build_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@ def build_break_email(build)

@failed_build_parts = @build.build_parts.failed_or_errored

email_user, email_domain = Settings.sender_email_address.split('@')

mail :to => @emails,
:bcc => Settings.kochiku_notifications_email_address,
:subject => "[kochiku] #{@build.project.name} build for branch #{@build.branch} failed",
:from => "#{email_user}+#{@build.project.name.parameterize}@#{email_domain}"
:from => Settings.sender_email_address
end

def build_success_email(build)
@build = build
@email = GitBlame.emails_in_branch(@build).first
@git_changes = GitBlame.changes_in_branch(@build)

mail :to => @email,
:bcc => Settings.kochiku_notifications_email_address,
:subject => "[kochiku] #{@build.project.name} build for branch #{@build.branch} passed",
:from => Settings.sender_email_address
end
end
16 changes: 11 additions & 5 deletions app/models/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,17 @@ def branch_or_ref
end

def send_build_status_email!
return if (project.main? && !previous_successful_build) || !repository.send_build_failure_email?

if completed? && failed? && !build_failure_email_sent?
if Build.where(id: self.id, build_failure_email_sent: nil).update_all(build_failure_email_sent: true) == 1
BuildMailer.build_break_email(self).deliver
return if project.main? && !previous_successful_build

if completed?
if failed? && !build_failure_email_sent? && repository.send_build_failure_email?
if Build.where(id: self.id, build_failure_email_sent: false).update_all(build_failure_email_sent: true) == 1
BuildMailer.build_break_email(self).deliver
end
elsif succeeded? && !project.main? && !build_success_email_sent? && repository.send_build_success_email?
if Build.where(id: self.id, build_success_email_sent: false).update_all(build_success_email_sent: true) == 1
BuildMailer.build_success_email(self).deliver
end
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions app/views/build_mailer/build_success_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
</head>
<body>
<h2>Project <%= @build.project.name %> succeeded on branch <%= @build.branch %></h2>
<%= project_build_url(@build.project, @build) %><br>
<h2>Changes included in build</h2>
<% @git_changes.each do |git_change| %>
<b>commit <%= link_to(git_change[:hash], "#{@build.project.repository.remote_server.href_for_commit(git_change[:hash])}") %></b><br>
Author: <%= git_change[:author] %><br>
Date: <%= git_change[:date] %><br>
<br>
<%= git_change[:message] %>
<br><br>
<% end %>
</body>
</html>
16 changes: 16 additions & 0 deletions app/views/build_mailer/build_success_email.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Project <%= @build.project.name %> succeded on branch <%= @build.branch %>
<%= project_build_url(@build.project, @build) %>

--------------------------------------------------------------------------------
Changes included in build
--------------------------------------------------------------------------------

<% @git_changes.each do |git_change| %>
commit <%= git_change[:hash] %>
Author: <%= git_change[:author] %>
Date: <%= git_change[:date] %>

<%= git_change[:message] %>


<% end %>
3 changes: 3 additions & 0 deletions app/views/repositories/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
%div
%label{:for => "on_success_script"} Run:
= f.text_field :on_success_script, {:id => "on_success_script"}
%div
%label{:for => "send_build_success_email"} Send email to contributers:
= f.check_box :send_build_success_email, {:id => "send_build_success_email"}

%fieldset
%legend On a red build
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20140617214701_add_success_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AddSuccessEmail < ActiveRecord::Migration
def change
add_column :builds, :build_success_email_sent, :boolean, :default => false, :null => false
add_column :repositories, :send_build_success_email, :boolean, :default => true, :null => false
reversible do |dir|
change_table :builds do |t|
dir.up { t.change :build_failure_email_sent, :boolean, :default => false, :null => false }
dir.down { t.change :build_failure_email_sent, :boolean, :default => nil, :null => true }
end
change_table :repositories do |t|
dir.up { t.change :send_build_failure_email, :boolean, :default => true, :null => false }
dir.down { t.change :send_build_failure_email, :boolean, :default => true, :null => true }
end
end
end
end
14 changes: 8 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140507184819) do
ActiveRecord::Schema.define(version: 20140617214701) do

create_table "build_artifacts", force: true do |t|
t.integer "build_attempt_id"
Expand Down Expand Up @@ -49,17 +49,18 @@
add_index "build_parts", ["paths"], name: "index_build_parts_on_paths", length: {"paths"=>255}, using: :btree

create_table "builds", force: true do |t|
t.string "ref", limit: 40, null: false
t.string "ref", limit: 40, null: false
t.string "state"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "project_id"
t.boolean "merge_on_success"
t.string "branch"
t.boolean "build_failure_email_sent"
t.boolean "build_failure_email_sent", default: false, null: false
t.boolean "promoted"
t.string "on_success_script_log_file"
t.text "error_details"
t.boolean "build_success_email_sent", default: false, null: false
end

add_index "builds", ["project_id"], name: "index_builds_on_project_id", using: :btree
Expand All @@ -86,14 +87,15 @@
t.boolean "build_pull_requests"
t.string "on_green_update"
t.string "repo_cache_dir"
t.boolean "send_build_failure_email", default: true
t.boolean "send_build_failure_email", default: true, null: false
t.string "on_success_script"
t.integer "timeout", default: 40
t.string "on_success_note"
t.string "name", null: false
t.boolean "allows_kochiku_merges", default: true
t.string "host", null: false
t.string "namespace"
t.boolean "send_build_success_email", default: true, null: false
end

add_index "repositories", ["host", "namespace", "name"], name: "index_repositories_on_host_and_namespace_and_name", unique: true, using: :btree
Expand Down
23 changes: 23 additions & 0 deletions spec/mailers/build_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,27 @@
end
end
end

describe '#build_success_email' do
let(:build) { FactoryGirl.create(:build, :branch => "branch-of-master") }

before do
allow(GitBlame).to receive(:changes_in_branch).and_return([{:hash => "sha", :author => "Joe", :date => "some day", :message => "always be shipping it"}])
allow(GitBlame).to receive(:emails_in_branch).and_return(["[email protected]"])
end

it "sends an email" do
build_part = build.build_parts.create!(:paths => ["a", "b"], :kind => "cucumber", :queue => :ci)
build_part.build_attempts.create!(:state => :passed, :builder => "test-builder")

email = BuildMailer.build_success_email(build)

expect(email.to).to eq(["[email protected]"])

expect(email.html_part.body).to include(build_part.project.name)
expect(email.text_part.body).to include(build_part.project.name)
expect(email.html_part.body).to include("http://")
expect(email.text_part.body).to include("http://")
end
end
end
8 changes: 7 additions & 1 deletion spec/models/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@
end
end

context "send_build_status_email!" do
describe "#send_build_status_email!" do
let(:project) { FactoryGirl.create(:big_rails_project, :repository => repository, :name => name) }
let(:repository) { FactoryGirl.create(:repository)}
let(:build) { FactoryGirl.create(:build, :state => :runnable, :project => project) }
Expand Down Expand Up @@ -432,6 +432,12 @@
expect(BuildMailer).not_to receive(:build_break_email)
build.send_build_status_email!
end

it "should send a success email" do
build.update_attribute(:state, :succeeded)
expect(BuildMailer).to receive(:build_success_email).and_return(OpenStruct.new(:deliver => nil))
build.send_build_status_email!
end
end
end
end
Expand Down

0 comments on commit 56fd48a

Please sign in to comment.