Class: GithubPivotalFlow::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/github_pivotal_flow/configuration.rb

Overview

A class that exposes configuration that commands can use

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



7
8
9
10
# File 'lib/github_pivotal_flow/configuration.rb', line 7

def initialize(options = {})
  @options = options
  @github_password_cache = {}
end

Instance Method Details

#api_tokenString

Returns the user’s Pivotal Tracker API token. If this token has not been configured, prompts the user for the value. The value is checked for in the inherited Git configuration, but is stored in the global Git configuration so that it can be used across multiple repositories.

Returns:

  • (String)

    The user’s Pivotal Tracker API token



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/github_pivotal_flow/configuration.rb', line 43

def api_token
  api_token = @options[:api_token] || Git.get_config(KEY_API_TOKEN, :inherited)

  if api_token.blank?
    api_token = ask('Pivotal API Token (found at https://www.pivotaltracker.com/profile): ').strip
    Git.set_config(KEY_API_TOKEN, api_token, :local) unless api_token.blank?
    puts
  end

  api_token
end

#ask_auth_codeObject



275
276
277
278
279
280
# File 'lib/github_pivotal_flow/configuration.rb', line 275

def ask_auth_code
  print "two-factor authentication code: "
  $stdin.gets.chomp
rescue Interrupt
  abort
end

#ask_github_password(username = nil) ⇒ Object

special prompt that has hidden input



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/github_pivotal_flow/configuration.rb', line 260

def ask_github_password(username = nil)
  username ||= github_username
  print "Github password for #{username} (never stored): "
  if $stdin.tty?
    password = askpass
    puts ''
    password
  else
    # in testing
    $stdin.gets.chomp
  end
rescue Interrupt
  abort
end

#askpassObject



282
283
284
285
286
# File 'lib/github_pivotal_flow/configuration.rb', line 282

def askpass
  noecho $stdin do |input|
    input.gets.chomp
  end
end

#clear_github_auth_data!Object



225
226
227
228
# File 'lib/github_pivotal_flow/configuration.rb', line 225

def clear_github_auth_data!
  @github_password_cache = {}
  Git.delete_config(KEY_GITHUB_USERNAME, :local)
end

#clear_pivotal_api_token!Object



68
69
70
71
# File 'lib/github_pivotal_flow/configuration.rb', line 68

def clear_pivotal_api_token!
  PivotalTracker::Client.token = nil
  Git.delete_config(KEY_API_TOKEN, :local)
end

#development_branchObject



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/github_pivotal_flow/configuration.rb', line 167

def development_branch
  development_branch = Git.get_config KEY_DEVELOPMENT_BRANCH, :inherited

  if development_branch.empty?
    development_branch = ask('Please enter your git-flow development branch name: [development]').strip
    development_branch = 'development' if development_branch.blank?
    Git.set_config KEY_DEVELOPMENT_BRANCH, development_branch, :local
  end
  Git.ensure_branch_exists(development_branch)

  development_branch
end

#ensure_gitflow_configObject



193
194
195
# File 'lib/github_pivotal_flow/configuration.rb', line 193

def ensure_gitflow_config
  development_branch && master_branch && feature_prefix && hotfix_prefix && release_prefix
end

#ensure_github_api_tokenObject



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

def ensure_github_api_token
  begin
    repo_found = github_client.repo_exists?(project)
  end while github_api_token.blank?
  raise("Could not find github project") unless repo_found
rescue Net::HTTPServerException => e
  case e.response.code
  when '401'
    say "Invalid username/password combination. Please try again:"
  else
    say "Unknown error (#{e.response.code}). Please try again: "
  end
  clear_github_auth_data!
  retry
end

#ensure_pivotal_api_tokenObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/github_pivotal_flow/configuration.rb', line 55

def ensure_pivotal_api_token
  PivotalTracker::Client.use_ssl = true
  while (PivotalTracker::Client.token = self.api_token).blank? || PivotalTracker::Project.all.empty?
    puts "No projects found."
    clear_pivotal_api_token!
  end
  return true
rescue RestClient::Unauthorized => e
  puts "Invalid Pivotal token"
  clear_pivotal_api_token!
  retry
end

#fallback_noecho(io) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/github_pivotal_flow/configuration.rb', line 295

def fallback_noecho io
  tty_state = `stty -g 2>#{NULL}`
  system 'stty raw -echo -icanon isig' if $?.success?
  pass = ''
  while char = getbyte(io) and !(char == 13 or char == 10)
    if char == 127 or char == 8
      pass[-1,1] = '' unless pass.empty?
    else
      pass << char.chr
    end
  end
  pass
ensure
  system "stty #{tty_state}" unless tty_state.empty?
end

#feature_prefixObject



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/github_pivotal_flow/configuration.rb', line 128

def feature_prefix
  feature_prefix = Git.get_config KEY_FEATURE_PREFIX, :inherited

  if feature_prefix.empty?
    feature_prefix = ask('Please enter your git-flow feature branch prefix: [feature/]').strip
    feature_prefix = 'feature/' if feature_prefix.blank?
    feature_prefix = "#{feature_prefix}/" unless feature_prefix[-1,1] == '/'
    Git.set_config KEY_FEATURE_PREFIX, feature_prefix, :local
  end

  feature_prefix
end

#getbyte(io) ⇒ Object



311
312
313
314
315
316
317
318
# File 'lib/github_pivotal_flow/configuration.rb', line 311

def getbyte(io)
  if io.respond_to?(:getbyte)
    io.getbyte
  else
    # In Ruby <= 1.8.6, getc behaved the same
    io.getc
  end
end

#github_api_token(host = nil, user = nil) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/github_pivotal_flow/configuration.rb', line 246

def github_api_token(host = nil, user = nil)
  host ||= github_host
  user ||= github_username
  github_token = Git.get_config KEY_GITHUB_API_TOKEN, :inherited
  if github_token.blank?
    if block_given?
      github_token = yield
    end
    Git.set_config(KEY_GITHUB_API_TOKEN, github_token, :global) unless github_token.blank?
  end
  github_token
end

#github_clientObject



197
198
199
# File 'lib/github_pivotal_flow/configuration.rb', line 197

def github_client
  @ghclient ||= GitHubAPI.new(self, :app_url => 'http://github.com/roomorama/github-pivotal-flow')
end

#github_hostObject



201
202
203
# File 'lib/github_pivotal_flow/configuration.rb', line 201

def github_host
  project.host
end

#github_password(host = github_host, user = nil) ⇒ Object



219
220
221
222
223
# File 'lib/github_pivotal_flow/configuration.rb', line 219

def github_password(host = github_host, user = nil)
  return ENV['GITHUB_PASSWORD'] unless ENV['GITHUB_PASSWORD'].to_s.blank?
  user ||= github_username(host)
  @github_password_cache["#{user}"] ||= ask_github_password(user)
end

#github_username(host = github_host) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'lib/github_pivotal_flow/configuration.rb', line 205

def github_username(host = github_host)
  return ENV['GITHUB_USER'] unless ENV['GITHUB_USER'].to_s.blank?
  github_username = Git.get_config KEY_GITHUB_USERNAME, :inherited
  if github_username.blank?
    github_username = ask('Github username: ').strip
    Git.set_config KEY_GITHUB_USERNAME, github_username, :local
  end
  github_username
end

#github_username=(username) ⇒ Object



215
216
217
# File 'lib/github_pivotal_flow/configuration.rb', line 215

def github_username=(username)
  Git.set_config KEY_GITHUB_USERNAME, username, :local unless username.blank?
end

#hotfix_prefixObject



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/github_pivotal_flow/configuration.rb', line 141

def hotfix_prefix
  hotfix_prefix = Git.get_config KEY_HOTFIX_PREFIX, :inherited

  if hotfix_prefix.empty?
    hotfix_prefix = ask('Please enter your git-flow hotfix branch prefix: [hotfix/]').strip
    hotfix_prefix = 'hotfix/' if hotfix_prefix.blank?
    hotfix_prefix = "#{hotfix_prefix}/" unless hotfix_prefix[-1,1] == '/'
    Git.set_config KEY_HOTFIX_PREFIX, hotfix_prefix, :local
  end

  hotfix_prefix
end

#master_branchObject



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/github_pivotal_flow/configuration.rb', line 180

def master_branch
  master_branch = Git.get_config KEY_MASTER_BRANCH, :inherited

  if master_branch.blank?
    master_branch = ask('Please enter your git-flow production branch name: [master]').strip
    master_branch = 'master' if master_branch.blank?
    Git.set_config KEY_MASTER_BRANCH, master_branch, :local
  end
  Git.ensure_branch_exists(master_branch)

  master_branch
end

#noecho(io) ⇒ Object



288
289
290
291
292
293
# File 'lib/github_pivotal_flow/configuration.rb', line 288

def noecho io
  require 'io/console'
  io.noecho { yield io }
rescue LoadError
  fallback_noecho io
end

#projectObject



98
99
100
# File 'lib/github_pivotal_flow/configuration.rb', line 98

def project
  @project ||= Project.new(config: self)
end

#project_idString

Returns the Pivotal Tracker project id for this repository. If this id has not been configuration, prompts the user for the value. The value is checked for in the inherited Git configuration, but is stored in the local Git configuration so that it is specific to this repository.

Returns:

  • (String)

    The repository’s Pivotal Tracker project id



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/github_pivotal_flow/configuration.rb', line 79

def project_id
  project_id = @options[:project_id] || Git.get_config(KEY_PROJECT_ID, :inherited)

  if project_id.empty?
    project_id = choose do |menu|
      menu.prompt = 'Choose project associated with this repository: '

      PivotalTracker::Project.all.sort_by { |project| project.name }.each do |project|
        menu.choice(project.name) { project.id }
      end
    end

    Git.set_config(KEY_PROJECT_ID, project_id, :local)
    puts
  end

  project_id
end

#proxy_uri(with_ssl) ⇒ Object



320
321
322
323
324
325
326
# File 'lib/github_pivotal_flow/configuration.rb', line 320

def proxy_uri(with_ssl)
  env_name = "HTTP#{with_ssl ? 'S' : ''}_PROXY"
  if proxy = ENV[env_name] || ENV[env_name.downcase] and !proxy.empty?
    proxy = "http://#{proxy}" unless proxy.include? '://'
    URI.parse proxy
  end
end

#release_prefixObject



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/github_pivotal_flow/configuration.rb', line 154

def release_prefix
  release_prefix = Git.get_config KEY_RELEASE_PREFIX, :inherited

  if release_prefix.empty?
    release_prefix = ask('Please enter your git-flow release branch prefix: [release/]').strip
    release_prefix = 'release' if release_prefix.blank?
    release_prefix = "#{release_prefix}/" unless release_prefix[-1,1] == '/'
    Git.set_config(KEY_RELEASE_PREFIX, release_prefix, :local)
  end

  release_prefix
end

#repository_rootObject



22
23
24
# File 'lib/github_pivotal_flow/configuration.rb', line 22

def repository_root
  @repository_root ||= Git.repository_root
end

#storyStory

Returns the story associated with the branch

Returns:

  • (Story)

    the story associated with the current development branch



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/github_pivotal_flow/configuration.rb', line 105

def story
  return @story if @story
  story_id = Git.get_config(KEY_STORY_ID, :branch)
  if story_id.blank? && (matchdata = /^[a-z0-9_\-]+\/(\d+)(-[a-z0-9_\-]+)?$/i.match(Git.current_branch))
    story_id = matchdata[1]
    Git.set_config(KEY_STORY_ID, story_id, :branch) unless story_id.blank?
  end
  if story_id.blank?
    story_id = ask('What Pivotal story ID is this branch associated with?').strip
    Git.set_config(KEY_STORY_ID, story_id, :branch) unless story_id.blank?
  end
  return nil if story_id.blank?
  return (@story = Story.new(project, project.stories.find(story_id.to_i), branch_name: Git.current_branch))
end

#story=(story) ⇒ void

This method returns an undefined value.

Stores the story associated with the current development branch

Parameters:

  • story (PivotalTracker::Story)

    the story associated with the current development branch



124
125
126
# File 'lib/github_pivotal_flow/configuration.rb', line 124

def story=(story)
  Git.set_config KEY_STORY_ID, story.id, :branch
end

#user_nameObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/github_pivotal_flow/configuration.rb', line 26

def user_name
  user_name = Git.get_config(KEY_USER_NAME, :inherited).strip
  if user_name.blank?
    user_name = ask('Github user name (Should be the same as in your Pivotal profile): ').strip
    Git.set_config(KEY_USER_NAME, user_name, :local) unless user_name.blank?
    puts
  end

  user_name
end

#validateObject



12
13
14
15
16
17
18
19
20
# File 'lib/github_pivotal_flow/configuration.rb', line 12

def validate
  repository_root
  user_name
  ensure_github_api_token
  ensure_pivotal_api_token
  ensure_gitflow_config
  project.config = self
  return true
end