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

Commit ae714c3

Browse files
author
Melanie Gilman
committed
Return hash instead of nil when no changes in snapshot
https://trello.com/c/FVI4JXBI Before this change, attempting to send a snapshot to Slack when there were no alert or improved constants would return nil, which is an unexpected value in the app. This change checks to see if those changes exist before attempting to send a snapshot, and if they don't, returns a hash with ignored: true instead of nil.
1 parent e847241 commit ae714c3

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

lib/cc/formatters/snapshot_formatter.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def initialize(payload)
5959
@improved_constants_payload = data.merge("constants" => improved_constants) if improved_constants.any?
6060
end
6161

62+
def changed?
63+
alert_constants_payload || improved_constants_payload
64+
end
65+
6266
private
6367

6468
def new_constants_selector

lib/cc/services/slack.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ def receive_test
2323
end
2424

2525
def receive_snapshot
26-
send_snapshot_to_slack(CC::Formatters::SnapshotFormatter::Base.new(payload))
26+
snapshot = CC::Formatters::SnapshotFormatter::Base.new(payload)
27+
28+
if snapshot.changed?
29+
send_snapshot_to_slack(snapshot)
30+
else
31+
{ ok: false, ignored: true, message: "No changes in snapshot" }
32+
end
2733
end
2834

2935
def receive_coverage

test/formatters/snapshot_formatter_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,16 @@ def test_snapshot_formatter_test_with_relaxed_constraints
4444
refute_nil f.alert_constants_payload
4545
refute_nil f.improved_constants_payload
4646
end
47+
48+
def test_changed_when_snapshot_changed
49+
f = described_class.new({"new_constants" => [],
50+
"changed_constants" => [{"to" => {"rating" => "A"}, "from" => {"rating" => "D"}}]
51+
})
52+
assert f.changed?
53+
end
54+
55+
def test_changed_when_no_changes
56+
f = described_class.new({"new_constants" => [], "changed_constants" => []})
57+
refute f.changed?
58+
end
4759
end

test/slack_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ def test_receive_test
184184
assert_equal "Test message sent", response[:message]
185185
end
186186

187+
def test_no_changes_in_snapshot
188+
data = { "name" => "snapshot", "repo_name" => "Rails",
189+
"new_constants" => [],
190+
"changed_constants" => [],
191+
}
192+
response = receive_event(data)
193+
194+
assert_equal false, response[:ok]
195+
assert response[:ignored]
196+
end
197+
187198
private
188199

189200
def assert_slack_receives(color, event_data, expected_body)

0 commit comments

Comments
 (0)