-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticketprinter.rb
More file actions
121 lines (102 loc) · 3.28 KB
/
ticketprinter.rb
File metadata and controls
121 lines (102 loc) · 3.28 KB
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
#!/usr/bin/env ruby
require 'yaml'
require 'optimist'
require 'erb'
require 'net/http'
require 'json'
def show_yml_example_and_exit
puts <<eot
eot
exit
end
def show_template_example_and_exit
puts File.read(template_path)
exit
end
def template_path
File.join(File.dirname(__FILE__), 'ticket_template')
end
def read_yml(opts)
if opts[:stdin]
data = StringIO.new
data << STDIN.read until STDIN.eof?
yaml = data.string
else
yaml = File.read(opts[:yml])
end
YAML.safe_load(yaml).inject(opts) do |memo, item|
memo[item[0].to_sym] = item[1]
memo
end
fail('Items not in the right format, something is missing.') unless opts[:cases].is_a?(Array)
opts
rescue Errno::ENOENT
raise "YML file #{opts[:yml]} not found or can't be read."
end
def read_params
Optimist.options do
opt :user, 'User to access JIRA.', :type => :string
opt :password, 'Password to access JIRA.', :type => :string
opt :hostname, 'Hostname of the JIRA server.', :type => :string
opt 'dry-run', 'Only show the JSON data to be submitted to JIRA'
opt 'show-yml-example', 'Show an example of a YML file that can be used by this script.'
opt 'show-template-example', 'Show an example of a ticket template.'
opt :stdin, 'Read YML file from STDIN.'
opt :yml, 'YML file with values for parameters not given into command line.', :default => 'cases.yml'
end
end
$json_template = {
"fields" => {
"project" => {"key" => "DEV"},
"summary" => "",
"description" => "",
"issuetype" => {"name" => "Test"},
"components" => [{"name" => "openvpn"}],
"labels" => ["DEVOPS"]
}
}
def get_template
File.read(template_path)
end
def generate_description(use_case, commands)
description = get_template.gsub("%use_case%", use_case.to_s).gsub("%commands%", commands.join('\n'))
end
def generate_tickets(opts, json_template)
tickets = opts[:cases].inject('') do |ticket, use_case|
description = generate_description(use_case, opts[:commands])
title = opts[:title].gsub("%use_case%", use_case.to_s)
json_template["fields"]["description"] = description
json_template["fields"]["summary"] = title
raw_ticket = json_template.to_json
ticket + raw_ticket
end
end
def dry_run(tickets)
puts tickets
exit
end
def send_tickets(opts, tickets)
uri = URI("https://#{opts[:hostname]}/rest/api/2")
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
req.basic_auth opts[:user], opts[:password]
req.body = tickets
res = http.request(req)
puts "response #{res.body}"
rescue => e
puts "failed #{e}"
end
def main
opts = read_params
show_yml_example_and_exit if opts['show-yml-example_given']
show_template_example_and_exit if opts['show-template-example_given']
read_yml(opts)
tickets = generate_tickets(opts, $json_template)
dry_run(tickets) if opts['dry-run']
send_tickets(opts, tickets)
rescue Errno::ENOENT => e
abort e.message
rescue RuntimeError => e
abort e.message
end
main if $PROGRAM_NAME == __FILE__