From c6fa271fbfe012e2c648cb36b92fc6c4db78bcb1 Mon Sep 17 00:00:00 2001 From: William Boman Date: Wed, 11 Oct 2023 15:25:15 +0200 Subject: refactor(providers): inline GitHub API calls in the client provider (#1518) --- lua/mason/health.lua | 44 ++------------------------------------- lua/mason/providers/client/gh.lua | 36 +++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 47 deletions(-) (limited to 'lua') diff --git a/lua/mason/health.lua b/lua/mason/health.lua index f12ac811..50fe3a46 100644 --- a/lua/mason/health.lua +++ b/lua/mason/health.lua @@ -3,9 +3,9 @@ local Result = require "mason-core.result" local _ = require "mason-core.functional" local a = require "mason-core.async" local control = require "mason-core.async.control" -local github_client = require "mason-core.managers.github.client" local platform = require "mason-core.platform" local providers = require "mason-core.providers" +local registry = require "mason-registry" local registry_sources = require "mason-registry.sources" local settings = require "mason.settings" local spawn = require "mason-core.spawn" @@ -67,6 +67,7 @@ end local function check_registries() report_start "mason.nvim [Registries]" + a.wait(registry.refresh) for source in registry_sources.iter { include_uninstalled = true } do if source:is_installed() then report_ok(("Registry `%s` is installed."):format(source:get_display_name())) @@ -79,46 +80,6 @@ local function check_registries() end end ----@async -local function check_github() - report_start "mason.nvim [GitHub]" - github_client - .fetch_rate_limit() - :on_success( - ---@param rate_limit GitHubRateLimitResponse - function(rate_limit) - a.scheduler() - local remaining = rate_limit.resources.core.remaining - local used = rate_limit.resources.core.used - local limit = rate_limit.resources.core.limit - local reset = rate_limit.resources.core.reset - local diagnostics = ("Used: %d. Remaining: %d. Limit: %d. Reset: %s."):format( - used, - remaining, - limit, - vim.fn.strftime("%c", reset) - ) - if remaining <= 0 then - report_error(("GitHub API rate limit exceeded. %s"):format(diagnostics)) - else - local NON_AUTH_LIMIT = 60 - if limit > NON_AUTH_LIMIT then - report_ok(("GitHub API rate limit. %s"):format(diagnostics)) - else - report_ok( - ("GitHub API rate limit. %s\nInstall and authenticate via gh-cli to increase rate limit."):format( - diagnostics - ) - ) - end - end - end - ) - :on_failure(function() - report_warn "Failed to check GitHub API rate limit status." - end) -end - local function check_neovim() if vim.fn.has "nvim-0.9.0" == 1 then report_ok "neovim version >= 0.9.0" @@ -318,7 +279,6 @@ function M.check() check_registries() check_core_utils() check_languages() - check_github() a.wait(vim.schedule) end) end diff --git a/lua/mason/providers/client/gh.lua b/lua/mason/providers/client/gh.lua index bca11460..b97ab1d6 100644 --- a/lua/mason/providers/client/gh.lua +++ b/lua/mason/providers/client/gh.lua @@ -1,16 +1,42 @@ -local Result = require "mason-core.result" local _ = require "mason-core.functional" -local client = require "mason-core.managers.github.client" +local fetch = require "mason-core.fetch" +local spawn = require "mason-core.spawn" + +local stringify_params = _.compose(_.join "&", _.map(_.join "="), _.sort_by(_.head), _.to_pairs) + +---@param path string +---@param opts { params: table? }? +---@return Result # JSON decoded response. +local function gh_api_call(path, opts) + if opts and opts.params then + local params = stringify_params(opts.params) + path = ("%s?%s"):format(path, params) + end + return spawn + .gh({ "api", path, env = { CLICOLOR_FORCE = 0 } }) + :map(_.prop "stdout") + :or_else(function() + return fetch(("https://api.github.com/%s"):format(path), { + headers = { + Accept = "application/vnd.github.v3+json; q=1.0, application/json; q=0.8", + }, + }) + end) + :map_catching(vim.json.decode) +end ---@type GitHubProvider return { get_latest_release = function(repo) - return client.fetch_latest_release(repo) + local path = ("repos/%s/releases/latest"):format(repo) + return gh_api_call(path) end, get_all_release_versions = function(repo) - return client.fetch_all_releases(repo):map(_.map(_.prop "tag_name")) + local path = ("repos/%s/releases"):format(repo) + return gh_api_call(path):map(_.map(_.prop "tag_name")) end, get_all_tags = function(repo) - return client.fetch_all_tags(repo):map(_.map(_.compose(_.gsub("^refs/tags/", ""), _.prop "ref"))) + local path = ("repos/%s/git/matching-refs/tags"):format(repo) + return gh_api_call(path):map(_.map(_.compose(_.gsub("^refs/tags/", ""), _.prop "ref"))) end, } -- cgit v1.2.3-70-g09d2