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.



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

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



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

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



287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/git/story/app.rb', line 287

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



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

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



271
272
273
274
275
276
# File 'lib/git/story/app.rb', line 271

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



238
239
240
241
# File 'lib/git/story/app.rb', line 238

def deploy_last
  tag = deploy_tags_last
  format_tag(tag)
end

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



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

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



279
280
281
282
283
284
# File 'lib/git/story/app.rb', line 279

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



117
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
# File 'lib/git/story/app.rb', line 117

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



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

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 }
  pivotal_ids.map { |pid| status(pid) } * (?┄ * Tins::Terminal.cols << ?\n)
end

#deploy_tagsObject



222
223
224
# File 'lib/git/story/app.rb', line 222

def deploy_tags
  capture(tags).lines.map(&:chomp)
end

#deploy_tags_lastObject



233
234
235
# File 'lib/git/story/app.rb', line 233

def deploy_tags_last
  deploy_tags.last
end

#deploysObject Also known as: deploy_list



227
228
229
# File 'lib/git/story/app.rb', line 227

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

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



212
213
214
215
216
217
218
# File 'lib/git/story/app.rb', line 212

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

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



330
331
332
333
334
335
# File 'lib/git/story/app.rb', line 330

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

#helpObject



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

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



204
205
206
207
208
209
# File 'lib/git/story/app.rb', line 204

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



338
339
340
341
342
343
344
# File 'lib/git/story/app.rb', line 338

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



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

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



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

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



160
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
# File 'lib/git/story/app.rb', line 160

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.green
      else
        cs.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
    result = <<~end
      Id: #{(?# + story.id.to_s).green}
      Name: #{story.name.inspect.bold}
      Type: #{color_type}
      Estimate: #{story.estimate.to_s.yellow.bold}
      State: #{color_state}
      Branch: #{current_branch_checked?&.color('#ff5f00')}
      Labels: #{story.labels.map(&:name).join(' ').on_color(91)}
      Pivotal: #{story.url}
    end
    if url = github_url(current_branch_checked?)
      result << "Github: #{url}\n"
    end
    result
  end
rescue => e
  "Getting pivotal story status => #{e.class}: #{e}".red
end

#switch(pattern = nil) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/git/story/app.rb', line 301

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



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

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