Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

creation of project and add initial code

  • Loading branch information...
commit b655b50a4572a52a3ee1182ebd6029ee7d10e38e 0 parents
@chris authored
Showing with 189 additions and 0 deletions.
  1. +2 −0  .gitignore
  2. +31 −0 README
  3. +56 −0 test.rb
  4. +100 −0 tracker_github_hook.rb
2  .gitignore
@@ -0,0 +1,2 @@
+config.yml
+
31 README
@@ -0,0 +1,31 @@
+This app is a small server to serve as a GitHub Post-Receive hook to add
+comments, and update state in Pivotal Tracker, similar to say the Lighthouse
+service integration.
+
+Configure your Tracker API key, and Project ID in a config.yml file placed in
+the same directory as this app. It should look something like:
+
+tracker_api_token: a1230e72340e3babc96d5e2fab67c18d
+tracker_project_id: 123
+
+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:
+
+ [Story#####]
+
+or
+
+ [Story##### state:finished]
+
+where ##### is the story number (see the bottom of an expanded story in
+Tracker for its ID).
+
+This project also requires the "rest-client" rubygem.
+
+More information for, and thanks to:
+
+Pivotal Tracker API: http://www.pivotaltracker.com/help/api
+GitHub Post-Receive Hooks: http://github.com/guides/post-receive-hooks
+Sinatra: http://sinatra.rubyforge.org/
+RestClient: http://rubyforge.org/projects/rest-client/
56 test.rb
@@ -0,0 +1,56 @@
+#!/usr/bin/ruby
+# Test our Sinatra Tracker Post-receive hook
+#
+require 'net/http'
+require 'rubygems'
+
+payload = <<-eos
+payload={
+ "before": "5aef35982fb2d34e9d9d4502f6ede1072793222d",
+ "repository": {
+ "url": "http://github.com/defunkt/github",
+ "name": "github",
+ "description": "You're lookin' at it.",
+ "watchers": 5,
+ "forks": 2,
+ "private": 1,
+ "owner": {
+ "email": "chris@cobaltedge.com",
+ "name": "chris"
+ }
+ },
+ "commits": [
+ {
+ "id": "41a212ee83ca127e3c8cf465891ab7216a705f59",
+ "url": "http://github.com/defunkt/github/commit/41a212ee83ca127e3c8cf465891ab7216a705f59",
+ "author": {
+ "email": "chris@cobaltedge.com",
+ "name": "Chris Bailey"
+ },
+ "message": "This one is a comment only 10 [Story294325]",
+ "timestamp": "2008-02-15T14:57:17-08:00",
+ "added": ["filepath.rb"]
+ },
+ {
+ "id": "de8251ff97ee194a289832576287d6f8ad74e3d0",
+ "url": "http://github.com/defunkt/github/commit/de8251ff97ee194a289832576287d6f8ad74e3d0",
+ "author": {
+ "email": "chris@cobaltedge.com",
+ "name": "Chris Bailey"
+ },
+ "message": "comment and state change 10 [Story294325 state:finished]",
+ "timestamp": "2008-02-15T14:36:34-08:00"
+ }
+ ],
+ "after": "de8251ff97ee194a289832576287d6f8ad74e3d0",
+ "ref": "refs/heads/master"
+}
+eos
+
+headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
+
+http = Net::HTTP.new('localhost', 4567)
+resp, data = http.post('/', payload, headers)
+
+puts "Response code: #{resp.code}"
+puts "Response body: #{data}"
100 tracker_github_hook.rb
@@ -0,0 +1,100 @@
+#!/usr/bin/env ruby
+#
+# GitHub Post-Receive hook handler to add comments, and update state in Pivotal Tracker
+# Configure your Tracker API key, and Project ID in a config.yml file placed in the
+# same directory as this app.
+# When you make commits to Git/GitHub, and want a comment and optionally a state update
+# made to Tracker, add the following syntax to your commit message:
+#
+# [Story#####]
+# or
+# [Story##### state:finished]
+#
+
+require 'rubygems'
+require 'sinatra'
+require 'json'
+require 'rest_client'
+require 'yaml'
+
+
+Sinatra::Application.default_options.merge!(
+ :run => true,
+ :env => :production,
+ :raise_errors => true
+)
+
+TRACKER_HOST = 'www.pivotaltracker.com'
+
+
+# load up configuration from YAML file
+configure do
+ begin
+ config = open(File.expand_path(File.dirname(__FILE__) + '/config.yml')) { |f| YAML.load(f) }
+
+ TRACKER_API_TOKEN = config['tracker_api_token']
+ TRACKER_PROJECT_ID = config['tracker_project_id']
+
+ raise "required configuration settings not found" unless TRACKER_API_TOKEN && TRACKER_PROJECT_ID
+ rescue => e
+ puts "Failed to startup: #{e.message}"
+ puts "Ensure you have a config.yml in this directory with the'tracker_api_token' and 'tracker_project_id' keys/values set."
+ exit(-1)
+ end
+end
+
+
+# The handler for the GitHub post-receive hook
+post '/' do
+ push = JSON.parse(params[:payload])
+ push['commits'].each { |commit| process_commit(commit) }
+ num_commits = push['commits'].length
+ "Processed #{num_commits} commits"
+end
+
+
+helpers do
+ def process_commit(commit)
+ # get commit message
+ message = commit['message']
+
+ # see if there is a Tracker story trigger, and if so, get story ID
+ tracker_trigger = message.match(/\[Story(\d+)(.*)\]/)
+ if tracker_trigger
+ story_id = tracker_trigger[1]
+
+ # post comment to the story
+ post_tracker_comment(story_id, commit['id'], message)
+
+ # See if we have a state change
+ state = tracker_trigger[2].match(/.*state:(\s?\w+).*/)
+ if state
+ state = state[1]
+ state.strip!
+
+ change_tracker_story_state(story_id, state)
+ end
+ end
+ end
+
+ def post_tracker_comment(story_id, commit_id, comment)
+ RestClient.post(create_api_url(story_id, '/notes'),
+ "<note><text>(from [#{commit_id}]) #{comment}</text></note>",
+ tracker_api_headers)
+ end
+
+ def change_tracker_story_state(story_id, state)
+ RestClient.put(create_api_url(story_id),
+ "<story><current_state>#{state}</current_state></story>",
+ tracker_api_headers)
+ end
+
+ def create_api_url(story_id, extra_path_elemets='')
+ "http://www.pivotaltracker.com/services/v1/projects/#{TRACKER_PROJECT_ID}/stories/#{story_id}#{extra_path_elemets}"
+ end
+
+ def tracker_api_headers
+ { 'X-TrackerToken' => TRACKER_API_TOKEN, 'Content-type' => 'application/xml' }
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.