diff options
| author | William Boman <william@redwill.se> | 2023-04-05 19:50:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-05 19:50:43 +0200 |
| commit | e57d9bd671adce24970480a194690d207e2a141d (patch) | |
| tree | 238431a4bf3ba863dd658cd40f77834af241731e /lua/mason-core/managers/github | |
| parent | fix(npm): set install-strategy on npm >= 9 (#1179) (diff) | |
| download | mason-e57d9bd671adce24970480a194690d207e2a141d.tar mason-e57d9bd671adce24970480a194690d207e2a141d.tar.gz mason-e57d9bd671adce24970480a194690d207e2a141d.tar.bz2 mason-e57d9bd671adce24970480a194690d207e2a141d.tar.lz mason-e57d9bd671adce24970480a194690d207e2a141d.tar.xz mason-e57d9bd671adce24970480a194690d207e2a141d.tar.zst mason-e57d9bd671adce24970480a194690d207e2a141d.zip | |
fix(github): fall back to curl/wget if gh is not available (#1181)
Diffstat (limited to 'lua/mason-core/managers/github')
| -rw-r--r-- | lua/mason-core/managers/github/client.lua | 51 | ||||
| -rw-r--r-- | lua/mason-core/managers/github/init.lua | 15 |
2 files changed, 23 insertions, 43 deletions
diff --git a/lua/mason-core/managers/github/client.lua b/lua/mason-core/managers/github/client.lua index 03e6a10e..06130e36 100644 --- a/lua/mason-core/managers/github/client.lua +++ b/lua/mason-core/managers/github/client.lua @@ -1,12 +1,11 @@ local _ = require "mason-core.functional" local fetch = require "mason-core.fetch" -local log = require "mason-core.log" -local providers = require "mason-core.providers" local spawn = require "mason-core.spawn" local M = {} ----@alias GitHubCommit {sha: string} +---@alias GitHubCommit { sha: string } +---@alias GitHubRef { ref: string } local stringify_params = _.compose(_.join "&", _.map(_.join "="), _.sort_by(_.head), _.to_pairs) @@ -21,12 +20,12 @@ local function gh_api_call(path, opts) return spawn .gh({ "api", path, env = { CLICOLOR_FORCE = 0 } }) :map(_.prop "stdout") - :recover_catching(function() + :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", }, - }):get_or_throw() + }) end) :map_catching(vim.json.decode) end @@ -35,48 +34,26 @@ M.api_call = gh_api_call ---@async ---@param repo string The GitHub repo ("username/repo"). ----@return Result # Result<GitHubRelease[]> -function M.fetch_releases(repo) - log.fmt_trace("Fetching GitHub releases for repo=%s", repo) - local path = ("repos/%s/releases"):format(repo) - return gh_api_call(path):map_err(function() - return ("Failed to fetch releases for GitHub repository %s."):format(repo) - end) -end - ----@async ----@param repo string The GitHub repo ("username/repo"). ----@param tag_name string The tag_name of the release to fetch. -function M.fetch_release(repo, tag_name) - log.fmt_trace("Fetching GitHub release for repo=%s, tag_name=%s", repo, tag_name) - local path = ("repos/%s/releases/tags/%s"):format(repo, tag_name) - return gh_api_call(path):map_err(function() - return ("Failed to fetch release %q for GitHub repository %s."):format(tag_name, repo) - end) -end - ----@async ----@param repo string The GitHub repo ("username/repo"). ---@return Result # Result<GitHubRelease> function M.fetch_latest_release(repo) - return providers.github.get_latest_release(repo) + local path = ("repos/%s/releases/latest"):format(repo) + return gh_api_call(path) end ---@async ---@param repo string The GitHub repo ("username/repo"). ----@return Result # Result<GitHubTag[]> -function M.fetch_tags(repo) - local path = ("repos/%s/tags"):format(repo) - return gh_api_call(path):map_err(function() - return ("Failed to fetch tags for GitHub repository %s."):format(repo) - end) +---@return Result # Result<GitHubRelease[]> +function M.fetch_all_releases(repo) + local path = ("repos/%s/releases"):format(repo) + return gh_api_call(path) end ---@async ---@param repo string The GitHub repo ("username/repo"). ----@return Result # Result<string> The latest tag name. -function M.fetch_latest_tag(repo) - return providers.github.get_latest_tag(repo):map(_.prop "tag") +---@return Result # Result<GitHubRef[]> +function M.fetch_all_tags(repo) + local path = ("repos/%s/git/matching-refs/tags"):format(repo) + return gh_api_call(path) end ---@async diff --git a/lua/mason-core/managers/github/init.lua b/lua/mason-core/managers/github/init.lua index 8e767d44..2d593790 100644 --- a/lua/mason-core/managers/github/init.lua +++ b/lua/mason-core/managers/github/init.lua @@ -1,8 +1,8 @@ local Result = require "mason-core.result" local _ = require "mason-core.functional" -local client = require "mason-core.managers.github.client" local installer = require "mason-core.installer" local platform = require "mason-core.platform" +local providers = require "mason-core.providers" local settings = require "mason.settings" local std = require "mason-core.managers.std" @@ -53,8 +53,8 @@ function M.release_version(opts) local ctx = installer.context() ---@type string local release = _.coalesce(opts.version, ctx.requested_version):or_else_get(function() - return client - .fetch_latest_release(opts.repo) + return providers.github + .get_latest_release(opts.repo) :map(_.prop "tag_name") :get_or_throw "Failed to fetch latest release from GitHub API. Refer to :h mason-provider-errors for more information." end) @@ -105,7 +105,10 @@ end function M.tag(opts) local ctx = installer.context() local tag = _.coalesce(opts.version, ctx.requested_version):or_else_get(function() - return client.fetch_latest_tag(opts.repo):get_or_throw "Failed to fetch latest tag from GitHub API." + return providers.github + .get_latest_tag(opts.repo) + :map(_.prop "tag") + :get_or_throw "Failed to fetch latest tag from GitHub API." end) return { @@ -168,7 +171,7 @@ function M.check_outdated_primary_package_release(receipt) if source.type ~= "github_release" and source.type ~= "github_release_file" then return Result.failure "Receipt does not have a primary source of type (github_release|github_release_file)." end - return client.fetch_latest_release(source.repo):map_catching( + return providers.github.get_latest_release(source.repo):map_catching( ---@param latest_release GitHubRelease function(latest_release) if source.release ~= latest_release.tag_name then @@ -190,7 +193,7 @@ function M.check_outdated_primary_package_tag(receipt) if source.type ~= "github_tag" then return Result.failure "Receipt does not have a primary source of type github_tag." end - return client.fetch_latest_tag(source.repo):map_catching(function(latest_tag) + return providers.github.get_latest_tag(source.repo):map(_.prop "tag"):map_catching(function(latest_tag) if source.tag ~= latest_tag then return { name = source.repo, |
