From dab18076fad3fd331793786e042d11cfec93c681 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 20 Jul 2020 21:52:35 +0200 Subject: [PATCH] audit: error on archived repos --- Library/Homebrew/dev-cmd/audit.rb | 24 ++++++++++++++++ Library/Homebrew/test/dev-cmd/audit_spec.rb | 28 +++++++++++++++++++ Library/Homebrew/utils/notability.rb | 31 +++++++++++++++------ 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 373f60669f..ba6b71172d 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -522,6 +522,30 @@ module Homebrew problem "Formulae in homebrew/core should not use `bottle :disabled`" end + def audit_github_repository_archived + return if formula.deprecated? + + user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}) if @online + return if user.blank? + + metadata = SharedAudits.github_repo_data(user, repo) + return if metadata.nil? + + problem "GitHub repo is archived" if metadata["archived"] + end + + def audit_gitlab_repository_archived + return if formula.deprecated? + + user, repo = get_repo_data(%r{https?://gitlab\.com/([^/]+)/([^/]+)/?.*}) if @online + return if user.blank? + + metadata = SharedAudits.gitlab_repo_data(user, repo) + return if metadata.nil? + + problem "GitLab repo is archived" if metadata["archived"] + end + def audit_github_repository user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}) if @new_formula diff --git a/Library/Homebrew/test/dev-cmd/audit_spec.rb b/Library/Homebrew/test/dev-cmd/audit_spec.rb index 9b80945ebf..17f8cd3350 100644 --- a/Library/Homebrew/test/dev-cmd/audit_spec.rb +++ b/Library/Homebrew/test/dev-cmd/audit_spec.rb @@ -195,6 +195,20 @@ module Homebrew end end + describe "#audit_github_repository_archived" do + specify "#audit_github_repository_archived when HOMEBREW_NO_GITHUB_API is set" do + fa = formula_auditor "foo", <<~RUBY, strict: true, online: true + class Foo < Formula + homepage "https://github.com/example/example" + url "https://brew.sh/foo-1.0.tgz" + end + RUBY + + fa.audit_github_repository_archived + expect(fa.problems).to eq([]) + end + end + describe "#audit_gitlab_repository" do specify "#audit_gitlab_repository for stars, forks and creation date" do fa = formula_auditor "foo", <<~RUBY, strict: true, online: true @@ -209,6 +223,20 @@ module Homebrew end end + describe "#audit_gitlab_repository_archived" do + specify "#audit gitlab repository for archived status" do + fa = formula_auditor "foo", <<~RUBY, strict: true, online: true + class Foo < Formula + homepage "https://gitlab.com/libtiff/libtiff" + url "https://brew.sh/foo-1.0.tgz" + end + RUBY + + fa.audit_gitlab_repository_archived + expect(fa.problems).to eq([]) + end + end + describe "#audit_bitbucket_repository" do specify "#audit_bitbucket_repository for stars, forks and creation date" do fa = formula_auditor "foo", <<~RUBY, strict: true, online: true diff --git a/Library/Homebrew/utils/notability.rb b/Library/Homebrew/utils/notability.rb index ef6099844d..831eb9b6cd 100644 --- a/Library/Homebrew/utils/notability.rb +++ b/Library/Homebrew/utils/notability.rb @@ -5,13 +5,30 @@ require "utils/curl" module SharedAudits module_function - def github(user, repo) - begin - metadata = GitHub.repository(user, repo) - rescue GitHub::HTTPNotFoundError - return + def github_repo_data(user, repo) + @github_repo_data ||= {} + @github_repo_data["#{user}/#{repo}"] ||= GitHub.repository(user, repo) + + @github_data["#{user}/#{repo}"] + rescue GitHub::HTTPNotFoundError + nil + end + + def gitlab_repo_data(user, repo) + @gitlab_repo_data ||= {} + @gitlab_repo_data["#{user}/#{repo}"] ||= begin + out, _, status= curl_output("--request", "GET", "https://gitlab.com/api/v4/projects/#{user}%2F#{repo}") + return unless status.success? + + JSON.parse(out) end + @gitlab_data["#{user}/#{repo}"] + end + + def github(user, repo) + metadata = github_repo_data(user, repo) + return if metadata.nil? return "GitHub fork (not canonical repository)" if metadata["fork"] @@ -26,10 +43,8 @@ module SharedAudits end def gitlab(user, repo) - out, _, status= curl_output("--request", "GET", "https://gitlab.com/api/v4/projects/#{user}%2F#{repo}") - return unless status.success? + metadata = gitlab_repo_data(user, repo) - metadata = JSON.parse(out) return if metadata.nil? return "GitLab fork (not canonical repository)" if metadata["fork"]