Fixed brew style offences

This commit is contained in:
lionellloh 2020-06-16 01:00:36 +08:00
parent ff1016b729
commit 52c6e5de76
2 changed files with 24 additions and 21 deletions

View File

@ -5,6 +5,7 @@ source "https://rubygems.org"
# installed gems # installed gems
gem "byebug" gem "byebug"
gem "coveralls", "~> 0.8", require: false gem "coveralls", "~> 0.8", require: false
gem "json", require: false
gem "parallel_tests" gem "parallel_tests"
gem "ronn", require: false gem "ronn", require: false
gem "rspec" gem "rspec"
@ -13,7 +14,6 @@ gem "rspec-retry", require: false
gem "rspec-wait", require: false gem "rspec-wait", require: false
gem "rubocop" gem "rubocop"
gem "simplecov", require: false gem "simplecov", require: false
gem "json", require: false
if ENV["HOMEBREW_SORBET"] if ENV["HOMEBREW_SORBET"]
gem "sorbet" gem "sorbet"

View File

@ -12,7 +12,7 @@ require "date"
require "missing_formula" require "missing_formula"
require "digest" require "digest"
require "cli/parser" require "cli/parser"
require 'json' require "json"
module Homebrew module Homebrew
module_function module_function
@ -112,7 +112,7 @@ module Homebrew
style_results = Style.check_style_json(style_files, options) if style_files style_results = Style.check_style_json(style_files, options) if style_files
# load licenses # load licenses
path = File.join(File.dirname(__FILE__), "spdx.json") path = File.join(File.dirname(__FILE__), "spdx.json")
spdx_ids = JSON.load( File.open(File.expand_path(path))) spdx_ids = JSON.parse(File.open(File.expand_path(path)).read)
new_formula_problem_lines = [] new_formula_problem_lines = []
audit_formulae.sort.each do |f| audit_formulae.sort.each do |f|
only = only_cops ? ["style"] : args.only only = only_cops ? ["style"] : args.only
@ -123,7 +123,7 @@ module Homebrew
git: git, git: git,
only: only, only: only,
except: args.except, except: args.except,
spdx_ids: spdx_ids spdx_ids: spdx_ids,
} }
options[:style_offenses] = style_results.file_offenses(f.path) if style_results options[:style_offenses] = style_results.file_offenses(f.path) if style_results
options[:display_cop_names] = args.display_cop_names? options[:display_cop_names] = args.display_cop_names?
@ -347,17 +347,18 @@ module Homebrew
].freeze ].freeze
def audit_licenses def audit_licenses
unless formula.license.nil? if formula.license
if @spdx_ids.key?(formula.license) if @spdx_ids.key?(formula.license)
return unless @online return unless @online
user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}, false) user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}, false)
return if user.nil? return if user.nil?
github_license = get_repo_license_data(user, repo) github_license = get_repo_license_data(user, repo)
if github_license && (github_license == formula.license) return if github_license && (github_license == formula.license)
return
else problem "License mismatch - Github license is: #{github_license}, \
problem "License mismatch - Github license is: #{github_license}, but Formulae license states: #{formula.license}" but Formulae license states: #{formula.license}"
end
else else
problem "#{formula.license} is not an SPDX license." problem "#{formula.license} is not an SPDX license."
end end
@ -366,14 +367,14 @@ module Homebrew
end end
end end
def get_repo_license_data(user, repo) def get_repo_license_data(user, repo)
return unless @online return unless @online
begin begin
res = GitHub.open_api("#{GitHub::API_URL}/repos/#{user}/#{repo}/license") res = GitHub.open_api("#{GitHub::API_URL}/repos/#{user}/#{repo}/license")
return nil unless res.key?("license") return unless res.key?("license")
return res["license"]["spdx_id"] || nil
res["license"]["spdx_id"] || nil
rescue GitHub::HTTPNotFoundError rescue GitHub::HTTPNotFoundError
nil nil
end end
@ -586,7 +587,9 @@ module Homebrew
def get_repo_data(regex, new_formula_only = true) def get_repo_data(regex, new_formula_only = true)
return unless @core_tap return unless @core_tap
return unless @online return unless @online
return unless @new_formula if new_formula_only
return unless @new_formula || !new_formula_only
_, user, repo = *regex.match(formula.stable.url) if formula.stable _, user, repo = *regex.match(formula.stable.url) if formula.stable
_, user, repo = *regex.match(formula.homepage) unless user _, user, repo = *regex.match(formula.homepage) unless user
return if !user || !repo return if !user || !repo