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



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



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

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



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/git/story/app.rb', line 295

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



256
257
258
259
260
261
# File 'lib/git/story/app.rb', line 256

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
# 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)
    attendees.merge story.owners
    stories << "  \u2022 [#\#{story.id}] \#{story.name}\n    \u25CB Pivotal: https://www.pivotaltracker.com/story/show/\#{pid}\n    \u25CB Type: \#{story.story_type}\n    \u25CB Status: \#{story.current_state}\n    \u25CB Owners: \#{story.owners.map { |o| \"%s <%s>\" % [ o.name, o.email ] }.join(', ')}\n    \u25CB Tasks to be done before deployment:\n      \u2610 \u2026\n    \u25CB Tasks to be done after deployment:\n     \u2610 \u2026\n  end\nend\nattendees.map! { |a| \"\u2022 @\#{a.email}\" }\n<<~end\n  \#{attendees.join(?\\n)}\n\n  \#{stories}\nend\n"

#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



264
265
266
267
268
269
# File 'lib/git/story/app.rb', line 264

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.story_author} #{b.story_created_at.iso8601.yellow}"
  }.compact
end

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



308
309
310
311
312
313
# File 'lib/git/story/app.rb', line 308

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



331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/git/story/app.rb', line 331

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



316
317
318
319
320
321
322
# File 'lib/git/story/app.rb', line 316

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



325
326
327
328
# File 'lib/git/story/app.rb', line 325

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 = "      Id: \#{(?# + story.id.to_s).green}\n      Name: \#{story.name.inspect.bold}\n      Type: \#{color_type}\n      Estimate: \#{story.estimate.to_s.full? { |e| e.yellow.bold } || 'n/a'}\n      State: \#{color_state}\n      Labels: \#{story.labels.map { |l| l.name.on_color(91) }.join(' ')}\n      Owners: \#{story.owners.map { |o| \"%s <%s>\" % [ o.name, o.email ] }.join(', ').yellow}\n      Pivotal: \#{story.url.color(33)}\n    end\n    if branch = current_branch_checked? and url = github_url(current_branch_checked?)\n      result << <<~end\n        Github: \#{url.color(33)}\n        Branch: \#{branch.color('#ff5f00')}\n      end\n    end\n    result\n  end\nrescue => e\n  \"Getting pivotal story status => \#{e.class}: \#{e}\\n\#{e.backtrace.join(?n)}\".red\n"

#switchObject



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

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