Skip to content

Commit

Permalink
Merge pull request #20 from kid-icarus/ki/transition-ticket
Browse files Browse the repository at this point in the history
add transition command
  • Loading branch information
kid-icarus authored Apr 9, 2023
2 parents 3fe4468 + 8ec0e59 commit f0dfd5c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ There is only an Jira <object> <action> [arguments] command.
| Object | Action | Description |
|---|---|---|
| issue | view [issue_id] | View the given issue, if none provided it will attempt to extract one out of the current git branch (disabled via `use_git_branch_issue_id`), else falls back to a prompt |
| | transition [issue_id] [transition_name] | Transition the ticket to a given status. Will attempt to extract issue ID from git branch, and will prompt if no options given

60 changes: 60 additions & 0 deletions lua/jira/api_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,64 @@ M.get_issue = function(issue_id, callback)
}
end

M.get_transitions = function(issue_id, callback)
local url = get_base_url() .. '/issue/' .. issue_id .. '/transitions'
http.request {
http.methods.GET,
url,
nil,
nil,
headers = get_auth_headers(),
callback = callback,
}
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, callback)
M.get_transitions(issue_id, function(err, response)
if err then
print('Error getting transitions: ' .. err)
return
end
if response.code >= 400 then
print('Error getting transitions: ' .. response.body)
return
end
vim.schedule(function()
local result = vim.fn.json_decode(response.body)
if err then
print('Error getting transitions: ' .. err)
return
end
local transitions = result.transitions
for _, transition in ipairs(transitions) do
if transition.name == transition_name then
M.transition_issue(issue_id, transition.id, callback)
return
end
end
assert(transition_name, 'Transition not found: ' .. transition_name)
end)
end)
end

-- Transitions the issue to the given transition id
M.transition_issue = function(issue_id, transition_id, callback)
local url = get_base_url() .. '/issue/' .. issue_id .. '/transitions'
local body = vim.json.encode {
transition = {
id = transition_id,
},
}
http.request {
http.methods.POST,
url,
body,
nil,
headers = get_auth_headers(),
callback = callback,
}
end

return M
43 changes: 27 additions & 16 deletions lua/jira/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ 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)
end,
},
}
end
Expand Down Expand Up @@ -42,22 +45,7 @@ function M.jira(object, action, ...)
end

function M.view_issue(issue_id)
local config = require('jira.config').get_config()
if not issue_id then
if config.use_git_branch_issue_id then
issue_id = utils.get_issue_id_from_git_branch()
end
end

-- fallback to user input
if not issue_id then
vim.ui.input({
prompt = 'Issue ID: ',
}, function(id)
issue_id = id
end)
end

issue_id = issue_id or utils.get_issue_id()
if not issue_id then
print 'Missing issue id'
return
Expand Down Expand Up @@ -103,4 +91,27 @@ function M.view_issue(issue_id)
end)
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)
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('_', ' ')
api_client.transition_issue_name(issue_id, transition_name, function(err, response)
if err then
print('Error: ' .. err)
return
end
if response.code < 400 then
print('Transitioned issue ' .. issue_id .. ' to ' .. transition_name)
else
print('Non 200 response: ' .. response.code)
end
end)
end

return M
15 changes: 15 additions & 0 deletions lua/jira/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,24 @@ M.convert_adf_to_markdown = convert_adf_to_markdown
-- e.g. feature/ABC-1234
-- e.g. ABC-1234
M.get_issue_id_from_git_branch = function()
local config = require('jira.config').get_config()
if config.use_git_branch == false then
return nil
end
local branch = vim.fn.system 'git rev-parse --abbrev-ref HEAD'
local issue_id = string.match(branch, '([A-Z]+%-[0-9]+)')
return issue_id
end

M.get_issue_id = function(issue_id)
if issue_id then
return issue_id
end
issue_id = M.get_issue_id_from_git_branch()
if issue_id == nil then
issue_id = vim.fn.input 'Enter issue id: '
end
return issue_id
end

return M

0 comments on commit f0dfd5c

Please sign in to comment.