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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
module TicGitNG
class Ticket
attr_reader :base, :opts
attr_accessor :ticket_id, :ticket_name
attr_accessor :title, :state, :milestone, :assigned, :opened, :points
attr_accessor :comments, :tags, :attachments # arrays
def initialize(base, options = {})
# FIXME: what/where/who/how changed config to hash?
if (cfg = base.git.config).is_a? Hash
options[:user_name] ||= cfg["user.name"]
options[:user_email] ||= cfg["user.email"]
else
options[:user_name] ||= cfg("user.name")
options[:user_email] ||= cfg("user.email")
end
@base = base
@opts = options || {}
@state = 'open' # by default
@comments = []
@tags = []
@attachments = []
end
def self.create(base, title, options = {}, time=nil)
t = Ticket.new(base, options)
t.title = title
t.ticket_name = self.create_ticket_name(title, time)
t.save_new
t
end
def self.open(base, ticket_name, ticket_hash, options = {})
tid = nil
t = Ticket.new(base, options)
t.ticket_name = ticket_name
title, date = self.parse_ticket_name(ticket_name)
t.opened = date
ticket_hash['files'].each do |fname, sha|
if fname == 'TICKET_ID'
tid = sha
elsif fname == 'TICKET_TITLE'
t.title = base.git.gblob(sha).contents
else
# matching
data = fname.split('_')
case data[0]
when 'ASSIGNED'
t.assigned = data[1..-1].join('_')
when 'ATTACHMENTS'
#Attachments dir naming format:
#ticket_name/ATTACHMENTS/123456_jeff.welling@gmail.com_fubar.jpg
#data[] format:
#"ATTACHMENTS_1342116799_jeff.welling@gmail.com_Rakefile".split('_')
filename=File.join( 'ATTACHMENTS', fname.gsub(/^ATTACHMENTS_/,'') )
t.attachments << TicGitNG::Attachment.new( filename )
when 'COMMENT'
t.comments << TicGitNG::Comment.read(base, fname, sha)
when 'POINTS'
t.points = base.git.gblob(sha).contents.to_i
when 'STATE'
t.state = data[1]
when 'TAG'
t.tags << data[1]
when 'TITLE'
t.title = base.git.gblob(sha).contents
end
end
end
if !t.attachments.class==NilClass and t.attachments.size > 1
t.attachments= t.attachments.sort {|a1, a2| a1.added <=> a2.added }
end
t.ticket_id = tid
t
end
def self.parse_ticket_name(name)
epoch, title, rand = name.split('_')
title = title.gsub('-', ' ')
return [title, Time.at(epoch.to_i)]
end
# write this ticket to the git database
def save_new
base.in_branch do |wd|
files=[]
t=nil
base.logger.puts "saving #{ticket_name}"
Dir.mkdir(ticket_name)
Dir.chdir(ticket_name) do
base.new_file('TICKET_ID', ticket_name)
files << File.join( ticket_name, 'TICKET_ID' )
base.new_file('TICKET_TITLE', title)
files << File.join( ticket_name, 'TICKET_TITLE' )
base.new_file( (t='ASSIGNED_'+email) , email)
files << File.join( ticket_name, t )
base.new_file( (t='STATE_'+state) , state)
files << File.join( ticket_name, t )
base.new_file('TITLE', title)
files << File.join( ticket_name, 'TITLE' )
# add initial comment
#COMMENT_080315060503045__schacon_at_gmail
if opts[:comment]
base.new_file(t=comment_name(email), opts[:comment])
files << File.join( ticket_name, t )
end
# add initial tags
if opts[:tags] && opts[:tags].size > 0
opts[:tags] = opts[:tags].map { |t| t.strip }.compact
opts[:tags].each do |tag|
if tag.size > 0
tag_filename = 'TAG_' + Ticket.clean_string(tag)
if !File.exists?(tag_filename)
base.new_file(tag_filename, tag_filename)
files << File.join( ticket_name, tag_filename )
end
end
end
end
end
files.each {|file|
base.git.add file
}
base.git.commit("added ticket #{ticket_name}")
end
# ticket_id
end
def self.clean_string(string)
string.downcase.gsub(/[^a-z0-9]+/i, '-')
end
def add_comment(comment)
return false if !comment
base.in_branch do |wd|
t=nil
Dir.chdir(ticket_name) do
base.new_file(t=comment_name(email), comment)
end
base.git.add File.join(ticket_name, t)
base.git.commit("added comment to ticket #{ticket_name}")
end
@comments << TicGitNG::Comment.new( comment, email )
self
end
def add_attach( base, filename, time=nil )
filename=File.expand_path(filename)
#FIXME Refactor -- Attachment.new should be called from Ticket.rb
# -- Attachment filename creation should be handled
# by the Attachment.rb code
base.in_branch do |wd|
attachments << (a=TicGitNG::Attachment.create( filename, self, time))
base.git.add File.join( ticket_name, a.filename )
base.git.commit("added attachment #{File.basename(a.filename)} to ticket #{ticket_name}")
end
if attachments.class!=NilClass and attachments.size > 1
@attachments=attachments.sort {|a1,a2| a1.added <=> a2.added }
end
self
end
#file_id can be one of:
# - An index number of the attachment (1,2,3,...)
# - A filename (fubar.jpg)
# - nil (nil) means use the last attachment
#
#if new_filename is nil, use existing filename
def get_attach file_id=nil, new_filename=nil
attachment=nil
pwd=Dir.pwd
base.in_branch do |wd|
if file_id.to_i==0 and (file_id=="0" or file_id.class==Fixnum)
if !attachments[file_id.to_i].nil?
attachment= attachments[0]
else
puts "No attachments match file id #{file_id}"
exit 1
end
elsif file_id.to_i > 0
if !attachments[file_id.to_i].nil?
attachment= attachments[file_id.to_i]
else
puts "No attachments match file id #{file_id}"
exit 1
end
else
#find attachment by filename
attachments.each {|a|
attachment=a if a.attachment_name==file_id
}
if attachment.nil?
puts "No attachments match filename #{file_id}"
exit 1
end
end
if !new_filename
#if no filename is specified...
filename= attachment.attachment_name
else
#if there is a new_filename given
if File.exist?( new_filename ) and File.directory?( new_filename )
#if it is a directory, not a filename
filename= File.join(
new_filename,
File.basename(attachment.attachment_name)
)
else
#if it is a filename, not a dir
filename= new_filename
end
end
unless File.exist?( File.dirname(filename) )
FileUtils.mkdir_p( File.dirname(filename) )
end
#save attachment [as new_filename]
t=File.join( ticket_name, attachment.filename )
unless filename[/^\//]
filename=File.join( pwd, filename )
end
FileUtils.cp( t, filename )
end
self
end
def change_state(new_state)
return false if !new_state
return false if new_state == state
t=nil
base.in_branch do |wd|
Dir.chdir(ticket_name) do
base.new_file(t='STATE_' + new_state, new_state)
end
base.git.remove(File.join(ticket_name,'STATE_' + state))
base.git.add File.join(ticket_name, t)
base.git.commit("added state (#{new_state}) to ticket #{ticket_name}")
end
@state=new_state
self
end
def change_assigned(new_assigned)
new_assigned ||= email
old_assigned= assigned || ''
return false if new_assigned == old_assigned
base.in_branch do |wd|
t=nil
Dir.chdir(ticket_name) do
base.new_file(t='ASSIGNED_' + new_assigned, new_assigned)
end
base.git.remove(File.join(ticket_name,'ASSIGNED_' + old_assigned))
base.git.add File.join(ticket_name,t)
base.git.commit("assigned #{new_assigned} to ticket #{ticket_name}")
end
@assigned=new_assigned
self
end
def change_points(new_points)
return false if new_points == points
base.in_branch do |wd|
Dir.chdir(ticket_name) do
base.new_file('POINTS', new_points)
end
base.git.add File.join(ticket_name, 'POINTS')
base.git.commit("set points to #{new_points} for ticket #{ticket_name}")
end
@points=new_points
self
end
def add_tag(tag)
return false if !tag
files=[]
added = false
tags = tag.split(',').map { |t| t.strip }
base.in_branch do |wd|
Dir.chdir(ticket_name) do
tags.each do |add_tag|
if add_tag.size > 0
tag_filename = 'TAG_' + Ticket.clean_string(add_tag)
if !File.exists?(tag_filename)
base.new_file(tag_filename, tag_filename)
files << File.join( ticket_name, tag_filename )
added = true
end
end
end
end
if added
files.each {|file|
base.git.add file
}
base.git.commit("added tags (#{tag}) to ticket #{ticket_name}")
end
end
tags.each {|tag|
@tags << tag
}
self
end
def remove_tag(tag)
return false if !tag
removed = false
tags = tag.split(',').map { |t| t.strip }
base.in_branch do |wd|
tags.each do |add_tag|
tag_filename = File.join(ticket_name, 'TAG_' + Ticket.clean_string(add_tag))
if File.exists?(tag_filename)
base.git.remove(tag_filename)
removed = true
end
end
if removed
base.git.commit("removed tags (#{tag}) from ticket #{ticket_name}")
end
end
@tags.delete_if {|t| t==tag }
self
end
def path
File.join(state, ticket_name)
end
def comment_name(email)
'COMMENT_' + Time.now.to_i.to_s + '_' + email
end
def email
opts[:user_email] || 'anon'
end
def assigned_name
assigned.split('@').first rescue ''
end
def self.create_ticket_name(title, time=nil)
[time.nil? ? Time.now.to_i.to_s : time, Ticket.clean_string(title), rand(999).to_i.to_s].join('_')
end
def create_attachment_name( attachment_name, time=nil )
raise ArgumentError, "create_attachment_name( ) only takes a string" unless attachment_name.class==String
if time
if time.to_i == 0
raise ArgumentError, "argument 'time' is not valid" unless time.class==Fixnum
else
time=time.to_i
end
end
time or time=Time.now.to_i
time.to_s+'_'+email+'_'+File.basename( attachment_name )
end
def ==(ticket2)
self.instance_variables.each {|instance_var|
unless send( instance_var.gsub('@','').to_sym ) == ticket2.instance_eval {send(instance_var.gsub('@','').to_sym)}
return false
end
}
return true
end
end
end
|