-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtickey.rb
executable file
·227 lines (184 loc) · 5.02 KB
/
tickey.rb
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
#!/usr/bin/env ruby
#
require 'rubygems'
require 'readline' # http://bogojoker.com/readline/
require 'trollop'
require 'pp'
require 'redmine_client'
require 'yaml'
require 'tempfile'
require 'benchmark'
configfile = "#{ENV['HOME']}/.tickey.conf"
config = ''
if File.exists?(configfile)
config = YAML::load(File.open(configfile))
else
puts "no config file at #{configfile}"
exit 1
end
api_token = config['api_token']
redmine_url = config['redmine_url']
redmine_project = config['redmine_project']
redmine_projects = config['redmine_projects']
cache_file = config['cache_file']
body = ''
# Monkey patching!
# https://github.com/anibalcucco/basecamp-wrapper/issues/11
class Hash
def collect!(&block)
ret = []
self.each {|key,val|
if val.kind_of? Array
val.collect!{|subval| block.call subval }
ret = val
end
}
return ret
end
end
def get_project( redmine_project )
proj = nil
time = Benchmark.realtime do
proj = RedmineClient::Project.find( redmine_project )
end
unless proj
puts "Unable to find the project of #{redmine_project}"
exit 10
end
puts "Time to find that project '#{redmine_project}' was #{ '%.2f' % time } seconds by the way."
return proj
end
def redmine_login( redmine_url , api_token )
RedmineClient::Base.configure do
self.site = redmine_url
self.user = api_token
# self.format = :json
end
end
def get_project_list
project_list = {}
projects = RedmineClient::Project.find(:all)
projects.each do |p|
project_list[p.id] = p.identifier
end
end
def read_body
body = []
prompt = 'Body: '
while line = Readline.readline( prompt )
break if line.downcase == 'exit' or
line.downcase == 'quit' or
line.downcase == '.'
body << line
prompt = '`---> '
end
body.join( "\n" )
end
def die( string )
puts string
exit 20
end
# See http://trollop.rubyforge.org/
opts = Trollop::options do
opt :subject, "Subject line for ticket", :short => 's', :type => :string
opt :editor, "Fire up vim", :short => 'e'
opt :debug, "Be noisey"
opt :apitoken, "API token to use for redmine", :short => 'a'
opt :url, "URL to Redmine/Chili", :short => 'u'
end
# can pass a file to it, or stdin. Or we'll start asking.
if ARGV.length != 0
if ARGV[0] == "-"
die "Sorry, you need to specify a subject on the command line to use STDIN" \
unless opts[:subject]
body = $stdin.read
elsif File.exists? ARGV[0]
body = File.open( ARGV[0] ) { |f| f.read }
else
die "File #{ARGV[0]} doesn't exist"
end
end
api_token = opts[:apitoken] if opts[:apitoken]
redmine_login( redmine_url, api_token )
project = nil
# before we do readline, deal cleanly with Ctrl-C action
if body.empty? or opts[:subject].empty?
stty_save = `stty -g`.chomp
trap('INT') { system('stty', stty_save); exit }
end
# Readline completition from http://bogojoker.com/readline/
list = [ 'puppet', 'puppetlabs', 'puppetlabs-modules',
'@james', '@zleslie', '@adrient', '@zach'
].sort
# Add in all the projects as #project for tab completion
list << redmine_projects.collect { |x| '#' + x }
list.flatten!
comp = proc { |s| list.grep( /^#{Regexp.escape(s)}/ ) }
Readline.completion_append_character = " "
Readline.completion_proc = comp
# If we've got a subject, use it.
subject = opts[:subject]
subject ||= Readline.readline( 'Subject: ' , true )
# Now we need to do something clever with the subject, to parse it for
# things. Only picks the first one it finds, which is kinda lame, but oh
# well.
redmine_projects.each do |proj|
if subject =~ /\# #{proj} \b /ix
begin
project = get_project( proj )
subject = $` + $' # so obvious, this is:
# $` contains the string before the actual matched
# string of the previous successful pattern match.
# and then
# $' contains the string after the actual matched string of the
# previous successful pattern match.
rescue => e
puts "#{e} happened. bad."
exit
end
break
end
end
# Default to redmine_project.
if project.nil?
project = get_project( redmine_project )
end
if body.empty?
if opts[:editor]
# vi tempfile...
temp = Tempfile.new( 'tickeypoos', '/tmp/' )
temp.close
system( "${EDITOR:vi} #{temp.path}" )
File.open( temp.path , 'r' ) do |f|
body = f.readlines.join
end
temp.unlink
else
body = read_body
end
end
if opts[:debug]
puts "Project of #{project} with id of #{project.id}"
puts "Subject: #{subject}"
puts "Body: #{body}"
end
# now post the issue!
issue = RedmineClient::Issue.new(
:subject => subject,
:project_id => project.id,
:description => body
)
# Try and save it.
begin
issue.save
rescue => e
puts "Failed to save ticket because of #{e}"
exit 10
end
# Strip multiple //s but not the one in http://
issue_url = "#{redmine_url}/issues/#{issue.id}".gsub( /\/+/ , '/' ).sub( /^(https?:)\/\b/ , '\1//' )
puts "#{issue_url} created at #{issue.created_on}"
if RUBY_PLATFORM =~ /darwin/
system( "echo '#{issue_url}' | pbcopy" )
end
# pp issue