Class: CreateTicket

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/create_ticket.rb,
lib/create_ticket/cli.rb,
lib/create_ticket/version.rb,
lib/create_ticket/could_not_create_ticket_error.rb

Defined Under Namespace

Classes: CLI, CouldNotCreateTicketError

Constant Summary collapse

VERSION =
'0.4.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ CreateTicket

Returns a new instance of CreateTicket.



15
16
17
18
# File 'lib/create_ticket.rb', line 15

def initialize(conf)
  conf = OpenStruct.new(conf) if conf.is_a? Hash
  @conf = conf
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



13
14
15
# File 'lib/create_ticket.rb', line 13

def conf
  @conf
end

Instance Method Details

#contentObject



69
70
71
# File 'lib/create_ticket.rb', line 69

def content
  ERB.new(template).result(binding)
end

#create_ticket!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/create_ticket.rb', line 36

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')
  rescue KeyError
    raise CouldNotCreateTicketError, "Response from JIRA was: #{response.body}"
  end

  "#{jira_url}/browse/#{key}"
end

#descriptionObject



89
90
91
92
93
94
# File 'lib/create_ticket.rb', line 89

def description
  # TODO: Assumes Markdown with an h1 at the top.
  markdown = content.sub(/# .*\n/, '')

  Kramdown::Document.new(markdown, kramdown_options).to_confluence
end

#fieldsObject



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_requestObject



28
29
30
31
32
33
34
# File 'lib/create_ticket.rb', line 28

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_jsonObject



63
64
65
66
67
# File 'lib/create_ticket.rb', line 63

def jira_ticket_json
  {
    fields: fields
  }.to_json
end

#kramdown_optionsObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/create_ticket.rb', line 78

def kramdown_options
  {
    input: 'GFM',
    syntax_highlighter: 'coderay',
    syntax_highlighter_opts: {
      css: 'style',
      line_numbers: 'table'
    }
  }
end

#summaryObject



73
74
75
76
# File 'lib/create_ticket.rb', line 73

def summary
  # TODO: Assumes Markdown with an h1 at the top.
  content.lines.first[2..-1].strip
end

#templateObject



24
25
26
# File 'lib/create_ticket.rb', line 24

def template
  @template ||= File.open(template_filename).read
end