fun_ci 2.3.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +143 -0
  3. data/README.md +20 -10
  4. data/lib/fun_ci/agent/events.rb +15 -7
  5. data/lib/fun_ci/agent/job_events.rb +12 -5
  6. data/lib/fun_ci/agent/onward.rb +21 -0
  7. data/lib/fun_ci/agent/output.rb +7 -0
  8. data/lib/fun_ci/agent/reports.rb +3 -0
  9. data/lib/fun_ci/agent/snapshots.rb +25 -7
  10. data/lib/fun_ci/agent/status_text.rb +9 -5
  11. data/lib/fun_ci/agent/wait_command.rb +37 -5
  12. data/lib/fun_ci/cli.rb +2 -0
  13. data/lib/fun_ci/cli_command_help.rb +51 -0
  14. data/lib/fun_ci/cli_help.rb +9 -9
  15. data/lib/fun_ci/console/board_data.rb +50 -10
  16. data/lib/fun_ci/console/branch_page.rb +9 -0
  17. data/lib/fun_ci/console/console_state.rb +22 -8
  18. data/lib/fun_ci/console/cursor.rb +22 -0
  19. data/lib/fun_ci/console/cursor_rows.rb +37 -0
  20. data/lib/fun_ci/console/job_message.rb +5 -4
  21. data/lib/fun_ci/console/job_order.rb +5 -3
  22. data/lib/fun_ci/console/job_row.rb +17 -0
  23. data/lib/fun_ci/console/job_rows.rb +14 -3
  24. data/lib/fun_ci/console/key_handler.rb +88 -51
  25. data/lib/fun_ci/console/question.rb +63 -0
  26. data/lib/fun_ci/console/run_message.rb +3 -3
  27. data/lib/fun_ci/console/trunk_marks.rb +3 -1
  28. data/lib/fun_ci/console/view.rb +20 -6
  29. data/lib/fun_ci/evidence/about_stage.rb +1 -1
  30. data/lib/fun_ci/evidence/collector.rb +6 -3
  31. data/lib/fun_ci/evidence/outcome.rb +6 -3
  32. data/lib/fun_ci/jobs/job_run.rb +10 -9
  33. data/lib/fun_ci/jobs/locks.rb +1 -1
  34. data/lib/fun_ci/persistence/active_jobs.rb +2 -11
  35. data/lib/fun_ci/persistence/active_runs.rb +48 -16
  36. data/lib/fun_ci/persistence/arrival.rb +18 -0
  37. data/lib/fun_ci/persistence/branch_visibility.rb +40 -0
  38. data/lib/fun_ci/persistence/database.rb +28 -2
  39. data/lib/fun_ci/persistence/died.rb +21 -0
  40. data/lib/fun_ci/persistence/hidden_row_tables.rb +24 -0
  41. data/lib/fun_ci/persistence/job_recorder.rb +1 -0
  42. data/lib/fun_ci/persistence/job_runs.rb +17 -7
  43. data/lib/fun_ci/persistence/job_visibility.rb +43 -0
  44. data/lib/fun_ci/persistence/null_recorder.rb +4 -0
  45. data/lib/fun_ci/persistence/pipeline_recorder.rb +23 -3
  46. data/lib/fun_ci/persistence/pipeline_run.rb +25 -11
  47. data/lib/fun_ci/persistence/stage_job.rb +25 -0
  48. data/lib/fun_ci/persistence/trunk_checks.rb +7 -6
  49. data/lib/fun_ci/pipeline/group_kill.rb +38 -0
  50. data/lib/fun_ci/pipeline/pipeline_forker.rb +3 -1
  51. data/lib/fun_ci/pipeline/process_runner.rb +14 -8
  52. data/lib/fun_ci/pipeline/process_tree.rb +37 -0
  53. data/lib/fun_ci/pipeline/progress_reporter.rb +2 -1
  54. data/lib/fun_ci/pipeline/run_canceller.rb +16 -6
  55. data/lib/fun_ci/pipeline/slot.rb +12 -0
  56. data/lib/fun_ci/pipeline/slot_run.rb +20 -5
  57. data/lib/fun_ci/pipeline/slow_turns.rb +74 -0
  58. data/lib/fun_ci/pipeline/stage_end.rb +2 -1
  59. data/lib/fun_ci/pipeline/stale_pipeline_canceller.rb +1 -1
  60. data/lib/fun_ci/pipeline/trigger.rb +9 -1
  61. data/lib/fun_ci/pipeline/trigger_params.rb +7 -3
  62. data/lib/fun_ci/pipeline/turn_queue.rb +65 -0
  63. data/lib/fun_ci/pipeline/waiting_slots.rb +30 -0
  64. data/lib/fun_ci/pipeline/workspaces.rb +2 -1
  65. data/lib/fun_ci/pipeline/worktree_pool.rb +27 -7
  66. data/lib/fun_ci/pipeline/worktree_prune.rb +9 -4
  67. data/lib/fun_ci/pipeline/worktrees.rb +41 -7
  68. data/lib/fun_ci/setup/commands.rb +16 -7
  69. data/lib/fun_ci/setup/eslint_detection.rb +38 -0
  70. data/lib/fun_ci/setup/init_options.rb +34 -0
  71. data/lib/fun_ci/setup/installer.rb +41 -7
  72. data/lib/fun_ci/setup/placeholders.rb +30 -0
  73. data/lib/fun_ci/setup/project_config.rb +1 -0
  74. data/lib/fun_ci/setup/settings.rb +21 -1
  75. data/lib/fun_ci/setup/setup_checker.rb +5 -3
  76. data/lib/fun_ci/setup/stage_templates/javascript.rb +14 -4
  77. data/lib/fun_ci/setup/stage_templates/placeholders.rb +21 -0
  78. data/lib/fun_ci/setup/stage_templates.rb +6 -4
  79. data/lib/fun_ci/setup/template_writer.rb +3 -5
  80. data/lib/fun_ci/trunk/check.rb +7 -2
  81. data/lib/fun_ci/version.rb +1 -1
  82. metadata +34 -11
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cli_help"
4
+
5
+ module FunCi
6
+ # What `fun-ci COMMAND --help` prints in place of running the command: its
7
+ # usage, what it is for and the options it takes, the last two read from
8
+ # `fun-ci --help`, whose options name in brackets the commands taking them.
9
+ module CliCommandHelp
10
+ FLAGS = %w[-h --help].freeze
11
+ USAGES = {
12
+ "trigger" => "fun-ci trigger [--background] <commit-hash> <branch>",
13
+ "console" => "fun-ci console",
14
+ "init" => "fun-ci init [--everything] [--stack NAME]",
15
+ "install-hooks" => "fun-ci install-hooks [post-commit | pre-push]",
16
+ "check" => "fun-ci check",
17
+ "prune" => "fun-ci prune",
18
+ "status" => "fun-ci status [REV] [--need LEVEL] [--trunk] [--json]",
19
+ "wait" => "fun-ci wait [REV] [--need LEVEL] [--within TIME] [--follow-branch] [--trunk] [--json]",
20
+ "runs" => "fun-ci runs [-n N] [--branch NAME] [--json]",
21
+ "events" => "fun-ci events [--follow] [--only failures]",
22
+ "why" => "fun-ci why [REV] [STAGE | trunk] [--need LEVEL] [--raw] [--json]\n " \
23
+ "fun-ci why --job NAME [--raw] [--json]",
24
+ "jobs" => "fun-ci jobs [--json]",
25
+ "cancel" => "fun-ci cancel --job NAME",
26
+ "extract" => "fun-ci extract STAGE --output FILE [--exit N | --timed-out] [--json]"
27
+ }.freeze
28
+
29
+ def self.asked?(args) = args.any? { |arg| FLAGS.include?(arg) }
30
+
31
+ def self.help(out, command)
32
+ out.puts "Usage: #{USAGES.fetch(command)}", "", purpose(command)
33
+ options = options_of(command)
34
+ out.puts "", "Options:", *options unless options.empty?
35
+ 0
36
+ end
37
+
38
+ def self.purpose(command) = CliHelp::TEXT[/^ #{Regexp.escape(command)} +(.+)$/, 1]
39
+
40
+ def self.options_of(command)
41
+ lines = CliHelp::TEXT.lines.select { |line| takes?(line, command) }
42
+ lines.map { |line| line.sub(/\([^)]+\) /, "") }
43
+ end
44
+
45
+ def self.takes?(option_line, command)
46
+ commands = option_line[/^\s+-\S.*?\(([^)]+)\)/, 1]
47
+ !commands.nil? && commands.split(", ").include?(command)
48
+ end
49
+ private_class_method :purpose, :options_of, :takes?
50
+ end
51
+ end
@@ -36,13 +36,13 @@ module FunCi
36
36
  extract Try a stage's evidence extractors on a saved output
37
37
 
38
38
  Options:
39
- -h, --help Show this help message
39
+ -h, --help Show this help message; after a command, that command's usage
40
40
  --version Show version
41
41
 
42
- Trigger options:
43
- --background Run the pipeline in the background and return at once
44
-
45
- Agent options:
42
+ Command options:
43
+ --background (trigger) Run the pipeline in the background and return at once
44
+ --everything (init) Run init + install-hooks + check in one step
45
+ --stack NAME (init) Write the scripts for this stack, such as go, instead of the one detected
46
46
  --need LEVEL (status, wait, why) What must pass: build, fast (default) or all
47
47
  --within TIME (wait) Give up undecided after 30, 30s or 5m
48
48
  --follow-branch (wait) Move on to the newer commit that superseded the run
@@ -53,13 +53,13 @@ module FunCi
53
53
  --branch NAME (runs) Only runs on this branch
54
54
  --follow (events) Keep printing events as they happen, until stopped
55
55
  --only failures (events) Print only failed stages, superseded runs and failed jobs
56
- --json Print JSON instead of text (events always print JSON lines)
56
+ --output FILE (extract) The saved output of the stage to read
57
+ --exit N (extract) The exit status the stage ended with (default 1)
58
+ --timed-out (extract) The stage ran out of time instead of exiting
59
+ --json (status, wait, runs, why, jobs, extract) Print JSON instead of text; events always print JSON lines
57
60
  Exit codes: 0 passed, 1 failed, 2 over budget, 3 undecided,
58
61
  4 superseded, 5 no run, 6 conflicts with the trunk (--trunk), 64 usage error
59
62
 
60
- Init options:
61
- --everything Run init + install-hooks + check in one step
62
-
63
63
  Examples:
64
64
  fun-ci init --everything
65
65
  fun-ci trigger abc1234 main
@@ -9,9 +9,11 @@ require_relative "streak_counter"
9
9
  require_relative "trunk_marks"
10
10
  require_relative "cancelled_folding"
11
11
  require_relative "row_order"
12
+ require_relative "branch_page"
12
13
  require_relative "paging"
13
14
  require_relative "job_rows"
14
15
  require_relative "../persistence/active_jobs"
16
+ require_relative "../persistence/branch_visibility"
15
17
 
16
18
  module FunCi
17
19
  module Console
@@ -19,6 +21,9 @@ module FunCi
19
21
  # How many of a branch's runs are read to fold its cancelled ones and
20
22
  # find its standing against the trunk.
21
23
  BRANCH_WINDOW = 100
24
+ # How many branches the board holds at most: those whose newest run
25
+ # changed most recently, so old failures leave it in time.
26
+ RECENT_BRANCHES = 20
22
27
 
23
28
  # `clock` answers the time now, which decides which jobs are due.
24
29
  def initialize(db, page_size: 15, run_canceller: Pipeline::RunCanceller.new, clock: -> { Time.now })
@@ -33,17 +38,29 @@ module FunCi
33
38
  # Pages by `page_size` from now on, loading at least one such page.
34
39
  def resize(page_size) = @paging.resize(page_size)
35
40
 
36
- # Whether there are branches beyond those `runs` loads.
37
- def more? = Persistence::PipelineRun.branch_heads(@db, limit: @paging.limit + 1).size > @paging.limit
38
-
39
41
  # Records failed each slow suite whose process died (acceptance-tests.md, AT-8.3).
40
42
  def record_dead_slow_suites = @run_canceller.record_dead(@db)
41
43
 
42
- # The job section's rows (JobRows): the jobs of the projects among `runs`.
44
+ # The job section's rows (JobRows): the jobs of the projects among
45
+ # `runs`, those the user hid among them, marked.
43
46
  def jobs(runs)
44
47
  JobRows.new(@db).of(runs.map { |run| run[:project_path] }.uniq.compact, now: @clock.call)
45
48
  end
46
49
 
50
+ # Keeps the job of `row` (a JobRow) out of view from now: `snoozed`, until it runs again, or `ignored`.
51
+ def hide_job(row, kind)
52
+ Persistence::JobVisibility.new(@db).hide(row.project, row.name, kind, at: @clock.call)
53
+ end
54
+
55
+ def show_job(row) = Persistence::JobVisibility.new(@db).show(row.project, row.name)
56
+
57
+ # Keeps the branch of the run `row` out of view from now: `snoozed`, until its next run, or `ignored`.
58
+ def hide_run(row, kind)
59
+ Persistence::BranchVisibility.new(@db).hide(row[:project_path], row[:branch], kind, at: @clock.call)
60
+ end
61
+
62
+ def show_run(row) = Persistence::BranchVisibility.new(@db).show(row[:project_path], row[:branch])
63
+
47
64
  # Records failed each job whose process died (acceptance-tests.md, AT-13.6).
48
65
  def record_dead_jobs = @run_canceller.record_dead_jobs(@db)
49
66
 
@@ -53,9 +70,19 @@ module FunCi
53
70
 
54
71
  # One row per branch, its newest run, for the branches that most need you
55
72
  # and then those run most recently, in RowOrder: a run of cancelled
56
- # runs folded into one, with its stages and the branch's standing against the trunk.
57
- def runs
58
- RowOrder.of(Persistence::PipelineRun.branch_heads(@db, limit: @paging.limit).map { |head| branch_row(head) })
73
+ # runs folded into one, with its stages, the branch's standing against
74
+ # the trunk, and how the user hid the branch (`hidden`, nil while in view).
75
+ # A hidden branch leaves its place to the next one: as many more are read, and the page keeps as many in view.
76
+ def runs = branch_page.runs
77
+
78
+ # The rows `runs` says, and whether there are branches in view beyond
79
+ # them, from one read: the hidden branches among those read, and a page
80
+ # of the others, the first.
81
+ def branch_page
82
+ hidden = Persistence::BranchVisibility.new(@db).hidden
83
+ in_view, out_of_view = heads_read(hidden)
84
+ rows = (out_of_view + in_view.first(@paging.limit)).map { |head| branch_row(head, hidden) }
85
+ BranchPage.new(runs: RowOrder.of(rows), more: in_view.size > @paging.limit)
59
86
  end
60
87
 
61
88
  # The projects among `runs` whose trunk is stale.
@@ -74,10 +101,23 @@ module FunCi
74
101
 
75
102
  private
76
103
 
77
- def branch_row(head)
78
- branch_runs = Persistence::PipelineRun.of_branch(@db, head[:project_path], head[:branch], limit: BRANCH_WINDOW)
104
+ def heads(limit, more_among: 0)
105
+ Persistence::PipelineRun.branch_heads(@db, limit: limit, among: RECENT_BRANCHES + more_among)
106
+ end
107
+
108
+ # The heads in view and those hidden, of a page and one more in view.
109
+ def heads_read(hidden)
110
+ read = heads(@paging.limit + hidden.size + 1, more_among: hidden.size)
111
+ read.partition { |head| !hidden.key?(branch_of(head)) }
112
+ end
113
+
114
+ def branch_of(head) = head.values_at(:project_path, :branch)
115
+
116
+ def branch_row(head, hidden)
117
+ branch_runs = Persistence::PipelineRun.leading_to(@db, head, limit: BRANCH_WINDOW)
79
118
  row = CancelledFolding.fold(branch_runs).first
80
- enrich_with_stages(row.merge(trunk: TrunkMarks.new(@db).mark(branch_runs).first[:trunk]))
119
+ trunk = TrunkMarks.new(@db).mark(branch_runs).first[:trunk]
120
+ enrich_with_stages(row.merge(trunk: trunk, hidden: hidden[branch_of(head)]))
81
121
  end
82
122
 
83
123
  def enrich_with_stages(run)
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FunCi
4
+ module Console
5
+ # A page of the board's branch rows (BoardData#runs), and whether there
6
+ # are more branches in view beyond it.
7
+ BranchPage = Data.define(:runs, :more)
8
+ end
9
+ end
@@ -7,7 +7,7 @@ require_relative "job_message"
7
7
  module FunCi
8
8
  module Console
9
9
  # What the console shows: the page of runs BoardData reads that the View
10
- # puts on screen, the daily and weekly jobs below it, and the clock's
10
+ # puts on screen, the daily and weekly jobs in view below it, and the clock's
11
11
  # time, as a protocol `board` message, after the `event`s for what changed
12
12
  # since the last one.
13
13
  class ConsoleState
@@ -26,23 +26,37 @@ module FunCi
26
26
  # Pages for a terminal of `rows`.
27
27
  def resize(rows) = @board_data.resize(@view.resize(rows))
28
28
 
29
+ # The events of the branches in view, hidden or not shown, so `a` plays no scene; then the board.
29
30
  def updates
30
31
  @board_data.record_dead_slow_suites
31
32
  @board_data.record_dead_jobs
32
- runs = @board_data.runs
33
- [*@events.since_last(runs), board(runs, @board_data.jobs(runs))]
33
+ branches = @board_data.branch_page
34
+ runs = branches.runs
35
+ [*@events.since_last(runs.reject { |run| run[:hidden] }), board(branches, @board_data.jobs(runs))]
34
36
  end
35
37
 
36
38
  private
37
39
 
38
- def board(runs, jobs)
39
- page = @view.page(runs, more: @board_data.more?, jobs: jobs)
40
+ def board(branches, job_rows)
41
+ runs = branches.runs
42
+ shown = @view.shown(job_rows)
43
+ page = @view.page(@view.shown_runs(runs), more: branches.more, jobs: shown)
40
44
  { t: "board", now: @clock.call.to_i, streak: @board_data.streak, **page,
41
- runs: page[:runs].map { |run| RunMessage.from(run) }, **job_section(jobs), **stale_trunks(runs) }
45
+ runs: page[:runs].map { |run| RunMessage.from(run) }, **job_section(shown),
46
+ **hidden_rows(runs, job_rows), **stale_trunks(runs) }
42
47
  end
43
48
 
44
- # Only when there are jobs, as the protocol lets a board leave them out.
45
- def job_section(jobs) = jobs.empty? ? {} : { jobs: jobs.map { |job| JobMessage.from(job) } }
49
+ # Only when there are jobs in view, as the protocol lets a board leave them out.
50
+ def job_section(rows)
51
+ rows.empty? ? {} : { jobs: rows.map { |row| JobMessage.from(row.standing, hidden: row.hidden) } }
52
+ end
53
+
54
+ # How many branches and jobs the user hid, in view or not; only when some
55
+ # are, as the protocol lets a board leave it out.
56
+ def hidden_rows(runs, job_rows)
57
+ count = runs.count { |run| run[:hidden] } + job_rows.count(&:hidden)
58
+ count.zero? ? {} : { hidden_rows: count }
59
+ end
46
60
 
47
61
  # Only when a trunk is stale, as the protocol lets a board leave it out.
48
62
  def stale_trunks(runs)
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FunCi
4
+ module Console
5
+ # Where the cursor is among CursorRows: its row's index, and which row
6
+ # that is (CursorRows#key_at), so it stays on that row when the rows move
7
+ # under it.
8
+ Cursor = Data.define(:index, :on)
9
+
10
+ class Cursor
11
+ # The cursor on the row at `index`, or on the last row when `index` is
12
+ # past it; none when there is no row.
13
+ def self.at(rows, index)
14
+ within = rows.within(index)
15
+ within && new(within, rows.key_at(within))
16
+ end
17
+
18
+ # The cursor on its row where `rows` hold it, or at its index when that row left them.
19
+ def within(rows) = Cursor.at(rows, rows.index_of(on) || index)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FunCi
4
+ module Console
5
+ # The rows the cursor moves through: the runs loaded, then the job rows in view.
6
+ CursorRows = Data.define(:runs, :jobs)
7
+
8
+ class CursorRows
9
+ # The last row's index; negative when there is no row.
10
+ def last = runs.length + jobs.length - 1
11
+
12
+ # [:run, the run] or [:job, the job's row] at `index`; none without one.
13
+ def at(index)
14
+ return [] unless index
15
+ return [:run, runs[index]] if index < runs.length
16
+
17
+ [:job, jobs[index - runs.length]]
18
+ end
19
+
20
+ # `index`, or the last row's when it is past it; nil when there is no row.
21
+ def within(index) = index && !last.negative? ? [index, last].min : nil
22
+
23
+ # Which row `row` is, of `kind` (:run or :job): a branch of a project, or a job of one.
24
+ def self.key(kind, row) = kind == :run ? [:run, row[:project_path], row[:branch]] : [:job, row.project, row.name]
25
+
26
+ # Which row is at `index`.
27
+ def key_at(index) = keys[index]
28
+
29
+ # The index of the row `key` names; nil when it is none of these.
30
+ def index_of(key) = keys.index(key)
31
+
32
+ private
33
+
34
+ def keys = runs.map { |run| CursorRows.key(:run, run) } + jobs.map { |job| CursorRows.key(:job, job) }
35
+ end
36
+ end
37
+ end
@@ -5,17 +5,18 @@ require "time"
5
5
  module FunCi
6
6
  module Console
7
7
  # Where a job stands (Jobs::Standing) as the protocol's `board` carries it
8
- # under `jobs`: protocol status names and epoch seconds. Formatting them
9
- # is the renderer's job.
8
+ # under `jobs`: protocol status names and epoch seconds, and how the user
9
+ # hid it, for a hidden job the board shows. Formatting them is the
10
+ # renderer's job.
10
11
  module JobMessage
11
12
  # The protocol's word for a Jobs::State, where it has one of its own:
12
13
  # a cancelled job is due again, and one it can't read is shown due.
13
14
  STATUS = { "over_budget" => "timeout", "cancelled" => "due", "unknown" => "due" }.freeze
14
15
 
15
- def self.from(standing)
16
+ def self.from(standing, hidden: nil)
16
17
  { project: standing.project, name: standing.name, cadence: standing.cadence,
17
18
  status: STATUS.fetch(standing.state, standing.state), **run(standing.run, standing.starts_at),
18
- due_at: standing.due_at&.to_i }.compact
19
+ due_at: standing.due_at&.to_i, hidden: hidden }.compact
19
20
  end
20
21
 
21
22
  # What the latest run says: which, on what, and when; a run waiting its
@@ -5,8 +5,9 @@ module FunCi
5
5
  # The order of the job section's rows (design.md, The console): what
6
6
  # needs you first, then running, waiting its turn to (Jobs::Schedule),
7
7
  # due and passed; among equals, the
8
- # board's projects in its order, then each project's jobs by name. Rows
9
- # answer #state (Jobs::State), #project and #name.
8
+ # board's projects in its order, then each project's jobs by name. A
9
+ # hidden job, shown because the user asked, comes after every job in
10
+ # view. Rows answer #state (Jobs::State), #project, #name and #hidden.
10
11
  module JobOrder
11
12
  URGENCY = { "failed" => 0, "lost" => 0, "over_budget" => 1, "running" => 2, "scheduled" => 3, "due" => 4,
12
13
  "cancelled" => 4, "passed" => 5 }.freeze
@@ -15,7 +16,8 @@ module FunCi
15
16
 
16
17
  def self.of(rows, projects:)
17
18
  rows.sort_by do |row|
18
- [URGENCY.fetch(row.state, UNKNOWN), projects.index(row.project) || projects.size, row.name]
19
+ [row.hidden ? 1 : 0, URGENCY.fetch(row.state, UNKNOWN), projects.index(row.project) || projects.size,
20
+ row.name]
19
21
  end
20
22
  end
21
23
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FunCi
4
+ module Console
5
+ # A job as the job section holds it: where it stands (Jobs::Standing),
6
+ # and how the user hid it, `snoozed` or `ignored` (Persistence::JobVisibility);
7
+ # nil while it is in view.
8
+ JobRow = Data.define(:standing, :hidden)
9
+
10
+ class JobRow
11
+ def name = standing.name
12
+ def project = standing.project
13
+ def state = standing.state
14
+ def run = standing.run
15
+ end
16
+ end
17
+ end
@@ -1,20 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "job_order"
4
+ require_relative "job_row"
4
5
  require_relative "../jobs/standings"
6
+ require_relative "../persistence/job_visibility"
5
7
 
6
8
  module FunCi
7
9
  module Console
8
10
  # The job section's rows (design.md, The console): where each daily and
9
- # weekly job of the board's projects stands (Jobs::Standing), in JobOrder.
11
+ # weekly job of the board's projects stands (Jobs::Standing) and how the
12
+ # user hid it, if they did (Persistence::JobVisibility), in JobOrder.
10
13
  class JobRows
11
14
  def initialize(db)
12
15
  @db = db
13
16
  end
14
17
 
15
18
  def of(projects, now:)
16
- standings = projects.flat_map { |project| Jobs::Standings.new(@db, project, now: now).all }
17
- JobOrder.of(standings, projects: projects)
19
+ JobOrder.of(projects.flat_map { |project| rows_of(project, now) }, projects: projects)
20
+ end
21
+
22
+ private
23
+
24
+ def rows_of(project, now)
25
+ hidden = Persistence::JobVisibility.new(@db).hidden(project)
26
+ Jobs::Standings.new(@db, project, now: now).all.map do |standing|
27
+ JobRow.new(standing: standing, hidden: hidden[standing.name])
28
+ end
18
29
  end
19
30
  end
20
31
  end
@@ -1,94 +1,131 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "cursor"
4
+ require_relative "cursor_rows"
5
+ require_relative "question"
6
+
3
7
  module FunCi
4
8
  module Console
5
9
  # What a key means for the runs BoardData loads, and the daily and weekly
6
- # jobs after them: moving the cursor through both, quitting, and
7
- # cancelling the run or job under the cursor (a running one after the
8
- # user confirms with `y`).
10
+ # jobs after them: moving the cursor through both, quitting, cancelling
11
+ # the run or job under the cursor (a running one after the user confirms
12
+ # with `y`), hiding the branch or job under it (`h`, then `s` to snooze it
13
+ # or `i` to ignore it), and showing the hidden ones (`a`), where `h` brings one back.
14
+ # The cursor stays on its row as the rows move, and a question ends when
15
+ # the row it asks about is no longer under the cursor as it was.
9
16
  class KeyHandler
10
- CONFIRMATION_ANSWERS = ["y", "n", :escape].freeze
11
-
12
- attr_reader :cursor_index
13
-
14
17
  def initialize(board_data:)
15
18
  @board_data = board_data
16
- @confirm_cancel = nil
19
+ @question = nil
20
+ @revealed = false
17
21
  end
18
22
 
23
+ # The index of the row under the cursor among the runs and the job rows in view; nil without a cursor.
24
+ def cursor_index = @cursor&.index
25
+
19
26
  # :quit when the key ends the session.
20
27
  def handle_key(key)
21
- return confirm(key) if @confirm_cancel
22
28
  return :quit if key == "q"
23
29
 
24
- act(key)
30
+ rows = self.rows
31
+ follow(rows.runs, rows.jobs)
32
+ return answer(key) if @question
33
+
34
+ act(key, rows)
25
35
  end
26
36
 
27
- def confirming?
28
- !@confirm_cancel.nil?
37
+ # Puts the cursor back on its row among `runs` and the job rows in view,
38
+ # `jobs`; on the first of them, `from_the_first`, while it is on none.
39
+ def follow(runs, jobs, from_the_first: false)
40
+ rows = CursorRows.new(runs, jobs)
41
+ @cursor = @cursor ? @cursor.within(rows) : (Cursor.at(rows, 0) if from_the_first)
42
+ @question = nil unless @question&.about?(rows.at(cursor_index))
29
43
  end
30
44
 
45
+ # What the footer asks, :cancel or :hide; nil while it asks nothing.
46
+ def asking = @question&.asks
47
+
48
+ def confirming? = asking == :cancel
49
+
50
+ # The job rows in view: not the hidden ones, unless `a` asked for them.
51
+ def shown(job_rows) = @revealed ? job_rows : job_rows.reject(&:hidden)
52
+
53
+ # The branches' runs in view: not those of the hidden branches, unless `a` asked for them.
54
+ def shown_runs(runs) = @revealed ? runs : runs.reject { |run| run[:hidden] }
55
+
31
56
  private
32
57
 
33
- def act(key)
58
+ def act(key, rows)
34
59
  case key
35
- when "j", :down then move_cursor_down
36
- when "k", :up then move_cursor_up
37
- when "c" then initiate_cancel
60
+ when "j", :down then move_cursor_down(rows)
61
+ when "k", :up then move_cursor_up(rows)
62
+ when "c" then initiate_cancel(rows)
63
+ when "h" then hide_or_show(rows)
64
+ when "a" then settle(revealed: !@revealed)
38
65
  end
39
66
  end
40
67
 
41
- # Onto the last run loaded, which loads the next page, then into the jobs.
42
- def move_cursor_down
43
- runs = @board_data.runs
44
- last = runs.length + @board_data.jobs(runs).length - 1
45
- return if last.negative?
46
- return @cursor_index = 0 if @cursor_index.nil?
47
- return unless @cursor_index < last
68
+ def answer(key)
69
+ @question.take(key)
70
+ return unless @question.ended_by?(key)
48
71
 
49
- @cursor_index += 1
50
- @board_data.load_more if @cursor_index == runs.length - 1
72
+ @question = nil
73
+ settle
51
74
  end
52
75
 
53
- def move_cursor_up
54
- @cursor_index -= 1 if @cursor_index&.positive?
76
+ # After job rows left the view or came into it: hidden jobs are shown
77
+ # only while some job is hidden, and the cursor stays on a row.
78
+ def settle(revealed: @revealed)
79
+ runs = @board_data.runs
80
+ jobs = @board_data.jobs(runs)
81
+ @revealed = revealed && (jobs.any?(&:hidden) || runs.any? { |run| run[:hidden] })
82
+ follow(shown_runs(runs), shown(jobs))
55
83
  end
56
84
 
57
- def initiate_cancel
58
- kind, row = under_cursor
59
- return unless row
60
-
61
- kind == :job ? offer_job_cancel(row) : offer_run_cancel(row)
85
+ def rows
86
+ runs = @board_data.runs
87
+ CursorRows.new(shown_runs(runs), shown(@board_data.jobs(runs)))
62
88
  end
63
89
 
64
- # A run waiting to start is cancelled at once; a running one once the user confirms.
65
- def offer_run_cancel(run)
66
- @board_data.cancel_run(run[:id]) if run[:status] == "scheduled"
67
- @confirm_cancel = [:run, run] if run[:status] == "running"
90
+ # Onto the last run loaded, which loads the next page, then into the jobs.
91
+ def move_cursor_down(rows)
92
+ return @cursor = Cursor.at(rows, 0) unless @cursor
93
+ return unless cursor_index < rows.last
94
+
95
+ @cursor = Cursor.at(rows, cursor_index + 1)
96
+ @board_data.load_more if cursor_index == rows.runs.length - 1
68
97
  end
69
98
 
70
- # A job (a Jobs::Standing) waiting its turn is cancelled at once; a
71
- # running one once the user confirms.
72
- def offer_job_cancel(job)
73
- cancel(:job, job) if job.state == "scheduled"
74
- @confirm_cancel = [:job, job] if job.state == "running"
99
+ def move_cursor_up(rows)
100
+ @cursor = Cursor.at(rows, cursor_index - 1) if cursor_index&.positive?
75
101
  end
76
102
 
77
- # [:run, the run] or [:job, the job row] under the cursor, or none.
78
- def under_cursor
79
- runs = @board_data.runs
80
- return [] unless @cursor_index
81
- return [:run, runs[@cursor_index]] if @cursor_index < runs.length
103
+ def initiate_cancel(rows)
104
+ kind, row = rows.at(cursor_index)
105
+ return unless row
106
+
107
+ kind == :job ? offer_cancel(kind, row, row.state) : offer_cancel(kind, row, row[:status])
108
+ end
82
109
 
83
- [:job, @board_data.jobs(runs)[@cursor_index - runs.length]]
110
+ # A run or job waiting to start is cancelled at once, nothing of it
111
+ # having run; a running one once the user confirms.
112
+ def offer_cancel(kind, row, status)
113
+ cancel = Question::Cancel.new(@board_data, kind, row)
114
+ cancel.carry_out if status == "scheduled"
115
+ @question = cancel if status == "running"
84
116
  end
85
117
 
86
- def confirm(key)
87
- cancel(*@confirm_cancel) if key == "y"
88
- @confirm_cancel = nil if CONFIRMATION_ANSWERS.include?(key)
118
+ # Asks how to hide the branch or job under the cursor; one already hidden comes back at once.
119
+ def hide_or_show(rows)
120
+ kind, row = rows.at(cursor_index)
121
+ return unless row
122
+ return @question = Question::Hide.new(@board_data, kind, row) unless hidden?(kind, row)
123
+
124
+ kind == :job ? @board_data.show_job(row) : @board_data.show_run(row)
125
+ settle
89
126
  end
90
127
 
91
- def cancel(kind, row) = kind == :job ? @board_data.cancel_job(row.run[:id]) : @board_data.cancel_run(row[:id])
128
+ def hidden?(kind, row) = kind == :job ? row.hidden : row[:hidden]
92
129
  end
93
130
  end
94
131
  end