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
# 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 || {}
  
  @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

#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



108
109
110
# File 'lib/ticgit/ticket.rb', line 108

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

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



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

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



208
209
210
# File 'lib/ticgit/ticket.rb', line 208

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



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

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.title = title
  t.opened = date
  
  ticket_hash['files'].each do |fname, value|
    if fname == 'TICKET_ID'
      tid = value
    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.ticket_id = tid
  t
end

.parse_ticket_name(name) ⇒ Object



67
68
69
70
71
# File 'lib/ticgit/ticket.rb', line 67

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

#add_comment(comment) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/ticgit/ticket.rb', line 112

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



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ticgit/ticket.rb', line 151

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



204
205
206
# File 'lib/ticgit/ticket.rb', line 204

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

#change_assigned(new_assigned) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ticgit/ticket.rb', line 137

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



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

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



196
197
198
# File 'lib/ticgit/ticket.rb', line 196

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

#emailObject



200
201
202
# File 'lib/ticgit/ticket.rb', line 200

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

#pathObject



192
193
194
# File 'lib/ticgit/ticket.rb', line 192

def path
  File.join(state, ticket_name)
end

#remove_tag(tag) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ticgit/ticket.rb', line 174

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



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

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)

      # 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