Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 31, 2019. It is now read-only.

Commit 2fdc300

Browse files
committed
Merge pull request #854 from github/fixup-asana-ref-bug
Fix Asana exceptions when parsing non-existent Asana task IDs
2 parents ae1c845 + a7811da commit 2fdc300

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

lib/services/asana.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def receive_push
3333
end
3434

3535
def check_commit(commit, push_msg)
36-
message = " (" + commit['url'] + ")\n- " + commit['message']
36+
message = "(#{commit['url']})\n- #{commit['message']}"
3737

3838
task_list = []
3939
message.split("\n").each do |line|
@@ -42,16 +42,25 @@ def check_commit(commit, push_msg)
4242
end
4343

4444
# post commit to every taskid found
45-
task_list.each do |taskid|
45+
task_list.flatten.each do |taskid|
46+
deliver_story taskid, "#{push_msg} #{message}"
47+
end
48+
end
4649

47-
http.basic_auth(data['auth_token'], "")
48-
http.headers['X-GitHub-Event'] = event.to_s
50+
def deliver_story(task_id, text)
51+
http.basic_auth(data['auth_token'], "")
52+
http.headers['X-GitHub-Event'] = event.to_s
4953

50-
res = http_post "https://app.asana.com/api/1.0/tasks/" + taskid[0] + "/stories", "text=" + push_msg + message
51-
if res.status < 200 || res.status > 299
52-
raise_config_error res.message
53-
end
54+
res = http_post "https://app.asana.com/api/1.0/tasks/#{task_id}/stories", "text=#{text}"
55+
case res.status
56+
when 200..299
57+
# Success
58+
when 400
59+
# Unknown task. Could be GitHub issue or pull request number. Ignore it.
60+
else
61+
# Try to pull out an error message from the Asana response
62+
error_message = JSON.parse(res.body)['errors'][0]['message'] rescue nil
63+
raise_config_error(error_message || "Unexpected Error")
5464
end
5565
end
56-
5766
end

test/asana_test.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,50 @@ def test_restricted_branch_commit_push
7171
svc.receive_push
7272
end
7373

74+
def test_merge_pull_request_payload
75+
@stubs.post "/api/1.0/tasks/42/stories" do |env|
76+
[400, {}, ''] # Asana responds with 400 for unknown tasks
77+
end
78+
79+
@stubs.post "/api/1.0/tasks/1234/stories" do |env|
80+
assert_match /#1234/, env[:body]
81+
[200, {}, '']
82+
end
83+
84+
svc = service({'auth_token' => '0000'}, merge_payload)
85+
assert_nothing_raised { svc.receive_push }
86+
end
87+
88+
def test_error_response
89+
@stubs.post "/api/1.0/tasks/1234/stories" do |env|
90+
[401, {"Content-Type" => "application/json; charset=UTF-8"}, '{"errors":[{"message":"Not Authorized"}]}']
91+
end
92+
93+
svc = service( {'auth_token' => 'bad-token'}, modified_payload)
94+
95+
begin
96+
svc.receive_push
97+
rescue StandardError => e
98+
assert_equal Service::ConfigurationError, e.class
99+
assert_equal "Not Authorized", e.message
100+
end
101+
end
102+
103+
def test_asana_exception
104+
@stubs.post "/api/1.0/tasks/1234/stories" do |env|
105+
[500, {}, 'Boom!']
106+
end
107+
108+
svc = service( {'auth_token' => '0000'}, modified_payload)
109+
110+
begin
111+
svc.receive_push
112+
rescue StandardError => e
113+
assert_equal Service::ConfigurationError, e.class
114+
assert_equal "Unexpected Error", e.message
115+
end
116+
end
117+
74118
def service(*args)
75119
super Service::Asana, *args
76120
end
@@ -83,4 +127,10 @@ def modified_payload
83127
pay
84128
end
85129

130+
def merge_payload
131+
pay = payload
132+
pay['commits'][0]['message'] = "Merge pull request #42. Fixes Asana task #1234."
133+
pay
134+
end
135+
86136
end

0 commit comments

Comments
 (0)