Class: Toolshed::TicketTracking::Jira

Inherits:
Base
  • Object
show all
Includes:
HTTParty
Defined in:
lib/toolshed/ticket_tracking/jira.rb

Constant Summary collapse

USE_PROJECT_NAME =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

wait_for_command

Constructor Details

#initialize(options = {}) ⇒ Jira

Returns a new instance of Jira.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/toolshed/ticket_tracking/jira.rb', line 12

def initialize(options={})
  username = (options[:username].nil?) ? Toolshed::Client.instance.ticket_tracker_username : options[:username]
  password = (options[:password].nil?) ? Toolshed::Client.instance.ticket_tracker_password : options[:password]

  self.owner      = Toolshed::Client.instance.ticket_tracker_owner
  self.default_pull_request_title_format = Toolshed::Client.instance.default_pull_request_title_format ||= "[summary]"

  self.client = JIRA::Client.new({
    username:     username,
    password:     password,
    site:         "https://#{self.owner}.atlassian.net",
    context_path: '',
    auth_type:    :basic,
    use_ssl:      true,
  })

  self.project = self.client.Project.find(options[:project])
  self.ticket = self.client.Issue.find(options[:ticket_id])
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/toolshed/ticket_tracking/jira.rb', line 10

def client
  @client
end

#default_pull_request_title_formatObject

Returns the value of attribute default_pull_request_title_format.



10
11
12
# File 'lib/toolshed/ticket_tracking/jira.rb', line 10

def default_pull_request_title_format
  @default_pull_request_title_format
end

#ownerObject

Returns the value of attribute owner.



10
11
12
# File 'lib/toolshed/ticket_tracking/jira.rb', line 10

def owner
  @owner
end

#projectObject

Returns the value of attribute project.



10
11
12
# File 'lib/toolshed/ticket_tracking/jira.rb', line 10

def project
  @project
end

#ticketObject

Returns the value of attribute ticket.



10
11
12
# File 'lib/toolshed/ticket_tracking/jira.rb', line 10

def ticket
  @ticket
end

Class Method Details

.create_instance(options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/toolshed/ticket_tracking/jira.rb', line 93

def create_instance(options={})
  raise 'Unable to use Jira as project name was not supplied' unless (options.has_key?(:project))
  raise 'Unable to use Jira as ticket id was not supplied' unless (options.has_key?(:ticket_id))

  jira = Toolshed::TicketTracking::Jira.new({
    project:    options[:project],
    username:   Toolshed::TicketTracking::Jira.username,
    password:   Toolshed::TicketTracking::Jira.password,
    ticket_id:  options[:ticket_id]
  })
end

.passwordObject



83
84
85
86
87
88
89
90
91
# File 'lib/toolshed/ticket_tracking/jira.rb', line 83

def password
  return Toolshed::Client.instance.ticket_tracker_password unless Toolshed::Client.instance.ticket_tracker_password.nil?

  # prompt to ask for password
  system "stty -echo"
  puts "Jira password? "
  password = $stdin.gets.chomp.strip
  system "stty echo"
end

.usernameObject



75
76
77
78
79
80
81
# File 'lib/toolshed/ticket_tracking/jira.rb', line 75

def username
  return Toolshed::Client.instance.ticket_tracker_username unless Toolshed::Client.instance.ticket_tracker_username.nil?

  # prompt to ask for username
  puts "Jira username? "
  username = $stdin.gets.chomp.strip
end

Instance Method Details

#add_note(note_text) ⇒ Object



32
33
34
35
# File 'lib/toolshed/ticket_tracking/jira.rb', line 32

def add_note(note_text)
  issue = self.ticket.comments.build
  issue.save({ 'body' => note_text })
end

#attribute_value(attribute) ⇒ Object



66
67
68
# File 'lib/toolshed/ticket_tracking/jira.rb', line 66

def attribute_value(attribute)
  self.ticket.send(attribute)
end

#available_statusesObject



44
45
46
# File 'lib/toolshed/ticket_tracking/jira.rb', line 44

def available_statuses
  self.client.Transition.all(issue: self.ticket)
end

#default_titleObject



58
59
60
61
62
63
64
# File 'lib/toolshed/ticket_tracking/jira.rb', line 58

def default_title
  default_title_text = self.default_pull_request_title_format
  self.default_pull_request_title_format.scan(/(\[\w*\])/).each do |replacement_element|
    default_title_text = default_title_text.gsub(replacement_element.first.to_s, self.send(replacement_element.first.to_s.gsub("[", "").gsub("]", "")))
  end
  default_title_text
end

#transition_status_id_by_status(status) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/toolshed/ticket_tracking/jira.rb', line 48

def transition_status_id_by_status(status)
  self.available_statuses.each do |transition_status|
    if (status == transition_status.name)
      return transition_status.id
    end
  end

  raise "Unable to find available status"
end

#update_ticket_status(status, options = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/toolshed/ticket_tracking/jira.rb', line 37

def update_ticket_status(status, options={})
  available_statuses

  transition = self.ticket.transitions.build
  transition.save({ 'transition' => { "id" => transition_status_id_by_status(status) } })
end

#urlObject



70
71
72
# File 'lib/toolshed/ticket_tracking/jira.rb', line 70

def url
  "https://#{self.owner}.atlassian.net/browse/#{self.ticket.key}"
end