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

Commit b8111a7

Browse files
committed
Merge pull request #62 from codeclimate/ia-new-pr-status-only-for-repos-with-feature-flag
Show new PR status message only for repos with beta feature enabled
2 parents 910193f + 57d2d60 commit b8111a7

8 files changed

+81
-37
lines changed

lib/cc/service.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ def self.slug
6969
end
7070
end
7171

72-
def initialize(config, payload)
73-
@payload = payload.stringify_keys
74-
@config = create_config(config)
75-
@event = @payload["name"].to_s
72+
def initialize(config, payload, repo_config)
73+
@payload = payload.stringify_keys
74+
@config = create_config(config)
75+
@event = @payload["name"].to_s
76+
@repo_config = repo_config
7677

7778
load_helper
7879
validate_event

lib/cc/services/github_pull_requests.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ def update_status_skipped
7070

7171
def update_status_success
7272
add_comment
73-
update_status(
74-
"success",
75-
CC::Service::GitHubPullRequests::Presenter.new(@payload).success_message
76-
)
73+
update_status("success", presenter.success_message)
74+
end
75+
76+
def presenter
77+
CC::Service::GitHubPullRequests::Presenter.new(@payload, @repo_config)
7778
end
7879

7980
def update_status_error

lib/cc/services/github_pull_requests_presenter.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
class CC::Service::GitHubPullRequests::Presenter
2-
def initialize(payload)
3-
@fixed_count = payload["fixed_issue_count"]
4-
@new_count = payload["new_issue_count"]
2+
def initialize(payload, repo_config)
3+
issue_comparison_counts = payload["issue_comparison_counts"]
4+
5+
if issue_comparison_counts
6+
@fixed_count = issue_comparison_counts["fixed"]
7+
@new_count = issue_comparison_counts["new"]
8+
end
9+
10+
@repo_config = repo_config
511
end
612

713
def success_message
8-
if issue_counts_in_payload?
14+
if @repo_config.pr_status_quality_stats?
915
if both_issue_counts_zero?
1016
"Code Climate didn't find any new or fixed issues."
1117
else

service_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ def call
3131
end
3232
end
3333

34+
class FalsyRepoConfig
35+
def method_missing(*args)
36+
false
37+
end
38+
end
39+
3440
class ServiceTest
3541
def initialize(klass, *params)
3642
@klass = klass
@@ -68,7 +74,11 @@ def to_env_var(param)
6874
def test_service(klass, config, payload)
6975
repo_name = ENV["REPO_NAME"] || "App"
7076

71-
service = klass.new(config, { name: :test, repo_name: repo_name }.merge(payload))
77+
service = klass.new(
78+
config,
79+
{ name: :test, repo_name: repo_name }.merge(payload),
80+
FalsyRepoConfig.new
81+
)
7282

7383
CC::Service::Invocation.new(service) do |i|
7484
i.wrap(WithResponseLogging)
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
11
require File.expand_path('../helper', __FILE__)
22

33
class TestGitHubPullRequestsPresenter < CC::Service::TestCase
4-
def test_message_no_issue_counts_in_payload
4+
def test_message_quality_stats_not_enabled
55
assert_equal(
66
"Code Climate has analyzed this pull request.",
7-
message_from_payload({})
7+
build_presenter(false, "fixed" => 1, "new" => 1).success_message
88
)
99
end
1010

1111
def test_message_singular
1212
assert_equal(
1313
"Code Climate found 1 new issue and 1 fixed issue.",
14-
message_from_payload("fixed_issue_count" => 1, "new_issue_count" => 1)
14+
build_presenter(true, "fixed" => 1, "new" => 1).success_message
1515
)
1616
end
1717

1818
def test_message_plural
1919
assert_equal(
2020
"Code Climate found 2 new issues and 1 fixed issue.",
21-
message_from_payload("fixed_issue_count" => 1, "new_issue_count" => 2)
21+
build_presenter(true, "fixed" => 1, "new" => 2).success_message
2222
)
2323
end
2424

2525
def test_message_only_fixed
2626
assert_equal(
2727
"Code Climate found 1 fixed issue.",
28-
message_from_payload("fixed_issue_count" => 1, "new_issue_count" => 0)
28+
build_presenter(true, "fixed" => 1, "new" => 0).success_message
2929
)
3030
end
3131

3232
def test_message_only_new
3333
assert_equal(
3434
"Code Climate found 3 new issues.",
35-
message_from_payload("fixed_issue_count" => 0, "new_issue_count" => 3)
35+
build_presenter(true, "fixed" => 0, "new" => 3).success_message
3636
)
3737
end
3838

3939
def test_message_no_new_or_fixed
4040
assert_equal(
4141
"Code Climate didn't find any new or fixed issues.",
42-
message_from_payload("fixed_issue_count" => 0, "new_issue_count" => 0)
42+
build_presenter(true, "fixed" => 0, "new" => 0).success_message
4343
)
4444
end
4545

4646
private
4747

48-
def message_from_payload(payload)
49-
CC::Service::GitHubPullRequests::Presenter.new(payload).success_message
48+
def build_payload(issue_counts)
49+
{ "issue_comparison_counts" => issue_counts }
50+
end
51+
52+
def build_presenter(quality_stats_enabled, issue_counts)
53+
CC::Service::GitHubPullRequests::Presenter.new(
54+
build_payload(issue_counts),
55+
OpenStruct.new(pr_status_quality_stats?: quality_stats_enabled)
56+
)
5057
end
5158
end

test/github_pull_requests_test.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ def test_pull_request_status_success_detailed
2020
"description" => "Code Climate found 1 new issue and 2 fixed issues.",
2121
})
2222

23-
receive_pull_request({ update_status: true }, {
24-
github_slug: "pbrisbin/foo",
25-
commit_sha: "abc123",
26-
state: "success",
27-
new_issue_count: 1,
28-
fixed_issue_count: 2,
29-
})
23+
receive_pull_request(
24+
{ update_status: true },
25+
{
26+
github_slug: "pbrisbin/foo",
27+
commit_sha: "abc123",
28+
state: "success",
29+
issue_comparison_counts: {
30+
"fixed" => 2,
31+
"new" => 1,
32+
}
33+
},
34+
true
35+
)
3036
end
3137

3238
def test_pull_request_status_success_generic
@@ -238,11 +244,12 @@ def expect_comment(repo, number, content)
238244
end
239245
end
240246

241-
def receive_pull_request(config, event_data)
247+
def receive_pull_request(config, event_data, truthy_repo_config = false)
242248
receive(
243249
CC::Service::GitHubPullRequests,
244250
{ oauth_token: "123" }.merge(config),
245-
{ name: "pull_request" }.merge(event_data)
251+
{ name: "pull_request" }.merge(event_data),
252+
truthy_repo_config ? TruthyRepoConfig.new : FalsyRepoConfig.new
246253
)
247254
end
248255

test/helper.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def teardown
2525
@stubs.verify_stubbed_calls
2626
end
2727

28-
def service(klass, data, payload)
29-
service = klass.new(data, payload)
28+
def service(klass, data, payload, repo_config = FalsyRepoConfig.new)
29+
service = klass.new(data, payload, repo_config)
3030
service.http :adapter => [:test, @stubs]
3131
service
3232
end
@@ -47,4 +47,16 @@ def stub_http(url, response = nil, &block)
4747
block ||= lambda{|*args| response }
4848
@stubs.post(url, &block)
4949
end
50+
51+
class FalsyRepoConfig
52+
def method_missing(*args)
53+
false
54+
end
55+
end
56+
57+
class TruthyRepoConfig
58+
def method_missing(*args)
59+
true
60+
end
61+
end
5062
end

test/service_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
class TestService < CC::Service::TestCase
44
def test_validates_events
55
assert_raises(ArgumentError) do
6-
CC::Service.new(:foo, {}, {})
6+
CC::Service.new(:foo, {}, {}, {})
77
end
88
end
99

1010
def test_default_path_to_ca_file
11-
s = CC::Service.new({}, {name: "test"})
11+
s = CC::Service.new({}, {name: "test"}, FalsyRepoConfig.new)
1212
assert_equal(File.expand_path("../../config/cacert.pem", __FILE__), s.ca_file)
1313
assert File.exist?(s.ca_file)
1414
end
1515

1616
def test_custom_path_to_ca_file
1717
ENV["CODECLIMATE_CA_FILE"] = "/tmp/cacert.pem"
18-
s = CC::Service.new({}, {name: "test"})
18+
s = CC::Service.new({}, {name: "test"}, FalsyRepoConfig.new)
1919
assert_equal("/tmp/cacert.pem", s.ca_file)
2020
ensure
2121
ENV.delete("CODECLIMATE_CA_FILE")
2222
end
2323

2424
def test_nothing_has_a_handler
25-
service = CC::Service.new({}, {name: "test"})
25+
service = CC::Service.new({}, {name: "test"}, FalsyRepoConfig.new)
2626

2727
result = service.receive
2828

0 commit comments

Comments
 (0)