Skip to content

Commit

Permalink
Merge pull request #17357 from Homebrew/brew_style_actionlint
Browse files Browse the repository at this point in the history
style: run actionlint.
  • Loading branch information
MikeMcQuaid committed May 27, 2024
2 parents ff83a4f + 7d0ac4d commit 44d0d64
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions Library/Homebrew/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,24 @@ def self.check_style_impl(files, output_type,
debug: false, verbose: false)
raise ArgumentError, "Invalid output type: #{output_type.inspect}" if [:print, :json].exclude?(output_type)

shell_files, ruby_files =
Array(files).map(&method(:Pathname))
.partition { |f| f.realpath == HOMEBREW_BREW_FILE.realpath || f.extname == ".sh" }
ruby_files = []
shell_files = []
actionlint_files = []
Array(files).map(&method(:Pathname))
.each do |path|
case path.extname
when ".rb"
ruby_files << path
when ".sh"
shell_files << path
when ".yml"
actionlint_files << path if path.realpath.to_s.include?("/.github/workflows/")
else
shell_files << path if path.realpath == HOMEBREW_BREW_FILE.realpath
end
end

rubocop_result = if shell_files.any? && ruby_files.none?
rubocop_result = if files.present? && ruby_files.empty?
(output_type == :json) ? [] : true
else
run_rubocop(ruby_files, output_type,
Expand All @@ -59,22 +72,28 @@ def self.check_style_impl(files, output_type,
debug:, verbose:)
end

shellcheck_result = if ruby_files.any? && shell_files.none?
shellcheck_result = if files.present? && shell_files.empty?
(output_type == :json) ? [] : true
else
run_shellcheck(shell_files, output_type, fix:)
end

shfmt_result = if ruby_files.any? && shell_files.none?
shfmt_result = if files.present? && shell_files.empty?
true
else
run_shfmt(shell_files, fix:)
end

actionlint_result = if files.present? && actionlint_files.empty?
true
else
run_actionlint(actionlint_files)
end

if output_type == :json
Offenses.new(rubocop_result + shellcheck_result)
else
rubocop_result && shellcheck_result && shfmt_result
rubocop_result && shellcheck_result && shfmt_result && actionlint_result
end
end

Expand Down Expand Up @@ -242,6 +261,14 @@ def self.run_shfmt(files, fix: false)
$CHILD_STATUS.success?
end

def self.run_actionlint(files)
files = github_workflow_files if files.blank?
system actionlint, "-shellcheck", shellcheck,
"-config-file", HOMEBREW_REPOSITORY/".github/actionlint.yaml",
*files
$CHILD_STATUS.success?
end

def self.json_result!(result)
# An exit status of 1 just means violations were found; other numbers mean
# execution errors.
Expand Down Expand Up @@ -270,6 +297,15 @@ def self.shell_scripts
]
end

def self.github_workflow_files
HOMEBREW_REPOSITORY.glob(".github/workflows/*.yml")
end

def self.rubocop
ensure_formula_installed!("rubocop", latest: true,
reason: "Ruby style checks").opt_bin/"rubocop"
end

def self.shellcheck
ensure_formula_installed!("shellcheck", latest: true,
reason: "shell style checks").opt_bin/"shellcheck"
Expand All @@ -281,6 +317,11 @@ def self.shfmt
HOMEBREW_LIBRARY/"Homebrew/utils/shfmt.sh"
end

def self.actionlint
ensure_formula_installed!("actionlint", latest: true,
reason: "GitHub Actions checks").opt_bin/"actionlint"
end

# Collection of style offenses.
class Offenses
include Enumerable
Expand Down

0 comments on commit 44d0d64

Please sign in to comment.