Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/asana
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This service adds commit messages as comments to Asana tasks. Once enabled, commits messages are checked for Asana task ID
in the format #<task id>. Every task ID found will get the commit comment added to it.

A commit with the message:
`stub git call for Grit#heads test f:15 Case#1234`

Will show up as:
`<commiter> pushed to branch <branch> of <user>/<repo>
(http://github.com/<user>/<repo>/commit/<commit id>)
-stub git call for Grit#heads test f:15 Case#1234`

Install Notes
-------------
1. **Auth Token** - User API token. User must have access to task, all comments will be attributed to this user. See: http://developer.asana.com/documentation/#Authentication
2. **Restrict to Branch** - List of branches to which will be inspected for task ID's, if blank all will be inspected
3. **Restrict to Last Comment** - Will only look in the last comment of the push for task ID's
57 changes: 57 additions & 0 deletions services/asana.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Service::Asana < Service
string :auth_token, :restrict_to_branch
boolean :restrict_to_last_commit
white_list :restrict_to_branch, :restrict_to_last_comment

def receive_push
# make sure we have what we need
raise_config_error "Missing 'auth_token'" if data['auth_token'].to_s == ''

user = payload['pusher']['name']
branch = payload['ref'].split('/').last

branch_restriction = data['restrict_to_branch'].to_s
commit_restriction = data['restrict_to_last_comment'] == "1"

# check the branch restriction is poplulated and branch is not included
if branch_restriction.length > 0 && branch_restriction.index(branch) == nil
return
end

rep = payload['repository']['url'].split('/').last(2).join('/')
push_msg = user + " pushed to branch " + branch + " of " + rep

# code heavily derived from fog_bugz.rb
# iterate over commits
if commit_restriction
check_commit( payload['commits'].last, push_msg )
else
payload['commits'].each do |commit|
check_commit( commit, push_msg )
end
end

end

def check_commit(commit, push_msg)
message = " (" + commit['url'] + ")\n- " + commit['message']

task_list = []
message.split("\n").each do |line|
task_list.concat( line.scan(/#(\d+)/) )
end

# post commit to every taskid found
task_list.each do |taskid|

http.basic_auth(data['auth_token'], "")
http.headers['X-GitHub-Event'] = event.to_s

res = http_post "https://app.asana.com/api/1.0/tasks/" + taskid[0] + "/stories", "text=" + push_msg + message
if res.status < 200 || res.status > 299
raise_config_error res.message
end
end
end

end
86 changes: 86 additions & 0 deletions test/asana_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require File.expand_path('../helper', __FILE__)

class AsanaTest < Service::TestCase
def setup
@stubs = Faraday::Adapter::Test::Stubs.new
end

def test_push

@stubs.post "/api/1.0/tasks/1234/stories" do |env|
assert_match /rtomayko pushed to branch master of mojombo\/grit/, env[:body]
assert_match /#1234/, env[:body]
assert_match /Basic MDAwMDo=/, env[:request_headers][:authorization]
[200, {}, '']
end

@stubs.post "/api/1.0/tasks/1235/stories" do |env|
assert_match /rtomayko pushed to branch master of mojombo\/grit/, env[:body]
assert_match /#1235/, env[:body]
assert_match /Basic MDAwMDo=/, env[:request_headers][:authorization]
[200, {}, '']
end

svc = service(
{'auth_token' => '0000'},
modified_payload)
svc.receive_push
end

def test_restricted_comment_commit_push

@stubs.post "/api/1.0/tasks/1234/stories" do |env|
assert_match /rtomayko pushed to branch master of mojombo\/grit/, env[:body]
assert_no_match /stub git call for Grit#heads test f:15 Case#1234/, env[:body]
assert_match /add more comments about #1235 and #1234 throughout/, env[:body]
assert_match /#1234/, env[:body]
assert_match /Basic MDAwMDo=/, env[:request_headers][:authorization]
[200, {}, '']
end

@stubs.post "/api/1.0/tasks/1235/stories" do |env|
assert_match /rtomayko pushed to branch master of mojombo\/grit/, env[:body]
assert_no_match /#1234 clean up heads test f:2hrs #1235/, env[:body]
assert_match /add more comments about #1235 and #1234 throughout/, env[:body]
assert_match /#1235/, env[:body]
assert_match /Basic MDAwMDo=/, env[:request_headers][:authorization]
[200, {}, '']
end

svc = service(
{'auth_token' => '0000',"restrict_to_last_comment" => "1"},
modified_payload)
svc.receive_push
end

def test_restricted_branch_commit_push

@stubs.post "/api/1.0/tasks/1234/stories" do |env|
assert_no_match /stub git call for Grit#heads test f:15 Case#1234/, env[:body]
[200, {}, '']
end

@stubs.post "/api/1.0/tasks/1235/stories" do |env|
assert_no_match /#1234 clean up heads test f:2hrs #1235/, env[:body]
[200, {}, '']
end

svc = service(
{'auth_token' => '0000',"restrict_to_branch" => "foo,bar"},
modified_payload)
svc.receive_push
end

def service(*args)
super Service::Asana, *args
end

def modified_payload
pay = payload
pay['commits'][0]['message'] = "stub git call for Grit#heads test f:15 Case#1234"
pay['commits'][1]['message'] = "#1234 clean up heads test f:2hrs #1235"
pay['commits'][2]['message'] = "add more comments about #1235 and #1234 throughout"
pay
end

end