Class: Git::Story::App
- Inherits:
-
Object
- Object
- Git::Story::App
- 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
- #build_status(branch = current(check: false)) ⇒ Object
- #create(story_id = nil) ⇒ Object
- #current(check: true) ⇒ Object
- #deploy_diff(ref = default_ref, rest: []) ⇒ Object
- #deploy_last ⇒ Object
- #deploy_log(ref = default_ref, rest: []) ⇒ Object
- #deploy_migrate_diff(ref = default_ref, rest: []) ⇒ Object
- #deploy_status(server = complex_config.story.semaphore_default_server) ⇒ Object
- #deploy_stories(ref = default_ref, rest: []) ⇒ Object
- #deploy_tags ⇒ Object
- #deploy_tags_last ⇒ Object
- #deploys ⇒ Object (also: #deploy_list)
- #details(author = nil, mark_red: current(check: false)) ⇒ Object (also: #list_details)
- #fetch_statuses(pivotal_ids) ⇒ Object
- #github(branch = current(check: false)) ⇒ Object
- #help ⇒ Object
-
#initialize(argv = ARGV.dup, debug: ENV['DEBUG'].to_i == 1) ⇒ App
constructor
A new instance of App.
- #list(author = nil, mark_red: current(check: false)) ⇒ Object
- #pivotal(branch = current(check: true)) ⇒ Object
- #provide_name(story_id = nil) ⇒ Object
- #run ⇒ Object
- #status(story_id = current(check: true)&.[](/_(\d+)\z/, 1)&.to_i) ⇒ Object
- #switch(pattern = nil) ⇒ Object
- #test_status(branch = current(check: false)) ⇒ Object
Methods included from Utils
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 |
# 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 @command = @argv.shift&.to_sym @debug = debug end |
Instance Method Details
#build_status(branch = current(check: false)) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/git/story/app.rb', line 151 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
304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/git/story/app.rb', line 304 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
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/git/story/app.rb', line 73 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
288 289 290 291 292 293 |
# File 'lib/git/story/app.rb', line 288 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_last ⇒ Object
244 245 246 |
# File 'lib/git/story/app.rb', line 244 def deploy_last format_tag(.last) end |
#deploy_log(ref = default_ref, rest: []) ⇒ Object
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/git/story/app.rb', line 249 def deploy_log(ref = default_ref, rest: []) ref = build_ref_range(ref) fetch_commits 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
296 297 298 299 300 301 |
# File 'lib/git/story/app.rb', line 296 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
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 121 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
261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/git/story/app.rb', line 261 def deploy_stories(ref = default_ref, rest: []) ref = build_ref_range(ref) fetch_commits 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_tags ⇒ Object
228 229 230 |
# File 'lib/git/story/app.rb', line 228 def .map { |t| tag_name(t) } end |
#deploy_tags_last ⇒ Object
239 240 241 |
# File 'lib/git/story/app.rb', line 239 def tag_name(.last) end |
#deploys ⇒ Object Also known as: deploy_list
233 234 235 |
# File 'lib/git/story/app.rb', line 233 def deploys .map { |t| format_tag(t) } end |
#details(author = nil, mark_red: current(check: false)) ⇒ Object Also known as: list_details
218 219 220 221 222 223 224 |
# File 'lib/git/story/app.rb', line 218 def details( = nil, mark_red: current(check: false)) stories.sort_by { |b| -b.story_created_at.to_f }.map { |b| next if && !b..include?() 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
275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/git/story/app.rb', line 275 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.(label: 'Story').map do |t| + t.join t[:status] end end |
#github(branch = current(check: false)) ⇒ Object
349 350 351 352 353 354 |
# File 'lib/git/story/app.rb', line 349 def github(branch = current(check: false)) if url = github_url(branch) system "open #{url.inspect}" end nil end |
#help ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/git/story/app.rb', line 62 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
210 211 212 213 214 215 |
# File 'lib/git/story/app.rb', line 210 def list( = nil, mark_red: current(check: false)) stories.map { |b| next if && !b..include?() (bn = b.story_base_name) == mark_red ? bn.red : bn.green }.compact end |
#pivotal(branch = current(check: true)) ⇒ Object
357 358 359 360 361 362 363 |
# File 'lib/git/story/app.rb', line 357 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/git/story/app.rb', line 85 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 |
#run ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/git/story/app.rb', line 44 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
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 205 206 207 |
# File 'lib/git/story/app.rb', line 164 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
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/git/story/app.rb', line 318 def switch(pattern = nil) fetch_commits ss = stories.map(&:story_base_name) branch = Search.new( match: -> answer { answer = answer.strip.delete(?#).downcase matcher = Amatch::PairDistance.new(answer) matches = ss.map { |n| [ n, -matcher.similar(n.downcase) ] }. select { |_, s| s < 0 }.sort_by(&:last).map(&:first) matches.empty? and matches = ss matches.first(Tins::Terminal.lines - 1) }, query: -> _answer, matches, selector { matches.each_with_index. map { |m, i| i == selector ? '⏻ ' + Search.on_blue(m) : '┊ ' + m } * ?\n }, found: -> _answer, matches, selector { matches[selector] }, prompt: 'Story? %s'.bold, output: STDOUT ).start if branch sh "git checkout #{branch}" return "Switched to story: #{branch}".green end end |
#test_status(branch = current(check: false)) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/git/story/app.rb', line 108 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 |