Class: TicGitNG::Ticket

Inherits:
Object
  • Object
show all
Defined in:
lib/ticgit-ng/ticket.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, options = {}) ⇒ Ticket

Returns a new instance of Ticket.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ticgit-ng/ticket.rb', line 9

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

Instance Attribute Details

#assignedObject

Returns the value of attribute assigned.



6
7
8
# File 'lib/ticgit-ng/ticket.rb', line 6

def assigned
  @assigned
end

#attachmentsObject

arrays



7
8
9
# File 'lib/ticgit-ng/ticket.rb', line 7

def attachments
  @attachments
end

#baseObject (readonly)

Returns the value of attribute base.



4
5
6
# File 'lib/ticgit-ng/ticket.rb', line 4

def base
  @base
end

#commentsObject

arrays



7
8
9
# File 'lib/ticgit-ng/ticket.rb', line 7

def comments
  @comments
end

#milestoneObject

Returns the value of attribute milestone.



6
7
8
# File 'lib/ticgit-ng/ticket.rb', line 6

def milestone
  @milestone
end

#openedObject

Returns the value of attribute opened.



6
7
8
# File 'lib/ticgit-ng/ticket.rb', line 6

def opened
  @opened
end

#optsObject (readonly)

Returns the value of attribute opts.



4
5
6
# File 'lib/ticgit-ng/ticket.rb', line 4

def opts
  @opts
end

#pointsObject

Returns the value of attribute points.



6
7
8
# File 'lib/ticgit-ng/ticket.rb', line 6

def points
  @points
end

#stateObject

Returns the value of attribute state.



6
7
8
# File 'lib/ticgit-ng/ticket.rb', line 6

def state
  @state
end

#tagsObject

arrays



7
8
9
# File 'lib/ticgit-ng/ticket.rb', line 7

def tags
  @tags
end

#ticket_idObject

Returns the value of attribute ticket_id.



5
6
7
# File 'lib/ticgit-ng/ticket.rb', line 5

def ticket_id
  @ticket_id
end

#ticket_nameObject

Returns the value of attribute ticket_name.



5
6
7
# File 'lib/ticgit-ng/ticket.rb', line 5

def ticket_name
  @ticket_name
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'lib/ticgit-ng/ticket.rb', line 6

def title
  @title
end

Class Method Details

.clean_string(string) ⇒ Object



140
141
142
# File 'lib/ticgit-ng/ticket.rb', line 140

def self.clean_string(string)
  string.downcase.gsub(/[^a-z0-9]+/i, '-')
end

.create(base, title, options = {}, time = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/ticgit-ng/ticket.rb', line 28

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

.create_ticket_name(title, time = nil) ⇒ Object



355
356
357
# File 'lib/ticgit-ng/ticket.rb', line 355

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

.open(base, ticket_name, ticket_hash, options = {}) ⇒ Object



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
# File 'lib/ticgit-ng/ticket.rb', line 36

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/[email protected]_fubar.jpg
          #data[] format:
          #"[email protected]_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

.parse_ticket_name(name) ⇒ Object



85
86
87
88
89
# File 'lib/ticgit-ng/ticket.rb', line 85

def self.parse_ticket_name(name)
  epoch, title, rand = name.split('_')
  title = title.gsub('-', ' ')
  return [title, Time.at(epoch.to_i)]
end

Instance Method Details

#==(ticket2) ⇒ Object



371
372
373
374
375
376
377
378
# File 'lib/ticgit-ng/ticket.rb', line 371

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

#add_attach(base, filename, time = nil) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ticgit-ng/ticket.rb', line 158

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

#add_comment(comment) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ticgit-ng/ticket.rb', line 144

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

#add_tag(tag) ⇒ Object



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
# File 'lib/ticgit-ng/ticket.rb', line 288

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

#assigned_nameObject



351
352
353
# File 'lib/ticgit-ng/ticket.rb', line 351

def assigned_name
  assigned.split('@').first rescue ''
end

#change_assigned(new_assigned) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/ticgit-ng/ticket.rb', line 256

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

#change_points(new_points) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ticgit-ng/ticket.rb', line 274

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

#change_state(new_state) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/ticgit-ng/ticket.rb', line 239

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

#comment_name(email) ⇒ Object



343
344
345
# File 'lib/ticgit-ng/ticket.rb', line 343

def comment_name(email)
  'COMMENT_' + Time.now.to_i.to_s + '_' + email
end

#create_attachment_name(attachment_name, time = nil) ⇒ Object

Raises:

  • (ArgumentError)


359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/ticgit-ng/ticket.rb', line 359

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

#emailObject



347
348
349
# File 'lib/ticgit-ng/ticket.rb', line 347

def email
  opts[:user_email] || 'anon'
end

#get_attach(file_id = nil, new_filename = nil) ⇒ Object

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



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
# File 'lib/ticgit-ng/ticket.rb', line 180

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

#pathObject



339
340
341
# File 'lib/ticgit-ng/ticket.rb', line 339

def path
  File.join(state, ticket_name)
end

#remove_tag(tag) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/ticgit-ng/ticket.rb', line 319

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

#save_newObject

write this ticket to the git database



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
# File 'lib/ticgit-ng/ticket.rb', line 92

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