Class: TicGit::Ticket

Inherits:
Object
  • Object
show all
Defined in:
lib/ticgit/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
# File 'lib/ticgit/ticket.rb', line 9

def initialize(base, options = {})
  options[:user_name] ||= base.git.config('user.name') 
  options[:user_email] ||= base.git.config('user.email')      
  
  @base = base
  @opts = options || {}
  
  @description = options.delete(:description)
  @state = 'open' # by default
  @comments = []
  @tags = []
  @attachments = []
end

Instance Attribute Details

#assignedObject

Returns the value of attribute assigned.



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

def assigned
  @assigned
end

#attachmentsObject

arrays



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

def attachments
  @attachments
end

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

#commentsObject

arrays



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

def comments
  @comments
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#milestoneObject

Returns the value of attribute milestone.



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

def milestone
  @milestone
end

#openedObject

Returns the value of attribute opened.



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

def opened
  @opened
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

#tagsObject

arrays



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

def tags
  @tags
end

#ticket_idObject

Returns the value of attribute ticket_id.



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

def ticket_name
  @ticket_name
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Class Method Details

.clean_string(string) ⇒ Object



118
119
120
# File 'lib/ticgit/ticket.rb', line 118

def self.clean_string(string)
  string.downcase.gsub(/[^a-zA-Z0-9_:;,.]+/, '-')
end

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



23
24
25
26
27
28
29
# File 'lib/ticgit/ticket.rb', line 23

def self.create(base, title, options = {})
  t = Ticket.new(base, options)
  t.title = title
  t.ticket_name = self.create_ticket_name(title)
  t.save_new
  t
end

.create_ticket_name(title) ⇒ Object



218
219
220
# File 'lib/ticgit/ticket.rb', line 218

def self.create_ticket_name(title)
  [Time.now.to_i.to_s, Ticket.clean_string(title), rand(999).to_i.to_s].join('_')
end

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



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

def self.open(base, ticket_name, ticket_hash, options = {})
  tid = nil

  t = Ticket.new(base, options)
  t.ticket_name = ticket_name
  
  basic_title, date = self.parse_ticket_name(ticket_name)
  
  # t.title will be overridden below if the file TICKET_TITLE exists
  t.title = basic_title      
  t.opened = date
  
  ticket_hash['files'].each do |fname, value|
    if fname == 'TICKET_ID'
      t.ticket_id = value
    elsif fname == 'TICKET_TITLE'
      t.title = base.git.gblob(value).contents rescue nil
    elsif fname == 'TICKET_DESCRIPTION'
      t.description = base.git.gblob(value).contents rescue nil
    else
      # matching
      data = fname.split('_')
      if data[0] == 'ASSIGNED'
        t.assigned = data[1]
      end
      if data[0] == 'COMMENT'
        t.comments << TicGit::Comment.new(base, fname, value)
      end
      if data[0] == 'TAG'
        t.tags << data[1]
      end
      if data[0] == 'STATE'
        t.state = data[1]
      end          
    end
  end
  

  t
end

.parse_ticket_name(name) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/ticgit/ticket.rb', line 73

def self.parse_ticket_name(name)
  unless name =~ /^([^_]+)_(.+)_([^_]+)$/
    raise "invalid ticket name #{name.inspect}"
  end
  epoch, title, rand = $1, $2.gsub('-', ' '), $3
  return [title, Time.at(epoch.to_i)]
end

Instance Method Details

#add_comment(comment) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/ticgit/ticket.rb', line 122

def add_comment(comment)
  return false if !comment
  base.in_branch do |wd|
    Dir.chdir(ticket_name) do
      base.new_file(comment_name(email), comment) 
    end
    base.git.add
    base.git.commit("added comment to ticket #{ticket_name}")
  end
end

#add_tag(tag) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ticgit/ticket.rb', line 161

def add_tag(tag)
  return false if !tag
  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)
            added = true
          end
        end
      end
    end
    if added
      base.git.add
      base.git.commit("added tags (#{tag}) to ticket #{ticket_name}")
    end
  end
end

#assigned_nameObject



214
215
216
# File 'lib/ticgit/ticket.rb', line 214

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

#change_assigned(new_assigned) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ticgit/ticket.rb', line 147

def change_assigned(new_assigned)
  new_assigned ||= email
  return false if new_assigned == assigned

  base.in_branch do |wd|
    Dir.chdir(ticket_name) do
      base.new_file('ASSIGNED_' + new_assigned, new_assigned)
    end
    base.git.remove(File.join(ticket_name,'ASSIGNED_' + assigned))
    base.git.add
    base.git.commit("assigned #{new_assigned} to ticket #{ticket_name}")
  end
end

#change_state(new_state) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ticgit/ticket.rb', line 133

def change_state(new_state)
  return false if !new_state
  return false if new_state == state
  
  base.in_branch do |wd|
    Dir.chdir(ticket_name) do
      base.new_file('STATE_' + new_state, new_state)
    end
    base.git.remove(File.join(ticket_name,'STATE_' + state))
    base.git.add
    base.git.commit("added state (#{new_state}) to ticket #{ticket_name}")
  end
end

#comment_name(email) ⇒ Object



206
207
208
# File 'lib/ticgit/ticket.rb', line 206

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

#emailObject



210
211
212
# File 'lib/ticgit/ticket.rb', line 210

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

#pathObject



202
203
204
# File 'lib/ticgit/ticket.rb', line 202

def path
  File.join(state, ticket_name)
end

#remove_tag(tag) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ticgit/ticket.rb', line 184

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
end

#save_newObject

write this ticket to the git database



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

def save_new
  base.in_branch do |wd|
    base.logger.info "saving #{ticket_name}"
    
    Dir.mkdir(ticket_name)
    Dir.chdir(ticket_name) do
      base.new_file('TICKET_ID', ticket_name)
      base.new_file('ASSIGNED_' + email, email)
      base.new_file('STATE_' + state, state)
      base.new_file('TICKET_TITLE',self.title)
      base.new_file('TICKET_DESCRIPTION',self.description) if self.description

      # add initial comment
      #COMMENT_080315060503045__schacon_at_gmail
      base.new_file(comment_name(email), opts[:comment]) if opts[:comment]

      # 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)
            end
          end
        end
      end            
    end
   
    base.git.add
    base.git.commit("added ticket #{ticket_name}")
  end
  # ticket_id
end