Class: Toolshed::TicketTracking::Jira

Inherits:
Object
  • Object
show all
Extended by:
Toolshed::TicketTracking
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 included from Toolshed::TicketTracking

clean, story_id_from_branch_name

Constructor Details

#initialize(options = {}) ⇒ Jira

Returns a new instance of Jira.



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

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

  self.owner      = Toolshed::Client::ticket_tracker_owner

  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.



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

def client
  @client
end

#ownerObject

Returns the value of attribute owner.



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

def owner
  @owner
end

#projectObject

Returns the value of attribute project.



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

def project
  @project
end

#ticketObject

Returns the value of attribute ticket.



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

def ticket
  @ticket
end

Class Method Details

.create_instance(options = {}) ⇒ Object



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

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

  unless (options.has_key?(:ticket_id))
    raise 'Unable to use Jira as ticket id was not supplied'
  end

  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



78
79
80
81
82
83
84
85
86
# File 'lib/toolshed/ticket_tracking/jira.rb', line 78

def self.password
  return Toolshed::Client::ticket_tracker_password unless Toolshed::Client::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

Class methods



70
71
72
73
74
75
76
# File 'lib/toolshed/ticket_tracking/jira.rb', line 70

def self.username
  return Toolshed::Client::ticket_tracker_username unless Toolshed::Client::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

Instance methods



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

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

#available_statusesObject



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

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

#titleObject



59
60
61
# File 'lib/toolshed/ticket_tracking/jira.rb', line 59

def title
  self.ticket.summary
end

#transition_status_id_by_status(status) ⇒ Object



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

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



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

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



63
64
65
# File 'lib/toolshed/ticket_tracking/jira.rb', line 63

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