Module: PT::Helper

Included in:
CLI
Defined in:
lib/pt/helper.rb

Constant Summary collapse

GLOBAL_CONFIG_PATH =
ENV['HOME'] + "/.pt"
LOCAL_CONFIG_PATH =
Dir.pwd + '/.pt'

Instance Method Summary collapse

Instance Method Details

#ask(msg) ⇒ Object



120
121
122
# File 'lib/pt/helper.rb', line 120

def ask(msg)
  @io.ask("#{msg.bold}")
end

#ask_secret(msg) ⇒ Object



124
125
126
# File 'lib/pt/helper.rb', line 124

def ask_secret(msg)
  @io.ask("#{msg.bold}"){ |q| q.echo = '*' }
end

#check_local_config_pathObject



77
78
79
80
81
82
# File 'lib/pt/helper.rb', line 77

def check_local_config_path
  if GLOBAL_CONFIG_PATH == get_local_config_path()
    error("Please execute .pt inside your project directory and not in your home.")
    exit
  end
end

#compact_message(*msg) ⇒ Object



107
108
109
# File 'lib/pt/helper.rb', line 107

def compact_message(*msg)
  puts "#{split_lines(msg)}"
end

#congrats(*msg) ⇒ Object



99
100
101
# File 'lib/pt/helper.rb', line 99

def congrats(*msg)
  puts "\n#{split_lines(msg).green.bold}"
end

#edit_story_task(task) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/pt/helper.rb', line 189

def edit_story_task(task)
  action_class = Struct.new(:action, :key)

  table = ActionTable.new([
    action_class.new('Complete', :complete),
    # action_class.new('Delete', :delete),
    action_class.new('Edit', :edit)
    # Move?
  ])
  action_to_execute = select('What to do with todo?', table)

  task.project_id = project.id
  task.client = project.client
  case action_to_execute.key
  when :complete then
    task.complete = true
    congrats('Todo task completed!')
    # when :delete then
    #   task.delete
    #   congrats('Todo task removed')
  when :edit then
    new_description = ask('New task description')
    task.description = new_description
    congrats("Todo task changed to: \"#{task.description}\"")
  end
  task.save
end

#error(*msg) ⇒ Object



111
112
113
# File 'lib/pt/helper.rb', line 111

def error(*msg)
  puts "\n#{split_lines(msg).red.bold}"
end

#find_owner(query) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/pt/helper.rb', line 163

def find_owner query
  if query
    member = @client.get_member(query)
    return member ? member.person : nil
  end
  nil
end

#find_task(query) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/pt/helper.rb', line 153

def find_task query
  members = @client.get_members
  members.each do | member |
    if member.name.downcase.index query
      return member.name
    end
  end
  nil
end

#get_local_config_pathObject



32
33
34
35
36
37
38
39
40
# File 'lib/pt/helper.rb', line 32

def get_local_config_path
  # If the local config path does not exist, check to see if we're in a git repo
  # And if so, try the top level of the checkout
  if (!File.exist?(LOCAL_CONFIG_PATH) && system('git rev-parse 2> /dev/null'))
    return `git rev-parse --show-toplevel`.chomp() + '/.pt'
  else
    return LOCAL_CONFIG_PATH
  end
end

#get_open_story_task_from_params(task) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/pt/helper.rb', line 176

def get_open_story_task_from_params(task)
  title "Pending tasks for '#{task.name}'"
  task_struct = Struct.new(:description, :position)

  pending_tasks = [
    task_struct.new('<< Add new task >>', -1)
  ]

  task.tasks.each{ |t| pending_tasks << t unless t.complete }
  table = TodoTaskTable.new(pending_tasks)
  select("Pick task to edit, 1 to add new task", table)
end

#load_global_configObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pt/helper.rb', line 7

def load_global_config

  # skip global config if env vars are set
  if ENV['PIVOTAL_EMAIL'] and ENV['PIVOTAL_API_KEY']
    config = {
      :email => ENV['PIVOTAL_EMAIL'],
      :api_number => ENV['PIVOTAL_API_KEY']
    }
    return config
  end

  config = YAML.load(File.read(GLOBAL_CONFIG_PATH)) rescue {}
  if config.empty?
    message "I can't find info about your Pivotal Tracker account in #{GLOBAL_CONFIG_PATH}."
    while !config[:api_number] do
      config[:api_number] = ask "What is your token?"
    end
    congrats "Thanks!",
      "Your API id is " + config[:api_number],
      "I'm saving it in #{GLOBAL_CONFIG_PATH} so you don't have to log in again."
    save_config(config, GLOBAL_CONFIG_PATH)
  end
  config
end

#load_local_configObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pt/helper.rb', line 42

def load_local_config
  check_local_config_path
  config = YAML.load(File.read(get_local_config_path())) rescue {}


  if ENV['PIVOTAL_PROJECT_ID']

    config[:project_id] = ENV['PIVOTAL_PROJECT_ID']

    @client = Client.new(@global_config[:api_number])
    project = @client.get_project(config[:project_id])
    config[:project_name] = project.name

    membership = @client.get_my_info
    config[:user_name], config[:user_id], config[:user_initials] = membership.name, membership.id, membership.initials
    save_config(config, get_local_config_path())

  end

  if config.empty?
    message "I can't find info about this project in #{get_local_config_path()}"
    @client = Client.new(@global_config[:api_number])
    projects = ProjectTable.new(@client.get_projects)
    project = select("Please select the project for the current directory", projects)
    config[:project_id], config[:project_name] = project.id, project.name
    project = @client.get_project(project.id)
    membership = @client.get_my_info
    config[:user_name], config[:user_id], config[:user_initials] = membership.name, membership.id, membership.initials
    congrats "Thanks! I'm saving this project's info",
      "in #{get_local_config_path()}: remember to .gitignore it!"
    save_config(config, get_local_config_path())
  end
  config
end

#message(*msg) ⇒ Object



103
104
105
# File 'lib/pt/helper.rb', line 103

def message(*msg)
  puts "\n#{split_lines(msg)}"
end


247
248
249
250
251
# File 'lib/pt/helper.rb', line 247

def print_stories_table(stories)
  table = TasksTable.new(stories)
  puts "[#{@client.current_page(options[:limit])}/#{@client.total_page(options[:limit])}]"
  table.print @global_config
end

#project_to_sObject



132
133
134
# File 'lib/pt/helper.rb', line 132

def project_to_s
  "Project #{@local_config[:project_name].upcase}"
end

#quitObject



115
116
117
118
# File 'lib/pt/helper.rb', line 115

def quit
  message "bye!"
  exit
end

#save_config(config, path) ⇒ Object



84
85
86
87
# File 'lib/pt/helper.rb', line 84

def save_config(config, path)
  File.new(path, 'w') unless File.exists?(path)
  File.open(path, 'w') {|f| f.write(config.to_yaml) }
end

#save_recent_task(task_id) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/pt/helper.rb', line 217

def save_recent_task( task_id )
  # save list of recently accessed tasks
  unless (@local_config[:recent_tasks])
    @local_config[:recent_tasks] = Array.new();
  end
  @local_config[:recent_tasks].unshift( task_id )
  @local_config[:recent_tasks] = @local_config[:recent_tasks].uniq()
  if @local_config[:recent_tasks].length > 10
    @local_config[:recent_tasks].pop()
  end
  save_config( @local_config, get_local_config_path() )
end

#select(msg, table) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/pt/helper.rb', line 230

def select(msg, table)
  if table.length > 0
    begin
      table.print @global_config
      row = ask "#{msg} (1-#{table.length}, 'q' to exit)"
      quit if row == 'q'
      selected = table[row]
      error "Invalid selection, try again:" unless selected
    end until selected
    selected
  else
    table.print @global_config
    message "Sorry, there are no options to select."
    quit
  end
end

#select_story_from_paginated_table(stories) ⇒ Object



253
254
255
256
257
# File 'lib/pt/helper.rb', line 253

def select_story_from_paginated_table(stories)
  puts "[#{@client.current_page(options[:limit])}/#{@client.total_page(options[:limit])}]"
  table = TasksTable.new(stories)
  select("Please select a story", table)
end

#show_activity(activity, tasks) ⇒ Object



172
173
174
# File 'lib/pt/helper.rb', line 172

def show_activity(activity, tasks)
  message("#{activity.message}")
end

#split_lines(text) ⇒ Object

I/O



91
92
93
# File 'lib/pt/helper.rb', line 91

def split_lines(text)
  text.respond_to?(:join) ? text.join("\n") : text
end

#task_by_id_or_pt_id(id) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/pt/helper.rb', line 143

def task_by_id_or_pt_id id
  if id < 1000
    tasks = @client.get_my_work(@local_config[:user_name])
    table = TasksTable.new(tasks)
    table[id]
  else
    @client.get_task_by_id id
  end
end

#task_type_or_nil(query) ⇒ Object



136
137
138
139
140
141
# File 'lib/pt/helper.rb', line 136

def task_type_or_nil query
  if (["feature", "bug", "chore"].index query)
    return query
  end
  nil
end

#title(*msg) ⇒ Object



95
96
97
# File 'lib/pt/helper.rb', line 95

def title(*msg)
  puts "\n#{split_lines(msg)}".bold
end

#user_sObject



128
129
130
# File 'lib/pt/helper.rb', line 128

def user_s
  "#{@local_config[:user_name]} (#{@local_config[:user_initials]})"
end