Class: Mxrb::Github::Annotator

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/github/annotator.rb

Overview

Maps a Compare::Result to GitHub annotations.

Supports two output modes:

1. GitHub Actions workflow commands (stdout lines) for CI annotation
2. `gh pr comment` for pull request review comments

Usage:

result = Mxrb::Compare::Comparator.new(base_mpr, head_mpr).compare
ann = Mxrb::Github::Annotator.new(result, exported_dir: "out/", repo: "org/repo")
ann.print_actions_annotations          # write to stdout for GH Actions
ann.post_pr_comment!(pr_number: 42)   # post summary via gh CLI

Defined Under Namespace

Classes: Annotation

Constant Summary collapse

LAYER_MAP =

Maps Compare path segments to file-layer hints used in exported trees.

{
  "entities"   => "domain",
  "attributes" => "domain",
  "microflows" => "logic",
  "nanoflows"  => "presentation",
  "pages"      => "presentation",
  "menus"      => "presentation",
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(compare_result, exported_dir: nil, repo: nil) ⇒ Annotator

Returns a new instance of Annotator.



32
33
34
35
36
# File 'lib/mxrb/github/annotator.rb', line 32

def initialize(compare_result, exported_dir: nil, repo: nil)
  @result       = compare_result
  @exported_dir = exported_dir && File.expand_path(exported_dir)
  @repo         = repo
end

Instance Method Details

#annotationsObject

Returns an array of Annotation structs for each changed artifact.



39
40
41
42
43
# File 'lib/mxrb/github/annotator.rb', line 39

def annotations
  @annotations ||= @result.changes.map do |change|
    build_annotation(change)
  end
end

#comment_bodyObject

Returns the comment body Markdown string without posting it.



69
70
71
# File 'lib/mxrb/github/annotator.rb', line 69

def comment_body
  build_comment_body
end

#post_pr_comment!(pr_number:, repo: @repo) ⇒ Object

Posts a Markdown summary comment to a GitHub PR via the gh CLI.

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mxrb/github/annotator.rb', line 56

def post_pr_comment!(pr_number:, repo: @repo)
  raise ArgumentError, "repo required (org/name)" unless repo
  raise ArgumentError, "pr_number required" unless pr_number

  body = build_comment_body
  cmd  = ["gh", "pr", "comment", pr_number.to_s, "--repo", repo, "--body", body]
  out, err, status = Open3.capture3(*cmd)
  raise "gh pr comment failed: #{err}" unless status.success?

  out.strip
end

Emits GitHub Actions workflow command lines to stdout. When run inside a GH Actions job these create inline PR annotations.



47
48
49
50
51
52
53
# File 'lib/mxrb/github/annotator.rb', line 47

def print_actions_annotations(out: $stdout)
  annotations.each do |ann|
    file_part = ann.file ? "file=#{ann.file}" : ""
    line_part = ann.line ? ",line=#{ann.line}" : ""
    out.puts "::#{ann.level} #{file_part}#{line_part},title=#{ann.title}::#{ann.message}"
  end
end