Permalink
Browse files

Add support for individual Pivotal Tracker API keys, so you can have …

…the Tracker comment made by the samer person who did the GitHub commit.
  • Loading branch information...
1 parent 198ac48 commit d30027575906961da33190bee633ea8827a06b4c @chris committed Dec 3, 2009
Showing with 35 additions and 4 deletions.
  1. +15 −0 README
  2. +20 −4 tracker_github_hook.rb
View
15 README
@@ -12,6 +12,14 @@ tracker_github_hook:
tracker_api_token: a1230e72340e3babc96d5e2fab67c18d
tracker_project_id: 123
ref: refs/heads/master
+ user_api_tokens:
+ chris:
+ email: chris@cobaltedge.com
+ tracker_api_token: a1b2c3d4e5f67890
+ alan:
+ email: alan@example.com
+ tracker_api_token: 0987654321abcdef
+
The label ('tracker_github_hook' in this case) is arbitrary and not used, it's
just their to be a useful bit of info to humans/organize the nested settings,
@@ -25,6 +33,13 @@ The "ref" field is also optional; it will tell the hook to ignore commits on
any branches other than the one listed. This is useful to prevent duplication
of comments being pushed into Tracker if you have multiple remote branches.
+The "user_api_tokens" is optional - the primary "tracker_api_token" will be used
+by default. But, if you do supply this block, then the hook will correlate the
+email address of the author of the GitHub commit, to that within this list, and
+if it finds a match, will use the specified Tracker API token. This makes it so
+that the comment in your Tracker story shows up as being made by the same person
+making the GitHub commit (instead of whoever owns the default API token).
+
When you make commits to Git/GitHub, and want a comment and optionally a state
update made to a story in Tracker, add the following text to your commit
message:
View
@@ -25,8 +25,17 @@
PROJECTS = Hash.new
config.each do |project|
- raise "required configuration settings not found" unless project[1]['tracker_api_token'] && project[1]['tracker_project_id']
- PROJECTS[project[1]['github_url']] = { :api_token => project[1]['tracker_api_token'], :project_id => project[1]['tracker_project_id'], :ref => project[1]['ref'] }
+ raise "required configuration settings not found" unless project[1]['tracker_api_token'] && project[1]['tracker_project_id']
+ api_tokens = Hash.new
+ if project[1]['user_api_tokens']
+ project[1]['user_api_tokens'].each_value do |user_info|
+ api_tokens[user_info['email'].downcase] = user_info['tracker_api_token']
+ end
+ end
+ PROJECTS[project[1]['github_url']] = { :api_token => project[1]['tracker_api_token'],
+ :project_id => project[1]['tracker_project_id'],
+ :ref => project[1]['ref'],
+ :user_api_tokens => api_tokens }
end
rescue => e
puts "Failed to startup: #{e.message}"
@@ -57,6 +66,9 @@
def process_commit(tracker_info, commit)
# get commit message
message = commit['message']
+
+ # get API token for the user who made the commit, if possible
+ api_token = api_token_for_user(tracker_info, commit['author']['email'])
# see if there is a Tracker story trigger, and if so, get story ID
message.scan(/\[Story(\d+)([^\]]*)\]/) do |tracker_trigger|
@@ -66,7 +78,7 @@ def process_commit(tracker_info, commit)
# post comment to the story
RestClient.post(create_api_url(tracker_info[:project_id], story_id, '/notes'),
"<note><text>(from [#{commit['id']}]) #{message}</text></note>",
- tracker_api_headers(tracker_info[:api_token]))
+ tracker_api_headers(api_token))
# See if we have a state change
state = tracker_trigger[1].match(/.*state:(\s?\w+).*/)
@@ -75,10 +87,14 @@ def process_commit(tracker_info, commit)
RestClient.put(create_api_url(tracker_info[:project_id], story_id),
"<story><current_state>#{state}</current_state></story>",
- tracker_api_headers(tracker_info[:api_token]))
+ tracker_api_headers(api_token))
end
end
end
+
+ def api_token_for_user(tracker_info, email)
+ tracker_info[:user_api_tokens][email.downcase] || tracker_info[:api_token]
+ end
def create_api_url(project_id, story_id, extra_path_elemets='')
"http://www.pivotaltracker.com/services/v1/projects/#{project_id}/stories/#{story_id}#{extra_path_elemets}"

0 comments on commit d300275

Please sign in to comment.