From 56e0c3d9e83983be943bb4600cb3c5a79c5f16b7 Mon Sep 17 00:00:00 2001 From: nandahkrishna Date: Mon, 15 Feb 2021 21:48:21 +0530 Subject: [PATCH] Update GitHub API usage --- Library/Homebrew/cmd/gist-logs.rb | 20 +++----------------- Library/Homebrew/dev-cmd/pr-pull.rb | 4 ++-- Library/Homebrew/dev-cmd/pr-upload.rb | 2 +- Library/Homebrew/dev-cmd/release.rb | 6 +++--- Library/Homebrew/exceptions.rb | 2 +- Library/Homebrew/formula_creator.rb | 2 +- Library/Homebrew/search.rb | 2 +- Library/Homebrew/tap.rb | 4 ++-- Library/Homebrew/test/search_spec.rb | 4 ++-- Library/Homebrew/test/tap_spec.rb | 2 +- Library/Homebrew/utils/github.rb | 12 ++++++++++++ Library/Homebrew/utils/shared_audits.rb | 6 +++--- Library/Homebrew/utils/spdx.rb | 2 +- 13 files changed, 33 insertions(+), 35 deletions(-) diff --git a/Library/Homebrew/cmd/gist-logs.rb b/Library/Homebrew/cmd/gist-logs.rb index 86436de503..38b17ee320 100644 --- a/Library/Homebrew/cmd/gist-logs.rb +++ b/Library/Homebrew/cmd/gist-logs.rb @@ -55,7 +55,7 @@ module Homebrew files["00.tap.out"] = { content: tap } end - odie "`brew gist-logs` requires HOMEBREW_GITHUB_API_TOKEN to be set!" if GitHub.api_credentials_type == :none + odie "`brew gist-logs` requires HOMEBREW_GITHUB_API_TOKEN to be set!" if GitHub::API.api_credentials_type == :none # Description formatted to work well as page title when viewing gist descr = if f.core_formula? @@ -63,9 +63,9 @@ module Homebrew else "#{f.name} (#{f.full_name}) on #{OS_VERSION} - Homebrew build logs" end - url = create_gist(files, descr, private: args.private?) + url = GitHub.create_gist(files, descr, private: args.private?) - url = create_issue(f.tap, "#{f.name} failed to build on #{MacOS.full_version}", url) if args.new_issue? + url = GitHub.create_issue(f.tap, "#{f.name} failed to build on #{MacOS.full_version}", url) if args.new_issue? puts url if url end @@ -108,20 +108,6 @@ module Homebrew logs end - def create_gist(files, description, private:) - url = "https://api.github.com/gists" - data = { "public" => !private, "files" => files, "description" => description } - scopes = GitHub::CREATE_GIST_SCOPES - GitHub.open_api(url, data: data, scopes: scopes)["html_url"] - end - - def create_issue(repo, title, body) - url = "https://api.github.com/repos/#{repo}/issues" - data = { "title" => title, "body" => body } - scopes = GitHub::CREATE_ISSUE_FORK_OR_PR_SCOPES - GitHub.open_api(url, data: data, scopes: scopes)["html_url"] - end - def gist_logs args = gist_logs_args.parse diff --git a/Library/Homebrew/dev-cmd/pr-pull.rb b/Library/Homebrew/dev-cmd/pr-pull.rb index 369fe76bc4..e345ac1a51 100644 --- a/Library/Homebrew/dev-cmd/pr-pull.rb +++ b/Library/Homebrew/dev-cmd/pr-pull.rb @@ -337,9 +337,9 @@ module Homebrew end def download_artifact(url, dir, pr) - odie "Credentials must be set to access the Artifacts API" if GitHub.api_credentials_type == :none + odie "Credentials must be set to access the Artifacts API" if GitHub::API.api_credentials_type == :none - token = GitHub.api_credentials + token = GitHub::API.api_credentials curl_args = ["--header", "Authorization: token #{token}"] # Download the artifact as a zip file and unpack it into `dir`. This is diff --git a/Library/Homebrew/dev-cmd/pr-upload.rb b/Library/Homebrew/dev-cmd/pr-upload.rb index 09fb40e08c..951d24fef0 100644 --- a/Library/Homebrew/dev-cmd/pr-upload.rb +++ b/Library/Homebrew/dev-cmd/pr-upload.rb @@ -114,7 +114,7 @@ module Homebrew rel = GitHub.get_release user, repo, tag odebug "Existing GitHub release \"#{tag}\" found" rel - rescue GitHub::HTTPNotFoundError + rescue GitHub::API::HTTPNotFoundError odebug "Creating new GitHub release \"#{tag}\"" GitHub.create_or_update_release user, repo, tag end diff --git a/Library/Homebrew/dev-cmd/release.rb b/Library/Homebrew/dev-cmd/release.rb index a94b1e15ce..62aae87414 100755 --- a/Library/Homebrew/dev-cmd/release.rb +++ b/Library/Homebrew/dev-cmd/release.rb @@ -39,7 +39,7 @@ module Homebrew begin latest_release = GitHub.get_latest_release "Homebrew", "brew" - rescue GitHub::HTTPNotFoundError + rescue GitHub::API::HTTPNotFoundError odie "No existing releases found!" end latest_version = Version.new latest_release["tag_name"] @@ -48,7 +48,7 @@ module Homebrew one_month_ago = Date.today << 1 latest_major_minor_release = begin GitHub.get_release "Homebrew", "brew", "#{latest_version.major_minor}.0" - rescue GitHub::HTTPNotFoundError + rescue GitHub::API::HTTPNotFoundError nil end @@ -89,7 +89,7 @@ module Homebrew begin release = GitHub.create_or_update_release "Homebrew", "brew", new_version, body: release_notes, draft: true - rescue *GitHub::API_ERRORS => e + rescue *GitHub::API::API_ERRORS => e odie "Unable to create release: #{e.message}!" end diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index 1a8d0abee2..9fca2b5b9e 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -423,7 +423,7 @@ class BuildError < RuntimeError def fetch_issues GitHub.issues_for_formula(formula.name, tap: formula.tap, state: "open") - rescue GitHub::RateLimitExceededError => e + rescue GitHub::API::RateLimitExceededError => e opoo e.message [] end diff --git a/Library/Homebrew/formula_creator.rb b/Library/Homebrew/formula_creator.rb index 16e4a6f95c..f9aa3f0c30 100644 --- a/Library/Homebrew/formula_creator.rb +++ b/Library/Homebrew/formula_creator.rb @@ -79,7 +79,7 @@ module Homebrew @desc = metadata["description"] @homepage = metadata["homepage"] @license = metadata["license"]["spdx_id"] if metadata["license"] - rescue GitHub::HTTPNotFoundError + rescue GitHub::API::HTTPNotFoundError # If there was no repository found assume the network connection is at # fault rather than the input URL. nil diff --git a/Library/Homebrew/search.rb b/Library/Homebrew/search.rb index b278bccc60..773579de0b 100644 --- a/Library/Homebrew/search.rb +++ b/Library/Homebrew/search.rb @@ -48,7 +48,7 @@ module Homebrew filename: query, extension: "rb", ) - rescue GitHub::Error => e + rescue GitHub::API::Error => e opoo "Error searching on GitHub: #{e}\n" nil end diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index f6a5a1bf80..dc67a56ddf 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -692,9 +692,9 @@ class Tap else GitHub.private_repo?(full_name) end - rescue GitHub::HTTPNotFoundError + rescue GitHub::API::HTTPNotFoundError true - rescue GitHub::Error + rescue GitHub::API::Error false end end diff --git a/Library/Homebrew/test/search_spec.rb b/Library/Homebrew/test/search_spec.rb index 84377c4757..e3aafe488f 100644 --- a/Library/Homebrew/test/search_spec.rb +++ b/Library/Homebrew/test/search_spec.rb @@ -21,7 +21,7 @@ describe Homebrew::Search do end it "does not raise if the network fails" do - allow(GitHub).to receive(:open_api).and_raise(GitHub::Error) + allow(GitHub::API).to receive(:open_api).and_raise(GitHub::API::Error) expect(mod.search_taps("some-formula")) .to match(formulae: [], casks: []) @@ -45,7 +45,7 @@ describe Homebrew::Search do ], } - allow(GitHub).to receive(:open_api).and_yield(json_response) + allow(GitHub::API).to receive(:open_api).and_yield(json_response) expect(mod.search_taps("some-formula")) .to match(formulae: ["homebrew/foo/some-formula"], casks: ["homebrew/bar/some-cask"]) diff --git a/Library/Homebrew/test/tap_spec.rb b/Library/Homebrew/test/tap_spec.rb index 03bf027c9f..0b467db916 100644 --- a/Library/Homebrew/test/tap_spec.rb +++ b/Library/Homebrew/test/tap_spec.rb @@ -216,7 +216,7 @@ describe Tap do end specify "#private?" do - skip "HOMEBREW_GITHUB_API_TOKEN is required" unless GitHub.api_credentials + skip "HOMEBREW_GITHUB_API_TOKEN is required" unless GitHub::API.api_credentials expect(homebrew_foo_tap).to be_private end diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 42f7796405..e0a4758cdb 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -30,6 +30,18 @@ module GitHub search("issues", query, **qualifiers) end + def create_gist(files, description, private:) + url = "https://api.github.com/gists" + data = { "public" => !private, "files" => files, "description" => description } + API.open_api(url, data: data, scopes: CREATE_GIST_SCOPES)["html_url"] + end + + def create_issue(repo, title, body) + url = "https://api.github.com/repos/#{repo}/issues" + data = { "title" => title, "body" => body } + API.open_api(url, data: data, scopes: CREATE_ISSUE_FORK_OR_PR_SCOPES)["html_url"] + end + def repository(user, repo) API.open_api(url_to("repos", user, repo)) end diff --git a/Library/Homebrew/utils/shared_audits.rb b/Library/Homebrew/utils/shared_audits.rb index 310b497521..b5ed7cb408 100644 --- a/Library/Homebrew/utils/shared_audits.rb +++ b/Library/Homebrew/utils/shared_audits.rb @@ -17,17 +17,17 @@ module SharedAudits @github_repo_data["#{user}/#{repo}"] ||= GitHub.repository(user, repo) @github_repo_data["#{user}/#{repo}"] - rescue GitHub::HTTPNotFoundError + rescue GitHub::API::HTTPNotFoundError nil end def github_release_data(user, repo, tag) id = "#{user}/#{repo}/#{tag}" @github_release_data ||= {} - @github_release_data[id] ||= GitHub.open_api("#{GitHub::API_URL}/repos/#{user}/#{repo}/releases/tags/#{tag}") + @github_release_data[id] ||= GitHub::API.open_api("#{GitHub::API_URL}/repos/#{user}/#{repo}/releases/tags/#{tag}") @github_release_data[id] - rescue GitHub::HTTPNotFoundError + rescue GitHub::API::HTTPNotFoundError nil end diff --git a/Library/Homebrew/utils/spdx.rb b/Library/Homebrew/utils/spdx.rb index 1fe89ae6fa..e1b96d6931 100644 --- a/Library/Homebrew/utils/spdx.rb +++ b/Library/Homebrew/utils/spdx.rb @@ -29,7 +29,7 @@ module SPDX end def latest_tag - @latest_tag ||= GitHub.open_api(API_URL)["tag_name"] + @latest_tag ||= GitHub::API.open_api(API_URL)["tag_name"] end def download_latest_license_data!(to: DATA_PATH)