jdhuntington (owner)

Forks

Revisions

gist: 14625 Download_button fork
public
Public Clone URL: git://gist.github.com/14625.git
Embed All Files: show embed
bugflow #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env ruby
 
# 2008.10.03
# JD Huntington
# Darell Fuhriman
# shell script to assist in working with lighthouse and git
#
# requires lighthouse api ruby wrapper and git library
# $ gem sources -a http://gems.github.com/
# $ gem install Caged-lighthouse-api
 
$initialized = false
 
$:.unshift( File.expand_path( File.dirname( __FILE__ ) ) )
 
require 'rubygems'
require 'lighthouse'
require 'tempfile'
 
$config = nil
 
module BugFixing
  module_function
 
  def usage
    STDERR.puts "
 
A bug fixing workflow.
 
Workflow:
git checkout master
bugflow start <bugid>
[do some work]
bugflow fixed <commit message>
hack && ship
bugflow shipped
 
Usage:
usage display this message
open view the active ticket in your default browser [OSX only]
start <bugid> create a new branch based on bugid, and assigns to you
fixed <commit message> commits all tracked and untracked files with <commit message>,
appends lighthouse tags to set to fixed state and assigns to 'none'
shipped deletes the current working branch
"
  end
  
  def load!
    return if $initialized == true
    load_config
    Lighthouse.token = $config['token']
    Lighthouse.account = $config['account']
    $initialized = true
  end
 
  def load_config(directory='.')
    if File.expand_path(directory) == '/'
      STDERR.puts "Bugflow config [.bugflow.yml] not found!"
      raise RuntimeError, "Bugflow config [.bugflow.yml] not found!"
    end
 
    filename = File.expand_path(directory + '/.bugflow.yml')
    if File.exists?(filename)
      $config = YAML.load(File.read(filename))
    else
      load_config(File.expand_path(directory + '/..'))
    end
  end
 
  def open
    load!
    url = "http://#{$config['account']}.lighthouseapp.com/projects/#{$config['project']}/tickets/#{find_current_ticket}"
    %x{open "#{url}"}
  end
 
  def fixed commit_msg=nil
    load!
    # %x{git add .}
    bug_info="[##{find_current_ticket} responsible:none state:#{$config['status']}]"
    if commit_msg.nil?
      require 'tempfile'
      tmp=Tempfile.new('commitmsg')
      tmp.puts bug_info
      tmp.close
      system "git commit -a -v -t #{tmp.path}"
      tmp.delete
    else
      commit_string = "#{commit_msg} #{bug_info}"
      puts %x{git commit -a -m '#{commit_string}'}
    end
  end
 
  def shipped
    current = find_current_branch
    %x{git checkout master}
    %x{git cherry master #{current}}.each do |line|
      add,commit=line.split(/\s+/)
      next unless add == '+'
      %x{git cherry-pick #{commit}}
    end
      
    %x{git branch -d #{current}}
  end
  
  def get_user_id
    load!
    Lighthouse::Token.find($config['token']).user_id
  end
  
  def find_current_branch
    output = %x{git branch}
    return output.grep(/^\*/).first.strip[2 .. -1]
  end
 
  def find_current_ticket
    branch = find_current_branch
    unless branch[0 .. 2] == 'bf-'
      STDERR.puts "Error - Not working on a bugflow branch"
      exit!
    end
    branch.split('-')[1].to_i
  end
  
  def start bug_id
    load!
    ticket = Lighthouse::Ticket.find(bug_id, :params => { :project_id => $config['project'] })
    new_tagname = "bf-#{ticket.number}-#{ticket.permalink[0 .. 20]}"
    %x{git checkout -b #{new_tagname}}
    puts ''
    puts "Now working on ticket #{ticket.number} \"#{ticket.title}\" (#{new_tagname})"
    puts ''
    ticket.assigned_user_id = get_user_id
    ticket.save
  rescue NoMethodError
    puts "(There was an error while assigning this ticket, you man need to run 'bugflow open' and make the necessary changes)"
  end
 
  def gitdir
    ret = %x{git rev-parse --git-dir}.chomp
    return Dir.pwd if ret=='.git'
    return File.dirname ret
  end
end
 
begin
  Dir.chdir BugFixing.gitdir
end
 
begin
  BugFixing.send(*ARGV)
rescue ArgumentError
  STDERR.puts "Wrong number of arguments."
rescue NoMethodError
  STDERR.puts "Command not recognized."
  BugFixing.usage
end