Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit d30027575906961da33190bee633ea8827a06b4c 1 parent 198ac48
Chris Bailey authored
15  README
@@ -12,6 +12,14 @@ tracker_github_hook:
12 12
   tracker_api_token: a1230e72340e3babc96d5e2fab67c18d
13 13
   tracker_project_id: 123
14 14
   ref: refs/heads/master
  15
+  user_api_tokens:
  16
+    chris:
  17
+      email: chris@cobaltedge.com
  18
+      tracker_api_token: a1b2c3d4e5f67890
  19
+    alan:
  20
+      email: alan@example.com
  21
+      tracker_api_token: 0987654321abcdef
  22
+  
15 23
   
16 24
 The label ('tracker_github_hook' in this case) is arbitrary and not used, it's
17 25
 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
25 33
 any branches other than the one listed. This is useful to prevent duplication
26 34
 of comments being pushed into Tracker if you have multiple remote branches.
27 35
 
  36
+The "user_api_tokens" is optional - the primary "tracker_api_token" will be used
  37
+by default.  But, if you do supply this block, then the hook will correlate the
  38
+email address of the author of the GitHub commit, to that within this list, and
  39
+if it finds a match, will use the specified Tracker API token.  This makes it so
  40
+that the comment in your Tracker story shows up as being made by the same person
  41
+making the GitHub commit (instead of whoever owns the default API token).
  42
+
28 43
 When you make commits to Git/GitHub, and want a comment and optionally a state 
29 44
 update made to a story in Tracker, add the following text to your commit 
30 45
 message:
24  tracker_github_hook.rb
@@ -25,8 +25,17 @@
25 25
     
26 26
     PROJECTS = Hash.new
27 27
     config.each do |project|
28  
-      raise "required configuration settings not found" unless project[1]['tracker_api_token'] && project[1]['tracker_project_id']    
29  
-      PROJECTS[project[1]['github_url']] = { :api_token => project[1]['tracker_api_token'], :project_id => project[1]['tracker_project_id'], :ref => project[1]['ref'] }
  28
+      raise "required configuration settings not found" unless project[1]['tracker_api_token'] && project[1]['tracker_project_id'] 
  29
+      api_tokens = Hash.new
  30
+      if project[1]['user_api_tokens']
  31
+        project[1]['user_api_tokens'].each_value do |user_info|
  32
+          api_tokens[user_info['email'].downcase] = user_info['tracker_api_token']
  33
+        end
  34
+      end
  35
+      PROJECTS[project[1]['github_url']] = { :api_token => project[1]['tracker_api_token'], 
  36
+                                             :project_id => project[1]['tracker_project_id'], 
  37
+                                             :ref => project[1]['ref'],
  38
+                                             :user_api_tokens => api_tokens }
30 39
     end
31 40
   rescue => e
32 41
     puts "Failed to startup: #{e.message}"
@@ -57,6 +66,9 @@
57 66
   def process_commit(tracker_info, commit)
58 67
     # get commit message
59 68
     message = commit['message']
  69
+    
  70
+    # get API token for the user who made the commit, if possible
  71
+    api_token = api_token_for_user(tracker_info, commit['author']['email'])
60 72
 
61 73
     # see if there is a Tracker story trigger, and if so, get story ID
62 74
     message.scan(/\[Story(\d+)([^\]]*)\]/) do |tracker_trigger|
@@ -66,7 +78,7 @@ def process_commit(tracker_info, commit)
66 78
       # post comment to the story
67 79
       RestClient.post(create_api_url(tracker_info[:project_id], story_id, '/notes'),
68 80
                       "<note><text>(from [#{commit['id']}]) #{message}</text></note>", 
69  
-                      tracker_api_headers(tracker_info[:api_token]))
  81
+                      tracker_api_headers(api_token))
70 82
     
71 83
       # See if we have a state change
72 84
       state = tracker_trigger[1].match(/.*state:(\s?\w+).*/)
@@ -75,10 +87,14 @@ def process_commit(tracker_info, commit)
75 87
 
76 88
         RestClient.put(create_api_url(tracker_info[:project_id], story_id), 
77 89
                        "<story><current_state>#{state}</current_state></story>", 
78  
-                       tracker_api_headers(tracker_info[:api_token]))
  90
+                       tracker_api_headers(api_token))
79 91
       end     
80 92
     end
81 93
   end
  94
+  
  95
+  def api_token_for_user(tracker_info, email)
  96
+    tracker_info[:user_api_tokens][email.downcase] || tracker_info[:api_token]
  97
+  end
82 98
 
83 99
   def create_api_url(project_id, story_id, extra_path_elemets='')
84 100
     "http://www.pivotaltracker.com/services/v1/projects/#{project_id}/stories/#{story_id}#{extra_path_elemets}"

0 notes on commit d300275

Please sign in to comment.
Something went wrong with that request. Please try again.