Skip to content

feat: gh run rerun|cancel #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ require"octo".setup({
expand_step = { lhs = "o", desc = "expand workflow step" },
open_in_browser = { lhs = "<C-b>", desc = "open workflow run in browser" },
refresh = { lhs = "<C-r>", desc = "refresh workflow" },
rerun = { lhs = "<C-o>", desc = "rerun workflow" },
rerun_failed = { lhs = "<C-f>", desc = "rerun failed workflow" },
cancel = { lhs = "<C-x>", desc = "cancel workflow" },
copy_url = { lhs = "<C-y>", desc = "copy url to system clipboard" },
},
issue = {
Expand Down
3 changes: 3 additions & 0 deletions lua/octo/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ function M.get_default_values()
expand_step = { lhs = "o", desc = "expand workflow step" },
open_in_browser = { lhs = "<C-b>", desc = "open workflow run in browser" },
refresh = { lhs = "<C-r>", desc = "refresh workflow" },
rerun = { lhs = "<C-o>", desc = "rerun workflow" },
rerun_failed = { lhs = "<C-f>", desc = "rerun failed workflow" },
cancel = { lhs = "<C-x>", desc = "cancel workflow" },
copy_url = { lhs = "<C-y>", desc = "copy url to system clipboard" },
},
issue = {
Expand Down
22 changes: 21 additions & 1 deletion lua/octo/pickers/telescope/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,32 @@ function M.workflow_runs(workflow_runs, title, on_select_cb)
},
sorter = conf.generic_sorter {},
previewer = previewers.workflow_runs.new {},
attach_mappings = function()
attach_mappings = function(_, map)
actions.select_default:replace(function(prompt_bufnr)
local selection = action_state.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
on_select_cb(selection.value)
end)
local mappings = require("octo.config").values.mappings.runs

map("i", mappings.rerun.lhs, function(prompt_bufnr)
local selection = action_state.get_selected_entry(prompt_bufnr)
local id = selection.value.id
require("octo.workflow_runs").rerun { db_id = id }
end)

map("i", mappings.rerun_failed.lhs, function(prompt_bufnr)
local selection = action_state.get_selected_entry(prompt_bufnr)
local id = selection.value.id
require("octo.workflow_runs").rerun { db_id = id, failed = true }
end)

map("i", mappings.cancel.lhs, function(prompt_bufnr)
local selection = action_state.get_selected_entry(prompt_bufnr)
local id = selection.value.id
require("octo.workflow_runs").cancel(id)
actions.close(prompt_bufnr)
end)
return true
end,
})
Expand Down
52 changes: 50 additions & 2 deletions lua/octo/workflow_runs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ local gh = require "octo.gh"
---@field wf_cache table<string,WorkflowRun>
---@field refresh function
---@field refetch function
---@field cancel function
---@field rerun function

---@class WorkflowNode
---@field id string
Expand Down Expand Up @@ -309,7 +311,6 @@ local function get_logs(id)
local sanitized_job_id = node.job_id:gsub("/", ""):gsub(":", ""):gsub("%.+$", "*/")
local file_name = string.format("%s_%s.txt", node.number, sanitized_name)
local path = vim.fs.joinpath(sanitized_job_id, file_name)
print(path)
local res = vim
.system({
"unzip",
Expand Down Expand Up @@ -353,9 +354,21 @@ end
local keymaps = {
---@param api Handler
[mappings.refresh.lhs] = function(api)
utils.info "refreshing..."
utils.info "Refreshing..."
api.refetch()
end,
[mappings.rerun.lhs] = function(api)
utils.info "Rerunning..."
api.rerun()
end,
[mappings.rerun_failed.lhs] = function(api)
utils.info "Rerunning failed jobs..."
api.rerun { failed = true }
end,
[mappings.cancel.lhs] = function(api)
utils.info "Cancelling..."
api.cancel()
end,
[mappings.open_in_browser.lhs] = function(api)
local id = api.current_wf.databaseId
navigation.open_in_browser("workflow_run", nil, id)
Expand Down Expand Up @@ -723,4 +736,39 @@ M.refetch = function()
populate_preview_buffer(id, M.buf)
end

---@param db_id number | nil
M.cancel = function(db_id)
local id = db_id or M.current_wf.databaseId
local _, stderr = gh.run.cancel {
id,
opts = { mode = "sync" },
}
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
utils.error "Failed to cancel workflow run"
else
utils.info "Cancelled"
end
M.refetch()
end

---@param opts { db_id: number | nil, failed: boolean | nil }
M.rerun = function(opts)
opts = opts or {}
local failed_jobs = opts.failed == true
local id = opts.db_id or (M.current_wf and M.current_wf.databaseId)
local _, stderr = gh.run.rerun {
id,
failed = failed_jobs,
opts = { mode = "sync" },
}
if stderr and not utils.is_blank(stderr) then
vim.api.nvim_err_writeln(stderr)
utils.error "Failed to rerun workflow run"
else
utils.info "Rerun queued"
end
M.refetch()
end

return M
Loading