Class: CreateTicket
- Inherits:
-
Object
- Object
- CreateTicket
- Extended by:
- Forwardable
- Defined in:
- lib/create_ticket.rb,
lib/create_ticket/cli.rb,
lib/create_ticket/version.rb
Defined Under Namespace
Classes: CLI
Constant Summary collapse
- VERSION =
'0.3.0'
Instance Attribute Summary collapse
-
#conf ⇒ Object
readonly
Returns the value of attribute conf.
Instance Method Summary collapse
- #content ⇒ Object
- #create_ticket! ⇒ Object
- #description ⇒ Object
- #fields ⇒ Object
-
#initialize(conf) ⇒ CreateTicket
constructor
A new instance of CreateTicket.
- #jira_request ⇒ Object
- #jira_ticket_json ⇒ Object
- #kramdown_options ⇒ Object
- #summary ⇒ Object
- #template ⇒ Object
Constructor Details
#initialize(conf) ⇒ CreateTicket
Returns a new instance of CreateTicket.
14 15 16 17 |
# File 'lib/create_ticket.rb', line 14 def initialize(conf) conf = OpenStruct.new(conf) if conf.is_a? Hash @conf = conf end |
Instance Attribute Details
#conf ⇒ Object (readonly)
Returns the value of attribute conf.
12 13 14 |
# File 'lib/create_ticket.rb', line 12 def conf @conf end |
Instance Method Details
#content ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/create_ticket.rb', line 69 def content ERB.new(template).result(binding) rescue KeyError => e puts "Please set all required ENV variables for your template: #{ENV['TEMPLATE_FILENAME']}" puts e. exit 1 end |
#create_ticket! ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/create_ticket.rb', line 35 def create_ticket! response = jira_request.post do |req| req.url '/rest/api/2/issue' req.body = jira_ticket_json end begin key = JSON.parse(response.body).fetch('key') puts "#{jira_url}/browse/#{key}" rescue KeyError, JSON::ParserError puts 'Could not create a JIRA ticket.' puts 'Response from server:' puts response.body exit 1 end end |
#description ⇒ Object
93 94 95 96 97 98 |
# File 'lib/create_ticket.rb', line 93 def description # TODO: Assumes Markdown with an h1 at the top. markdown = content.sub(/# .*\n/, '') Kramdown::Document.new(markdown, ).to_confluence end |
#fields ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/create_ticket.rb', line 51 def fields { project: { key: project }, issuetype: { name: issue_type }, summary: summary, description: description, assignee: { name: assignee }, reporter: { name: assignee }, duedate: duedate }.merge(custom_fields) end |
#jira_request ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/create_ticket.rb', line 27 def jira_request Faraday.new(url: jira_url) do |faraday| faraday.adapter Faraday.default_adapter faraday.headers['Content-Type'] = 'application/json' faraday.headers['Authorization'] = 'Basic ' + jira_token end end |
#jira_ticket_json ⇒ Object
63 64 65 66 67 |
# File 'lib/create_ticket.rb', line 63 def jira_ticket_json { fields: fields }.to_json end |
#kramdown_options ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/create_ticket.rb', line 82 def { input: 'GFM', syntax_highlighter: 'coderay', syntax_highlighter_opts: { css: 'style', line_numbers: 'table' } } end |
#summary ⇒ Object
77 78 79 80 |
# File 'lib/create_ticket.rb', line 77 def summary # TODO: Assumes Markdown with an h1 at the top. content.lines.first[2..-1].strip end |
#template ⇒ Object
23 24 25 |
# File 'lib/create_ticket.rb', line 23 def template @template ||= File.open(template_filename).read end |