Skip to content

Instantly share code, notes, and snippets.

@mzp

mzp/git-ticket Secret

Created June 16, 2011 14:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mzp/014371584dbf85d053ed to your computer and use it in GitHub Desktop.
Save mzp/014371584dbf85d053ed to your computer and use it in GitHub Desktop.
git ticket subcommand
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
=begin rdoc
Install
----------
Move into a directory in PATH environment variable.
$ mv git-ticket /path/to/some/dir
$ chmod a+x /path/to/some/dir/git-ticket
If you use old ruby(e.g. Ruby 1.8), you should install json library.
$ gem install json
$ export RUBYOPT=rubygems
Setup
-----------
Set your redmine root and API Key.
git config redmine.url http://example.com/redmine
git config redmine.apikey some_api_key
Usage
-----------
And create topic branch.
git ticket 42
At topic branch, you could see ticket summary.
git ticket
This subcommand use bleis-style branch name. Please see also https://github.com/bleis-tift/Git-Hooks/ .
=end
require 'openssl'
require 'uri'
require 'open-uri'
require 'json'
require 'optparse'
Root = %x(git config redmine.url).strip
ApiKey = %x(git config redmine.apikey).strip
if Root.empty? or ApiKey.empty? then
puts <<-END
please set redmine url & redmine api key
git config redmine.url http://example.com/redmine
git config redmine.apikey some_api_key
END
exit 0
end
# ------------------------------
# main
# ------------------------------
ProgramConfig = {}
opts = OptionParser.new
opts.banner = 'git ticket [options] [id]'
opts.on("-f", "--force","force create branch"){
ProgramConfig[:force] = true
}
opts.on("-n", "--no-verify", "not verify ssl"){
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
}
opts.parse!(ARGV)
unless ARGV.empty?
ticket, _ = *ARGV
if ProgramConfig[:force]
system "git branch -D id/#{ticket}"
end
system "git checkout -b id/#{ticket}"
end
branch = %x(git branch -l | grep "*" | cut -d " " -f 2).strip
if branch =~ %r!id/(\d+)! then
ticket = $1
url = URI(Root+"/") + "./issues/#{ticket}.json?key=#{ApiKey}&include=journals"
begin
open(url) do |io|
issue = JSON.parse(io.read)['issue']
puts <<-END
id: #{issue['id']}
Subject: #{issue['subject']}
Author: #{issue['author']['name']}
Date: #{issue['updated_on']}
#{issue['description']}
END
puts "","notes: "
issue['journals'].each_with_index do |journal, i|
unless journal['notes'] == ''
puts <<-END
##{i+1}:
#{journal['notes']}
END
end
end
end
rescue => e
puts "cannot access: #{url}"
puts e
puts e.backtrace
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment