Module: Moult::Formatters::GateGithub

Defined in:
lib/moult/formatters/gate_github.rb

Overview

GitHub Actions workflow-command annotations: one ::error line per contributing finding, so a PR shows the gate's findings inline when run in Actions. Format and escaping follow the GitHub Actions workflow-commands spec. A passing gate emits a single ::notice.

Class Method Summary collapse

Class Method Details

.annotation(rule, finding) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/moult/formatters/gate_github.rb', line 29

def annotation(rule, finding)
  props = {file: finding.path}
  props[:line] = finding.line if finding.line
  props[:title] = "Moult gate: #{rule.rule}"
  prop_str = props.map { |k, v| "#{k}=#{escape_prop(v.to_s)}" }.join(",")
  "::error #{prop_str}::#{escape_data(message(rule, finding))}"
end

.escape_data(value) ⇒ Object

Per the workflow-command spec: escape % CR LF in message data; additionally escape : and , in property values.



43
44
45
# File 'lib/moult/formatters/gate_github.rb', line 43

def escape_data(value)
  value.gsub("%", "%25").gsub("\r", "%0D").gsub("\n", "%0A")
end

.escape_prop(value) ⇒ Object



47
48
49
# File 'lib/moult/formatters/gate_github.rb', line 47

def escape_prop(value)
  escape_data(value).gsub(":", "%3A").gsub(",", "%2C")
end

.message(rule, finding) ⇒ Object



37
38
39
# File 'lib/moult/formatters/gate_github.rb', line 37

def message(rule, finding)
  GateMessage.for(rule, finding)
end

.pass_notice(report) ⇒ Object



25
26
27
# File 'lib/moult/formatters/gate_github.rb', line 25

def pass_notice(report)
  "::notice title=#{escape_prop("Moult gate")}::#{escape_data("gate passed (#{report.summary[:evaluated]} rules evaluated)")}"
end

.render(report) ⇒ String

Parameters:

Returns:

  • (String)


18
19
20
21
22
23
# File 'lib/moult/formatters/gate_github.rb', line 18

def render(report)
  failed = report.rules.select { |r| r.evaluated && r.passed == false }
  return pass_notice(report) if failed.empty?

  failed.flat_map { |rule| rule.findings.map { |f| annotation(rule, f) } }.join("\n")
end