Merge branch 'license' into license-create

This commit is contained in:
Lionell 2020-06-18 00:47:00 +08:00
commit 7b2e6644a7
3 changed files with 42 additions and 18 deletions

View File

@ -349,6 +349,8 @@ module Homebrew
].freeze
def audit_license
return unless @new_formula
if !formula.license.blank?
if @spdx_ids.key?(formula.license)
return unless @online
@ -356,7 +358,7 @@ module Homebrew
user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}, false)
return if user.nil?
github_license = get_repo_license_data(user, repo)
github_license = GitHub.get_repo_license(user, repo)
return if github_license && (github_license == formula.license)
problem "License mismatch - Github license is: #{github_license}, "\
@ -369,18 +371,18 @@ module Homebrew
end
end
def get_repo_license_data(user, repo)
return unless @online
begin
res = GitHub.open_api("#{GitHub::API_URL}/repos/#{user}/#{repo}/license")
return unless res.key?("license")
res["license"]["spdx_id"] || nil
rescue GitHub::HTTPNotFoundError
nil
end
end
# def get_github_repo_license_data(user, repo)
# return unless @online
#
# begin
# res = GitHub.open_api("#{GitHub::API_URL}/repos/#{user}/#{repo}/license")
# return unless res.key?("license")
#
# res["license"]["spdx_id"] || nil
# rescue GitHub::HTTPNotFoundError
# nil
# end
# end
def audit_deps
@specs.each do |spec|

View File

@ -106,8 +106,21 @@ module Homebrew
let(:custom_spdx_id) { "zzz" }
let(:standard_mismatch_spdx_id) { "0BSD" }
it "does not check if the formula is not a new formula" do
fa = formula_auditor "foo", <<~RUBY, spdx_ids: spdx_ids, new_formula: false
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
license ""
end
RUBY
fa.audit_license
p fa.problems
expect(fa.problems).to be_empty
end
it "detects no license info" do
fa = formula_auditor "foo", <<~RUBY, spdx_ids: spdx_ids
fa = formula_auditor "foo", <<~RUBY, spdx_ids: spdx_ids, new_formula: true
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
license ""
@ -120,7 +133,7 @@ module Homebrew
end
it "detects if license is not a standard spdx-id" do
fa = formula_auditor "foo", <<~RUBY, spdx_ids: spdx_ids
fa = formula_auditor "foo", <<~RUBY, spdx_ids: spdx_ids, new_formula: true
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
license "#{custom_spdx_id}"
@ -132,7 +145,7 @@ module Homebrew
end
it "verifies that a license info is a standard spdx id" do
fa = formula_auditor "foo", <<~RUBY, spdx_ids: spdx_ids
fa = formula_auditor "foo", <<~RUBY, spdx_ids: spdx_ids, new_formula: true
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
license "0BSD"
@ -145,7 +158,7 @@ module Homebrew
it "checks online and verifies that a standard license id is the same "\
"as what is indicated on its Github repo" do
fa = formula_auditor "cask", <<~RUBY, spdx_ids: spdx_ids, online: true, core_tap: true
fa = formula_auditor "cask", <<~RUBY, spdx_ids: spdx_ids, online: true, core_tap: true, new_formula: true
class Cask < Formula
url "https://github.com/cask/cask/archive/v0.8.4.tar.gz"
head "https://github.com/cask/cask.git"
@ -159,7 +172,7 @@ module Homebrew
it "checks online and detects that a formula-specified license is not "\
"the same as what is indicated on its Github repository" do
fa = formula_auditor "cask", <<~RUBY, online: true, spdx_ids: spdx_ids, core_tap: true
fa = formula_auditor "cask", <<~RUBY, online: true, spdx_ids: spdx_ids, core_tap: true, new_formula: true
class Cask < Formula
url "https://github.com/cask/cask/archive/v0.8.4.tar.gz"
head "https://github.com/cask/cask.git"

View File

@ -476,6 +476,15 @@ module GitHub
open_api(url, scopes: ["admin:org", "user"], data: data, request_method: "POST")
end
def get_repo_license(user, repo)
res = GitHub.open_api("#{GitHub::API_URL}/repos/#{user}/#{repo}/license")
return unless res.key?("license")
res["license"]["spdx_id"] || nil
rescue GitHub::HTTPNotFoundError
nil
end
def api_errors
[GitHub::AuthenticationFailedError, GitHub::HTTPNotFoundError,
GitHub::RateLimitExceededError, GitHub::Error, JSON::ParserError].freeze