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

Commit 8cd9a1d

Browse files
committed
Merge pull request #52 from codeclimate/jy-http-calls-return-useful-data
Services return the HTTP call's status
2 parents 0b76fc7 + a58e91d commit 8cd9a1d

30 files changed

+612
-244
lines changed

lib/cc/service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def receive
8787
end
8888
end
8989

90-
nil
90+
{ ok: false, message: "No service handler found" }
9191
end
9292

9393
private

lib/cc/service/http.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,23 @@ def default_http_options
1515
end
1616
end
1717

18-
def http_get(url = nil, params = nil, headers = nil)
18+
def service_get(url = nil, body = nil, headers = nil, &block)
19+
raw_get(url, body, headers, &block)
20+
end
21+
22+
def service_post(url, body = nil, headers = nil, &block)
23+
block ||= lambda{|*args| Hash.new }
24+
response = raw_post(url, body, headers)
25+
{
26+
ok: response.success?,
27+
params: body.as_json,
28+
endpoint_url: url,
29+
status: response.status,
30+
message: "Success"
31+
}.merge(block.call(response))
32+
end
33+
34+
def raw_get(url = nil, params = nil, headers = nil)
1935
http.get do |req|
2036
req.url(url) if url
2137
req.params.update(params) if params
@@ -24,7 +40,7 @@ def http_get(url = nil, params = nil, headers = nil)
2440
end
2541
end
2642

27-
def http_post(url = nil, body = nil, headers = nil)
43+
def raw_post(url = nil, body = nil, headers = nil)
2844
block = Proc.new if block_given?
2945
http_method :post, url, body, headers, &block
3046
end

lib/cc/service/invocation/with_error_handling.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,37 @@ def initialize(invocation, logger, prefix = nil)
88

99
def call
1010
@invocation.call
11-
rescue => ex
12-
@logger.error(error_message(ex))
13-
14-
nil
11+
rescue CC::Service::HTTPError => e
12+
@logger.error(error_message(e))
13+
{
14+
ok: false,
15+
params: e.params,
16+
status: e.status,
17+
endpoint_url: e.endpoint_url,
18+
message: e.user_message || e.message,
19+
log_message: error_message(e)
20+
}
21+
rescue => e
22+
@logger.error(error_message(e))
23+
{
24+
ok: false,
25+
message: e.message,
26+
log_message: error_message(e)
27+
}
1528
end
1629

1730
private
1831

19-
def error_message(ex)
20-
if ex.respond_to?(:response_body)
21-
response_body = ". Response: <#{ex.response_body.inspect}>"
32+
def error_message(e)
33+
if e.respond_to?(:response_body)
34+
response_body = ". Response: <#{e.response_body.inspect}>"
2235
else
2336
response_body = ""
2437
end
2538

2639
message = "Exception invoking service:"
2740
message << " [#{@prefix}]" if @prefix
28-
message << " (#{ex.class}) #{ex.message}"
41+
message << " (#{e.class}) #{e.message}"
2942
message << response_body
3043
end
3144
end

lib/cc/service/response_check.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
class CC::Service
22
class HTTPError < StandardError
3-
attr_reader :response_body, :status
3+
attr_reader :response_body, :status, :params, :endpoint_url
4+
attr_accessor :user_message
45

56
def initialize(message, env)
67
@response_body = env[:body]
78
@status = env[:status]
9+
@params = env[:params]
10+
@endpoint_url = env[:url]
811

912
super(message)
1013
end

lib/cc/services/asana.rb

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ class Config < CC::Service::Config
1414
validates :workspace_id, presence: true
1515
end
1616

17+
ENDPOINT = "https://app.asana.com/api/1.0/tasks"
18+
1719
self.title = "Asana"
1820
self.description = "Create tasks in Asana"
1921
self.issue_tracker = true
2022

2123
def receive_test
2224
result = create_task("Test task from Code Climate")
23-
24-
{
25-
ok: true,
26-
message: "Ticked <a href='#{result[:url]}'>#{result[:id]}</a> created."
27-
}
28-
rescue => ex
29-
{ ok: false, message: ex.message }
25+
result.merge(
26+
message: "Ticket <a href='#{result[:url]}'>#{result[:id]}</a> created."
27+
)
28+
rescue CC::Service::HTTPError => ex
29+
body = JSON.parse(ex.response_body)
30+
ex.user_message = body["errors"].map{|e| e["message"] }.join(" ")
31+
raise ex
3032
end
3133

34+
3235
def receive_quality
3336
title = "Refactor #{constant_name} from #{rating} on Code Climate"
3437

@@ -45,6 +48,18 @@ def receive_vulnerability
4548
private
4649

4750
def create_task(name)
51+
params = generate_params(name)
52+
authenticate_http
53+
http.headers["Content-Type"] = "application/json"
54+
service_post(ENDPOINT, params.to_json) do |response|
55+
body = JSON.parse(response.body)
56+
id = body['data']['id']
57+
url = "https://app.asana.com/0/#{config.workspace_id}/#{id}"
58+
{ id: id, url: url }
59+
end
60+
end
61+
62+
def generate_params(name)
4863
params = {
4964
data: { workspace: config.workspace_id, name: name }
5065
}
@@ -58,18 +73,11 @@ def create_task(name)
5873
params[:data][:assignee] = config.assignee
5974
end
6075

61-
http.headers["Content-Type"] = "application/json"
62-
http.basic_auth(config.api_key, "")
63-
64-
url = "https://app.asana.com/api/1.0/tasks"
65-
res = http_post(url, params.to_json)
66-
67-
body = JSON.parse(res.body)
68-
69-
id = body['data']['id']
70-
url = "https://app.asana.com/0/#{config.workspace_id}/#{id}"
76+
params
77+
end
7178

72-
{ id: id, url: url }
79+
def authenticate_http
80+
http.basic_auth(config.api_key, "")
7381
end
7482

7583
end

lib/cc/services/campfire.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ class Config < CC::Service::Config
1515
self.description = "Send messages to a Campfire chat room"
1616

1717
def receive_test
18-
speak(formatter.format_test)
19-
20-
{ ok: true, message: "Test message sent" }
21-
rescue => ex
22-
{ ok: false, message: ex.message }
18+
speak(formatter.format_test).merge(
19+
message: "Test message sent"
20+
)
2321
end
2422

2523
def receive_coverage
@@ -42,10 +40,10 @@ def formatter
4240

4341
def speak(line)
4442
http.headers['Content-Type'] = 'application/json'
45-
body = { message: { body: line } }
43+
params = { message: { body: line } }
4644

4745
http.basic_auth(config.token, "X")
48-
http_post(speak_uri, body.to_json)
46+
service_post(speak_uri, params.to_json)
4947
end
5048

5149
def speak_uri

lib/cc/services/flowdock.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ class Config < CC::Service::Config
1313
self.description = "Send messages to a Flowdock inbox"
1414

1515
def receive_test
16-
notify("Test", repo_name, formatter.format_test)
17-
18-
{ ok: true, message: "Test message sent" }
19-
rescue => ex
20-
{ ok: false, message: ex.message }
16+
notify("Test", repo_name, formatter.format_test).merge(
17+
message: "Test message sent"
18+
)
2119
end
2220

2321
def receive_coverage
@@ -57,6 +55,7 @@ def notify(subject, project, content)
5755

5856
url = "#{BASE_URL}/messages/team_inbox/#{config.api_token}"
5957
http.headers["User-Agent"] = "Code Climate"
60-
http_post(url, params)
58+
59+
service_post(url, params)
6160
end
6261
end

lib/cc/services/github_issues.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ class Config < CC::Service::Config
2121

2222
def receive_test
2323
result = create_issue("Test ticket from Code Climate", "")
24-
25-
{
26-
ok: true,
24+
result.merge(
2725
message: "Issue <a href='#{result[:url]}'>##{result[:number]}</a> created."
28-
}
29-
rescue => ex
30-
{ ok: false, message: ex.message }
26+
)
27+
rescue CC::Service::HTTPError => e
28+
body = JSON.parse(e.response_body)
29+
e.user_message = body["message"]
30+
raise e
3131
end
3232

3333
def receive_quality
@@ -59,15 +59,14 @@ def create_issue(title, issue_body)
5959
http.headers["User-Agent"] = "Code Climate"
6060

6161
url = "#{BASE_URL}/repos/#{config.project}/issues"
62-
res = http_post(url, params.to_json)
63-
64-
body = JSON.parse(res.body)
65-
66-
{
67-
id: body["id"],
68-
number: body["number"],
69-
url: body["html_url"]
70-
}
62+
service_post(url, params.to_json) do |response|
63+
body = JSON.parse(response.body)
64+
{
65+
id: body["id"],
66+
number: body["number"],
67+
url: body["html_url"]
68+
}
69+
end
7170
end
7271

7372
end

0 commit comments

Comments
 (0)