1+ require 'ostruct'
12require 'pronto'
23require_relative './annotation'
34
45module Pronto
56 module Formatter
7+ # The GithubActionCheckRunFormatter class is responsible for formatting and creating
8+ # GitHub Action Check Runs. It extends the Pronto::Formatter::Base class
9+ # and integrates with GitHub Actions to generate check runs based
10+ # on the messages produced by Pronto runners.
11+ #
12+ # This class handles the interaction with the GitHub API to create check runs for each
13+ # runner, providing detailed annotations and summaries of the analysis results.
14+ #
15+ # @attr_accessor [Array<Pronto::Message>] messages
16+ # The collection of messages that need to be formatted into check runs.
17+ #
18+ # @attr_accessor [Pronto::Git::Repository] repo
19+ # The repository object representing the Git repository being analyzed.
20+ #
21+ # @attr_accessor [String] sha
22+ # The commit SHA for which the check runs are being created. This is typically
23+ # the SHA of the head commit in the repository.
24+ #
25+ # @attr_accessor [Object] check_run
26+ # The check run object that represents the current check run being processed.
27+ # This object is used to store and manage the state of the check run.
628 class GithubActionCheckRunFormatter < Base
7-
829 attr_accessor :messages , :repo , :sha , :check_run
930
31+ # @return [String] The name of the formatter.
32+ def self . name
33+ 'github_action_check_run'
34+ end
35+
36+ # Formats the messages into GitHub Action Check Runs.
37+ #
38+ # @param messages [Array<Pronto::Message>] The collection of messages to be formatted.
39+ # @param repo [Pronto::Git::Repository] The repository being analyzed.
40+ # @return [String] A summary of the number of check runs created.
1041 def format ( messages , repo , _ )
1142 self . messages = messages
1243 self . repo = repo
@@ -19,22 +50,28 @@ def format(messages, repo, _)
1950 "#{ Runner . runners . size } Check Runs created"
2051 end
2152
53+ # @return [Octokit::Client] The Octokit client configured with GitHub API credentials.
2254 def client
2355 @client ||= Octokit ::Client . new (
2456 api_endpoint : config . github_api_endpoint ,
2557 web_endpoint : config . github_web_endpoint ,
26- access_token : ENV . fetch ( 'GITHUB_TOKEN' ) { config . github_access_token } ,
58+ access_token : ENV . fetch ( 'GITHUB_TOKEN' ) { config . github_access_token }
2759 )
2860 end
2961
62+ # Creates a GitHub Action Check Run for a given runner and its messages.
63+ #
64+ # @param runner [Object] The runner for which the check run is being created.
65+ # @param runner_messages [Array<Pronto::Message>] The messages associated with the runner.
66+ # @return [void]
3067 def create_check_run ( runner , runner_messages )
3168 line_annotations , no_line_annotations = runner_messages . map do |message |
3269 Annotation . new ( message )
3370 end . partition ( &:line? )
3471 output = OpenStruct . new (
3572 title : runner . title ,
3673 summary : check_run_summary ( runner , runner_messages ) ,
37- annotations : line_annotations . map ( &:to_h ) . first ( 50 ) ,
74+ annotations : line_annotations . map ( &:to_h ) . first ( 50 )
3875 )
3976 if no_line_annotations . any?
4077 output . text = <<~TXT
@@ -52,10 +89,15 @@ def create_check_run(runner, runner_messages)
5289 started_at : Time . now . iso8601 ,
5390 status : :completed ,
5491 completed_at : Time . now . iso8601 ,
55- accept : 'application/vnd.github.antiope-preview+json' ,
92+ accept : 'application/vnd.github.antiope-preview+json'
5693 )
5794 end
5895
96+ # Generates a summary for a GitHub Action Check Run.
97+ #
98+ # @param runner [Object] The runner for which the summary is being generated.
99+ # @param runner_messages [Array<Pronto::Message>] The messages associated with the runner.
100+ # @return [String] A summary of the check run, indicating the number of issues or a success message.
59101 def check_run_summary ( runner , runner_messages )
60102 if runner_messages . any?
61103 <<~TXT
@@ -67,32 +109,24 @@ def check_run_summary(runner, runner_messages)
67109 end
68110 end
69111
112+ # Retrieves the repository slug from the GitHub event or configuration.
113+ #
114+ # @return [String] The full name of the repository in the format "owner/repo".
115+ # @raise [RuntimeError] If the GitHub event path is not set and the slug is not found in the config.
70116 def repo_slug
71117 @repo_slug ||= if ENV . key? ( 'GITHUB_EVENT_PATH' )
72118 event = JSON . parse ( File . read ( ENV . fetch ( 'GITHUB_EVENT_PATH' ) ) )
73119 event . fetch ( 'repository' ) . fetch ( 'full_name' )
74120 else
75- config . github_slug || fail ( 'no github.slug in pronto config' )
121+ config . github_slug || raise ( 'no github.slug in pronto config' )
76122 end
77123 end
78124
79125 def messages_by_runner
80126 @messages_by_runner ||= messages . uniq . group_by ( &:runner )
81127 end
82-
83128 end
84129 end
85130end
86131
87- begin
88- warn_level = $VERBOSE
89- $VERBOSE = nil
90- Pronto ::Formatter . const_set (
91- :FORMATTERS ,
92- Pronto ::Formatter ::FORMATTERS . merge (
93- 'github_action_check_run' => Pronto ::Formatter ::GithubActionCheckRunFormatter
94- )
95- )
96- ensure
97- $VERBOSE = warn_level
98- end
132+ Pronto ::Formatter . register ( Pronto ::Formatter ::GithubActionCheckRunFormatter )
0 commit comments