Class: Git::Story::App

Inherits:
Object
  • Object
show all
Extended by:
Utils
Includes:
ComplexConfig::Provider::Shortcuts, Utils, Tins::GO
Defined in:
lib/git/story/app.rb

Defined Under Namespace

Modules: StoryAccessors

Constant Summary collapse

BRANCH_NAME_REGEX =
/\A(story|feature)_([a-z0-9-]+)_(\d+)\z/

Instance Method Summary collapse

Methods included from Utils

ask, capture, sh

Constructor Details

#initialize(argv = ARGV.dup, debug: ENV['DEBUG'].to_i == 1) ⇒ App

Returns a new instance of App.



33
34
35
36
37
38
39
# File 'lib/git/story/app.rb', line 33

def initialize(argv = ARGV.dup, debug: ENV['DEBUG'].to_i == 1)
  @rest_argv = (sep = argv.index('--')) ? argv.slice!(sep..-1).tap(&:shift) : []
  @argv      = argv
  @opts    = go 'n:', @argv
  @command = @argv.shift&.to_sym
  @debug   = debug
end

Instance Method Details

#build_status(branch = current(check: false)) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/git/story/app.rb', line 148

def build_status(branch = current(check: false))
  watch do
    [
      "Test Status".bold,
      test_status(branch) || 'n/a',
      "Deploy Status".bold,
      deploy_status       || 'n/a',
    ] * "\n\n"
  end
end

#create(story_id = nil) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/git/story/app.rb', line 301

def create(story_id = nil)
  fetch_commits
  name = provide_name(story_id) or
    error "Cannot provide a new story name for story ##{story_id}: #{@reason.inspect}"
  if old_story = stories.find { |s| s.story_id == @story_id }
    error "story ##{@story_id} already exists in #{old_story}".red
  end
  puts "Now creating story #{name.inspect}".green
  sh "git checkout --track -b #{name}"
  sh "git push -u origin #{name}"
  "Story #{name} created.".green
end

#current(check: true) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/git/story/app.rb', line 70

def current(check: true)
  if check
    if cb = current_branch_checked?
      cb
    else
      error 'Switch to a story branch first for this operation!'
    end
  else
    current_branch
  end
end

#deploy_diff(ref = default_ref, rest: []) ⇒ Object



285
286
287
288
289
290
# File 'lib/git/story/app.rb', line 285

def deploy_diff(ref = default_ref, rest: [])
  ref = build_ref_range(ref)
  fetch_commits
  opts = (%w[ --color -u ] | rest) * ' '
  capture("git diff #{opts} #{ref}")
end

#deploy_lastObject



241
242
243
# File 'lib/git/story/app.rb', line 241

def deploy_last
  format_tag(tags.last)
end

#deploy_log(ref = default_ref, rest: []) ⇒ Object



246
247
248
249
250
251
252
253
254
255
# File 'lib/git/story/app.rb', line 246

def deploy_log(ref = default_ref, rest: [])
  ref = build_ref_range(ref)
  fetch_commits
  fetch_tags
  opts = ([
    '--color',
    '--pretty=tformat:"%C(yellow)%h%Creset %C(green)%ci%Creset %s (%Cred%an <%ae>%Creset)"'
  ] | rest) * ' '
  capture("git log #{opts} #{ref}")
end

#deploy_migrate_diff(ref = default_ref, rest: []) ⇒ Object



293
294
295
296
297
298
# File 'lib/git/story/app.rb', line 293

def deploy_migrate_diff(ref = default_ref, rest: [])
  ref = build_ref_range(ref)
  fetch_commits
  opts = (%w[ --color -u ] | rest) * ' '
  capture("git diff #{opts} #{ref} -- db/migrate")
end

#deploy_status(server = complex_config.story.semaphore_default_server) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/git/story/app.rb', line 118

def deploy_status(server = complex_config.story.semaphore_default_server)
  url = nil
  watch do
    auth_token = complex_config.story.semaphore_auth_token
    project    = complex_config.story.semaphore_test_project
    url        = "https://semaphoreci.com/api/v1/projects/#{project}/servers/#{server}?auth_token=#{auth_token}"
    server   = Git::Story::SemaphoreResponse.get(url, debug: @debug)
    deploys  = server.deploys
    upcoming = deploys.select(&:pending?)&.last
    passed = deploys.select(&:passed?)
    current  = passed.first
    if !passed.empty? && upcoming
      upcoming.estimated_duration = passed.sum { |d| d.duration.to_f } / passed.size
    end
    <<~end
      Server: #{server.server_name&.green}
      Branch: #{server.branch_name&.color('#ff5f00')}
      Semaphore: #{server.server_url}
      Strategy: #{server.strategy}
      Upcoming:
      #{upcoming}
      Current:
      #{current}
    end
  end
rescue => e
  "Getting #{url.inspect} => #{e.class}: #{e}".red
end

#deploy_stories(ref = default_ref, rest: []) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/git/story/app.rb', line 258

def deploy_stories(ref = default_ref, rest: [])
  ref = build_ref_range(ref)
  fetch_commits
  fetch_tags
  opts = ([
    '--color=never',
    '--pretty=%B'
  ] | rest) * ' '
  output = capture("git log #{opts} #{ref}")
  pivotal_ids = SortedSet[]
  output.scan(/\[\s*#\s*(\d+)\s*\]/) { pivotal_ids << $1.to_i }
  fetch_statuses(pivotal_ids) * (?┄ * Tins::Terminal.cols << ?\n)
end

#deploy_tagsObject



225
226
227
# File 'lib/git/story/app.rb', line 225

def deploy_tags
  tags.map { |t| tag_name(t) }
end

#deploy_tags_lastObject



236
237
238
# File 'lib/git/story/app.rb', line 236

def deploy_tags_last
  tag_name(tags.last)
end

#deploysObject Also known as: deploy_list



230
231
232
# File 'lib/git/story/app.rb', line 230

def deploys
  tags.map { |t| format_tag(t) }
end

#details(author = nil, mark_red: current(check: false)) ⇒ Object Also known as: list_details



215
216
217
218
219
220
221
# File 'lib/git/story/app.rb', line 215

def details(author = nil, mark_red: current(check: false))
  stories.sort_by { |b| -b.story_created_at.to_f }.map { |b|
    next if author && !b..include?(author)
    name = (bn = b.story_base_name) == mark_red ? bn.red : bn.green
    "#{name} #{b.} #{b.story_created_at.iso8601.yellow}"
  }.compact
end

#fetch_statuses(pivotal_ids) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/git/story/app.rb', line 272

def fetch_statuses(pivotal_ids)
  tg = ThreadGroup.new
  pivotal_ids.each do |pid|
    tg.add Thread.new { Thread.current[:status] = status(pid) }
  end
  tg.list.with_infobar(label: 'Story').map do |t|
    +infobar
    t.join
    t[:status]
  end
end

#github(branch = current(check: false)) ⇒ Object



344
345
346
347
348
349
# File 'lib/git/story/app.rb', line 344

def github(branch = current(check: false))
  if url = github_url(branch)
    system "open #{url.inspect}"
  end
  nil
end

#helpObject



59
60
61
62
63
64
65
66
67
# File 'lib/git/story/app.rb', line 59

def help
  result = [ 'Available commands are:' ]
  longest = command_annotations.keys.map(&:size).max
  result.concat(
    command_annotations.map { |name, a|
      "#{name.to_s.ljust(longest)} #{a[:doc]}"
    }
  )
end

#list(author = nil, mark_red: current(check: false)) ⇒ Object



207
208
209
210
211
212
# File 'lib/git/story/app.rb', line 207

def list(author = nil, mark_red: current(check: false))
  stories.map { |b|
    next if author && !b..include?(author)
    (bn = b.story_base_name) == mark_red ? bn.red : bn.green
  }.compact
end

#pivotal(branch = current(check: true)) ⇒ Object



352
353
354
355
356
357
358
# File 'lib/git/story/app.rb', line 352

def pivotal(branch = current(check: true))
  if story_id = branch&.[](/_(\d+)\z/, 1)&.to_i
    story_url = fetch_story(story_id)&.url
    system "open #{story_url}"
  end
  nil
end

#provide_name(story_id = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/git/story/app.rb', line 82

def provide_name(story_id = nil)
  until story_id.present?
    story_id = ask(prompt: 'Story id? ').strip
  end
  story_id = story_id.gsub(/[^0-9]+/, '')
  @story_id = Integer(story_id)
  if stories.any? { |s| s.story_id == @story_id }
    @reason = "story for ##@story_id already created"
    return
  end
  if name = fetch_story_name(@story_id)
    name = normalize_name(
      name,
      max_size: 128 - 'story'.size - @story_id.to_s.size - 2 * ?_.size
    ).full? || name
    [ 'story', name, @story_id ] * ?_
  else
    @reason = "name for ##@story_id could not be fetched from tracker"
    return
  end
end

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/git/story/app.rb', line 41

def run
  Git::Story::Setup.perform
  if command_of(@command)
    if method(@command).parameters.include?(%i[key rest])
      puts __send__(@command, *@argv, rest: @rest_argv)
    else
      puts __send__(@command, *@argv)
    end
  else
    @command and @command = @command.inspect
    @command ||= 'n/a'
    STDERR.puts "Unknown command #{@command}\n\n#{help.join(?\n)}"
    exit 1
  end
rescue Errno::EPIPE
end

#status(story_id = current(check: true)&.[](/_(\d+)\z/, 1)&.to_i) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/git/story/app.rb', line 161

def status(story_id = current(check: true)&.[](/_(\d+)\z/, 1)&.to_i)
  if story = fetch_story(story_id)
    color_state =
      case cs = story.current_state
      when 'unscheduled', 'planned', 'unstarted'
        cs
      when 'rejected'
        cs.white.on_red
      when 'accepted'
        cs.black.on_green
      else
        cs.black.on_yellow
      end.italic
    color_type =
      case t = story.story_type
      when 'bug'
        t.red.bold
      when 'feature'
        t.yellow.bold
      when 'chore'
        t.white.bold
      else
        t
      end
    owners = Array(fetch_story_owners(story_id)).map { |o| "#{o.name} <#{o.email}>" }
    result = <<~end
      Id: #{(?# + story.id.to_s).green}
      Name: #{story.name.inspect.bold}
      Type: #{color_type}
      Estimate: #{story.estimate.to_s.full? { |e| e.yellow.bold } || 'n/a'}
      State: #{color_state}
      Branch: #{current_branch_checked?&.color('#ff5f00')}
      Labels: #{story.labels.map { |l| l.name.on_color(91) }.join(' ')}
      Owners: #{owners.join(', ').yellow}
      Pivotal: #{story.url.color(33)}
    end
    if url = github_url(current_branch_checked?)
      result << "Github: #{url.color(33)}\n"
    end
    result
  end
rescue => e
  "Getting pivotal story status => #{e.class}: #{e}\n#{e.backtrace.join(?n)}".red
end

#switch(pattern = nil) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/git/story/app.rb', line 315

def switch(pattern = nil)
  fetch_commits
  ss = stories.map(&:story_base_name)
  if pattern.present?
    b = apply_pattern(pattern, ss)
    if b.size == 1
      b = b.first
    else
      b = nil
    end
  end
  loop do
    unless b
      b = complete prompt: 'Story <TAB>? '.bright_blue do |pattern|
        apply_pattern(pattern, ss)
      end&.strip
      b.empty? and return
    end
    if branch = ss.find { |f| f == b }
      sh "git checkout #{branch}"
      return "Switched to story: #{branch}".green
    else
      b = nil
    end
  end
rescue Interrupt
end

#test_status(branch = current(check: false)) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/git/story/app.rb', line 105

def test_status(branch = current(check: false))
  url = nil
  watch do
    auth_token = complex_config.story.semaphore_auth_token
    project    = complex_config.story.semaphore_test_project
    url        = "https://semaphoreci.com/api/v1/projects/#{project}/#{branch}/status?auth_token=#{auth_token}"
    Git::Story::SemaphoreResponse.get(url, debug: @debug)
  end
rescue => e
  "Getting #{url.inspect} => #{e.class}: #{e}".red
end