discourse/lib/service/steps_inspector.rb
Loïc Guitaut a39785f3dd
DEV: Add only_if step to DRSF (#35247)
This patch introduces a new step to the Ruby Service Framework.

`only_if` will execute its block (other steps) only if the provided
condition evaluates to `true`. As for the other steps, the name provided
is the name of the method that will be executed.

```ruby
only_if(:can_update) do
  model :user
  step :update_user
  step :notify
end

private

def can_update(guardian:)
  …
end
```

This step cannot fail and the whole block will be skipped if the method
evaluates to a falsy value.

This is rendered by the steps inspector:

```
[ 1/13] [options] default 
[ 2/13] [model] model 
[ 3/13] [policy] policy 
[ 4/13] [params] default 
[ 5/13] [lock] parameter:other_param 
[ 6/13]   [transaction]
[ 7/13]     [step] in_transaction_step_1 
[ 8/13]     [step] in_transaction_step_2 
[ 9/13] [try]
[10/13]   [step] might_raise 
[11/13] [only_if] condition ⏭️ (condition was not met)
[12/13]   [step] optional_step
[13/13] [step] final_step 
```

The inspector also renders text with colors now.

<img width="419" height="336" alt="Copie d'écran_20251008_162920"
src="https://github.com/user-attachments/assets/add4fd16-076e-4f30-ae1a-3e933494967e"
/>
2025-10-08 17:20:22 +02:00

233 lines
5.4 KiB
Ruby

# frozen_string_literal: true
# = Service::StepsInspector
#
# This class takes a {Service::Base::Context} object and inspects it.
# It will output a list of steps and what is their known state.
class Service::StepsInspector
# @!visibility private
class Step
COLORS = { red: 31, green: 32, white: 37, gray: 90 }
attr_reader :step, :result, :nesting_level, :color
delegate :name, :result_key, to: :step
delegate :failure?,
:success?,
:error,
:raised_exception?,
:skipped?,
to: :step_result,
allow_nil: true
alias error? failure?
def self.for(step, result, nesting_level: 0, color: nil)
class_name =
"#{module_parent_name}::#{step.class.name.split("::").last.sub(/^(\w+)Step$/, "\\1")}"
class_name.constantize.new(step, result, nesting_level:, color:)
end
def initialize(step, result, nesting_level: 0, color: nil)
@step = step
@result = result
@nesting_level = nesting_level
@color = color
end
def type
self.class.name.split("::").last.underscore
end
alias inspect_type type
def emoji
"#{result_emoji}#{unexpected_result_emoji}"
end
def steps
[self]
end
def inspect
"#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}] #{name}#{runtime}\e[0m #{emoji}".rstrip
end
private
def runtime
return unless step_result&.__runtime__
" (#{(step_result.__runtime__ * 1000).round(4)} ms)"
end
def step_result
result[result_key]
end
def result_emoji
return "💥" if raised_exception?
return "⏭️" if skipped?
return "" if failure?
return "" if success?
""
end
def unexpected_result_emoji
" ⚠️#{unexpected_result_text}" if step_result.try(:[], "spec.unexpected_result")
end
def unexpected_result_text
return " <= expected to return true but got false instead" if error?
" <= expected to return false but got true instead"
end
def ansi_color
return color if color
return COLORS[:red] if failure?
return COLORS[:green] if success?
COLORS[:white]
end
end
# @!visibility private
class Model < Step
def error
return result[name].errors.inspect if step_result.invalid
step_result.exception&.full_message || "Model not found"
end
end
# @!visibility private
class Contract < Step
def error
"#{step_result.errors.inspect}\n\nProvided parameters: #{step_result.parameters.pretty_inspect}"
end
def inspect_type
"params"
end
end
# @!visibility private
class Policy < Step
def error
step_result.reason
end
end
# @!visibility private
class Transaction < Step
def steps
[
self,
*step.steps.map { Step.for(_1, result, nesting_level: nesting_level + 1, color:).steps },
]
end
def inspect
"#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}]#{runtime}\e[0m#{unexpected_result_emoji}"
end
end
# @!visibility private
class Options < Step
end
# @!visibility private
class Try < Transaction
def error?
step_result.exception
end
def error
step_result.exception.full_message
end
end
# @!visibility private
class Lock < Transaction
def inspect
"#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}] #{name}#{runtime}\e[0m #{emoji}".rstrip
end
def error
"Lock '#{name}' was not acquired."
end
end
# @!visibility private
class OnlyIf < Step
def steps
[
self,
*step.steps.map do
Step.for(_1, result, nesting_level: nesting_level + 1, color: skipped_color).steps
end,
]
end
def inspect
"#{" " * nesting_level}\e[#{ansi_color}m[#{inspect_type}] #{name}#{runtime}\e[0m #{emoji}#{explanation}".rstrip
end
private
def explanation
return unless skipped?
" (condition was not met)"
end
def ansi_color
return super unless skipped?
COLORS[:white]
end
def skipped_color
return unless skipped?
COLORS[:gray]
end
end
attr_reader :steps, :result
def initialize(result)
@steps = result.__steps__.map { Step.for(_1, result).steps }.flatten
@result = result
end
def inspect
output = <<~OUTPUT
Inspecting #{result.__service_class__} result object:
#{execution_flow}
OUTPUT
output += "\nWhy it failed:\n\n#{error}" if error.present?
output
end
# Example output:
# [1/4] [model] channel (0.02 ms) ✅
# [2/4] [params] default (0.1 ms) ✅
# [3/4] [policy] check_channel_permission ❌
# [4/4] [step] change_status
# @return [String] the steps of the result object with their state
def execution_flow
steps
.filter_map
.with_index do |step, index|
next if @encountered_error
@encountered_error = index + 1 if step.failure?
"[#{format("%#{steps.size.to_s.size}s", index + 1)}/#{steps.size}] #{step.inspect}"
end
.join("\n")
.then do |output|
skipped_steps = steps.size - @encountered_error.to_i
next output unless @encountered_error && skipped_steps.positive?
"#{output}\n\n(#{skipped_steps} more steps not shown as the execution flow was stopped before reaching them)"
end
end
# @return [String, nil] the first available error, if any.
def error
steps.detect(&:error?)&.error
end
end