Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.

Set up template_success and template_failure for notifications based on build result #368

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
3 changes: 1 addition & 2 deletions lib/travis/addons/campfire/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def send_line(url, line)
end

def template
template = config[:template] rescue nil
Array(template || DEFAULT_TEMPLATE)
Array(template_for(build[:state]) || DEFAULT_TEMPLATE)
end

def parse(target)
Expand Down
3 changes: 1 addition & 2 deletions lib/travis/addons/hipchat/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def process
end

def template
template = config[:template] rescue nil
Array(template || DEFAULT_TEMPLATE)
Array(template_for(build[:state]) || DEFAULT_TEMPLATE)
end

def color
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/addons/irc/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def join?
end

def template
Array(try_config(:template) || DEFAULT_TEMPLATE)
Array(template_for(build[:state]) || DEFAULT_TEMPLATE)
end

def client_options(port, ssl)
Expand Down
4 changes: 2 additions & 2 deletions lib/travis/addons/slack/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def color
end

def template_from_config
slack_config.is_a?(Hash) ? slack_config[:template] : nil
config.is_a?(Hash) ? template_for(build[:state]) : nil
end

def slack_config
def config
build[:config].try(:[], :notifications).try(:[], :slack) || {}
end

Expand Down
6 changes: 3 additions & 3 deletions lib/travis/addons/sqwiggle/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Sqwiggle
# configuration (`.travis.yml`).
#
class Task < Travis::Task
DEFAULT_TEMPLATE = %Q[
DEFAULT_TEMPLATE = %Q[
%{repository} - build number: %{build_number} (%{branch} - %{commit} : %{author}) -
<a href="%{build_url}" target="_blank">build</a> has
<a href="%{build_url}" target="_blank">build</a> has
<strong>%{result}</strong>
]

Expand All @@ -23,7 +23,7 @@ def message
private

def template
(config[:template] rescue nil) || DEFAULT_TEMPLATE
template_for(build[:state]) || DEFAULT_TEMPLATE
end

def process
Expand Down
18 changes: 18 additions & 0 deletions lib/travis/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,23 @@ def http_options
def timeout(options = { after: 60 }, &block)
Timeout::timeout(options[:after], &block)
end

def template_for(state = nil)
if state == 'passed' && config[:template_success]
config[:template_success]
elsif state == 'failed' && config[:template_failure]
config[:template_failure]
elsif state == 'errored' && config[:template_error]
config[:template_error]
else
config[:template]
end
rescue
nil
end

def config
raise NotImplementedError, "#config must be implemented by the subclass"
end
end
end
38 changes: 38 additions & 0 deletions spec/travis/addons/campfire/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,44 @@ def run(targets)
http.verify_stubbed_calls
end

it 'using a custom template_success' do
targets = ['account-1:token-1@1234']
template = ['%{repository}', '%{commit}']
messages = ['svenfuchs/minimal', '62aae5f']

payload['build']['config']['notifications'] = { campfire: { template_success: template } }
expect_campfire('account-1', '1234', 'token-1', messages)

run(targets)
http.verify_stubbed_calls
end

it 'using a custom template_failure' do
targets = ['account-1:token-1@1234']
template = ['%{repository}', '%{commit}']
messages = ['svenfuchs/minimal', '62aae5f']

payload['build']['config']['notifications'] = { campfire: { template_failure: template } }
payload['build']['state'] = 'failed'
expect_campfire('account-1', '1234', 'token-1', messages)

run(targets)
http.verify_stubbed_calls
end

it 'using a custom template_error' do
targets = ['account-1:token-1@1234']
template = ['%{repository}', '%{commit}']
messages = ['svenfuchs/minimal', '62aae5f']

payload['build']['config']['notifications'] = { campfire: { template_error: template } }
payload['build']['state'] = 'errored'
expect_campfire('account-1', '1234', 'token-1', messages)

run(targets)
http.verify_stubbed_calls
end

def expect_campfire(account, room, token, body)
host = "#{account}.campfirenow.com"
path = "room/#{room}/speak.json"
Expand Down
54 changes: 48 additions & 6 deletions spec/travis/addons/hipchat/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,51 @@ def run(targets)
http.verify_stubbed_calls
end

it 'uses template_success if defined' do
targets = ["#{room_1_token}@room_1"]
template = ['%{repository}', '%{commit}']
messages = ['svenfuchs/minimal', '62aae5f']

payload['build']['config']['notifications'] = { hipchat: { template_success: template } }
expect_hipchat('room_1', room_1_token, messages)

run(targets)
http.verify_stubbed_calls
end

it 'uses template_failure if defined for failed build' do
targets = ["#{room_1_token}@room_1"]
template = ['%{repository}', '%{commit}']
messages = ['svenfuchs/minimal', '62aae5f']

payload['build']['config']['notifications'] = { hipchat: { template_failure: template } }
payload['build']['state'] = 'failed'
expect_hipchat('room_1', room_1_token, messages, message_color: 'red')

run(targets)
http.verify_stubbed_calls
end

it 'uses template_error if defined for errored build' do
targets = ["#{room_1_token}@room_1"]
template = ['%{repository}', '%{commit}']
messages = ['svenfuchs/minimal', '62aae5f']

payload['build']['config']['notifications'] = { hipchat: { template_error: template } }
payload['build']['state'] = 'errored'
expect_hipchat('room_1', room_1_token, messages, message_color: 'gray')

run(targets)
http.verify_stubbed_calls
end

it "sends HTML notifications if requested" do
targets = ["#{room_1_token}@room_1"]
template = ['<a href="%{build_url}">Details</a>']
messages = ['<a href="http://travis-ci.org/svenfuchs/minimal/builds/1">Details</a>']

payload['build']['config']['notifications'] = { hipchat: { template: template, format: 'html' } }
expect_hipchat('room_1', room_1_token, messages, 'message_format' => 'html')
expect_hipchat('room_1', room_1_token, messages, extra_body: {'message_format' => 'html'})

run(targets)
http.verify_stubbed_calls
Expand Down Expand Up @@ -87,7 +125,7 @@ def run(targets)
]

payload["build"]["state"] = "errored"
expect_hipchat("room_1", room_1_token, messages, "color" => "gray")
expect_hipchat("room_1", room_1_token, messages, extra_body: {"color" => "gray"})

run(targets)
http.verify_stubbed_calls
Expand All @@ -101,19 +139,23 @@ def run(targets)
end
end

def expect_hipchat(room_id, token, lines, extra_body={})
def expect_hipchat(room_id, token, lines, opts={})
extra_body = opts[:extra_body] || {}
message_color = opts[:message_color] || 'green'
Array(lines).each do |line|
body = { 'room_id' => room_id, 'from' => 'Travis CI', 'message' => line, 'color' => 'green', 'message_format' => 'text' }.merge(extra_body)
body = { 'room_id' => room_id, 'from' => 'Travis CI', 'message' => line, 'color' => message_color, 'message_format' => 'text' }.merge(extra_body)
http.post("v1/rooms/message?format=json&auth_token=#{token}") do |env|
env[:url].host.should == 'api.hipchat.com'
Rack::Utils.parse_query(env[:body]).should == body
end
end
end

def expect_hipchat_v2(room_id, token, lines, extra_body={})
def expect_hipchat_v2(room_id, token, lines, opts={})
extra_body = opts[:extra_body] || {}
message_color = opts[:message_color] || 'green'
Array(lines).each do |line|
body = { 'message' => line, 'color' => 'green', 'message_format' => 'text' }.merge(extra_body).to_json
body = { 'message' => line, 'color' => message_color, 'message_format' => 'text' }.merge(extra_body).to_json
http.post("https://api.hipchat.com/v2/room/#{URI::encode(room_id, Travis::Addons::Hipchat::HttpHelper::UNSAFE_URL_CHARS)}/notification?auth_token=#{token}") do |env|
env[:request_headers]['Content-Type'].should == 'application/json'
env[:body].should == body
Expand Down
91 changes: 91 additions & 0 deletions spec/travis/addons/irc/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,50 @@ def run(channels = nil)
run
end

it 'with a custom message template_success' do
payload['build']['config']['notifications'] = { irc: { template_success: '%{repository} %{commit}' } }

expect_irc 'irc.freenode.net', 1234, 'travis', [
'NICK travis-ci',
'USER travis-ci travis-ci travis-ci :travis-ci',
'JOIN #travis',
'PRIVMSG #travis :[travis-ci] svenfuchs/minimal 62aae5f',
'PART #travis',
'QUIT'
]
run
end

it 'with a custom message template_failure for a failed build' do
payload['build']['config']['notifications'] = { irc: { template_failure: '%{repository} %{commit}' } }
payload['build']['state'] = 'failed'

expect_irc 'irc.freenode.net', 1234, 'travis', [
'NICK travis-ci',
'USER travis-ci travis-ci travis-ci :travis-ci',
'JOIN #travis',
'PRIVMSG #travis :[travis-ci] svenfuchs/minimal 62aae5f',
'PART #travis',
'QUIT'
]
run
end

it 'with a custom message template_error for a errored build' do
payload['build']['config']['notifications'] = { irc: { template_error: '%{repository} %{commit}' } }
payload['build']['state'] = 'errored'

expect_irc 'irc.freenode.net', 1234, 'travis', [
'NICK travis-ci',
'USER travis-ci travis-ci travis-ci :travis-ci',
'JOIN #travis',
'PRIVMSG #travis :[travis-ci] svenfuchs/minimal 62aae5f',
'PART #travis',
'QUIT'
]
run
end

it 'with multiple custom message templates' do
payload['build']['config']['notifications'] = { irc: { template: ['%{repository} %{commit}', '%{message}'] } }

Expand All @@ -106,6 +150,53 @@ def run(channels = nil)
run
end

it 'with multiple custom message templates for successful build' do
payload['build']['config']['notifications'] = { irc: { template_success: ['%{repository} %{commit}', '%{message}'] } }

expect_irc 'irc.freenode.net', 1234, 'travis', [
'NICK travis-ci',
'USER travis-ci travis-ci travis-ci :travis-ci',
'JOIN #travis',
'PRIVMSG #travis :[travis-ci] svenfuchs/minimal 62aae5f',
'PRIVMSG #travis :[travis-ci] The build passed.',
'PART #travis',
'QUIT'
]
run
end

it 'with multiple custom message templates for failed build' do
payload['build']['config']['notifications'] = { irc: { template_failure: ['%{repository} %{commit}', '%{message}'] } }
payload['build']['state'] = 'failed'

expect_irc 'irc.freenode.net', 1234, 'travis', [
'NICK travis-ci',
'USER travis-ci travis-ci travis-ci :travis-ci',
'JOIN #travis',
'PRIVMSG #travis :[travis-ci] svenfuchs/minimal 62aae5f',
'PRIVMSG #travis :[travis-ci] The build was broken.',
'PART #travis',
'QUIT'
]
run
end

it 'with multiple custom message templates for errored build' do
payload['build']['config']['notifications'] = { irc: { template_error: ['%{repository} %{commit}', '%{message}'] } }
payload['build']['state'] = 'errored'

expect_irc 'irc.freenode.net', 1234, 'travis', [
'NICK travis-ci',
'USER travis-ci travis-ci travis-ci :travis-ci',
'JOIN #travis',
'PRIVMSG #travis :[travis-ci] svenfuchs/minimal 62aae5f',
'PRIVMSG #travis :[travis-ci] The build has errored.',
'PART #travis',
'QUIT'
]
run
end

it 'with two irc notifications to different hosts' do
[['irc.freenode.net', 1234, 'travis'], ['irc.example.com', 6667, 'example']].each do |host, port, channel|
expect_irc host, port, channel, [
Expand Down
54 changes: 52 additions & 2 deletions spec/travis/addons/slack/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def run(targets)
run(targets)
http.verify_stubbed_calls
end

it "allows specifying a custom template" do
targets = ['team-1:token-1']
payload['build']['config']['notifications'] = { slack: { template: 'Custom: %{author}'}}
payload['build']['config']['notifications'] = { slack: { template: 'Custom: %{author}'}}
message = {
icon_url: "https://travis-ci.org/images/travis-mascot-150.png",
attachments: [{
Expand All @@ -67,6 +67,56 @@ def run(targets)
http.verify_stubbed_calls
end

it "uses template_success for successful build" do
targets = ['team-1:token-1']
payload['build']['config']['notifications'] = { slack: { template_success: 'Custom: %{author}'}}
message = {
icon_url: "https://travis-ci.org/images/travis-mascot-150.png",
attachments: [{
text: "Custom: Sven Fuchs",
color: 'good'
}.stringify_keys]
}.stringify_keys
expect_slack('team-1', 'token-1', message)

run(targets)
http.verify_stubbed_calls
end

it "uses template_error for errored build" do
targets = ['team-1:token-1']
payload['build']['config']['notifications'] = { slack: { template_error: 'Custom: %{author}'}}
payload['build']['state'] = 'errored'
message = {
icon_url: "https://travis-ci.org/images/travis-mascot-150.png",
attachments: [{
text: "Custom: Sven Fuchs",
color: 'warning'
}.stringify_keys]
}.stringify_keys
expect_slack('team-1', 'token-1', message)

run(targets)
http.verify_stubbed_calls
end

it "uses template_failure for failed build" do
targets = ['team-1:token-1']
payload['build']['config']['notifications'] = { slack: { template_failure: 'Custom: %{author}'}}
payload['build']['state'] = 'failed'
message = {
icon_url: "https://travis-ci.org/images/travis-mascot-150.png",
attachments: [{
text: "Custom: Sven Fuchs",
color: 'danger'
}.stringify_keys]
}.stringify_keys
expect_slack('team-1', 'token-1', message)

run(targets)
http.verify_stubbed_calls
end

it "ignores garbage configurations" do
targets = ['3109euaofjelw;arj;gfer//asfg=adfaf4lk3rj']
expect {
Expand Down
Loading