Skip to content

Commit

Permalink
Add GitHub Enterprise support
Browse files Browse the repository at this point in the history
* fixes #4
  • Loading branch information
tynsh committed Apr 24, 2024
1 parent 4e19683 commit c7157c6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
49 changes: 35 additions & 14 deletions lua/gh-actions/github.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local utils = require('gh-actions.utils')
local M = {}

---@param str string
---@return string
---@return table<integer, string>
local function strip_git_suffix(str)
if str:sub(-4) == '.git' then
return str:sub(1, -5)
Expand All @@ -28,12 +28,13 @@ function M.get_current_repository()

local origin_url = table.concat(origin_url_job:result(), '')

return strip_git_suffix(origin_url):match('github.com[:/](.+)$')
return strip_git_suffix(origin_url):match('([^@/:]+)[:/](.+)$')
end

---@param cmd? string
---@param server? string
---@return string|nil
local function get_token_from_gh_cli(cmd)
local function get_token_from_gh_cli(cmd, server)
local has_gh_installed = vim.fn.executable('gh') == 1
if not has_gh_installed and not cmd then
return nil
Expand All @@ -43,7 +44,11 @@ local function get_token_from_gh_cli(cmd)
if cmd then
res = vim.fn.system(cmd)
else
res = vim.fn.system('gh auth token')
local gh_enterprise_flag = ""
if server ~= nil and server ~= "" then
gh_enterprise_flag = " --hostname " .. server
end
res = vim.fn.system('gh auth token' .. gh_enterprise_flag)
end

local token = string.gsub(res or '', '\n', '')
Expand All @@ -57,24 +62,30 @@ end

---@param cmd? string
---@return string
function M.get_github_token(cmd)
function M.get_github_token(cmd, server)
return vim.env.GITHUB_TOKEN
or get_token_from_gh_cli(cmd)
or get_token_from_gh_cli(cmd, server)
-- TODO: We could also ask for the token here via nui
or assert(nil, 'No GITHUB_TOKEN found in env and no gh cli config found')
end

---@param server string
---@param path string
---@param opts? table
function M.fetch(path, opts)
function M.fetch(server, path, opts)
opts = opts or {}
opts.callback = opts.callback and vim.schedule_wrap(opts.callback)

local url = string.format('https://api.github.com%s', path)
if server ~= "github.com" then
url = string.format('https://%s/api/v3%s', server, path)
end

return curl[opts.method or 'get'](
string.format('https://api.github.com%s', path),
url,
vim.tbl_deep_extend('force', opts, {
headers = {
Authorization = string.format('Bearer %s', M.get_github_token()),
Authorization = string.format('Bearer %s', M.get_github_token(nil, server)),
},
})
)
Expand All @@ -96,12 +107,14 @@ end
---@field total_count number
---@field workflows GhWorkflow[]

---@param server string
---@param repo string
---@param opts? { callback?: fun(workflows: GhWorkflow[]): any }
function M.get_workflows(repo, opts)
function M.get_workflows(server, repo, opts)
opts = opts or {}

return M.fetch(
server,
string.format('/repos/%s/actions/workflows', repo),
vim.tbl_deep_extend('force', opts, {
callback = function(response)
Expand Down Expand Up @@ -166,43 +179,49 @@ local function process_workflow_runs_response(opts)
end
end

---@param server string
---@param repo string
---@param per_page? integer
---@param opts? { callback?: fun(workflow_runs: GhWorkflowRun[]): any }
function M.get_repository_workflow_runs(repo, per_page, opts)
function M.get_repository_workflow_runs(server, repo, per_page, opts)
opts = opts or {}

return M.fetch(
server,
string.format('/repos/%s/actions/runs', repo),
vim.tbl_deep_extend('force', { query = { per_page = per_page } }, opts, {
callback = process_workflow_runs_response(opts),
})
)
end

---@param server string
---@param repo string
---@param workflow_id integer
---@param per_page? integer
---@param opts? { callback?: fun(workflow_runs: GhWorkflowRun[]): any }
function M.get_workflow_runs(repo, workflow_id, per_page, opts)
function M.get_workflow_runs(server, repo, workflow_id, per_page, opts)
opts = opts or {}

return M.fetch(
server,
string.format('/repos/%s/actions/workflows/%d/runs', repo, workflow_id),
vim.tbl_deep_extend('force', { query = { per_page = per_page } }, opts, {
callback = process_workflow_runs_response(opts),
})
)
end

---@param server string
---@param repo string
---@param workflow_id integer
---@param ref string
---@param opts? table
function M.dispatch_workflow(repo, workflow_id, ref, opts)
function M.dispatch_workflow(server, repo, workflow_id, ref, opts)
opts = opts or {}

return M.fetch(
server,
string.format(
'/repos/%s/actions/workflows/%d/dispatches',
repo,
Expand Down Expand Up @@ -237,14 +256,16 @@ end
---@field total_count number
---@field jobs GhWorkflowRunJob[]

---@param server string
---@param repo string
---@param workflow_run_id integer
---@param per_page? integer
---@param opts? { callback?: fun(workflow_runs: GhWorkflowRunJob[]): any }
function M.get_workflow_run_jobs(repo, workflow_run_id, per_page, opts)
function M.get_workflow_run_jobs(server, repo, workflow_run_id, per_page, opts)
opts = opts or {}

return M.fetch(
server,
string.format('/repos/%s/actions/runs/%d/jobs', repo, workflow_run_id),
vim.tbl_deep_extend('force', { query = { per_page = per_page } }, opts, {
callback = function(response)
Expand Down
14 changes: 8 additions & 6 deletions lua/gh-actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@ end
--TODO Maybe send lsp progress events when fetching, to interact
-- with fidget.nvim
local function fetch_data()
local repo = gh.get_current_repository()
local server, repo = gh.get_current_repository()

store.update_state(function(state)
state.repo = repo
state.server = server
end)

gh.get_workflows(repo, {
gh.get_workflows(server, repo, {
callback = function(workflows)
store.update_state(function(state)
state.workflows = workflows
end)
end,
})

gh.get_repository_workflow_runs(repo, 100, {
gh.get_repository_workflow_runs(server, repo, 100, {
callback = function(workflow_runs)
local old_workflow_runs = store.get_state().workflow_runs

Expand All @@ -68,7 +69,7 @@ local function fetch_data()
)

for _, run in ipairs(running_workflows) do
gh.get_workflow_run_jobs(repo, run.id, 20, {
gh.get_workflow_run_jobs(server, repo, run.id, 20, {
callback = function(jobs)
store.update_state(function(state)
state.workflow_jobs[run.id] = jobs
Expand Down Expand Up @@ -195,6 +196,7 @@ function M.open()
local workflow = ui.get_workflow()

if workflow then
local server = store.get_state().server
local repo = store.get_state().repo

-- TODO should we get current ref instead or show an input with the
Expand Down Expand Up @@ -224,11 +226,11 @@ function M.open()
if #questions > 0 and i <= #questions then
questions[i]:mount()
else
gh.dispatch_workflow(repo, workflow.id, default_branch, {
gh.dispatch_workflow(server, repo, workflow.id, default_branch, {
body = { inputs = input_values or {} },
callback = function(_res)
utils.delay(2000, function()
gh.get_workflow_runs(repo, workflow.id, 5, {
gh.get_workflow_runs(server, repo, workflow.id, 5, {
callback = function(workflow_runs)
store.update_state(function(state)
state.workflow_runs = utils.uniq(function(run)
Expand Down
2 changes: 1 addition & 1 deletion lua/gh-actions/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function M.parse_yaml(yamlstr)
if has_rust_module then
return rust.parse_yaml(yamlstr or '')
else
return vim.json.decode(vim.fn.system('yq', yamlstr) or '')
return vim.json.decode(vim.fn.system({'yq', '-o', 'json'}, yamlstr) or '')
end
end

Expand Down

0 comments on commit c7157c6

Please sign in to comment.