Class: Jigit::Init

Inherits:
Runner
  • Object
show all
Defined in:
lib/jigit/commands/init.rb

Overview

This class is heavily based on the Init command from the Danger gem The original link is github.com/danger/danger/blob/master/lib/danger/commands/init.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Runner

#ui

Constructor Details

#initialize(argv) ⇒ Init

Returns a new instance of Init.



28
29
30
31
32
# File 'lib/jigit/commands/init.rb', line 28

def initialize(argv)
  super
  ui.no_delay = argv.flag?("impatient", false)
  ui.no_waiting = argv.flag?("mousey", false)
end

Instance Attribute Details

#current_jira_configObject

Returns the value of attribute current_jira_config.



15
16
17
# File 'lib/jigit/commands/init.rb', line 15

def current_jira_config
  @current_jira_config
end

Class Method Details

.optionsObject



21
22
23
24
25
26
# File 'lib/jigit/commands/init.rb', line 21

def self.options
  [
    ["--impatient", "'I've not got all day here. Don't add any thematic delays please.'"],
    ["--mousey", "'Don't make me press return to continue the adventure.'"]
  ].concat(super)
end

Instance Method Details

#ask_for_in_progress_status_name(status_names) ⇒ Object



164
165
166
167
# File 'lib/jigit/commands/init.rb', line 164

def ask_for_in_progress_status_name(status_names)
  in_progress_status_name = ui.ask_with_answers("What status do you set when work on the JIRA issue\n", status_names, true)
  in_progress_status_name
end

#ask_for_jira_account_emailObject



62
63
64
# File 'lib/jigit/commands/init.rb', line 62

def 
  ui.ask("What's is your JIRA's account email").strip
end

#ask_for_jira_account_password(email) ⇒ Object



66
67
68
# File 'lib/jigit/commands/init.rb', line 66

def (email)
  ui.ask("What's is the password for #{email}").strip
end

#ask_for_jira_host(polite) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jigit/commands/init.rb', line 70

def ask_for_jira_host(polite)
  return ui.ask("What's is the host for your JIRA server").strip unless polite

  ui.say "\nThanks, and the last one is a bit tricky. Jigit needs the " + "host".green + " of your JIRA server.\n"
  ui.pause 0.6
  ui.say "The easiest way to get it is to go to your JIRA website and check the browser address field.\n"
  ui.pause 0.6
  ui.say "Usually it looks like " + "your_company_name.atlassian.net".green + ".\n"
  ui.pause 0.6
  ui.ask("What's is the host for your JIRA server").strip
end

#ask_for_other_status_names(status_names) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/jigit/commands/init.rb', line 169

def ask_for_other_status_names(status_names)
  not_asked_status_names = status_names
  selected_status_names = []
  ui.say "Now Jigit needs to know, what status could you set when stop working on the issue.\n"
  ui.pause 0.6
  ui.say "We know you can have multiple, don't worry  and"
  ui.pause 0.6
  ui.say "when you're done select 'nothing' option.\n"
  ui.pause 1

  selected_status_name = nil
  loop do
    selected_status_names << selected_status_name unless selected_status_name.nil?
    break if not_asked_status_names.count.zero?
    selected_status_name = ui.ask_with_answers("Which one you want to select", not_asked_status_names + ["nothing"], true)
    break if selected_status_name == "nothing"
    ui.say selected_status_name
    not_asked_status_names.delete(selected_status_name)
  end
  return selected_status_names
end

#build_jira_config_politely(politely) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jigit/commands/init.rb', line 98

def build_jira_config_politely(politely)
  email = 
  password = (email)
  host = ask_for_jira_host(politely)

  ui.say "\nThanks, let's validate if the Jigit has access now...\n" if politely
  config = Jigit::JiraConfig.new(email, password, host)
  if validate_jira_config?(config)
    config
  else
    build_jira_config_politely(false)
  end
rescue Jigit::NetworkError => exception
  ui.error "Error while validating access to JIRA API: #{exception.message}"
  return nil
end

#fetch_jira_status_namesObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/jigit/commands/init.rb', line 135

def fetch_jira_status_names
  ui.say "Fetching all possible statuses from JIRA...\n"
  jira_api_client = Jigit::JiraAPIClient.new(self.current_jira_config, nil)
  begin
    all_statuses = jira_api_client.fetch_jira_statuses
    if all_statuses.nil? || all_statuses.count.zero?
      ui.error "Yikes 😕\n"
      ui.say "Jigit can not find any statuses for JIRA issue in your company setup.\n"
      return nil
    else
      all_statuses.map(&:name)
    end
  rescue Jigit::JiraAPIClientError => exception
    ui.error "Error while fetching statuses from JIRA API: #{exception.message}"
    return false
  rescue Jigit::NetworkError => exception
    ui.error "Error while fetching statuses from JIRA API: #{exception.message}"
    return false
  end
end

#handle_nicely_setup_jigitfile_failureObject



156
157
158
159
160
161
162
# File 'lib/jigit/commands/init.rb', line 156

def handle_nicely_setup_jigitfile_failure
  ui.say "Unfortunately, Jigit can not proceed without that information.\n"
  ui.pause 0.6
  ui.say "Try to check the JIRA setup and your internet connection status.\n"
  ui.pause 0.6
  ui.say "If everything looks fine, try to init Jigit once egain: `bundle exec jigit init`"
end

#infoObject



294
295
296
297
298
299
300
301
302
303
# File 'lib/jigit/commands/init.rb', line 294

def info
  ui.header "Useful info"
  ui.say "- This project is at it's early stage and may be unstable"
  ui.pause 0.6
  ui.say "- If you find any bug or want to add something, you're very welcome to the repo:"
  ui.link "https://github.com/Antondomashnev/jigit"
  ui.pause 0.6
  ui.say "- If you want to know more, follow " + "@antondomashnev".green + " on Twitter"
  ui.pause 1
end

#runObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jigit/commands/init.rb', line 34

def run
  ui.say "\nOK, thanks #{ENV['LOGNAME']}, have a seat and we'll get you started.\n".yellow
  ui.pause 1

  show_todo_state
  ui.pause 1.4

  return unless setup_access_to_jira
  return unless setup_jigitfile
  return unless setup_post_checkout_hook
  return unless setup_gitignore

  info
  thanks
end

#setup_access_to_jiraObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/jigit/commands/init.rb', line 115

def setup_access_to_jira
  ui.header "\nStep 1: Setting up an access to JIRA"
  ui.pause 0.6
  ui.say "In order to Jigit to be able to help you, it needs access to your JIRA account.\n"
  ui.pause 0.6
  ui.say "But don't worry it'll store it in a safe place.\n"
  ui.pause 1

  self.current_jira_config = build_jira_config_politely(true)
  if self.current_jira_config
    keychain_storage = Jigit::KeychainStorage.new
    keychain_storage.save(self.current_jira_config.user, self.current_jira_config.password, self.current_jira_config.host)
    ui.say "Let's move to next step, press return when ready..."
    ui.wait_for_return
    return true
  else
    return false
  end
end

#setup_gitignoreObject



283
284
285
286
287
288
289
290
291
292
# File 'lib/jigit/commands/init.rb', line 283

def setup_gitignore
  setup_gitignore_intro

  git_hook_installer = Jigit::GitIgnoreUpdater.new
  git_hook_installer.ignore(".jigit")

  setup_gitignore_outro

  return true
end

#setup_gitignore_introObject



268
269
270
271
272
273
274
# File 'lib/jigit/commands/init.rb', line 268

def setup_gitignore_intro
  ui.header "Step 4: Adding private jigit's related things to .gitignore."
  ui.say "Jigit has been setup for your personal usage with your personal info"
  ui.pause 0.6
  ui.say "therefore it can not be really used accross the team, so we need to git ignore the related files."
  ui.pause 0.6
end

#setup_gitignore_outroObject



276
277
278
279
280
281
# File 'lib/jigit/commands/init.rb', line 276

def setup_gitignore_outro
  ui.say "And the git ignore now ignores your .jigit folder 🎉.\n"
  ui.pause 0.6
  ui.say "That's all to finish initialization press return"
  ui.wait_for_return
end

#setup_jigitfileObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/jigit/commands/init.rb', line 221

def setup_jigitfile
  setup_jigitfile_into

  jira_status_names = fetch_jira_status_names
  unless jira_status_names
    handle_nicely_setup_jigitfile_failure
    return false
  end
  ui.pause 0.6

  setup_jigitfile_with_user_input(jira_status_names)
  setup_jigitfile_outro

  return true
end

#setup_jigitfile_intoObject



191
192
193
194
195
# File 'lib/jigit/commands/init.rb', line 191

def setup_jigitfile_into
  ui.header "Step 2: Setting up a Jigit configuration file"
  ui.say "In order to Jigit to be able to help you it needs to know something about your usual workflow.\n"
  ui.pause 1
end

#setup_jigitfile_outroObject



197
198
199
200
201
202
203
# File 'lib/jigit/commands/init.rb', line 197

def setup_jigitfile_outro
  ui.say "And the jigitfile is ready 🎉.\n"
  ui.say "You can find it at './.jigit/Jigitfile.yml'"
  ui.pause 0.6
  ui.say "Let's move to next step, press return when ready..."
  ui.wait_for_return
end

#setup_jigitfile_with_user_input(jira_status_names) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/jigit/commands/init.rb', line 205

def setup_jigitfile_with_user_input(jira_status_names)
  jigitfile_generator = Jigit::JigitfileGenerator.new
  jigitfile_generator.write_jira_host(self.current_jira_config.host)
  ui.pause 0.6

  in_progress_status_name = ask_for_in_progress_status_name(jira_status_names)
  jigitfile_generator.write_in_progress_status_name(in_progress_status_name)
  ui.pause 0.6

  selected_status_names = ask_for_other_status_names(jira_status_names)
  jigitfile_generator.write_other_statuses(selected_status_names)
  ui.pause 0.6

  jigitfile_generator.save
end

#setup_post_checkout_hookObject



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/jigit/commands/init.rb', line 256

def setup_post_checkout_hook
  setup_post_checkout_hook_intro

  git_hook_installer = Jigit::GitHookInstaller.new
  post_checkout_hook = Jigit::PostCheckoutHook
  git_hook_installer.install(post_checkout_hook)

  setup_post_checkout_hook_outro

  return true
end

#setup_post_checkout_hook_introObject



237
238
239
240
241
242
243
244
245
246
# File 'lib/jigit/commands/init.rb', line 237

def setup_post_checkout_hook_intro
  ui.header "Step 3: Setting up a git hooks to automate the process."
  ui.say "Jigit is going to create a post-checkout git hook."
  ui.pause 0.6
  ui.say "It will the 'git checkout' command and if it's a checkout to a branch."
  ui.pause 0.6
  ui.say "Jigit will ask it needs to put the new branch's related issue In Progress"
  ui.pause 0.6
  ui.say "and to update status for the old branch on JIRA"
end

#setup_post_checkout_hook_outroObject



248
249
250
251
252
253
254
# File 'lib/jigit/commands/init.rb', line 248

def setup_post_checkout_hook_outro
  ui.say "And the git hook is ready 🎉.\n"
  ui.say "You can find it at './.git/hooks/post-checkout'"
  ui.pause 0.6
  ui.say "One last step and we're done, press return to continue..."
  ui.wait_for_return
end

#show_todo_stateObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jigit/commands/init.rb', line 50

def show_todo_state
  ui.say "We need to do the following:\n"
  ui.pause 0.6
  ui.say " - [ ] Set up an access to JIRA."
  ui.pause 0.6
  ui.say " - [ ] Set up a Jigit configuration file."
  ui.pause 0.6
  ui.say " - [ ] Set up a git hooks to automate the process."
  ui.pause 0.6
  ui.say " - [ ] Add private jigit's related things to .gitignore."
end

#thanksObject



305
306
307
# File 'lib/jigit/commands/init.rb', line 305

def thanks
  ui.say "\n\nHave a happy coding 🎉"
end

#validate_jira_config?(config) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jigit/commands/init.rb', line 82

def validate_jira_config?(config)
  is_valid = Jigit::JiraAPIClient.new(config, nil).validate_api?
  if is_valid
    ui.inform "Hooray 🎉, everything is green.\n"
    return true
  else
    ui.error "Yikes 😕\n"
    ui.say "Let's try once again, you can do it 💪\n"
    return false
  end
rescue Jigit::JiraAPIClientError => exception
  ui.error "Yikes 😕\n"
  ui.say "Let's try once again, you can do it 💪\n"
  return false
end