Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit daccb23

Browse files
committed
Make sure the format for the Slack notifications is correct.
1 parent 48d07a3 commit daccb23

File tree

5 files changed

+131
-43
lines changed

5 files changed

+131
-43
lines changed

lib/cc/formatters/snapshot_formatter.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ class Base
4242
attr_reader :alert_constants_payload, :improved_constants_payload, :details_url, :compare_url
4343

4444
def initialize(payload)
45-
new_constants = Array(payload[:new_constants])
46-
changed_constants = Array(payload[:changed_constants])
45+
new_constants = Array(payload["new_constants"])
46+
changed_constants = Array(payload["changed_constants"])
4747

4848
alert_constants = new_constants.select(&new_constants_selector)
4949
alert_constants += changed_constants.select(&decreased_constants_selector)
5050

5151
improved_constants = changed_constants.select(&improved_constants_selector)
5252

5353
data = {
54-
from: { commit_sha: payload[:previous_commit_sha] },
55-
to: { commit_sha: payload[:commit_sha] }
54+
"from" => { "commit_sha" => payload["previous_commit_sha"] },
55+
"to" => { "commit_sha" => payload["commit_sha"] }
5656
}
5757

58-
@alert_constants_payload = data.merge(constants: alert_constants) if alert_constants.any?
59-
@improved_constants_payload = data.merge(constants: improved_constants) if improved_constants.any?
58+
@alert_constants_payload = data.merge("constants" => alert_constants) if alert_constants.any?
59+
@improved_constants_payload = data.merge("constants" => improved_constants) if improved_constants.any?
6060
end
6161

6262
private
@@ -74,11 +74,11 @@ def improved_constants_selector
7474
end
7575

7676
def to_rating(constant)
77-
Rating.new(constant[:to][:rating])
77+
Rating.new(constant["to"]["rating"])
7878
end
7979

8080
def from_rating(constant)
81-
Rating.new(constant[:from][:rating])
81+
Rating.new(constant["from"]["rating"])
8282
end
8383
end
8484

lib/cc/helpers/quality_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def with_article(letter, bold = false)
3636

3737
def constant_basename(name)
3838
if name.include?(".")
39-
file.basename(name)
39+
File.basename(name)
4040
else
4141
name
4242
end

lib/cc/services/slack.rb

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# encoding: UTF-8
22

33
class CC::Service::Slack < CC::Service
4+
include CC::Service::QualityHelper
5+
46
class Config < CC::Service::Config
57
attribute :webhook_url, String,
68
label: "Webhook URL",
@@ -57,35 +59,32 @@ def speak(message, color = nil)
5759
http_post(config.webhook_url, body.to_json)
5860
end
5961

60-
def send_snapshot_to_slack(payload, sample = false)
61-
snapshot = SnapshotEventFormatter.new(repo, payload, sample)
62-
63-
if formatter.alert_constants_payload
64-
speak(alerts_message(snapshot), RED_HEX)
62+
def send_snapshot_to_slack(snapshot)
63+
if snapshot.alert_constants_payload
64+
speak(alerts_message(snapshot.alert_constants_payload), RED_HEX)
6565
end
6666

67-
if formatter.improved_constants_payload
68-
speak(improvements_message(snapshot), GREEN_HEX)
67+
if snapshot.improved_constants_payload
68+
speak(improvements_message(snapshot.improved_constants_payload), GREEN_HEX)
6969
end
7070
end
7171

72-
def alert_message(snapshot)
73-
constants = snapshot.alert_constants_payload
74-
75-
message = ["Quality alert triggered for *#{repo_identifier}* (<#{compare_url}|Compare>)\n"]
72+
def alerts_message(constants_payload)
73+
constants = constants_payload["constants"]
74+
message = ["Quality alert triggered for *#{repo_name}* (<#{compare_url}|Compare>)\n"]
7675

7776
constants[0..2].each do |constant|
7877
object_identifier = constant_basename(constant["name"])
7978

8079
if constant["from"]
81-
from_rating = from_rating(constant)
82-
to_rating = to_rating(constant)
80+
from_rating = constant["from"]["rating"]
81+
to_rating = constant["to"]["rating"]
8382

8483
message << "• _#{object_identifier}_ just declined from #{with_article(from_rating, :bold)} to #{with_article(to_rating, :bold)}"
8584
else
86-
rating = to_rating(constant)
85+
rating = constant["to"]["rating"]
8786

88-
message << "• _#{object_identifier}_ was just created and is #{with_article(rating, :bold)} *#{rating}*"
87+
message << "• _#{object_identifier}_ was just created and is #{with_article(rating, :bold)}"
8988
end
9089
end
9190

@@ -97,15 +96,14 @@ def alert_message(snapshot)
9796
message.join("\n")
9897
end
9998

100-
def improvement_message(snapshot)
101-
constants = snapshot.improved_constants_payload
102-
103-
message = ["Quality improvements in *#{repo_identifier}* (<#{compare_url}|Compare>)\n"]
99+
def improvements_message(constants_payload)
100+
constants = constants_payload["constants"]
101+
message = ["Quality improvements in *#{repo_name}* (<#{compare_url}|Compare>)\n"]
104102

105103
constants[0..2].each do |constant|
106104
object_identifier = constant_basename(constant["name"])
107-
from_rating = from_rating(constant)
108-
to_rating = to_rating(constant)
105+
from_rating = constant["from"]["rating"]
106+
to_rating = constant["to"]["rating"]
109107

110108
message << "• _#{object_identifier}_ just improved from #{with_article(from_rating, :bold)} to #{with_article(to_rating, :bold)}"
111109
end

test/formatters/snapshot_formatter_test.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@ def described_class
66
end
77

88
def test_quality_alert_with_new_constants
9-
f = described_class.new({new_constants: [{to: {rating: "D"}}], changed_constants: []})
9+
f = described_class.new({"new_constants" => [{"to" => {"rating" => "D"}}], "changed_constants" => []})
1010
refute_nil f.alert_constants_payload
1111
end
1212

1313
def test_quality_alert_with_decreased_constants
14-
f = described_class.new({new_constants: [],
15-
changed_constants: [{to: {rating: "D"}, from: {rating: "A"}}]
14+
f = described_class.new({"new_constants" => [],
15+
"changed_constants" => [{"to" => {"rating" => "D"}, "from" => {"rating" => "A"}}]
1616
})
1717
refute_nil f.alert_constants_payload
1818
end
1919

2020
def test_quality_improvements_with_better_ratings
21-
f = described_class.new({new_constants: [],
22-
changed_constants: [{to: {rating: "A"}, from: {rating: "D"}}]
21+
f = described_class.new({"new_constants" => [],
22+
"changed_constants" => [{"to" => {"rating" => "A"}, "from" => {"rating" => "D"}}]
2323
})
2424
refute_nil f.improved_constants_payload
2525
end
2626

2727
def test_nothing_set_without_changes
28-
f = described_class.new({new_constants: [], changed_constants: []})
28+
f = described_class.new({"new_constants" => [], "changed_constants" => []})
2929
assert_nil f.alert_constants_payload
3030
assert_nil f.improved_constants_payload
3131
end
3232

3333
def test_snapshot_formatter_test_with_relaxed_constraints
3434
f = CC::Formatters::SnapshotFormatter::Sample.new({
35-
new_constants: [{name: "foo", to: {rating: "A"}}, {name: "bar", to: {rating: "A"}}],
36-
changed_constants: [
37-
{from: {rating: "B"}, to: {rating: "C"}},
38-
{from: {rating: "D"}, to: {rating: "D"}},
39-
{from: {rating: "D"}, to: {rating: "D"}},
40-
{from: {rating: "A"}, to: {rating: "B"}},
41-
{from: {rating: "C"}, to: {rating: "B"}}
35+
"new_constants" => [{"name" => "foo", "to" => {"rating" => "A"}}, {"name" => "bar", "to" => {"rating" => "A"}}],
36+
"changed_constants" => [
37+
{"from" => {"rating" => "B"}, "to" => {"rating" => "C"}},
38+
{"from" => {"rating" => "D"}, "to" => {"rating" => "D"}},
39+
{"from" => {"rating" => "D"}, "to" => {"rating" => "D"}},
40+
{"from" => {"rating" => "A"}, "to" => {"rating" => "B"}},
41+
{"from" => {"rating" => "C"}, "to" => {"rating" => "B"}}
4242
]})
4343

4444
refute_nil f.alert_constants_payload

test/slack_test.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,96 @@ def test_multiple_vulnerabilities
7272
].join(" "))
7373
end
7474

75+
def test_quality_alert_with_new_constants
76+
data = { "name" => "snapshot", "repo_name" => "Rails",
77+
"new_constants" => [{"name" => "Foo", "to" => {"rating" => "D"}}, {"name" => "bar.js", "to" => {"rating" => "F"}}],
78+
"changed_constants" => [],
79+
"compare_url" => "https://codeclimate.com/repos/1/compare/a...z" }
80+
81+
assert_slack_receives(CC::Service::Slack::RED_HEX, data,
82+
"""Quality alert triggered for *Rails* (<https://codeclimate.com/repos/1/compare/a...z|Compare>)
83+
84+
• _Foo_ was just created and is a *D*
85+
• _bar.js_ was just created and is an *F*""")
86+
end
87+
88+
def test_quality_alert_with_new_constants_and_declined_constants
89+
data = { "name" => "snapshot", "repo_name" => "Rails",
90+
"new_constants" => [{"name" => "Foo", "to" => {"rating" => "D"}}],
91+
"changed_constants" => [{"name" => "bar.js", "from" => {"rating" => "A"}, "to" => {"rating" => "F"}}],
92+
"compare_url" => "https://codeclimate.com/repos/1/compare/a...z" }
93+
94+
assert_slack_receives(CC::Service::Slack::RED_HEX, data,
95+
"""Quality alert triggered for *Rails* (<https://codeclimate.com/repos/1/compare/a...z|Compare>)
96+
97+
• _Foo_ was just created and is a *D*
98+
• _bar.js_ just declined from an *A* to an *F*""")
99+
end
100+
101+
def test_quality_alert_with_new_constants_and_declined_constants_overflown
102+
data = { "name" => "snapshot", "repo_name" => "Rails",
103+
"new_constants" => [{"name" => "Foo", "to" => {"rating" => "D"}}],
104+
"changed_constants" => [
105+
{"name" => "bar.js", "from" => {"rating" => "A"}, "to" => {"rating" => "F"}},
106+
{"name" => "baz.js", "from" => {"rating" => "B"}, "to" => {"rating" => "D"}},
107+
{"name" => "Qux", "from" => {"rating" => "A"}, "to" => {"rating" => "D"}}
108+
],
109+
"compare_url" => "https://codeclimate.com/repos/1/compare/a...z",
110+
"details_url" => "https://codeclimate.com/repos/1/feed"
111+
}
112+
113+
114+
assert_slack_receives(CC::Service::Slack::RED_HEX, data,
115+
"""Quality alert triggered for *Rails* (<https://codeclimate.com/repos/1/compare/a...z|Compare>)
116+
117+
• _Foo_ was just created and is a *D*
118+
• _bar.js_ just declined from an *A* to an *F*
119+
• _baz.js_ just declined from a *B* to a *D*
120+
121+
And <https://codeclimate.com/repos/1/feed|1 other change>""")
122+
end
123+
124+
def test_quality_improvements
125+
data = { "name" => "snapshot", "repo_name" => "Rails",
126+
"new_constants" => [],
127+
"changed_constants" => [
128+
{"name" => "bar.js", "from" => {"rating" => "F"}, "to" => {"rating" => "A"}},
129+
],
130+
"compare_url" => "https://codeclimate.com/repos/1/compare/a...z",
131+
"details_url" => "https://codeclimate.com/repos/1/feed"
132+
}
133+
134+
135+
assert_slack_receives(CC::Service::Slack::GREEN_HEX, data,
136+
"""Quality improvements in *Rails* (<https://codeclimate.com/repos/1/compare/a...z|Compare>)
137+
138+
• _bar.js_ just improved from an *F* to an *A*""")
139+
end
140+
141+
def test_quality_improvements_overflown
142+
data = { "name" => "snapshot", "repo_name" => "Rails",
143+
"new_constants" => [],
144+
"changed_constants" => [
145+
{"name" => "Foo", "from" => {"rating" => "F"}, "to" => {"rating" => "A"}},
146+
{"name" => "bar.js", "from" => {"rating" => "D"}, "to" => {"rating" => "B"}},
147+
{"name" => "baz.js", "from" => {"rating" => "D"}, "to" => {"rating" => "A"}},
148+
{"name" => "Qux", "from" => {"rating" => "F"}, "to" => {"rating" => "A"}},
149+
],
150+
"compare_url" => "https://codeclimate.com/repos/1/compare/a...z",
151+
"details_url" => "https://codeclimate.com/repos/1/feed"
152+
}
153+
154+
155+
assert_slack_receives(CC::Service::Slack::GREEN_HEX, data,
156+
"""Quality improvements in *Rails* (<https://codeclimate.com/repos/1/compare/a...z|Compare>)
157+
158+
• _Foo_ just improved from an *F* to an *A*
159+
• _bar.js_ just improved from a *D* to a *B*
160+
• _baz.js_ just improved from a *D* to an *A*
161+
162+
And <https://codeclimate.com/repos/1/feed|1 other improvement>""")
163+
end
164+
75165
private
76166

77167
def assert_slack_receives(color, event_data, expected_body)

0 commit comments

Comments
 (0)