Class: Git::Trac::Ticket

Inherits:
Object
  • Object
show all
Defined in:
lib/git/trac/ticket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, number) ⇒ Ticket

Returns a new instance of Ticket.



10
11
12
# File 'lib/git/trac/ticket.rb', line 10

def initialize(repo, number)
  @repository, @number = repo, Integer(number)
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



8
9
10
# File 'lib/git/trac/ticket.rb', line 8

def number
  @number
end

#repositoryObject (readonly)

Returns the value of attribute repository.



8
9
10
# File 'lib/git/trac/ticket.rb', line 8

def repository
  @repository
end

Instance Method Details

#attachment(name) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/git/trac/ticket.rb', line 60

def attachment(name)
  if name
    attachments.detect {|a| a.filename == name} or
    raise Git::Trac::Error, "no such attachment #{number}/#{name}"
  else
    attachments.last or
    raise Git::Trac::Error, "no attachments for #{number}"
  end
end

#attachment_urlObject



23
24
25
# File 'lib/git/trac/ticket.rb', line 23

def attachment_url
  "#{@repository.url}/attachment/ticket/#{@number}"
end

#attachmentsObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/git/trac/ticket.rb', line 49

def attachments
  response = repository.get_response(attachment_url)
  if html = response.body[/<dl class="attachments">.*?<\/dl>/m]
    return Attachment.from_html(self,html)
  elsif response.kind_of?(Net::HTTPSuccess)
    return []
  else
    response.error!
  end
end

#cleanup(options = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/git/trac/ticket.rb', line 118

def cleanup(options = {})
  revs = []
  repository.each_ref("refs/remotes/trac/#{number}") do |object, ref|
    revs << object
    repository.exec("git","update-ref","-d",ref,object) unless options[:only_branches]
  end
  begin
    Dir.unlink("#{repository.git_dir}/refs/remotes/trac/#{number}")
  rescue Errno::ENOENT, Errno::ENOTEMPTY
  end
  revs.any?
end

#comment!(body) ⇒ Object



78
79
80
81
82
# File 'lib/git/trac/ticket.rb', line 78

def comment!(body)
  form = form()
  form.fields.name("comment").value = body
  form.submit
end

#csvObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/git/trac/ticket.rb', line 27

def csv
  response = repository.get_response(url(:tab))
  no_such_ticket  if     response.kind_of?(Net::HTTPInternalServerError)
  response.error! unless response.kind_of?(Net::HTTPSuccess)
  body = response.body
  headers, values = body.split(/\r?\n/).map do |line|
    line.split("\t").map do |column|
      column.gsub(/\\(.)/) do
        case $1
        when "r" then ""
        when "n" then "\n"
        when "t" then "\t"
        else $1
        end
      end
    end
  end
  return headers.zip(values).inject({}) {|h,(k,v)| h[k] = v; h}
end

#fetch(options = {}, &block) ⇒ Object



131
132
133
134
135
136
# File 'lib/git/trac/ticket.rb', line 131

def fetch(options = {}, &block)
  cleanup(options)
  attachments.each do |attachment|
     attachment.fetch(options,&block)
  end
end

#formObject



74
75
76
# File 'lib/git/trac/ticket.rb', line 74

def form
  repository.agent.get(url).forms.last
end

#inspectObject



14
15
16
# File 'lib/git/trac/ticket.rb', line 14

def inspect
  "#<#{self.class.inspect} #{url}>"
end

#open?Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/git/trac/ticket.rb', line 113

def open?
  c = csv
  %w(new reopened).include?(c["status"])
end

#trac_dirObject



70
71
72
# File 'lib/git/trac/ticket.rb', line 70

def trac_dir
  "#{repository.git_dir}/refs/remotes/trac/#{number}"
end

#upload_attachment(description, filename, body) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/git/trac/ticket.rb', line 84

def upload_attachment(description, filename, body)
  form = repository.agent.get("#{attachment_url}?action=new").forms.last
  form.action = attachment_url
  author = form.fields.name("author")
  if author.any? && author.value == "anonymous"
    author.value = repository.options[:user].to_s
  end
  form.fields.name("description").value = description.to_s
  attachment = form.file_uploads.name("attachment").first
  attachment.file_name = filename
  attachment.file_data = body
  submission = form.submit
  submission.instance_variable_get(:@uri)
end

#upload_patch(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/git/trac/ticket.rb', line 99

def upload_patch(options = {})
  filename = options[:filename] || "#{File.basename(repository.current_checkout)}.patch"
  upstream = options[:upstream] || "refs/remotes/trunk"
  upstream += "...HEAD" unless upstream.include?(".")
  diff = repository.exec("git","diff",upstream)
  return false if diff.empty?
  # Don't upload the exact same patch that was pulled down
  return false if repository.generated_commits[repository.rev_parse("HEAD")] == number
  if block_given? # confirmation block
    return false if yield(diff) == false
  end
  upload_attachment(options[:description], filename, diff)
end

#url(format = nil) ⇒ Object



18
19
20
21
# File 'lib/git/trac/ticket.rb', line 18

def url(format = nil)
  query = "?format=#{format}" if format
  "#{@repository.url}/ticket/#{@number}#{query}"
end