Class: Toolshed::TicketTracking::PivotalTracker

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

Constant Summary collapse

DEFAULT_COMPLETED_STATUS =
'finished'
USE_PROJECT_ID =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

wait_for_command

Constructor Details

#initialize(options = {}) ⇒ PivotalTracker

Returns a new instance of PivotalTracker.



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

def initialize(options={})
  username = Toolshed::Client.instance.pivotal_tracker_username
  password = Toolshed::Client.instance.pivotal_tracker_password
  self.default_pull_request_title_format = Toolshed::Client.instance.default_pull_request_title_format ||= "[title]"

  unless (options[:username].nil?)
    username = options[:username]
  end

  unless (options[:password].nil?)
     password = options[:password]
  end

  self.token = ::PivotalTracker::Client.token(username, password)
  self.project_id = (options[:project_id].nil?) ? Toolshed::Client.instance.default_pivotal_tracker_project_id : options[:project_id]
  @pt_project = ::PivotalTracker::Project.find(self.project_id)
  self.story = @pt_project.stories.find(options[:ticket_id])
end

Instance Attribute Details

#default_pull_request_title_formatObject

Returns the value of attribute default_pull_request_title_format.



11
12
13
# File 'lib/toolshed/ticket_tracking/pivotal_tracker.rb', line 11

def default_pull_request_title_format
  @default_pull_request_title_format
end

#project_idObject

Returns the value of attribute project_id.



11
12
13
# File 'lib/toolshed/ticket_tracking/pivotal_tracker.rb', line 11

def project_id
  @project_id
end

#storyObject

Returns the value of attribute story.



11
12
13
# File 'lib/toolshed/ticket_tracking/pivotal_tracker.rb', line 11

def story
  @story
end

#tokenObject

Returns the value of attribute token.



11
12
13
# File 'lib/toolshed/ticket_tracking/pivotal_tracker.rb', line 11

def token
  @token
end

Class Method Details

.create_instance(options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/toolshed/ticket_tracking/pivotal_tracker.rb', line 101

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

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

  Toolshed::TicketTracking::PivotalTracker.new({
    project_id: options[:project_id],
    username:   Toolshed::TicketTracking::PivotalTracker.username,
    password:   Toolshed::TicketTracking::PivotalTracker.password,
    ticket_id:   options[:ticket_id],
  })
end

.passwordObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/toolshed/ticket_tracking/pivotal_tracker.rb', line 88

def password
  password = Toolshed::Client.instance.pivotal_tracker_password
  if (password.nil?)
    # prompt to ask for password
    system "stty -echo"
    puts "PivotalTracker password? "
    password = $stdin.gets.chomp.strip
    system "stty echo"
  end

  return password
end

.usernameObject



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

def username
  username = Toolshed::Client.instance.pivotal_tracker_username
  if (username.nil?)
    # prompt to ask for username
    puts "PivotalTracker username? "
    username = $stdin.gets.chomp.strip
  end

  return username
end

Instance Method Details

#add_note(note_text) ⇒ Object



36
37
38
# File 'lib/toolshed/ticket_tracking/pivotal_tracker.rb', line 36

def add_note(note_text)
  results = self.story.notes.create(text: note_text)
end

#attribute_value(attribute) ⇒ Object



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

def attribute_value(attribute)
  value = self.story.send(attribute)
  self.clean(value) if attribute == 'title'
  value
end

#default_titleObject



62
63
64
65
66
67
68
# File 'lib/toolshed/ticket_tracking/pivotal_tracker.rb', line 62

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

#ticketObject



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

def ticket
  self.story
end

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/toolshed/ticket_tracking/pivotal_tracker.rb', line 40

def update_ticket_status(current_state, options={})
  options.merge!({
    :headers => {
        "X-TrackerToken"  => self.token,
        "User-Agent"      => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17",
        "Content-Type"    => "application/json",
    },
    body: {
      current_state: current_state
    }.to_json
  })

  response = HTTParty.put("#{Toolshed::Client::PIVOTAL_TRACKER_BASE_API_URL}projects/#{self.project_id}/stories/#{self.story.id}", options).response
  response = JSON.parse(response.body)

  if (response["error"].nil?)
    response
  else
    raise "validation errors #{response.inspect}"
  end
end