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.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +2 -0
  3. data/.rubocop.yml +1 -0
  4. data/Gemfile +1 -2
  5. data/Rakefile +4 -7
  6. data/base_rubocop.yml +152 -0
  7. data/bin/bundler +5 -5
  8. data/bin/coderay +5 -5
  9. data/bin/nokogiri +5 -5
  10. data/bin/pry +5 -5
  11. data/bin/rake +5 -5
  12. data/codeclimate-services.gemspec +15 -15
  13. data/config/load.rb +2 -2
  14. data/lib/cc/formatters/linked_formatter.rb +2 -2
  15. data/lib/cc/formatters/snapshot_formatter.rb +10 -10
  16. data/lib/cc/formatters/ticket_formatter.rb +0 -2
  17. data/lib/cc/helpers/quality_helper.rb +2 -2
  18. data/lib/cc/helpers/vulnerability_helper.rb +0 -2
  19. data/lib/cc/presenters/pull_requests_presenter.rb +1 -5
  20. data/lib/cc/pull_requests.rb +7 -3
  21. data/lib/cc/service.rb +9 -10
  22. data/lib/cc/service/formatter.rb +3 -3
  23. data/lib/cc/service/helper.rb +2 -3
  24. data/lib/cc/service/http.rb +9 -9
  25. data/lib/cc/service/invocation.rb +6 -6
  26. data/lib/cc/service/invocation/with_error_handling.rb +9 -8
  27. data/lib/cc/service/invocation/with_metrics.rb +4 -4
  28. data/lib/cc/service/invocation/with_return_values.rb +0 -1
  29. data/lib/cc/service/response_check.rb +4 -5
  30. data/lib/cc/services.rb +2 -2
  31. data/lib/cc/services/asana.rb +8 -9
  32. data/lib/cc/services/campfire.rb +2 -3
  33. data/lib/cc/services/flowdock.rb +5 -5
  34. data/lib/cc/services/github_issues.rb +6 -7
  35. data/lib/cc/services/github_pull_requests.rb +42 -5
  36. data/lib/cc/services/gitlab_merge_requests.rb +6 -2
  37. data/lib/cc/services/hipchat.rb +3 -4
  38. data/lib/cc/services/jira.rb +8 -9
  39. data/lib/cc/services/lighthouse.rb +4 -5
  40. data/lib/cc/services/pivotal_tracker.rb +6 -7
  41. data/lib/cc/services/slack.rb +8 -8
  42. data/lib/cc/services/stash_pull_requests.rb +2 -2
  43. data/lib/cc/services/version.rb +1 -1
  44. data/pull_request_test.rb +11 -10
  45. data/service_test.rb +5 -5
  46. data/spec/axiom/types/password_spec.rb +14 -0
  47. data/spec/cc/formatters/snapshot_formatter_spec.rb +44 -0
  48. data/spec/cc/presenters/pull_requests_presenter_spec.rb +45 -0
  49. data/{test/asana_test.rb → spec/cc/service/asana_spec.rb} +27 -29
  50. data/{test/campfire_test.rb → spec/cc/service/campfire_spec.rb} +40 -44
  51. data/{test/flowdock_test.rb → spec/cc/service/flowdock_spec.rb} +46 -48
  52. data/{test/github_issues_test.rb → spec/cc/service/github_issues_spec.rb} +33 -35
  53. data/spec/cc/service/github_pull_requests_spec.rb +223 -0
  54. data/{test/gitlab_merge_requests_test.rb → spec/cc/service/gitlab_merge_requests_spec.rb} +30 -35
  55. data/{test/hipchat_test.rb → spec/cc/service/hipchat_spec.rb} +38 -40
  56. data/spec/cc/service/invocation/error_handling_spec.rb +49 -0
  57. data/spec/cc/service/invocation/return_values_spec.rb +19 -0
  58. data/spec/cc/service/invocation/with_metrics_spec.rb +18 -0
  59. data/{test/invocation_test.rb → spec/cc/service/invocation_spec.rb} +31 -34
  60. data/{test/jira_test.rb → spec/cc/service/jira_spec.rb} +25 -27
  61. data/{test/lighthouse_test.rb → spec/cc/service/lighthouse_spec.rb} +23 -26
  62. data/spec/cc/service/pivotal_tracker_spec.rb +70 -0
  63. data/{test/slack_test.rb → spec/cc/service/slack_spec.rb} +66 -77
  64. data/spec/cc/service/stash_pull_requests_spec.rb +132 -0
  65. data/spec/cc/service_spec.rb +78 -0
  66. data/{test → spec}/fixtures.rb +3 -4
  67. data/spec/spec_helper.rb +37 -0
  68. data/{test → spec}/support/fake_logger.rb +0 -0
  69. data/spec/support/service_context.rb +42 -0
  70. metadata +57 -52
  71. data/test/axiom/types/password_test.rb +0 -22
  72. data/test/formatters/snapshot_formatter_test.rb +0 -47
  73. data/test/github_pull_requests_test.rb +0 -217
  74. data/test/helper.rb +0 -58
  75. data/test/invocation_error_handling_test.rb +0 -51
  76. data/test/invocation_return_values_test.rb +0 -21
  77. data/test/pivotal_tracker_test.rb +0 -73
  78. data/test/presenters/pull_requests_presenter_test.rb +0 -70
  79. data/test/service_test.rb +0 -84
  80. data/test/stash_pull_requests_test.rb +0 -146
  81. data/test/with_metrics_test.rb +0 -19
@@ -1,22 +0,0 @@
1
- require File.expand_path('../../../helper', __FILE__)
2
-
3
- class Axiom::Types::PasswordTest < CC::Service::TestCase
4
- class TestConfiguration < CC::Service::Config
5
- attribute :password_attribute, Password
6
- attribute :string_attribute, String
7
- end
8
-
9
- def test_password_type_inference
10
- assert_equal(
11
- Axiom::Types::Password,
12
- TestConfiguration.attribute_set[:password_attribute].type
13
- )
14
- end
15
-
16
- def test_string_type_inference
17
- assert_equal(
18
- Axiom::Types::String,
19
- TestConfiguration.attribute_set[:string_attribute].type
20
- )
21
- end
22
- end
@@ -1,47 +0,0 @@
1
- require "helper"
2
-
3
- class TestSnapshotFormatter < Test::Unit::TestCase
4
- def described_class
5
- CC::Formatters::SnapshotFormatter::Base
6
- end
7
-
8
- def test_quality_alert_with_new_constants
9
- f = described_class.new({"new_constants" => [{"to" => {"rating" => "D"}}], "changed_constants" => []})
10
- refute_nil f.alert_constants_payload
11
- end
12
-
13
- def test_quality_alert_with_decreased_constants
14
- f = described_class.new({"new_constants" => [],
15
- "changed_constants" => [{"to" => {"rating" => "D"}, "from" => {"rating" => "A"}}]
16
- })
17
- refute_nil f.alert_constants_payload
18
- end
19
-
20
- def test_quality_improvements_with_better_ratings
21
- f = described_class.new({"new_constants" => [],
22
- "changed_constants" => [{"to" => {"rating" => "A"}, "from" => {"rating" => "D"}}]
23
- })
24
- refute_nil f.improved_constants_payload
25
- end
26
-
27
- def test_nothing_set_without_changes
28
- f = described_class.new({"new_constants" => [], "changed_constants" => []})
29
- assert_nil f.alert_constants_payload
30
- assert_nil f.improved_constants_payload
31
- end
32
-
33
- def test_snapshot_formatter_test_with_relaxed_constraints
34
- 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"}}
42
- ]})
43
-
44
- refute_nil f.alert_constants_payload
45
- refute_nil f.improved_constants_payload
46
- end
47
- end
@@ -1,217 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class TestGitHubPullRequests < CC::Service::TestCase
4
- def test_pull_request_status_pending
5
- expect_status_update("pbrisbin/foo", "abc123", {
6
- "state" => "pending",
7
- "description" => /is analyzing/,
8
- })
9
-
10
- receive_pull_request({}, {
11
- github_slug: "pbrisbin/foo",
12
- commit_sha: "abc123",
13
- state: "pending",
14
- })
15
- end
16
-
17
- def test_pull_request_status_success_detailed
18
- expect_status_update("pbrisbin/foo", "abc123", {
19
- "state" => "success",
20
- "description" => "Code Climate found 2 new issues and 1 fixed issue.",
21
- })
22
-
23
- receive_pull_request(
24
- {},
25
- {
26
- github_slug: "pbrisbin/foo",
27
- commit_sha: "abc123",
28
- state: "success"
29
- }
30
- )
31
- end
32
-
33
- def test_pull_request_status_failure
34
- expect_status_update("pbrisbin/foo", "abc123", {
35
- "state" => "failure",
36
- "description" => "Code Climate found 2 new issues and 1 fixed issue.",
37
- })
38
-
39
- receive_pull_request(
40
- {},
41
- {
42
- github_slug: "pbrisbin/foo",
43
- commit_sha: "abc123",
44
- state: "failure"
45
- }
46
- )
47
- end
48
-
49
- def test_pull_request_status_success_generic
50
- expect_status_update("pbrisbin/foo", "abc123", {
51
- "state" => "success",
52
- "description" => /found 2 new issues and 1 fixed issue/,
53
- })
54
-
55
- receive_pull_request({}, {
56
- github_slug: "pbrisbin/foo",
57
- commit_sha: "abc123",
58
- state: "success",
59
- })
60
- end
61
-
62
- def test_pull_request_status_error
63
- expect_status_update("pbrisbin/foo", "abc123", {
64
- "state" => "error",
65
- "description" => "Code Climate encountered an error attempting to analyze this pull request.",
66
- })
67
-
68
- receive_pull_request({}, {
69
- github_slug: "pbrisbin/foo",
70
- commit_sha: "abc123",
71
- state: "error",
72
- message: nil,
73
- })
74
- end
75
-
76
- def test_pull_request_status_error_message_provided
77
- expect_status_update("pbrisbin/foo", "abc123", {
78
- "state" => "error",
79
- "description" => "descriptive message",
80
- })
81
-
82
- receive_pull_request({}, {
83
- github_slug: "pbrisbin/foo",
84
- commit_sha: "abc123",
85
- state: "error",
86
- message: "descriptive message",
87
- })
88
- end
89
-
90
- def test_pull_request_status_skipped
91
- expect_status_update("pbrisbin/foo", "abc123", {
92
- "state" => "success",
93
- "description" => /skipped analysis/,
94
- })
95
-
96
- receive_pull_request({}, {
97
- github_slug: "pbrisbin/foo",
98
- commit_sha: "abc123",
99
- state: "skipped",
100
- })
101
- end
102
-
103
- def test_pull_request_coverage_status
104
- expect_status_update("pbrisbin/foo", "abc123", {
105
- "state" => "success",
106
- "description" => "87% test coverage (+2%)",
107
- })
108
-
109
- receive_pull_request_coverage({},
110
- github_slug: "pbrisbin/foo",
111
- commit_sha: "abc123",
112
- state: "success",
113
- covered_percent: 87,
114
- covered_percent_delta: 2.0,
115
- )
116
- end
117
-
118
- def test_pull_request_status_test_success
119
- @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
120
-
121
- assert receive_test({}, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
122
- end
123
-
124
- def test_pull_request_status_test_doesnt_blow_up_when_unused_keys_present_in_config
125
- @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [422, {}, ""] }
126
-
127
- assert receive_test({ wild_flamingo: true }, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
128
- end
129
-
130
- def test_pull_request_status_test_failure
131
- @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |env| [401, {}, ""] }
132
-
133
- assert_raises(CC::Service::HTTPError) do
134
- receive_test({}, { github_slug: "pbrisbin/foo" })
135
- end
136
- end
137
-
138
- def test_pull_request_unknown_state
139
- response = receive_pull_request({}, { state: "unknown" })
140
-
141
- assert_equal({ ok: false, message: "Unknown state" }, response)
142
- end
143
-
144
- def test_different_base_url
145
- @stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") do |env|
146
- assert env[:url].to_s == "http://example.com/repos/pbrisbin/foo/statuses/#{"0" * 40}"
147
- [422, { "x-oauth-scopes" => "gist, user, repo" }, ""]
148
- end
149
-
150
- assert receive_test({ base_url: "http://example.com" }, { github_slug: "pbrisbin/foo" })[:ok], "Expected test of pull request to be true"
151
- end
152
-
153
- def test_default_context
154
- expect_status_update("gordondiggs/ellis", "abc123", {
155
- "context" => "codeclimate",
156
- "state" => "pending",
157
- })
158
-
159
- receive_pull_request({}, {
160
- github_slug: "gordondiggs/ellis",
161
- commit_sha: "abc123",
162
- state: "pending",
163
- })
164
- end
165
-
166
- def test_different_context
167
- expect_status_update("gordondiggs/ellis", "abc123", {
168
- "context" => "sup",
169
- "state" => "pending",
170
- })
171
-
172
- receive_pull_request({ context: "sup" }, {
173
- github_slug: "gordondiggs/ellis",
174
- commit_sha: "abc123",
175
- state: "pending",
176
- })
177
- end
178
-
179
- private
180
-
181
- def expect_status_update(repo, commit_sha, params)
182
- @stubs.post "repos/#{repo}/statuses/#{commit_sha}" do |env|
183
- assert_equal "token 123", env[:request_headers]["Authorization"]
184
-
185
- body = JSON.parse(env[:body])
186
-
187
- params.each do |k, v|
188
- assert v === body[k],
189
- "Unexpected value for #{k}. #{v.inspect} !== #{body[k].inspect}"
190
- end
191
- end
192
- end
193
-
194
- def receive_pull_request(config, event_data)
195
- receive(
196
- CC::Service::GitHubPullRequests,
197
- { oauth_token: "123" }.merge(config),
198
- { name: "pull_request", issue_comparison_counts: {'fixed' => 1, 'new' => 2} }.merge(event_data)
199
- )
200
- end
201
-
202
- def receive_pull_request_coverage(config, event_data)
203
- receive(
204
- CC::Service::GitHubPullRequests,
205
- { oauth_token: "123" }.merge(config),
206
- { name: "pull_request_coverage", issue_comparison_counts: {'fixed' => 1, 'new' => 2} }.merge(event_data)
207
- )
208
- end
209
-
210
- def receive_test(config, event_data = {})
211
- receive(
212
- CC::Service::GitHubPullRequests,
213
- { oauth_token: "123" }.merge(config),
214
- { name: "test", issue_comparison_counts: {'fixed' => 1, 'new' => 2} }.merge(event_data)
215
- )
216
- end
217
- end
data/test/helper.rb DELETED
@@ -1,58 +0,0 @@
1
- require 'test/unit'
2
- require 'mocha/test_unit'
3
- require 'pp'
4
-
5
- require "codeclimate-test-reporter"
6
- CodeClimate::TestReporter.start
7
-
8
- cwd = File.expand_path(File.dirname(__FILE__))
9
- require "#{cwd}/../config/load"
10
- require "#{cwd}/fixtures"
11
- Dir["#{cwd}/support/*.rb"].sort.each do |helper|
12
- require helper
13
- end
14
- CC::Service.load_services
15
-
16
-
17
- class CC::Service::TestCase < Test::Unit::TestCase
18
- def setup
19
- @stubs = Faraday::Adapter::Test::Stubs.new
20
-
21
- I18n.enforce_available_locales = true
22
- end
23
-
24
- def teardown
25
- @stubs.verify_stubbed_calls
26
- end
27
-
28
- def service(klass, data, payload)
29
- service = klass.new(data, payload)
30
- service.http :adapter => [:test, @stubs]
31
- service
32
- end
33
-
34
- def receive(*args)
35
- service(*args).receive
36
- end
37
-
38
- def service_post(*args)
39
- service(
40
- CC::Service,
41
- { data: "my data" },
42
- event(:quality, to: "D", from: "C")
43
- ).service_post(*args)
44
- end
45
-
46
- def service_post_with_redirects(*args)
47
- service(
48
- CC::Service,
49
- { data: "my data" },
50
- event(:quality, to: "D", from: "C")
51
- ).service_post_with_redirects(*args)
52
- end
53
-
54
- def stub_http(url, response = nil, &block)
55
- block ||= lambda{|*args| response }
56
- @stubs.post(url, &block)
57
- end
58
- end
@@ -1,51 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class InvocationErrorHandling < CC::Service::TestCase
4
- def test_success_returns_upstream_result
5
- handler = CC::Service::Invocation::WithErrorHandling.new(
6
- lambda { :success },
7
- FakeLogger.new,
8
- "not important"
9
- )
10
-
11
- assert_equal :success, handler.call
12
- end
13
-
14
- def test_http_errors_return_relevant_data
15
- logger = FakeLogger.new
16
- env = {
17
- status: 401,
18
- params: "params",
19
- url: "url"
20
- }
21
-
22
- handler = CC::Service::Invocation::WithErrorHandling.new(
23
- lambda { raise CC::Service::HTTPError.new("foo", env) },
24
- logger,
25
- "prefix"
26
- )
27
-
28
- result = handler.call
29
- assert_equal false, result[:ok]
30
- assert_equal 401, result[:status]
31
- assert_equal "params", result[:params]
32
- assert_equal "url", result[:endpoint_url]
33
- assert_equal "foo", result[:message]
34
- assert_equal "Exception invoking service: [prefix] (CC::Service::HTTPError) foo. Response: <nil>", result[:log_message]
35
- end
36
-
37
- def test_error_returns_a_hash_with_explanations
38
- logger = FakeLogger.new
39
-
40
- handler = CC::Service::Invocation::WithErrorHandling.new(
41
- lambda { raise ArgumentError.new("lol") },
42
- logger,
43
- "prefix"
44
- )
45
-
46
- result = handler.call
47
- assert_equal false, result[:ok]
48
- assert_equal "lol", result[:message]
49
- assert_equal "Exception invoking service: [prefix] (ArgumentError) lol", result[:log_message]
50
- end
51
- end
@@ -1,21 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class InvocationReturnValuesTest < CC::Service::TestCase
4
- def test_success_returns_upstream_result
5
- handler = CC::Service::Invocation::WithReturnValues.new(
6
- lambda { :return_value },
7
- "error message"
8
- )
9
-
10
- assert_equal :return_value, handler.call
11
- end
12
-
13
- def test_empty_results_returns_hash
14
- handler = CC::Service::Invocation::WithReturnValues.new(
15
- lambda { nil },
16
- "error message"
17
- )
18
-
19
- assert_equal( {ok: false, message: "error message"}, handler.call )
20
- end
21
- end
@@ -1,73 +0,0 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class TestPivotalTracker < CC::Service::TestCase
4
- def test_quality
5
- response = assert_pivotal_receives(
6
- event(:quality, to: "D", from: "C"),
7
- "Refactor User from a D on Code Climate",
8
- "https://codeclimate.com/repos/1/feed"
9
- )
10
- assert_equal "123", response[:id]
11
- assert_equal "http://pivotaltracker.com/n/projects/123/stories/123",
12
- response[:url]
13
- end
14
-
15
- def test_vulnerability
16
- assert_pivotal_receives(
17
- event(:vulnerability, vulnerabilities: [{
18
- "warning_type" => "critical",
19
- "location" => "app/user.rb line 120"
20
- }]),
21
- "New critical issue found in app/user.rb line 120",
22
- "A critical vulnerability was found by Code Climate in app/user.rb line 120.\n\nhttps://codeclimate.com/repos/1/feed"
23
- )
24
- end
25
-
26
- def test_issue
27
- payload = {
28
- issue: {
29
- "check_name" => "Style/LongLine",
30
- "description" => "Line is too long [1000/80]"
31
- },
32
- constant_name: "foo.rb",
33
- details_url: "http://example.com/repos/id/foo.rb#issue_123"
34
- }
35
-
36
- assert_pivotal_receives(
37
- event(:issue, payload),
38
- "Fix \"Style/LongLine\" issue in foo.rb",
39
- "Line is too long [1000/80]\n\nhttp://example.com/repos/id/foo.rb#issue_123"
40
- )
41
- end
42
-
43
- def test_receive_test
44
- @stubs.post 'services/v3/projects/123/stories' do |env|
45
- [200, {}, '<story><id>123</id><url>http://foo.bar</url></story>']
46
- end
47
-
48
- response = receive_event(name: "test")
49
-
50
- assert_equal "Ticket <a href='http://foo.bar'>123</a> created.", response[:message]
51
- end
52
-
53
- private
54
-
55
- def assert_pivotal_receives(event_data, name, description)
56
- @stubs.post 'services/v3/projects/123/stories' do |env|
57
- body = Hash[URI.decode_www_form(env[:body])]
58
- assert_equal "token", env[:request_headers]["X-TrackerToken"]
59
- assert_equal name, body["story[name]"]
60
- assert_equal description, body["story[description]"]
61
- [200, {}, '<doc><story><id>123</id><url>http://pivotaltracker.com/n/projects/123/stories/123</url></story></doc>']
62
- end
63
- receive_event(event_data)
64
- end
65
-
66
- def receive_event(event_data = nil)
67
- receive(
68
- CC::Service::PivotalTracker,
69
- { api_token: "token", project_id: "123" },
70
- event_data || event(:quality, from: "C", to: "D")
71
- )
72
- end
73
- end