Sha256: 1ccf0167dab81dc1614c4721fd9fd1ecdcd7094a144fc9258cc36f124750740c

Contents?: true

Size: 1.27 KB

Versions: 14

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Formatter
    # This formatter formats report data as GitHub Workflow commands resulting
    # in GitHub check annotations when run within GitHub Actions.
    class GitHubActionsFormatter < BaseFormatter
      ESCAPE_MAP = {
        '%' => '%25',
        "\n" => '%0A',
        "\r" => '%0D'
      }.freeze

      def file_finished(file, offenses)
        offenses.each { |offense| report_offense(file, offense) }
      end

      private

      def github_escape(string)
        string.gsub(Regexp.union(ESCAPE_MAP.keys), ESCAPE_MAP)
      end

      def minimum_severity_to_fail
        @minimum_severity_to_fail ||= begin
          name = options.fetch(:fail_level, :refactor)
          RuboCop::Cop::Severity.new(name)
        end
      end

      def github_severity(offense)
        offense.severity < minimum_severity_to_fail ? 'warning' : 'error'
      end

      def report_offense(file, offense)
        output.printf(
          "\n::%<severity>s file=%<file>s,line=%<line>d,col=%<column>d::%<message>s\n",
          severity: github_severity(offense),
          file: file,
          line: offense.line,
          column: offense.real_column,
          message: github_escape(offense.message)
        )
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rubocop-1.8.1 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.8.0 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.7.0 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.6.1 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.6.0 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.5.2 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.5.1 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.5.0 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.4.2 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.4.1 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.4.0 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.3.1 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.3.0 lib/rubocop/formatter/git_hub_actions_formatter.rb
rubocop-1.2.0 lib/rubocop/formatter/git_hub_actions_formatter.rb