codeclimate-services 1.6.1 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/.rubocop.yml +1 -0
- data/Gemfile +1 -2
- data/Rakefile +4 -7
- data/base_rubocop.yml +152 -0
- data/bin/bundler +5 -5
- data/bin/coderay +5 -5
- data/bin/nokogiri +5 -5
- data/bin/pry +5 -5
- data/bin/rake +5 -5
- data/codeclimate-services.gemspec +15 -15
- data/config/load.rb +2 -2
- data/lib/cc/formatters/linked_formatter.rb +2 -2
- data/lib/cc/formatters/snapshot_formatter.rb +10 -10
- data/lib/cc/formatters/ticket_formatter.rb +0 -2
- data/lib/cc/helpers/quality_helper.rb +2 -2
- data/lib/cc/helpers/vulnerability_helper.rb +0 -2
- data/lib/cc/presenters/pull_requests_presenter.rb +1 -5
- data/lib/cc/pull_requests.rb +7 -3
- data/lib/cc/service.rb +9 -10
- data/lib/cc/service/formatter.rb +3 -3
- data/lib/cc/service/helper.rb +2 -3
- data/lib/cc/service/http.rb +9 -9
- data/lib/cc/service/invocation.rb +6 -6
- data/lib/cc/service/invocation/with_error_handling.rb +9 -8
- data/lib/cc/service/invocation/with_metrics.rb +4 -4
- data/lib/cc/service/invocation/with_return_values.rb +0 -1
- data/lib/cc/service/response_check.rb +4 -5
- data/lib/cc/services.rb +2 -2
- data/lib/cc/services/asana.rb +8 -9
- data/lib/cc/services/campfire.rb +2 -3
- data/lib/cc/services/flowdock.rb +5 -5
- data/lib/cc/services/github_issues.rb +6 -7
- data/lib/cc/services/github_pull_requests.rb +42 -5
- data/lib/cc/services/gitlab_merge_requests.rb +6 -2
- data/lib/cc/services/hipchat.rb +3 -4
- data/lib/cc/services/jira.rb +8 -9
- data/lib/cc/services/lighthouse.rb +4 -5
- data/lib/cc/services/pivotal_tracker.rb +6 -7
- data/lib/cc/services/slack.rb +8 -8
- data/lib/cc/services/stash_pull_requests.rb +2 -2
- data/lib/cc/services/version.rb +1 -1
- data/pull_request_test.rb +11 -10
- data/service_test.rb +5 -5
- data/spec/axiom/types/password_spec.rb +14 -0
- data/spec/cc/formatters/snapshot_formatter_spec.rb +44 -0
- data/spec/cc/presenters/pull_requests_presenter_spec.rb +45 -0
- data/{test/asana_test.rb → spec/cc/service/asana_spec.rb} +27 -29
- data/{test/campfire_test.rb → spec/cc/service/campfire_spec.rb} +40 -44
- data/{test/flowdock_test.rb → spec/cc/service/flowdock_spec.rb} +46 -48
- data/{test/github_issues_test.rb → spec/cc/service/github_issues_spec.rb} +33 -35
- data/spec/cc/service/github_pull_requests_spec.rb +223 -0
- data/{test/gitlab_merge_requests_test.rb → spec/cc/service/gitlab_merge_requests_spec.rb} +30 -35
- data/{test/hipchat_test.rb → spec/cc/service/hipchat_spec.rb} +38 -40
- data/spec/cc/service/invocation/error_handling_spec.rb +49 -0
- data/spec/cc/service/invocation/return_values_spec.rb +19 -0
- data/spec/cc/service/invocation/with_metrics_spec.rb +18 -0
- data/{test/invocation_test.rb → spec/cc/service/invocation_spec.rb} +31 -34
- data/{test/jira_test.rb → spec/cc/service/jira_spec.rb} +25 -27
- data/{test/lighthouse_test.rb → spec/cc/service/lighthouse_spec.rb} +23 -26
- data/spec/cc/service/pivotal_tracker_spec.rb +70 -0
- data/{test/slack_test.rb → spec/cc/service/slack_spec.rb} +66 -77
- data/spec/cc/service/stash_pull_requests_spec.rb +132 -0
- data/spec/cc/service_spec.rb +78 -0
- data/{test → spec}/fixtures.rb +3 -4
- data/spec/spec_helper.rb +37 -0
- data/{test → spec}/support/fake_logger.rb +0 -0
- data/spec/support/service_context.rb +42 -0
- metadata +57 -52
- data/test/axiom/types/password_test.rb +0 -22
- data/test/formatters/snapshot_formatter_test.rb +0 -47
- data/test/github_pull_requests_test.rb +0 -217
- data/test/helper.rb +0 -58
- data/test/invocation_error_handling_test.rb +0 -51
- data/test/invocation_return_values_test.rb +0 -21
- data/test/pivotal_tracker_test.rb +0 -73
- data/test/presenters/pull_requests_presenter_test.rb +0 -70
- data/test/service_test.rb +0 -84
- data/test/stash_pull_requests_test.rb +0 -146
- data/test/with_metrics_test.rb +0 -19
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class TestGitlabMergeRequests < CC::Service::TestCase
|
|
4
|
-
def test_merge_request_status_pending
|
|
1
|
+
describe CC::Service::GitlabMergeRequests, type: :service do
|
|
2
|
+
it "merge request status pending" do
|
|
5
3
|
expect_status_update(
|
|
6
4
|
"hal/hal9000",
|
|
7
5
|
"abc123",
|
|
@@ -17,7 +15,7 @@ class TestGitlabMergeRequests < CC::Service::TestCase
|
|
|
17
15
|
)
|
|
18
16
|
end
|
|
19
17
|
|
|
20
|
-
|
|
18
|
+
it "merge request status success detailed" do
|
|
21
19
|
expect_status_update(
|
|
22
20
|
"hal/hal9000",
|
|
23
21
|
"abc123",
|
|
@@ -33,7 +31,7 @@ class TestGitlabMergeRequests < CC::Service::TestCase
|
|
|
33
31
|
)
|
|
34
32
|
end
|
|
35
33
|
|
|
36
|
-
|
|
34
|
+
it "merge request status failure" do
|
|
37
35
|
expect_status_update(
|
|
38
36
|
"hal/hal9000",
|
|
39
37
|
"abc123",
|
|
@@ -49,7 +47,7 @@ class TestGitlabMergeRequests < CC::Service::TestCase
|
|
|
49
47
|
)
|
|
50
48
|
end
|
|
51
49
|
|
|
52
|
-
|
|
50
|
+
it "merge request status error" do
|
|
53
51
|
expect_status_update(
|
|
54
52
|
"hal/hal9000",
|
|
55
53
|
"abc123",
|
|
@@ -66,7 +64,7 @@ class TestGitlabMergeRequests < CC::Service::TestCase
|
|
|
66
64
|
)
|
|
67
65
|
end
|
|
68
66
|
|
|
69
|
-
|
|
67
|
+
it "merge request status error message provided" do
|
|
70
68
|
expect_status_update(
|
|
71
69
|
"hal/hal9000",
|
|
72
70
|
"abc123",
|
|
@@ -83,7 +81,7 @@ class TestGitlabMergeRequests < CC::Service::TestCase
|
|
|
83
81
|
)
|
|
84
82
|
end
|
|
85
83
|
|
|
86
|
-
|
|
84
|
+
it "merge request status skipped" do
|
|
87
85
|
expect_status_update(
|
|
88
86
|
"hal/hal9000",
|
|
89
87
|
"abc123",
|
|
@@ -99,7 +97,7 @@ class TestGitlabMergeRequests < CC::Service::TestCase
|
|
|
99
97
|
)
|
|
100
98
|
end
|
|
101
99
|
|
|
102
|
-
|
|
100
|
+
it "merge request coverage status success" do
|
|
103
101
|
expect_status_update(
|
|
104
102
|
"hal/hal9000",
|
|
105
103
|
"abc123",
|
|
@@ -117,71 +115,68 @@ class TestGitlabMergeRequests < CC::Service::TestCase
|
|
|
117
115
|
)
|
|
118
116
|
end
|
|
119
117
|
|
|
120
|
-
|
|
121
|
-
|
|
118
|
+
it "merge request status test success" do
|
|
119
|
+
http_stubs.post("api/v3/projects/hal%2Fhal9000/statuses/#{"0" * 40}") { |_env| [404, {}, ""] }
|
|
122
120
|
|
|
123
|
-
|
|
121
|
+
expect(receive_test({}, git_url: "ssh://git@gitlab.com/hal/hal9000.git")[:ok]).to eq(true)
|
|
124
122
|
end
|
|
125
123
|
|
|
126
|
-
|
|
127
|
-
|
|
124
|
+
it "merge request status test failure" do
|
|
125
|
+
http_stubs.post("api/v3/projects/hal%2Fhal9000/statuses/#{"0" * 40}") { |_env| [401, {}, ""] }
|
|
128
126
|
|
|
129
|
-
|
|
130
|
-
receive_test({}, { git_url: "ssh://git@gitlab.com/hal/hal9000.git" })
|
|
131
|
-
end
|
|
127
|
+
expect { receive_test({}, git_url: "ssh://git@gitlab.com/hal/hal9000.git") }.to raise_error(CC::Service::HTTPError)
|
|
132
128
|
end
|
|
133
129
|
|
|
134
|
-
|
|
135
|
-
response = receive_merge_request({},
|
|
130
|
+
it "merge request unknown state" do
|
|
131
|
+
response = receive_merge_request({}, state: "unknown")
|
|
136
132
|
|
|
137
|
-
|
|
133
|
+
expect({ ok: false, message: "Unknown state" }).to eq(response)
|
|
138
134
|
end
|
|
139
135
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
136
|
+
it "different base url" do
|
|
137
|
+
http_stubs.post("api/v3/projects/hal%2Fhal9000/statuses/#{"0" * 40}") do |env|
|
|
138
|
+
expect(env[:url].to_s).to eq("https://gitlab.hal.org/api/v3/projects/hal%2Fhal9000/statuses/#{"0" * 40}")
|
|
143
139
|
[404, {}, ""]
|
|
144
140
|
end
|
|
145
141
|
|
|
146
|
-
|
|
142
|
+
expect(receive_test({ base_url: "https://gitlab.hal.org" }, git_url: "ssh://git@gitlab.com/hal/hal9000.git")[:ok]).to eq(true)
|
|
147
143
|
end
|
|
148
144
|
|
|
149
145
|
private
|
|
150
146
|
|
|
151
147
|
def expect_status_update(repo, commit_sha, params)
|
|
152
|
-
|
|
153
|
-
|
|
148
|
+
http_stubs.post "api/v3/projects/#{CGI.escape(repo)}/statuses/#{commit_sha}" do |env|
|
|
149
|
+
expect(env[:request_headers]["PRIVATE-TOKEN"]).to eq("123")
|
|
154
150
|
|
|
155
151
|
body = JSON.parse(env[:body])
|
|
156
152
|
|
|
157
153
|
params.each do |k, v|
|
|
158
|
-
|
|
159
|
-
"Unexpected value for #{k}. #{v.inspect} !== #{body[k].inspect}"
|
|
154
|
+
expect(v).to match(body[k])
|
|
160
155
|
end
|
|
161
156
|
end
|
|
162
157
|
end
|
|
163
158
|
|
|
164
159
|
def receive_merge_request(config, event_data)
|
|
165
|
-
|
|
160
|
+
service_receive(
|
|
166
161
|
CC::Service::GitlabMergeRequests,
|
|
167
162
|
{ access_token: "123" }.merge(config),
|
|
168
|
-
{ name: "pull_request", issue_comparison_counts: {
|
|
163
|
+
{ name: "pull_request", issue_comparison_counts: { "fixed" => 1, "new" => 2 } }.merge(event_data),
|
|
169
164
|
)
|
|
170
165
|
end
|
|
171
166
|
|
|
172
167
|
def receive_merge_request_coverage(config, event_data)
|
|
173
|
-
|
|
168
|
+
service_receive(
|
|
174
169
|
CC::Service::GitlabMergeRequests,
|
|
175
170
|
{ access_token: "123" }.merge(config),
|
|
176
|
-
{ name: "pull_request_coverage", issue_comparison_counts: {
|
|
171
|
+
{ name: "pull_request_coverage", issue_comparison_counts: { "fixed" => 1, "new" => 2 } }.merge(event_data),
|
|
177
172
|
)
|
|
178
173
|
end
|
|
179
174
|
|
|
180
175
|
def receive_test(config, event_data = {})
|
|
181
|
-
|
|
176
|
+
service_receive(
|
|
182
177
|
CC::Service::GitlabMergeRequests,
|
|
183
178
|
{ oauth_token: "123" }.merge(config),
|
|
184
|
-
{ name: "test", issue_comparison_counts: {
|
|
179
|
+
{ name: "test", issue_comparison_counts: { "fixed" => 1, "new" => 2 } }.merge(event_data),
|
|
185
180
|
)
|
|
186
181
|
end
|
|
187
182
|
end
|
|
@@ -1,62 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class TestHipChat < CC::Service::TestCase
|
|
4
|
-
def test_test_hook
|
|
1
|
+
describe CC::Service::HipChat, type: :service do
|
|
2
|
+
it "test hook" do
|
|
5
3
|
assert_hipchat_receives(
|
|
6
4
|
"green",
|
|
7
5
|
{ name: "test", repo_name: "Rails" },
|
|
8
|
-
"[Rails] This is a test of the HipChat service hook"
|
|
6
|
+
"[Rails] This is a test of the HipChat service hook",
|
|
9
7
|
)
|
|
10
8
|
end
|
|
11
9
|
|
|
12
|
-
|
|
10
|
+
it "coverage improved" do
|
|
13
11
|
e = event(:coverage, to: 90.2, from: 80)
|
|
14
12
|
|
|
15
13
|
assert_hipchat_receives("green", e, [
|
|
16
14
|
"[Example]",
|
|
17
15
|
"<a href=\"https://codeclimate.com/repos/1/feed\">Test coverage</a>",
|
|
18
16
|
"has improved to 90.2% (+10.2%)",
|
|
19
|
-
"(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)"
|
|
17
|
+
"(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)",
|
|
20
18
|
].join(" "))
|
|
21
19
|
end
|
|
22
20
|
|
|
23
|
-
|
|
21
|
+
it "coverage declined" do
|
|
24
22
|
e = event(:coverage, to: 88.6, from: 94.6)
|
|
25
23
|
|
|
26
24
|
assert_hipchat_receives("red", e, [
|
|
27
25
|
"[Example]",
|
|
28
26
|
"<a href=\"https://codeclimate.com/repos/1/feed\">Test coverage</a>",
|
|
29
27
|
"has declined to 88.6% (-6.0%)",
|
|
30
|
-
"(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)"
|
|
28
|
+
"(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)",
|
|
31
29
|
].join(" "))
|
|
32
30
|
end
|
|
33
31
|
|
|
34
|
-
|
|
32
|
+
it "quality improved" do
|
|
35
33
|
e = event(:quality, to: "A", from: "B")
|
|
36
34
|
|
|
37
35
|
assert_hipchat_receives("green", e, [
|
|
38
36
|
"[Example]",
|
|
39
37
|
"<a href=\"https://codeclimate.com/repos/1/feed\">User</a>",
|
|
40
38
|
"has improved from a B to an A",
|
|
41
|
-
"(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)"
|
|
39
|
+
"(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)",
|
|
42
40
|
].join(" "))
|
|
43
41
|
end
|
|
44
42
|
|
|
45
|
-
|
|
43
|
+
it "quality declined without compare url" do
|
|
46
44
|
e = event(:quality, to: "D", from: "C")
|
|
47
45
|
|
|
48
46
|
assert_hipchat_receives("red", e, [
|
|
49
47
|
"[Example]",
|
|
50
48
|
"<a href=\"https://codeclimate.com/repos/1/feed\">User</a>",
|
|
51
49
|
"has declined from a C to a D",
|
|
52
|
-
"(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)"
|
|
50
|
+
"(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)",
|
|
53
51
|
].join(" "))
|
|
54
52
|
end
|
|
55
53
|
|
|
56
|
-
|
|
54
|
+
it "single vulnerability" do
|
|
57
55
|
e = event(:vulnerability, vulnerabilities: [
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
{ "warning_type" => "critical" },
|
|
57
|
+
])
|
|
60
58
|
|
|
61
59
|
assert_hipchat_receives("red", e, [
|
|
62
60
|
"[Example]",
|
|
@@ -65,11 +63,11 @@ class TestHipChat < CC::Service::TestCase
|
|
|
65
63
|
].join(" "))
|
|
66
64
|
end
|
|
67
65
|
|
|
68
|
-
|
|
66
|
+
it "single vulnerability with location" do
|
|
69
67
|
e = event(:vulnerability, vulnerabilities: [{
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
"warning_type" => "critical",
|
|
69
|
+
"location" => "app/user.rb line 120",
|
|
70
|
+
}])
|
|
73
71
|
|
|
74
72
|
assert_hipchat_receives("red", e, [
|
|
75
73
|
"[Example]",
|
|
@@ -78,14 +76,14 @@ class TestHipChat < CC::Service::TestCase
|
|
|
78
76
|
].join(" "))
|
|
79
77
|
end
|
|
80
78
|
|
|
81
|
-
|
|
79
|
+
it "multiple vulnerabilities" do
|
|
82
80
|
e = event(:vulnerability, warning_type: "critical", vulnerabilities: [{
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
81
|
+
"warning_type" => "unused",
|
|
82
|
+
"location" => "unused",
|
|
83
|
+
}, {
|
|
84
|
+
"warning_type" => "unused",
|
|
85
|
+
"location" => "unused",
|
|
86
|
+
}])
|
|
89
87
|
|
|
90
88
|
assert_hipchat_receives("red", e, [
|
|
91
89
|
"[Example]",
|
|
@@ -94,37 +92,37 @@ class TestHipChat < CC::Service::TestCase
|
|
|
94
92
|
].join(" "))
|
|
95
93
|
end
|
|
96
94
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
[200, {},
|
|
95
|
+
it "receive test" do
|
|
96
|
+
http_stubs.post "/v1/rooms/message" do |_env|
|
|
97
|
+
[200, {}, ""]
|
|
100
98
|
end
|
|
101
99
|
|
|
102
100
|
response = receive_event(name: "test")
|
|
103
101
|
|
|
104
|
-
|
|
102
|
+
expect(response[:message]).to eq("Test message sent")
|
|
105
103
|
end
|
|
106
104
|
|
|
107
105
|
private
|
|
108
106
|
|
|
109
107
|
def assert_hipchat_receives(color, event_data, expected_body)
|
|
110
|
-
|
|
108
|
+
http_stubs.post "/v1/rooms/message" do |env|
|
|
111
109
|
body = Hash[URI.decode_www_form(env[:body])]
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
[200, {},
|
|
110
|
+
expect(body["auth_token"]).to eq("token")
|
|
111
|
+
expect(body["room_id"]).to eq("123")
|
|
112
|
+
expect(body["notify"]).to eq("true")
|
|
113
|
+
expect(body["color"]).to eq(color)
|
|
114
|
+
expect(body["message"]).to eq(expected_body)
|
|
115
|
+
[200, {}, ""]
|
|
118
116
|
end
|
|
119
117
|
|
|
120
118
|
receive_event(event_data)
|
|
121
119
|
end
|
|
122
120
|
|
|
123
121
|
def receive_event(event_data = nil)
|
|
124
|
-
|
|
122
|
+
service_receive(
|
|
125
123
|
CC::Service::HipChat,
|
|
126
124
|
{ auth_token: "token", room_id: "123", notify: true },
|
|
127
|
-
event_data || event(:quality, from: "C", to: "D")
|
|
125
|
+
event_data || event(:quality, from: "C", to: "D"),
|
|
128
126
|
)
|
|
129
127
|
end
|
|
130
128
|
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
describe CC::Service::Invocation::WithErrorHandling do
|
|
2
|
+
it "success returns upstream result" do
|
|
3
|
+
handler = CC::Service::Invocation::WithErrorHandling.new(
|
|
4
|
+
-> { :success },
|
|
5
|
+
FakeLogger.new,
|
|
6
|
+
"not important",
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
expect(handler.call).to eq(:success)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "http errors return relevant data" do
|
|
13
|
+
logger = FakeLogger.new
|
|
14
|
+
env = {
|
|
15
|
+
status: 401,
|
|
16
|
+
params: "params",
|
|
17
|
+
url: "url",
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
handler = CC::Service::Invocation::WithErrorHandling.new(
|
|
21
|
+
-> { raise CC::Service::HTTPError.new("foo", env) },
|
|
22
|
+
logger,
|
|
23
|
+
"prefix",
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
result = handler.call
|
|
27
|
+
expect(result[:ok]).to eq(false)
|
|
28
|
+
expect(result[:status]).to eq(401)
|
|
29
|
+
expect(result[:params]).to eq("params")
|
|
30
|
+
expect(result[:endpoint_url]).to eq("url")
|
|
31
|
+
expect(result[:message]).to eq("foo")
|
|
32
|
+
expect(result[:log_message]).to eq("Exception invoking service: [prefix] (CC::Service::HTTPError) foo. Response: <nil>")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "error returns a hash with explanations" do
|
|
36
|
+
logger = FakeLogger.new
|
|
37
|
+
|
|
38
|
+
handler = CC::Service::Invocation::WithErrorHandling.new(
|
|
39
|
+
-> { raise ArgumentError, "lol" },
|
|
40
|
+
logger,
|
|
41
|
+
"prefix",
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
result = handler.call
|
|
45
|
+
expect(result[:ok]).to eq(false)
|
|
46
|
+
expect(result[:message]).to eq("lol")
|
|
47
|
+
expect(result[:log_message]).to eq("Exception invoking service: [prefix] (ArgumentError) lol")
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
describe CC::Service::Invocation::WithReturnValues do
|
|
2
|
+
it "success returns upstream result" do
|
|
3
|
+
handler = CC::Service::Invocation::WithReturnValues.new(
|
|
4
|
+
-> { :return_value },
|
|
5
|
+
"error message",
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
expect(handler.call).to eq(:return_value)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "empty results returns hash" do
|
|
12
|
+
handler = CC::Service::Invocation::WithReturnValues.new(
|
|
13
|
+
-> { nil },
|
|
14
|
+
"error message",
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
expect({ ok: false, message: "error message" }).to eq(handler.call)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
describe CC::Service::Invocation::WithMetrics, type: :service do
|
|
2
|
+
class FakeInvocation
|
|
3
|
+
def call
|
|
4
|
+
raise CC::Service::HTTPError.new("Whoa", {})
|
|
5
|
+
end
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "statsd error key" do
|
|
9
|
+
statsd = double(:statsd)
|
|
10
|
+
allow(statsd).to receive(:timing)
|
|
11
|
+
expect(statsd).to receive(:increment).with("services.errors.githubpullrequests.cc-service-http_error")
|
|
12
|
+
begin
|
|
13
|
+
CC::Service::Invocation::WithMetrics.new(FakeInvocation.new, statsd, "githubpullrequests").call
|
|
14
|
+
rescue CC::Service::HTTPError
|
|
15
|
+
#noop
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -1,38 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class TestInvocation < Test::Unit::TestCase
|
|
4
|
-
def test_success
|
|
1
|
+
describe CC::Service::Invocation do
|
|
2
|
+
it "success" do
|
|
5
3
|
service = FakeService.new(:some_result)
|
|
6
4
|
|
|
7
5
|
result = CC::Service::Invocation.invoke(service)
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
expect(service.receive_count).to eq(1)
|
|
8
|
+
expect(result).to eq(:some_result)
|
|
11
9
|
end
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
it "success with return values" do
|
|
14
12
|
service = FakeService.new(:some_result)
|
|
15
13
|
|
|
16
14
|
result = CC::Service::Invocation.invoke(service) do |i|
|
|
17
15
|
i.with :return_values, "error"
|
|
18
16
|
end
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
expect(service.receive_count).to eq(1)
|
|
19
|
+
expect(result).to eq(:some_result)
|
|
22
20
|
end
|
|
23
21
|
|
|
24
|
-
|
|
22
|
+
it "failure with return values" do
|
|
25
23
|
service = FakeService.new(nil)
|
|
26
24
|
|
|
27
25
|
result = CC::Service::Invocation.invoke(service) do |i|
|
|
28
26
|
i.with :return_values, "error"
|
|
29
27
|
end
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
expect(service.receive_count).to eq(1)
|
|
30
|
+
expect({ ok: false, message: "error" }).to eq(result)
|
|
33
31
|
end
|
|
34
32
|
|
|
35
|
-
|
|
33
|
+
it "retries" do
|
|
36
34
|
service = FakeService.new
|
|
37
35
|
service.fake_error = RuntimeError.new
|
|
38
36
|
error_occurred = false
|
|
@@ -45,22 +43,22 @@ class TestInvocation < Test::Unit::TestCase
|
|
|
45
43
|
error_occurred = true
|
|
46
44
|
end
|
|
47
45
|
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
expect(error_occurred).not_to be_nil
|
|
47
|
+
expect(service.receive_count).to eq(1 + 3)
|
|
50
48
|
end
|
|
51
49
|
|
|
52
|
-
|
|
50
|
+
it "metrics" do
|
|
53
51
|
statsd = FakeStatsd.new
|
|
54
52
|
|
|
55
53
|
CC::Service::Invocation.invoke(FakeService.new) do |i|
|
|
56
54
|
i.with :metrics, statsd, "a_prefix"
|
|
57
55
|
end
|
|
58
56
|
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
expect(statsd.incremented_keys.length).to eq(1)
|
|
58
|
+
expect(statsd.incremented_keys.first).to eq("services.invocations.a_prefix")
|
|
61
59
|
end
|
|
62
60
|
|
|
63
|
-
|
|
61
|
+
it "metrics on errors" do
|
|
64
62
|
statsd = FakeStatsd.new
|
|
65
63
|
service = FakeService.new
|
|
66
64
|
service.fake_error = RuntimeError.new
|
|
@@ -74,12 +72,12 @@ class TestInvocation < Test::Unit::TestCase
|
|
|
74
72
|
error_occurred = true
|
|
75
73
|
end
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
expect(error_occurred).not_to be_nil
|
|
76
|
+
expect(statsd.incremented_keys.length).to eq(1)
|
|
77
|
+
expect(statsd.incremented_keys.first).to match(/^services\.errors\.a_prefix/)
|
|
80
78
|
end
|
|
81
79
|
|
|
82
|
-
|
|
80
|
+
it "user message" do
|
|
83
81
|
service = FakeService.new
|
|
84
82
|
service.fake_error = CC::Service::HTTPError.new("Boom", {})
|
|
85
83
|
service.override_user_message = "Hey do this"
|
|
@@ -89,11 +87,11 @@ class TestInvocation < Test::Unit::TestCase
|
|
|
89
87
|
i.with :error_handling, logger, "a_prefix"
|
|
90
88
|
end
|
|
91
89
|
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
expect(result[:message]).to eq("Hey do this")
|
|
91
|
+
expect(result[:log_message]).to match(/Boom/)
|
|
94
92
|
end
|
|
95
93
|
|
|
96
|
-
|
|
94
|
+
it "error handling" do
|
|
97
95
|
service = FakeService.new
|
|
98
96
|
service.fake_error = RuntimeError.new("Boom")
|
|
99
97
|
logger = FakeLogger.new
|
|
@@ -102,12 +100,12 @@ class TestInvocation < Test::Unit::TestCase
|
|
|
102
100
|
i.with :error_handling, logger, "a_prefix"
|
|
103
101
|
end
|
|
104
102
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
expect({ ok: false, message: "Boom", log_message: "Exception invoking service: [a_prefix] (RuntimeError) Boom" }).to eq(result)
|
|
104
|
+
expect(logger.logged_errors.length).to eq(1)
|
|
105
|
+
expect(logger.logged_errors.first).to match(/^Exception invoking service: \[a_prefix\]/)
|
|
108
106
|
end
|
|
109
107
|
|
|
110
|
-
|
|
108
|
+
it "multiple middleware" do
|
|
111
109
|
service = FakeService.new
|
|
112
110
|
service.fake_error = RuntimeError.new("Boom")
|
|
113
111
|
logger = FakeLogger.new
|
|
@@ -117,9 +115,9 @@ class TestInvocation < Test::Unit::TestCase
|
|
|
117
115
|
i.with :error_handling, logger
|
|
118
116
|
end
|
|
119
117
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
118
|
+
expect({ ok: false, message: "Boom", log_message: "Exception invoking service: (RuntimeError) Boom" }).to eq(result)
|
|
119
|
+
expect(service.receive_count).to eq(1 + 3)
|
|
120
|
+
expect(logger.logged_errors.length).to eq(1)
|
|
123
121
|
end
|
|
124
122
|
|
|
125
123
|
private
|
|
@@ -163,5 +161,4 @@ class TestInvocation < Test::Unit::TestCase
|
|
|
163
161
|
def timing(key, value)
|
|
164
162
|
end
|
|
165
163
|
end
|
|
166
|
-
|
|
167
164
|
end
|