Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/cc/service/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def color
end
end

def hex_color
if improved?
"#38ae6f"
else
"#ed2f00"
end
end

def changed
if improved?
"improved"
Expand Down
15 changes: 8 additions & 7 deletions lib/cc/services/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def receive_test
end

def receive_coverage
speak(formatter.format_coverage, icon_emoji: emoji)
speak(formatter.format_coverage, hex_color)
end

def receive_quality
speak(formatter.format_quality, icon_emoji: emoji)
speak(formatter.format_quality, hex_color)
end

def receive_vulnerability
Expand All @@ -32,11 +32,12 @@ def formatter
CC::Formatters::LinkedFormatter.new(self, prefix: nil, link_style: :wiki)
end

def speak(message, options = {})
body = {
text: message,
username: "Code Climate"
}.merge(options)
def speak(message, color = nil)
body = { attachments: [{
color: color,
fallback: message,
fields: [{ value: message }]
}]}

if config.channel
body[:channel] = config.channel
Expand Down
18 changes: 10 additions & 8 deletions test/slack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_test_hook
def test_coverage_improved
e = event(:coverage, to: 90.2, from: 80)

assert_slack_receives(":sunny:", e, [
assert_slack_receives("#38ae6f", e, [
"[Example]",
"<https://codeclimate.com/repos/1/feed|Test coverage>",
"has improved to 90.2% (+10.2%)",
Expand All @@ -23,7 +23,7 @@ def test_coverage_improved
def test_coverage_declined
e = event(:coverage, to: 88.6, from: 94.6)

assert_slack_receives(":umbrella:", e, [
assert_slack_receives("#ed2f00", e, [
"[Example]",
"<https://codeclimate.com/repos/1/feed|Test coverage>",
"has declined to 88.6% (-6.0%)",
Expand All @@ -34,7 +34,7 @@ def test_coverage_declined
def test_quality_improved
e = event(:quality, to: "A", from: "B")

assert_slack_receives(":sunny:", e, [
assert_slack_receives("#38ae6f", e, [
"[Example]",
"<https://codeclimate.com/repos/1/feed|User>",
"has improved from a B to an A",
Expand All @@ -45,7 +45,7 @@ def test_quality_improved
def test_quality_declined_without_compare_url
e = event(:quality, to: "D", from: "C")

assert_slack_receives(":umbrella:", e, [
assert_slack_receives("#ed2f00", e, [
"[Example]",
"<https://codeclimate.com/repos/1/feed|User>",
"has declined from a C to a D",
Expand Down Expand Up @@ -96,12 +96,14 @@ def test_multiple_vulnerabilities

private

def assert_slack_receives(emoji, event_data, expected_body)
def assert_slack_receives(color, event_data, expected_body)
@stubs.post '/token' do |env|
body = JSON.parse(env[:body])
assert_equal "Code Climate", body["username"]
assert_equal emoji, body["icon_emoji"] # may be nil
assert_equal expected_body, body["text"]
attachment = body["attachments"].first
field = attachment["fields"].first
assert_equal color, attachment["color"] # may be nil
assert_equal expected_body, attachment["fallback"]
assert_equal expected_body, field["value"]
[200, {}, '']
end

Expand Down