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,72 +1,70 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class TestFlowdock < CC::Service::TestCase
4
- def test_valid_project_parameter
5
- @stubs.post '/v1/messages/team_inbox/token' do |env|
1
+ describe CC::Service::Flowdock, type: :service do
2
+ it "valid project parameter" do
3
+ http_stubs.post "/v1/messages/team_inbox/token" do |env|
6
4
  body = Hash[URI.decode_www_form(env[:body])]
7
- assert_equal "Exampleorg", body["project"]
8
- [200, {}, '']
5
+ expect(body["project"]).to eq("Exampleorg")
6
+ [200, {}, ""]
9
7
  end
10
8
 
11
- receive(
9
+ service_receive(
12
10
  CC::Service::Flowdock,
13
11
  { api_token: "token" },
14
- { name: "test", repo_name: "Example.org" }
12
+ name: "test", repo_name: "Example.org",
15
13
  )
16
14
  end
17
15
 
18
- def test_test_hook
16
+ it "test hook" do
19
17
  assert_flowdock_receives(
20
18
  "Test",
21
19
  { name: "test", repo_name: "Example" },
22
- "This is a test of the Flowdock service hook"
20
+ "This is a test of the Flowdock service hook",
23
21
  )
24
22
  end
25
23
 
26
- def test_coverage_improved
24
+ it "coverage improved" do
27
25
  e = event(:coverage, to: 90.2, from: 80)
28
26
 
29
27
  assert_flowdock_receives("Coverage", e, [
30
28
  "<a href=\"https://codeclimate.com/repos/1/feed\">Test coverage</a>",
31
29
  "has improved to 90.2% (+10.2%)",
32
- "(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)"
30
+ "(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)",
33
31
  ].join(" "))
34
32
  end
35
33
 
36
- def test_coverage_declined
34
+ it "coverage declined" do
37
35
  e = event(:coverage, to: 88.6, from: 94.6)
38
36
 
39
37
  assert_flowdock_receives("Coverage", e, [
40
38
  "<a href=\"https://codeclimate.com/repos/1/feed\">Test coverage</a>",
41
39
  "has declined to 88.6% (-6.0%)",
42
- "(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)"
40
+ "(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)",
43
41
  ].join(" "))
44
42
  end
45
43
 
46
- def test_quality_improved
44
+ it "quality improved" do
47
45
  e = event(:quality, to: "A", from: "B")
48
46
 
49
47
  assert_flowdock_receives("Quality", e, [
50
48
  "<a href=\"https://codeclimate.com/repos/1/feed\">User</a>",
51
49
  "has improved from a B to an A",
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
- def test_quality_declined
54
+ it "quality declined" do
57
55
  e = event(:quality, to: "D", from: "C")
58
56
 
59
57
  assert_flowdock_receives("Quality", e, [
60
58
  "<a href=\"https://codeclimate.com/repos/1/feed\">User</a>",
61
59
  "has declined from a C to a D",
62
- "(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)"
60
+ "(<a href=\"https://codeclimate.com/repos/1/compare\">Compare</a>)",
63
61
  ].join(" "))
64
62
  end
65
63
 
66
- def test_single_vulnerability
64
+ it "single vulnerability" do
67
65
  e = event(:vulnerability, vulnerabilities: [
68
- { "warning_type" => "critical" }
69
- ])
66
+ { "warning_type" => "critical" },
67
+ ])
70
68
 
71
69
  assert_flowdock_receives("Vulnerability", e, [
72
70
  "New <a href=\"https://codeclimate.com/repos/1/feed\">critical</a>",
@@ -74,11 +72,11 @@ class TestFlowdock < CC::Service::TestCase
74
72
  ].join(" "))
75
73
  end
76
74
 
77
- def test_single_vulnerability_with_location
75
+ it "single vulnerability with location" do
78
76
  e = event(:vulnerability, vulnerabilities: [{
79
- "warning_type" => "critical",
80
- "location" => "app/user.rb line 120"
81
- }])
77
+ "warning_type" => "critical",
78
+ "location" => "app/user.rb line 120",
79
+ }])
82
80
 
83
81
  assert_flowdock_receives("Vulnerability", e, [
84
82
  "New <a href=\"https://codeclimate.com/repos/1/feed\">critical</a>",
@@ -86,14 +84,14 @@ class TestFlowdock < CC::Service::TestCase
86
84
  ].join(" "))
87
85
  end
88
86
 
89
- def test_multiple_vulnerabilities
87
+ it "multiple vulnerabilities" do
90
88
  e = event(:vulnerability, warning_type: "critical", vulnerabilities: [{
91
- "warning_type" => "unused",
92
- "location" => "unused"
93
- }, {
94
- "warning_type" => "unused",
95
- "location" => "unused"
96
- }])
89
+ "warning_type" => "unused",
90
+ "location" => "unused",
91
+ }, {
92
+ "warning_type" => "unused",
93
+ "location" => "unused",
94
+ }])
97
95
 
98
96
  assert_flowdock_receives("Vulnerability", e, [
99
97
  "2 new <a href=\"https://codeclimate.com/repos/1/feed\">critical</a>",
@@ -101,14 +99,14 @@ class TestFlowdock < CC::Service::TestCase
101
99
  ].join(" "))
102
100
  end
103
101
 
104
- def test_receive_test
105
- @stubs.post request_url do |env|
106
- [200, {}, '']
102
+ it "receive test" do
103
+ http_stubs.post request_url do |_env|
104
+ [200, {}, ""]
107
105
  end
108
106
 
109
107
  response = receive_event(name: "test", repo_name: "foo")
110
108
 
111
- assert_equal "Test message sent", response[:message]
109
+ expect(response[:message]).to eq("Test message sent")
112
110
  end
113
111
 
114
112
  private
@@ -126,23 +124,23 @@ class TestFlowdock < CC::Service::TestCase
126
124
  end
127
125
 
128
126
  def assert_flowdock_receives(subject, event_data, expected_body)
129
- @stubs.post request_url do |env|
127
+ http_stubs.post request_url do |env|
130
128
  body = Hash[URI.decode_www_form(env[:body])]
131
- assert_equal "Code Climate", body["source"]
132
- assert_equal "hello@codeclimate.com", body["from_address"]
133
- assert_equal "Code Climate", body["from_name"]
134
- assert_equal "html", body["format"]
135
- assert_equal subject, body["subject"]
136
- assert_equal "Example", body["project"]
137
- assert_equal expected_body, body["content"]
138
- assert_equal "https://codeclimate.com", body["link"]
139
- [200, {}, '']
129
+ expect(body["source"]).to eq("Code Climate")
130
+ expect(body["from_address"]).to eq("hello@codeclimate.com")
131
+ expect(body["from_name"]).to eq("Code Climate")
132
+ expect(body["format"]).to eq("html")
133
+ expect(body["subject"]).to eq(subject)
134
+ expect(body["project"]).to eq("Example")
135
+ expect(body["content"]).to eq(expected_body)
136
+ expect(body["link"]).to eq("https://codeclimate.com")
137
+ [200, {}, ""]
140
138
  end
141
139
 
142
140
  receive_event(event_data)
143
141
  end
144
142
 
145
143
  def receive_event(event_data = nil)
146
- receive(CC::Service::Flowdock, { api_token: "token" }, event_data || event(:quality, from: "D", to: "C"))
144
+ service_receive(CC::Service::Flowdock, { api_token: "token" }, event_data || event(:quality, from: "D", to: "C"))
147
145
  end
148
146
  end
@@ -1,82 +1,80 @@
1
- require File.expand_path('../helper', __FILE__)
2
-
3
- class TestGitHubIssues < CC::Service::TestCase
4
- def test_creation_success
1
+ describe CC::Service::GitHubIssues, type: :service do
2
+ it "creation success" do
5
3
  id = 1234
6
4
  number = 123
7
5
  url = "https://github.com/#{project}/pulls/#{number}"
8
- stub_http(request_url, [201, {}, %<{"id": #{id}, "number": #{number}, "html_url":"#{url}"}>])
6
+ stub_http(request_url, [201, {}, %({"id": #{id}, "number": #{number}, "html_url":"#{url}"})])
9
7
 
10
8
  response = receive_event
11
9
 
12
- assert_equal id, response[:id]
13
- assert_equal number, response[:number]
14
- assert_equal url, response[:url]
10
+ expect(response[:id]).to eq(id)
11
+ expect(response[:number]).to eq(number)
12
+ expect(response[:url]).to eq(url)
15
13
  end
16
14
 
17
- def test_quality
15
+ it "quality" do
18
16
  assert_github_receives(
19
17
  event(:quality, to: "D", from: "C"),
20
18
  "Refactor User from a D on Code Climate",
21
- "https://codeclimate.com/repos/1/feed"
19
+ "https://codeclimate.com/repos/1/feed",
22
20
  )
23
21
  end
24
22
 
25
- def test_quality_without_rating
23
+ it "quality without rating" do
26
24
  assert_github_receives(
27
25
  event(:quality, to: nil),
28
26
  "Refactor User on Code Climate",
29
- "https://codeclimate.com/repos/1/feed"
27
+ "https://codeclimate.com/repos/1/feed",
30
28
  )
31
29
  end
32
30
 
33
- def test_issue
31
+ it "issue" do
34
32
  payload = {
35
33
  issue: {
36
34
  "check_name" => "Style/LongLine",
37
- "description" => "Line is too long [1000/80]"
35
+ "description" => "Line is too long [1000/80]",
38
36
  },
39
37
  constant_name: "foo.rb",
40
- details_url: "http://example.com/repos/id/foo.rb#issue_123"
38
+ details_url: "http://example.com/repos/id/foo.rb#issue_123",
41
39
  }
42
40
 
43
41
  assert_github_receives(
44
42
  event(:issue, payload),
45
43
  "Fix \"Style/LongLine\" issue in foo.rb",
46
- "Line is too long [1000/80]\n\nhttp://example.com/repos/id/foo.rb#issue_123"
44
+ "Line is too long [1000/80]\n\nhttp://example.com/repos/id/foo.rb#issue_123",
47
45
  )
48
46
  end
49
47
 
50
- def test_vulnerability
48
+ it "vulnerability" do
51
49
  assert_github_receives(
52
50
  event(:vulnerability, vulnerabilities: [{
53
- "warning_type" => "critical",
54
- "location" => "app/user.rb line 120"
55
- }]),
51
+ "warning_type" => "critical",
52
+ "location" => "app/user.rb line 120",
53
+ }]),
56
54
  "New critical issue found in app/user.rb line 120",
57
- "A critical vulnerability was found by Code Climate in app/user.rb line 120.\n\nhttps://codeclimate.com/repos/1/feed"
55
+ "A critical vulnerability was found by Code Climate in app/user.rb line 120.\n\nhttps://codeclimate.com/repos/1/feed",
58
56
  )
59
57
  end
60
58
 
61
- def test_receive_test
62
- @stubs.post request_url do |env|
59
+ it "receive test" do
60
+ http_stubs.post request_url do |_env|
63
61
  [200, {}, '{"number": 2, "html_url": "http://foo.bar"}']
64
62
  end
65
63
 
66
64
  response = receive_event(name: "test")
67
65
 
68
- assert_equal "Issue <a href='http://foo.bar'>#2</a> created.", response[:message]
66
+ expect(response[:message]).to eq("Issue <a href='http://foo.bar'>#2</a> created.")
69
67
  end
70
68
 
71
- def test_different_base_url
72
- @stubs.post request_url do |env|
73
- assert env[:url].to_s == "http://example.com/#{request_url}"
69
+ it "different base url" do
70
+ http_stubs.post request_url do |env|
71
+ expect(env[:url].to_s).to eq("http://example.com/#{request_url}")
74
72
  [200, {}, '{"number": 2, "html_url": "http://foo.bar"}']
75
73
  end
76
74
 
77
75
  response = receive_event({ name: "test" }, base_url: "http://example.com")
78
76
 
79
- assert_equal "Issue <a href='http://foo.bar'>#2</a> created.", response[:message]
77
+ expect(response[:message]).to eq("Issue <a href='http://foo.bar'>#2</a> created.")
80
78
  end
81
79
 
82
80
  private
@@ -94,22 +92,22 @@ class TestGitHubIssues < CC::Service::TestCase
94
92
  end
95
93
 
96
94
  def assert_github_receives(event_data, title, ticket_body)
97
- @stubs.post request_url do |env|
95
+ http_stubs.post request_url do |env|
98
96
  body = JSON.parse(env[:body])
99
- assert_equal "token #{oauth_token}", env[:request_headers]["Authorization"]
100
- assert_equal title, body["title"]
101
- assert_equal ticket_body, body["body"]
102
- [200, {}, '{}']
97
+ expect(env[:request_headers]["Authorization"]).to eq("token #{oauth_token}")
98
+ expect(body["title"]).to eq(title)
99
+ expect(body["body"]).to eq(ticket_body)
100
+ [200, {}, "{}"]
103
101
  end
104
102
 
105
103
  receive_event(event_data)
106
104
  end
107
105
 
108
106
  def receive_event(event_data = nil, config = {})
109
- receive(
107
+ service_receive(
110
108
  CC::Service::GitHubIssues,
111
109
  { oauth_token: "123", project: project }.merge(config),
112
- event_data || event(:quality, from: "D", to: "C")
110
+ event_data || event(:quality, from: "D", to: "C"),
113
111
  )
114
112
  end
115
113
  end
@@ -0,0 +1,223 @@
1
+ describe CC::Service::GitHubPullRequests, type: :service do
2
+ it "pull request status pending" do
3
+ expect_status_update("pbrisbin/foo", "abc123", "state" => "pending",
4
+ "description" => /is analyzing/)
5
+
6
+ receive_pull_request({}, github_slug: "pbrisbin/foo",
7
+ commit_sha: "abc123",
8
+ state: "pending")
9
+ end
10
+
11
+ it "pull request status success detailed" do
12
+ expect_status_update("pbrisbin/foo", "abc123", "state" => "success",
13
+ "description" => "Code Climate found 2 new issues and 1 fixed issue.")
14
+
15
+ receive_pull_request(
16
+ {},
17
+ github_slug: "pbrisbin/foo",
18
+ commit_sha: "abc123",
19
+ state: "success",
20
+ )
21
+ end
22
+
23
+ it "pull request status failure" do
24
+ expect_status_update("pbrisbin/foo", "abc123", "state" => "failure",
25
+ "description" => "Code Climate found 2 new issues and 1 fixed issue.")
26
+
27
+ receive_pull_request(
28
+ {},
29
+ github_slug: "pbrisbin/foo",
30
+ commit_sha: "abc123",
31
+ state: "failure",
32
+ )
33
+ end
34
+
35
+ it "pull request status success generic" do
36
+ expect_status_update("pbrisbin/foo", "abc123", "state" => "success",
37
+ "description" => /found 2 new issues and 1 fixed issue/)
38
+
39
+ receive_pull_request({}, github_slug: "pbrisbin/foo",
40
+ commit_sha: "abc123",
41
+ state: "success")
42
+ end
43
+
44
+ it "pull request status error" do
45
+ expect_status_update("pbrisbin/foo", "abc123", "state" => "error",
46
+ "description" => "Code Climate encountered an error attempting to analyze this pull request.")
47
+
48
+ receive_pull_request({}, github_slug: "pbrisbin/foo",
49
+ commit_sha: "abc123",
50
+ state: "error",
51
+ message: nil)
52
+ end
53
+
54
+ it "pull request status error message provided" do
55
+ expect_status_update("pbrisbin/foo", "abc123", "state" => "error",
56
+ "description" => "descriptive message")
57
+
58
+ receive_pull_request({}, github_slug: "pbrisbin/foo",
59
+ commit_sha: "abc123",
60
+ state: "error",
61
+ message: "descriptive message")
62
+ end
63
+
64
+ it "pull request status skipped" do
65
+ expect_status_update("pbrisbin/foo", "abc123", "state" => "success",
66
+ "description" => /skipped analysis/)
67
+
68
+ receive_pull_request({}, github_slug: "pbrisbin/foo",
69
+ commit_sha: "abc123",
70
+ state: "skipped")
71
+ end
72
+
73
+ it "pull request coverage status" do
74
+ expect_status_update("pbrisbin/foo", "abc123", "state" => "success",
75
+ "description" => "87% test coverage (+2%)")
76
+
77
+ receive_pull_request_coverage({},
78
+ github_slug: "pbrisbin/foo",
79
+ commit_sha: "abc123",
80
+ state: "success",
81
+ covered_percent: 87,
82
+ covered_percent_delta: 2.0)
83
+ end
84
+
85
+ it "pull request status test success" do
86
+ http_stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |_env| [422, {}, ""] }
87
+
88
+ expect(receive_test({}, github_slug: "pbrisbin/foo")[:ok]).to eq(true)
89
+ end
90
+
91
+ it "pull request status test doesnt blow up when unused keys present in config" do
92
+ http_stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |_env| [422, {}, ""] }
93
+
94
+ expect(receive_test({ wild_flamingo: true }, github_slug: "pbrisbin/foo")[:ok]).to eq(true)
95
+ end
96
+
97
+ it "pull request status test failure" do
98
+ http_stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") { |_env| [401, {}, ""] }
99
+
100
+ expect { receive_test({}, github_slug: "pbrisbin/foo") }.to raise_error(CC::Service::HTTPError)
101
+ end
102
+
103
+ it "pull request unknown state" do
104
+ response = receive_pull_request({}, state: "unknown")
105
+
106
+ expect({ ok: false, message: "Unknown state" }).to eq(response)
107
+ end
108
+
109
+ it "different base url" do
110
+ http_stubs.post("/repos/pbrisbin/foo/statuses/#{"0" * 40}") do |env|
111
+ expect(env[:url].to_s).to eq("http://example.com/repos/pbrisbin/foo/statuses/#{"0" * 40}")
112
+ [422, { "x-oauth-scopes" => "gist, user, repo" }, ""]
113
+ end
114
+
115
+ expect(receive_test({ base_url: "http://example.com" }, github_slug: "pbrisbin/foo")[:ok]).to eq(true)
116
+ end
117
+
118
+ it "default context" do
119
+ expect_status_update("gordondiggs/ellis", "abc123", "context" => "codeclimate",
120
+ "state" => "pending")
121
+
122
+ receive_pull_request({}, github_slug: "gordondiggs/ellis",
123
+ commit_sha: "abc123",
124
+ state: "pending")
125
+ end
126
+
127
+ it "different context" do
128
+ expect_status_update("gordondiggs/ellis", "abc123", "context" => "sup",
129
+ "state" => "pending")
130
+
131
+ receive_pull_request({ context: "sup" }, github_slug: "gordondiggs/ellis",
132
+ commit_sha: "abc123",
133
+ state: "pending")
134
+ end
135
+
136
+ describe "pr status rollout" do
137
+ it "does not send the status if username is not part of rollout" do
138
+ instance = service(
139
+ CC::Service::GitHubPullRequests,
140
+ { oauth_token: "123", rollout_usernames: "sup" },
141
+ { name: "pull_request", github_slug: "gordondiggs/ellis", commit_sha: "abc123", state: "pending", github_login: "abbynormal", github_user_id: 1234 },
142
+ )
143
+
144
+ expect(instance).not_to receive(:update_status_pending)
145
+
146
+ instance.receive
147
+ end
148
+
149
+ it "does not send the status if user not part of percentage" do
150
+ instance = service(
151
+ CC::Service::GitHubPullRequests,
152
+ { oauth_token: "123", rollout_percentage: 20 },
153
+ { name: "pull_request", github_slug: "gordondiggs/ellis", commit_sha: "abc123", state: "pending", github_login: "abbynormal", github_user_id: 1234 },
154
+ )
155
+
156
+ expect(instance).not_to receive(:update_status_pending)
157
+
158
+ instance.receive
159
+ end
160
+
161
+ it "does send the status if username is part of rollout" do
162
+ instance = service(
163
+ CC::Service::GitHubPullRequests,
164
+ { oauth_token: "123", rollout_usernames: "abbynormal", rollout_percentage: 0 },
165
+ { name: "pull_request", github_slug: "gordondiggs/ellis", commit_sha: "abc123", state: "pending", github_login: "abbynormal", github_user_id: 1234 },
166
+ )
167
+
168
+ expect_status_update("gordondiggs/ellis", "abc123", "state" => "pending")
169
+
170
+ instance.receive
171
+ end
172
+
173
+ it "does send the status if user falls under rollout percentage" do
174
+ instance = service(
175
+ CC::Service::GitHubPullRequests,
176
+ { oauth_token: "123", rollout_usernames: "sup", rollout_percentage: 60 },
177
+ { name: "pull_request", github_slug: "gordondiggs/ellis", commit_sha: "abc123", state: "pending", github_login: "abbynormal", github_user_id: 1234 },
178
+ )
179
+
180
+ expect_status_update("gordondiggs/ellis", "abc123", "state" => "pending")
181
+
182
+ instance.receive
183
+ end
184
+ end
185
+
186
+ private
187
+
188
+ def expect_status_update(repo, commit_sha, params)
189
+ http_stubs.post "repos/#{repo}/statuses/#{commit_sha}" do |env|
190
+ expect(env[:request_headers]["Authorization"]).to eq("token 123")
191
+
192
+ body = JSON.parse(env[:body])
193
+
194
+ params.each do |k, v|
195
+ expect(v).to match(body[k])
196
+ end
197
+ end
198
+ end
199
+
200
+ def receive_pull_request(config, event_data)
201
+ service_receive(
202
+ CC::Service::GitHubPullRequests,
203
+ { oauth_token: "123" }.merge(config),
204
+ { name: "pull_request", issue_comparison_counts: { "fixed" => 1, "new" => 2 } }.merge(event_data),
205
+ )
206
+ end
207
+
208
+ def receive_pull_request_coverage(config, event_data)
209
+ service_receive(
210
+ CC::Service::GitHubPullRequests,
211
+ { oauth_token: "123" }.merge(config),
212
+ { name: "pull_request_coverage", issue_comparison_counts: { "fixed" => 1, "new" => 2 } }.merge(event_data),
213
+ )
214
+ end
215
+
216
+ def receive_test(config, event_data = {})
217
+ service_receive(
218
+ CC::Service::GitHubPullRequests,
219
+ { oauth_token: "123" }.merge(config),
220
+ { name: "test", issue_comparison_counts: { "fixed" => 1, "new" => 2 } }.merge(event_data),
221
+ )
222
+ end
223
+ end