-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrac
executable file
·298 lines (265 loc) · 7.65 KB
/
trac
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
#!/usr/bin/ruby
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'rubygems'
require 'gli'
require 'trac4r'
require 'rainbow'
require 'cgi'
include GLI
OPENER_DEFAULT = RUBY_PLATFORM =~ /darwin/ ? 'open' : nil
begin
# TODO: Figure out a better way to do this
cols = `stty -a | grep "columns;" | head -1`.split(/;/)
COLUMNS_DEFAULT = cols[2].gsub(' columns','')
rescue
COLUMNS_DEFAULT = nil
end
config_file '.trac4r'
desc 'Command to use to open URLs'
default_value OPENER_DEFAULT
flag [:o,:open]
desc 'URL to trac'
flag [:url]
desc 'Your username'
flag [:u,:user]
desc 'Your password'
flag [:p,:password]
desc 'Do not format output'
long_desc 'By default, the output is formatted fo readability in the terminal. By disabling this, the output is more "machine readable" and parsable by other scripts'
switch [:noformat]
desc 'number of columns for formatting'
default_value COLUMNS_DEFAULT
flag [:cols]
desc 'Open the ticket or wiki page'
arg_name 'the id of the ticket or name of wiki page'
command :open do |c|
c.action do |global_options,options,args|
raise "You must supply a ticket ID or WikiName" if args.empty?
id = args[0]
if id =~ /^\d+$/
system "#{global_options[:o]} #{$url}/ticket/#{id}"
else
system "#{global_options[:o]} #{$url}/wiki/#{args.map{ |x| x[0..0].upcase + x[1..-1]}.join('')}"
end
end
end
desc 'Go to the new ticket web interface'
long_desc 'Navigates your browser to the web interface, initialized with the values you provide, but does not actually create the ticket'
arg_name 'summary for the ticket'
command [:newticket,:nt] do |c|
c.desc 'component'
c.flag [:c,:component]
c.desc 'type'
c.flag [:t,:type]
c.desc 'priority'
c.flag [:p,:priority]
c.desc 'milestone'
c.flag [:m,:milestone]
c.desc 'keywords'
c.flag [:k,:keywords]
c.action do |global_options,options,args|
query_params = {}
query_params['priority'] = options[:p] if options[:p]
query_params['component'] = options[:c] if options[:c]
query_params['type'] = options[:t] if options[:t]
query_params['milestone'] = options[:m] if options[:m]
query_params['keywords'] = options[:k] if options[:k]
query_params['summary'] = args.join(' ')
query_string = ''
query_params.each do |key,value|
query_string += key
query_string += '='
query_string += CGI.escape(value)
query_string += '&'
end
url = $url.gsub(/\/xmlrpc/,'') + '/newticket?' + query_string
system "#{global_options[:o]} \"#{url}\""
end
end
FORMATTING = {
'id' => { :color => :green, :size => 6 },
'status' => { :color => :cyan, :size => 9 },
'owner' => { :size => 17 },
'component' => { :size => 10, :color => :magenta },
'milestone' => { :size => 28, :color => :cyan },
}
desc 'Lists tickets'
arg_name 'ticket ids (blank for all)'
command [:tickets,:ls] do |c|
c.desc 'Show tickets accepted'
c.switch [:a,:accepted]
c.desc 'Component'
c.flag [:c,:component]
c.desc 'Show full description'
c.switch [:l,:description]
c.desc 'Arbitrary query (flags are included, too)'
c.flag [:q,:query]
c.desc 'Owner ("ME" for the -u user)'
c.flag [:o,:owner]
c.desc 'Columns to show'
c.long_desc 'comma-delimited list of the column names from the tickets to show in the output'
c.flag [:columns]
c.action do |global_options,options,args|
opts = {}
if options[:q]
raise "bad query" if options[:q] =~ /\}/
opts = instance_eval('{' + options[:q] + '}')
else
opts[:owner] = options[:o]
opts[:owner] = global_options[:u] if options[:o] == "ME"
if options[:a]
opts[:status] = :accepted
else
opts[:status] = ["!closed","testing"]
end
opts[:component] = options[:c] if options[:c]
end
ids = args
ids = $trac.tickets.query(opts) if ids.empty?
tickets = ids.map{ |id| $trac.tickets.get(id) }
tickets.sort{ |a,b| a.summary.downcase <=> b.summary.downcase }.each do |ticket|
if global_options[:noformat]
puts [ticket.id,ticket.status,ticket.owner,ticket.summary].join("\t")
else
columns = options[:columns]
columns = 'id,status,owner,summary' if !columns
columns = columns.split(/,/)
printf_string = ""
padding = 0
args = []
columns.each do |col|
if col != 'summary'
size = FORMATTING[col] && FORMATTING[col][:size]
size = 10 if !size
padding += size == "" ? 3 : (size.to_i + 3)
size += 9 if size && FORMATTING[col] && FORMATTING[col][:color]
printf_string += "%#{size}s - "
if FORMATTING[col] && FORMATTING[col][:color]
args << ticket.send(col.to_sym).to_s.color(FORMATTING[col][:color])
else
args << ticket.send(col.to_sym).to_s
end
end
end
printf_string.gsub!(/ - $/,'')
if columns.include? 'summary'
args << do_wrap(ticket.summary,padding+1,global_options[:cols])
printf_string += " - %s"
end
printf("#{printf_string}\n",*args)
end
if (options[:l])
description = ticket.description
if !global_options[:noformat]
description = bold(description)
description = italic(description)
description = code(description)
end
puts
puts "#{description}"
puts "----"
end
end
end
end
# Not sure about this
def do_wrap(string,indent,cols)
cols_left = cols.to_i - indent
raise "Your terminal is too small for formatting" if (cols_left < 5)
return_me = ""
line = ""
prev_word = ""
string.split(/\s/).each do |word|
if line.length <= cols_left
line += " #{word}" if line != ""
line += "#{word}" if line == ""
prev_word = word
else
line.gsub!(/#{prev_word}$/,'')
return_me += line.underline + "\n"
indent.times { return_me += " " }
line = prev_word
end
end
if (line.length > cols_left)
line.gsub!(/#{prev_word}$/,'')
return_me += line.underline + "\n"
indent.times { return_me += " " }
line = prev_word
end
return_me += line.underline
return_me.gsub(/^\s/,'')
end
def bold(string)
desc = string.split("'''")
description = ""
bold = false
desc.each do |part|
description << part.bright.foreground(:cyan) if bold
description << part.reset if !bold
bold = !bold
end
description
end
def italic(string)
desc = string.split("''")
description = ""
bold = false
desc.each do |part|
description << part.italic.foreground(:magenta) if bold
description << part.reset if !bold
bold = !bold
end
description
end
def code(string)
desc = string.split("`")
description = ""
bold = false
desc.each do |part|
description << part.bright.foreground(:green) if bold
description << part.reset if !bold
bold = !bold
end
in_code = false
coded = ''
description.split(/\n/).each do |line|
ignore = false
if line =~ /^\{\{\{\s*$/
in_code = true
ignore = true
elsif line =~ /^\}\}\}/
in_code = false
ignore = true
end
if !ignore
if in_code
coded += line.bright.foreground(:green)
else
coded += line
end
end
coded += "\n"
end
coded
end
pre do |global,command,options,args|
if command.nil? || command.name == :help
# not creating a trac instance
else
$url = global[:url]
raise "You must specify a URL" if $url.nil?
$trac = Trac.new($url,global[:u],global[:p])
end
true
end
post do |global,command,options,args|
# Post logic here
end
on_error do |exception|
raise exception
# Error logic here
# return false to skip default error handling
true
end
GLI.run(ARGV)