Skip to content
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

feat: add telescope picker #23

Merged
merged 1 commit into from
May 26, 2024
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
34 changes: 21 additions & 13 deletions lua/jira/api_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local config = require 'jira.config'
local curl = require 'plenary.curl'

local M = {}
local transition_cache = {}

local get_auth_headers = function()
local jira_api_config = config.get_config().jira_api
Expand All @@ -25,26 +26,33 @@ M.get_issue = function(issue_id)
end

M.get_transitions = function(issue_id)
issue_id = utils.get_issue_id(issue_id)
assert(issue_id, 'Missing issue id')
if transition_cache[issue_id] then
return transition_cache[issue_id]
end
local url = get_base_url() .. '/issue/' .. issue_id .. '/transitions'
return curl.get(url, {
local response = curl.get(url, {
headers = get_auth_headers(),
})
end

-- Gets the transition id for the given transition name and then transitions the issue
-- Helpful for when you don't know the transition id
M.transition_issue_name = function(issue_id, transition_name)
local response = M.get_transitions(issue_id)
if response.exit ~= 0 then
vim.print 'Error getting transitions'
end
if response.status ~= 200 then
print('Error getting transitions: ' .. response.body)
return
end

local result = vim.fn.json_decode(response.body)
local transitions = result.transitions
transition_cache[issue_id] = result.transitions
return result.transitions
end

-- Gets the transition id for the given transition name and then transitions the issue
-- Helpful for when you don't know the transition id
M.transition_issue_name = function(transition_name, issue_id)
assert(transition_name, 'Missing transition name')
issue_id = utils.get_issue_id(issue_id)
local transitions = M.get_transitions(issue_id)
local transition_id
for _, transition in ipairs(transitions) do
if transition.name == transition_name then
Expand All @@ -53,13 +61,13 @@ M.transition_issue_name = function(issue_id, transition_name)
end
end
assert(transition_id, 'Transition not found: ' .. transition_name)
return M.transition_issue(issue_id, transition_id)
return M.transition_issue(transition_id, issue_id)
end

-- Transitions the issue to the given transition id
M.transition_issue = function(issue_id, transition_id)
assert(issue_id, 'Missing issue id')
assert(transition_id, 'Missing issue id')
M.transition_issue = function(transition_id, issue_id)
assert(transition_id, 'Missing transition id')
issue_id = utils.get_issue_id(issue_id)
local url = get_base_url() .. '/issue/' .. issue_id .. '/transitions'
local body = vim.json.encode {
transition = {
Expand Down
10 changes: 5 additions & 5 deletions lua/jira/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ M.setup = function()
view = function(issue_id)
M.view_issue(issue_id)
end,
transition = function(issue_id, transition_name)
M.transition_issue_name(issue_id, transition_name)
transition = function(transition_name, issue_id)
M.transition_issue_name(transition_name, issue_id)
end,
},
}
Expand Down Expand Up @@ -88,15 +88,15 @@ end

-- @param issue_id string - the id of the issue to transition
-- @param transition_name string - the name of the transition to perform
function M.transition_issue_name(issue_id, transition_name)
function M.transition_issue_name(transition_name, issue_id)
transition_name = transition_name or vim.fn.input 'Transition to: '
issue_id = issue_id or utils.get_issue_id()
if not issue_id then
print 'Missing issue id'
return
end
transition_name = transition_name or vim.fn.input 'Transition name: '
transition_name = transition_name:gsub('_', ' ')
local response = api_client.transition_issue_name(issue_id, transition_name)
local response = api_client.transition_issue_name(transition_name, issue_id)
if response and (response.exit ~= 0 or response.status ~= 204) then
vim.print 'Error making request'
end
Expand Down
41 changes: 41 additions & 0 deletions lua/jira/pickers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local pickers = require 'telescope.pickers'
local finders = require 'telescope.finders'
local actions = require 'telescope.actions'
local action_state = require 'telescope.actions.state'
local conf = require('telescope.config').values
local api_client = require 'jira.api_client'

local M = {}

local transitions = function(opts)
opts = opts or {}
local results = api_client.get_transitions()
pickers
.new(opts, {
prompt_title = 'Transitions',
finder = finders.new_table {
results = results,
entry_maker = function(entry)
return {
value = entry,
display = entry.name,
ordinal = entry.name,
}
end,
},
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
api_client.transition_issue(selection.value.id)
end)
return true
end,
sorter = conf.generic_sorter(opts),
})
:find()
end

M.transitions = transitions

return M
7 changes: 7 additions & 0 deletions lua/telescope/_extensions/jira.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local transitions = require('jira.pickers').transitions

return require('telescope').register_extension {
exports = {
transitions = transitions,
},
}
Loading