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,5 +1,4 @@
1
1
  module CC::Service::VulnerabilityHelper
2
-
3
2
  def vulnerability
4
3
  vulnerabilities.first || {}
5
4
  end
@@ -27,5 +26,4 @@ module CC::Service::VulnerabilityHelper
27
26
  vulnerability["warning_type"]
28
27
  end
29
28
  end
30
-
31
29
  end
@@ -47,7 +47,7 @@ module CC
47
47
  end
48
48
  end
49
49
 
50
- private
50
+ private
51
51
 
52
52
  def both_issue_counts_zero?
53
53
  issue_counts.all?(&:zero?)
@@ -56,16 +56,12 @@ module CC
56
56
  def formatted_fixed_issues
57
57
  if @fixed_count > 0
58
58
  "#{number_to_delimited(@fixed_count)} fixed #{"issue".pluralize(@fixed_count)}"
59
- else
60
- nil
61
59
  end
62
60
  end
63
61
 
64
62
  def formatted_new_issues
65
63
  if @new_count > 0
66
64
  "#{number_to_delimited(@new_count)} new #{"issue".pluralize(@new_count)}"
67
- else
68
- nil
69
65
  end
70
66
  end
71
67
 
@@ -9,7 +9,7 @@ class CC::PullRequests < CC::Service
9
9
  setup_http
10
10
  state = @payload["state"]
11
11
 
12
- if %w[pending success failure skipped error].include?(state)
12
+ if %w[pending success failure skipped error].include?(state) && report_status?
13
13
  send("update_status_#{state}")
14
14
  else
15
15
  @response = simple_failure("Unknown state")
@@ -22,7 +22,7 @@ class CC::PullRequests < CC::Service
22
22
  setup_http
23
23
  state = @payload["state"]
24
24
 
25
- if state == "success"
25
+ if state == "success" && report_status?
26
26
  update_coverage_status_success
27
27
  else
28
28
  @response = simple_failure("Unknown state")
@@ -33,6 +33,10 @@ class CC::PullRequests < CC::Service
33
33
 
34
34
  private
35
35
 
36
+ def report_status?
37
+ raise NotImplementedError
38
+ end
39
+
36
40
  def simple_failure(message)
37
41
  { ok: false, message: message }
38
42
  end
@@ -80,7 +84,7 @@ class CC::PullRequests < CC::Service
80
84
  params: params.as_json,
81
85
  status: e.status,
82
86
  endpoint_url: url,
83
- message: "Access token is valid"
87
+ message: "Access token is valid",
84
88
  }
85
89
  else
86
90
  raise
data/lib/cc/service.rb CHANGED
@@ -7,12 +7,12 @@ module CC
7
7
  require "cc/service/invocation"
8
8
  require "axiom/types/password"
9
9
 
10
- dir = File.expand_path '../helpers', __FILE__
10
+ dir = File.expand_path "../helpers", __FILE__
11
11
  Dir["#{dir}/*_helper.rb"].sort.each do |helper|
12
12
  require helper
13
13
  end
14
14
 
15
- dir = File.expand_path '../formatters', __FILE__
15
+ dir = File.expand_path "../formatters", __FILE__
16
16
  Dir["#{dir}/*_formatter.rb"].sort.each do |formatter|
17
17
  require formatter
18
18
  end
@@ -71,7 +71,7 @@ module CC
71
71
  def self.title
72
72
  @title ||= begin
73
73
  hook = name.dup
74
- hook.sub!(/.*:/, '')
74
+ hook.sub!(/.*:/, "")
75
75
  hook
76
76
  end
77
77
  end
@@ -80,15 +80,15 @@ module CC
80
80
  @slug ||= begin
81
81
  hook = name.dup
82
82
  hook.downcase!
83
- hook.sub!(/.*:/, '')
83
+ hook.sub!(/.*:/, "")
84
84
  hook
85
85
  end
86
86
  end
87
87
 
88
88
  def initialize(config, payload)
89
- @payload = payload.stringify_keys
90
- @config = create_config(config)
91
- @event = @payload["name"].to_s
89
+ @payload = payload.stringify_keys
90
+ @config = create_config(config)
91
+ @event = @payload["name"].to_s
92
92
 
93
93
  load_helper
94
94
  validate_event
@@ -103,7 +103,7 @@ module CC
103
103
  end
104
104
  end
105
105
 
106
- { ok: false, ignored: true, message: "No service handler found" }
106
+ { ok: false, ignored: true, message: "No service handler found" }
107
107
  end
108
108
 
109
109
  private
@@ -119,7 +119,7 @@ module CC
119
119
 
120
120
  def validate_event
121
121
  unless ALL_EVENTS.include?(event)
122
- raise ArgumentError.new("Invalid event: #{event}")
122
+ raise ArgumentError, "Invalid event: #{event}"
123
123
  end
124
124
  end
125
125
 
@@ -138,6 +138,5 @@ module CC
138
138
  Config
139
139
  end
140
140
  end
141
-
142
141
  end
143
142
  end
@@ -1,4 +1,4 @@
1
- require 'delegate'
1
+ require "delegate"
2
2
 
3
3
  class CC::Service::Formatter < SimpleDelegator
4
4
  attr_reader :options
@@ -8,7 +8,7 @@ class CC::Service::Formatter < SimpleDelegator
8
8
 
9
9
  @options = {
10
10
  prefix: "[Code Climate]",
11
- prefix_with_repo: true
11
+ prefix_with_repo: true,
12
12
  }.merge(options)
13
13
  end
14
14
 
@@ -25,7 +25,7 @@ class CC::Service::Formatter < SimpleDelegator
25
25
  prefix << "[#{repo_name}]"
26
26
  end
27
27
 
28
- if !prefix.empty?
28
+ unless prefix.empty?
29
29
  prefix << " "
30
30
  end
31
31
 
@@ -1,6 +1,6 @@
1
1
  module CC::Service::Helper
2
- GREEN_HEX = "#38ae6f"
3
- RED_HEX = "#ed2f00"
2
+ GREEN_HEX = "#38ae6f".freeze
3
+ RED_HEX = "#ed2f00".freeze
4
4
 
5
5
  def repo_name
6
6
  payload["repo_name"]
@@ -50,5 +50,4 @@ module CC::Service::Helper
50
50
  raise NotImplementedError,
51
51
  "Event-specific helpers must define #{__method__}"
52
52
  end
53
-
54
53
  end
@@ -4,7 +4,7 @@ require "cc/service/response_check"
4
4
  module CC::Service::HTTP
5
5
  extend ActiveSupport::Concern
6
6
 
7
- REDIRECT_CODES = [302, 307]
7
+ REDIRECT_CODES = [302, 307].freeze
8
8
 
9
9
  module ClassMethods
10
10
  def default_http_options
@@ -12,7 +12,7 @@ module CC::Service::HTTP
12
12
  adapter: :net_http,
13
13
  request: { timeout: 10, open_timeout: 5 },
14
14
  ssl: { verify_depth: 5 },
15
- headers: {}
15
+ headers: {},
16
16
  }
17
17
  end
18
18
  end
@@ -22,13 +22,13 @@ module CC::Service::HTTP
22
22
  end
23
23
 
24
24
  def service_post(url, body = nil, headers = nil, &block)
25
- block ||= lambda{|*_args| Hash.new }
25
+ block ||= ->(*_args) { Hash.new }
26
26
  response = raw_post(url, body, headers)
27
27
  formatted_post_response(response, url, body).merge(block.call(response))
28
28
  end
29
29
 
30
30
  def service_post_with_redirects(url, body = nil, headers = nil, &block)
31
- block ||= lambda{|*_args| Hash.new }
31
+ block ||= ->(*_args) { Hash.new }
32
32
  response = raw_post(url, body, headers)
33
33
  if REDIRECT_CODES.include?(response.status)
34
34
  response = raw_post(response.headers["location"], body, headers)
@@ -39,8 +39,8 @@ module CC::Service::HTTP
39
39
 
40
40
  def raw_get(url = nil, params = nil, headers = nil)
41
41
  http.get do |req|
42
- req.url(url) if url
43
- req.params.update(params) if params
42
+ req.url(url) if url
43
+ req.params.update(params) if params
44
44
  req.headers.update(headers) if headers
45
45
  yield req if block_given?
46
46
  end
@@ -55,9 +55,9 @@ module CC::Service::HTTP
55
55
  block = Proc.new if block_given?
56
56
 
57
57
  http.send(method) do |req|
58
- req.url(url) if url
58
+ req.url(url) if url
59
59
  req.headers.update(headers) if headers
60
- req.body = body if body
60
+ req.body = body if body
61
61
  block.call req if block
62
62
  end
63
63
  end
@@ -87,7 +87,7 @@ module CC::Service::HTTP
87
87
  #
88
88
  # Returns a String path.
89
89
  def ca_file
90
- @ca_file ||= ENV.fetch("CODECLIMATE_CA_FILE", File.expand_path('../../../../config/cacert.pem', __FILE__))
90
+ @ca_file ||= ENV.fetch("CODECLIMATE_CA_FILE", File.expand_path("../../../../config/cacert.pem", __FILE__))
91
91
  end
92
92
 
93
93
  def formatted_post_response(response, url, body)
@@ -1,8 +1,8 @@
1
- require 'cc/service/invocation/invocation_chain'
2
- require 'cc/service/invocation/with_retries'
3
- require 'cc/service/invocation/with_metrics'
4
- require 'cc/service/invocation/with_error_handling'
5
- require 'cc/service/invocation/with_return_values'
1
+ require "cc/service/invocation/invocation_chain"
2
+ require "cc/service/invocation/with_retries"
3
+ require "cc/service/invocation/with_metrics"
4
+ require "cc/service/invocation/with_error_handling"
5
+ require "cc/service/invocation/with_return_values"
6
6
 
7
7
  class CC::Service::Invocation
8
8
  MIDDLEWARE = {
@@ -10,7 +10,7 @@ class CC::Service::Invocation
10
10
  metrics: WithMetrics,
11
11
  error_handling: WithErrorHandling,
12
12
  return_values: WithReturnValues,
13
- }
13
+ }.freeze
14
14
 
15
15
  attr_reader :result
16
16
 
@@ -16,27 +16,28 @@ class CC::Service::Invocation
16
16
  status: e.status,
17
17
  endpoint_url: e.endpoint_url,
18
18
  message: e.user_message || e.message,
19
- log_message: error_message(e)
19
+ log_message: error_message(e),
20
20
  }
21
21
  rescue => e
22
22
  @logger.error(error_message(e))
23
23
  {
24
24
  ok: false,
25
25
  message: e.message,
26
- log_message: error_message(e)
26
+ log_message: error_message(e),
27
27
  }
28
28
  end
29
29
 
30
30
  private
31
31
 
32
32
  def error_message(e)
33
- if e.respond_to?(:response_body)
34
- response_body = ". Response: <#{e.response_body.inspect}>"
35
- else
36
- response_body = ""
37
- end
33
+ response_body =
34
+ if e.respond_to?(:response_body)
35
+ ". Response: <#{e.response_body.inspect}>"
36
+ else
37
+ ""
38
+ end
38
39
 
39
- message = "Exception invoking service:"
40
+ message = "Exception invoking service:"
40
41
  message << " [#{@prefix}]" if @prefix
41
42
  message << " (#{e.class}) #{e.message}"
42
43
  message << response_body
@@ -22,16 +22,16 @@ class CC::Service::Invocation
22
22
  end
23
23
 
24
24
  def success_key
25
- ["services.invocations", @prefix].compact.join('.')
25
+ ["services.invocations", @prefix].compact.join(".")
26
26
  end
27
27
 
28
28
  def timing_key
29
- ["services.timing", @prefix].compact.join('.')
29
+ ["services.timing", @prefix].compact.join(".")
30
30
  end
31
31
 
32
32
  def error_key(ex)
33
- error_string = ex.class.name.underscore.gsub("/", "-")
34
- ["services.errors", @prefix, error_string].compact.join('.')
33
+ error_string = ex.class.name.underscore.tr("/", "-")
34
+ ["services.errors", @prefix, error_string].compact.join(".")
35
35
  end
36
36
  end
37
37
  end
@@ -15,4 +15,3 @@ class CC::Service::Invocation
15
15
  end
16
16
  end
17
17
  end
18
-
@@ -5,9 +5,9 @@ class CC::Service
5
5
 
6
6
  def initialize(message, env)
7
7
  @response_body = env[:body]
8
- @status = env[:status]
9
- @params = env[:params]
10
- @endpoint_url = env[:url].to_s
8
+ @status = env[:status]
9
+ @params = env[:params]
10
+ @endpoint_url = env[:url].to_s
11
11
 
12
12
  super(message)
13
13
  end
@@ -25,7 +25,7 @@ class CC::Service
25
25
  end
26
26
  end
27
27
 
28
- private
28
+ private
29
29
 
30
30
  def error_message(env)
31
31
  # We only handle Jira (or responses which look like Jira's). We will add
@@ -37,6 +37,5 @@ class CC::Service
37
37
  end
38
38
  rescue JSON::ParserError
39
39
  end
40
-
41
40
  end
42
41
  end
data/lib/cc/services.rb CHANGED
@@ -6,5 +6,5 @@ require "virtus"
6
6
  require "active_model"
7
7
  require "active_support/core_ext"
8
8
 
9
- require File.expand_path('../service', __FILE__)
10
- require File.expand_path('../pull_requests', __FILE__)
9
+ require File.expand_path("../service", __FILE__)
10
+ require File.expand_path("../pull_requests", __FILE__)
@@ -14,7 +14,7 @@ class CC::Service::Asana < CC::Service
14
14
  validates :workspace_id, presence: true
15
15
  end
16
16
 
17
- ENDPOINT = "https://app.asana.com/api/1.0/tasks"
17
+ ENDPOINT = "https://app.asana.com/api/1.0/tasks".freeze
18
18
 
19
19
  self.title = "Asana"
20
20
  self.description = "Create tasks in Asana"
@@ -23,16 +23,16 @@ class CC::Service::Asana < CC::Service
23
23
  def receive_test
24
24
  result = create_task("Test task from Code Climate")
25
25
  result.merge(
26
- message: "Ticket <a href='#{result[:url]}'>#{result[:id]}</a> created."
26
+ message: "Ticket <a href='#{result[:url]}'>#{result[:id]}</a> created.",
27
27
  )
28
28
  rescue CC::Service::HTTPError => ex
29
29
  body = JSON.parse(ex.response_body)
30
- ex.user_message = body["errors"].map{|e| e["message"] }.join(" ")
30
+ ex.user_message = body["errors"].map { |e| e["message"] }.join(" ")
31
31
  raise ex
32
32
  end
33
33
 
34
34
  def receive_issue
35
- title = %{Fix "#{issue["check_name"]}" issue in #{constant_name}}
35
+ title = %(Fix "#{issue["check_name"]}" issue in #{constant_name})
36
36
 
37
37
  body = [issue["description"], details_url].join("\n\n")
38
38
 
@@ -45,12 +45,12 @@ class CC::Service::Asana < CC::Service
45
45
 
46
46
  def receive_vulnerability
47
47
  formatter = CC::Formatters::TicketFormatter.new(self)
48
- title = formatter.format_vulnerability_title
48
+ title = formatter.format_vulnerability_title
49
49
 
50
50
  create_task("#{title} - #{details_url}")
51
51
  end
52
52
 
53
- private
53
+ private
54
54
 
55
55
  def create_task(name, notes = "")
56
56
  params = generate_params(name, notes)
@@ -58,7 +58,7 @@ private
58
58
  http.headers["Content-Type"] = "application/json"
59
59
  service_post(ENDPOINT, params.to_json) do |response|
60
60
  body = JSON.parse(response.body)
61
- id = body['data']['id']
61
+ id = body["data"]["id"]
62
62
  url = "https://app.asana.com/0/#{config.workspace_id}/#{id}"
63
63
  { id: id, url: url }
64
64
  end
@@ -66,7 +66,7 @@ private
66
66
 
67
67
  def generate_params(name, notes = nil)
68
68
  params = {
69
- data: { workspace: config.workspace_id, name: name, notes: notes }
69
+ data: { workspace: config.workspace_id, name: name, notes: notes },
70
70
  }
71
71
 
72
72
  if config.project_id.present?
@@ -84,5 +84,4 @@ private
84
84
  def authenticate_http
85
85
  http.basic_auth(config.api_key, "")
86
86
  end
87
-
88
87
  end
@@ -16,7 +16,7 @@ class CC::Service::Campfire < CC::Service
16
16
 
17
17
  def receive_test
18
18
  speak(formatter.format_test).merge(
19
- message: "Test message sent"
19
+ message: "Test message sent",
20
20
  )
21
21
  end
22
22
 
@@ -39,7 +39,7 @@ class CC::Service::Campfire < CC::Service
39
39
  end
40
40
 
41
41
  def speak(line)
42
- http.headers['Content-Type'] = 'application/json'
42
+ http.headers["Content-Type"] = "application/json"
43
43
  params = { message: { body: line } }
44
44
 
45
45
  http.basic_auth(config.token, "X")
@@ -51,5 +51,4 @@ class CC::Service::Campfire < CC::Service
51
51
  room_id = config.room_id
52
52
  "https://#{subdomain}.campfirenow.com/room/#{room_id}/speak.json"
53
53
  end
54
-
55
54
  end