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



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/github_pivotal_flow/configuration.rb', line 18

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

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

  api_token
end

#ask_auth_codeObject



189
190
191
192
193
194
# File 'lib/github_pivotal_flow/configuration.rb', line 189

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

#ask_password(user) ⇒ Object

special prompt that has hidden input



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

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

#askpassObject



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

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

#development_branchObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/github_pivotal_flow/configuration.rb', line 114

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.nil? || development_branch.empty?
    Git.set_config KEY_DEVELOPMENT_BRANCH, development_branch, :local
    puts
  end
  Git.ensure_branch_exists(development_branch)

  development_branch
end

#fallback_noecho(io) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/github_pivotal_flow/configuration.rb', line 209

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/github_pivotal_flow/configuration.rb', line 72

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
    puts
  end

  feature_prefix
end

#getbyte(io) ⇒ Object



225
226
227
228
229
230
231
232
# File 'lib/github_pivotal_flow/configuration.rb', line 225

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, user) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/github_pivotal_flow/configuration.rb', line 165

def github_api_token(host, user)
  github_token = Git.get_config KEY_GITHUB_API_TOKEN, :inherited
  if github_token.blank?
    github_token = yield
    Git.set_config KEY_GITHUB_API_TOKEN, github_token, :local
  end
  github_token
end

#github_password(host, user) ⇒ Object



160
161
162
163
# File 'lib/github_pivotal_flow/configuration.rb', line 160

def github_password(host, user)
  return ENV['GITHUB_PASSWORD'] unless ENV['GITHUB_PASSWORD'].to_s.empty?
  @github_password_cache["#{user}"] ||= ask_password(user)
end

#github_projectObject



142
143
144
# File 'lib/github_pivotal_flow/configuration.rb', line 142

def github_project
  @github_project ||= Project.from_url(Git.get_config("remote.#{Git.get_remote}.url"))
end

#github_username(host) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/github_pivotal_flow/configuration.rb', line 146

def github_username(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=(github_username) ⇒ Object



156
157
158
# File 'lib/github_pivotal_flow/configuration.rb', line 156

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

#hotfix_prefixObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/github_pivotal_flow/configuration.rb', line 86

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
    puts
  end

  hotfix_prefix
end

#master_branchObject



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

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.nil? || master_branch.empty?
    Git.set_config KEY_MASTER_BRANCH, master_branch, :local
    puts
  end
  Git.ensure_branch_exists(master_branch)

  master_branch
end

#noecho(io) ⇒ Object



202
203
204
205
206
207
# File 'lib/github_pivotal_flow/configuration.rb', line 202

def noecho io
  require 'io/console'
  io.noecho { yield io }
rescue LoadError
  fallback_noecho io
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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/github_pivotal_flow/configuration.rb', line 36

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



234
235
236
237
238
239
240
# File 'lib/github_pivotal_flow/configuration.rb', line 234

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



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/github_pivotal_flow/configuration.rb', line 100

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
    puts
  end

  release_prefix
end

#story(project) ⇒ PivotalTracker::Story

Returns the story associated with the current development branch

Parameters:

  • project (PivotalTracker::Project)

    the project the story belongs to

Returns:

  • (PivotalTracker::Story)

    the story associated with the current development branch



59
60
61
62
# File 'lib/github_pivotal_flow/configuration.rb', line 59

def story(project)
  story_id = Git.get_config(KEY_STORY_ID, :branch)
  Story.new(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



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

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