Skip to content

Instantly share code, notes, and snippets.

@apfelfabrik
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save apfelfabrik/261dd768ee9137b33909 to your computer and use it in GitHub Desktop.

Select an option

Save apfelfabrik/261dd768ee9137b33909 to your computer and use it in GitHub Desktop.
A Pivotal Tracker commit message hook.
set_bundle_gemfile () {
if [[ -f Gemfile.local ]]; then
export BUNDLE_GEMFILE=Gemfile.local
else
unset BUNDLE_GEMFILE
fi
}
preexec_functions+=(set_bundle_gemfile)
# Include the regular Gemfile
eval File.read('Gemfile')
gem 'pivotal-tracker'
gem 'git'
gem 'curses'
#!/usr/bin/env ruby
require 'git'
require 'pivotal-tracker'
message_file = ARGV[0]
original_message = File.read(message_file)
git = Git.init
STORY_STATES = ["Started"]
##### token management
token = git.config('pivotal.apitoken')
if !token || token.length == 0
puts "There is no pivotal.apitoken in your git configuration. Provide one now, and we'll kindly add it:"
token = STDIN.gets.chomp
git.config('pivotal.apitoken', token)
end
PivotalTracker::Client.token = token
##### project manangement
project = git.config('pivotal.project')
if !project || project.length == 0
puts "There is no pivotal.project in your git configuration. Add one of the following:"
projects = PivotalTracker::Project.all
projects.each do |p|
puts "Project #{p.id}: #{p.name}"
end
exit(-1)
end
begin
project = PivotalTracker::Project.find(project)
##### finding issues
name = git.config('user.name')
stories = project.stories.all(owned_by: name, current_state: STORY_STATES)
message = "# Pivotal Tracker stories suggest one of the following:\n#\n"
story_lines = stories.map do |story|
"# [##{story.id}] #{story.name}\n" # #{story.description}"
end
# message = "This was added by the commit hook."
message = message + story_lines.join
message = message + original_message
File.open(message_file, 'w') { |f| f.write message }
rescue
puts "Something went wrong with retrieving Pivotal Tracker stories."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment