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

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
'0.3.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#confObject (readonly)

Returns the value of attribute conf.



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

def conf
  @conf
end

Instance Method Details

#contentObject



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

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

#create_ticket!Object



35
36
37
38
39
40
41
42
43
# 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

  key = JSON.parse(response.body).fetch('key')
  "#{jira_url}/browse/#{key}"
end

#descriptionObject



83
84
85
86
87
88
# File 'lib/create_ticket.rb', line 83

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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/create_ticket.rb', line 45

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



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_jsonObject



57
58
59
60
61
# File 'lib/create_ticket.rb', line 57

def jira_ticket_json
  {
    fields: fields
  }.to_json
end

#kramdown_optionsObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/create_ticket.rb', line 72

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

#summaryObject



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

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

#templateObject



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

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