Class: Git::Story::App

Inherits:
Object
  • Object
show all
Extended by:
Utils
Includes:
ComplexConfig::Provider::Shortcuts, Utils, SearchUI, 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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/git/story/app.rb', line 36

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
  @debug     = debug
  determine_command
  if !cc.story? && @command != :setup
    warn "git story configuration file " +
      "config/story.yml".bold + " is missing.\n\nCall " +
      "git story setup".bold + " to create an initial one, inspect and " +
      "edit it yourself, and also set the appropriate env vars.\n\n"
    @command, @argv = :help, []
  end
end

Instance Method Details

#create(story_id = nil) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/git/story/app.rb', line 276

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



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/git/story/app.rb', line 91

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

#delete(pattern = nil) ⇒ Object



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

def delete(pattern = nil)
  fetch_commits
  if branch = pick_branch(prompt: 'Delete story branch? %s', symbol: ?⌦)
    sh "git pull origin #{branch}"
    sh "git branch -d #{branch}"
    if ask(prompt: 'Delete remote branch? (y/N) ') =~ /\Ay\z/i
      sh "git push origin #{branch} --delete"
    end
    return "Deleted story branch: #{branch}".green
  end
end

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



260
261
262
263
264
265
# File 'lib/git/story/app.rb', line 260

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_document(ref = default_ref, rest: []) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/git/story/app.rb', line 218

def deploy_document(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 }
  stories   = ''
  attendees = Set[]
  fetch_stories(pivotal_ids) do |pid|
    story = fetch_story(pid, with_owner: true)
    if !story
      stories << "• ** Story with id #{pid} could not be found **"
      next
    end
    attendees.merge story.owners
    stories << <<~end
    • [##{story.id}] #{story.name}
      ○ Pivotal: https://www.pivotaltracker.com/story/show/#{pid}
      ○ Type: #{story.story_type}
      ○ Status: #{story.current_state}
      ○ Owners: #{story.owners.map { |o| "%s <%s>" % [ o.name, o.email ] }.join(', ')}
      ○ Tasks to be done before deployment:
        ☐ …
      ○ Tasks to be done after deployment:
       ☐ …
    end
  end
  attendees.map! { |a| "• @#{a.email}" }
  <<~end
    #{attendees.join(?\n)}

    #{stories}
  end
end

#deploy_lastObject



185
186
187
# File 'lib/git/story/app.rb', line 185

def deploy_last
  format_tag(tags.last)
end

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



190
191
192
193
194
195
196
197
198
199
# File 'lib/git/story/app.rb', line 190

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



268
269
270
271
272
273
# File 'lib/git/story/app.rb', line 268

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_stories(ref = default_ref, rest: []) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/git/story/app.rb', line 202

def deploy_stories(ref = default_ref, rest: [])
  ref = build_ref_range(ref)
  fetch_commits
  fetch_tags
  opts = ([
    '--color=never',
    '--pretty=%B',
    '--reverse',
  ] | 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



169
170
171
# File 'lib/git/story/app.rb', line 169

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

#deploy_tags_lastObject



180
181
182
# File 'lib/git/story/app.rb', line 180

def deploy_tags_last
  tag_name(tags.last)
end

#deploysObject Also known as: deploy_list



174
175
176
# File 'lib/git/story/app.rb', line 174

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

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



159
160
161
162
163
164
165
# File 'lib/git/story/app.rb', line 159

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



312
313
314
315
316
317
# File 'lib/git/story/app.rb', line 312

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

#helpObject



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

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

#hotfix(ref = nil) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/git/story/app.rb', line 335

def hotfix(ref = nil)
  if ref
    start_point = ref
  elsif tag = tags.last
    start_point = tag_name(tag)
  else
    fail 'no last deployment tag found'
  end
  branch = "hotfix_#{start_point}"
  sh "git checkout -b #{branch.inspect} #{start_point.inspect}"
  nil
end

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



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

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



320
321
322
323
324
325
326
# File 'lib/git/story/app.rb', line 320

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

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/git/story/app.rb', line 51

def run
  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
    if @command
      @command = @command.inspect
    else
      @command = 'n/a'
    end
    STDERR.puts "Unknown command #{@command}\n\n#{help.join(?\n)}"
    exit 1
  end
rescue Errno::EPIPE
end

#semaphoreObject



329
330
331
332
# File 'lib/git/story/app.rb', line 329

def semaphore
  sh "open #{complex_config.story.semaphore_project_url}"
  nil
end

#setupObject



82
83
84
85
86
87
88
# File 'lib/git/story/app.rb', line 82

def setup
  if cc.config?
    red("config/story.yml configration already exists!")
  else
    Git::Story::Setup.perform
  end
end

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



104
105
106
107
108
109
110
111
112
113
114
115
116
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
145
146
147
148
# File 'lib/git/story/app.rb', line 104

def status(story_id = current(check: true)&.[](/_(\d+)\z/, 1)&.to_i)
  if story = fetch_story(story_id, with_owner: true)
    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
    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}
      Labels: #{story.labels.map { |l| l.name.on_color(91) }.join(' ')}
      Owners: #{story.owners.map { |o| "%s <%s>" % [ o.name, o.email ] }.join(', ').yellow}
      Pivotal: #{story.url.color(33)}
    end
    if branch = current_branch_checked? and url = github_url(current_branch_checked?)
      result << <<~end
        Github: #{url.color(33)}
        Branch: #{branch.color('#ff5f00')}
      end
    end
    result
  end
rescue => e
  "Getting pivotal story status => #{e.class}: #{e}\n#{e.backtrace.join(?n)}".red
end

#switchObject



290
291
292
293
294
295
296
# File 'lib/git/story/app.rb', line 290

def switch
  fetch_commits
  if branch = pick_branch(prompt: 'Switch to story? %s')
    sh "git checkout #{branch}"
    return "Switched to story: #{branch}".green
  end
end