diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8fc3657..76cb5a2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,5 +18,7 @@ jobs: test_interpreters: | neovim-stable neovim-nightly + dependencies: | + plenary.nvim env: LUAROCKS_API_KEY: ${{ secrets.LUAROCKS_API_KEY }} diff --git a/README.md b/README.md index 36b13de..6ffb1c3 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Using [packer.nvim](https://github.com/wbthomason/packer.nvim) use { 'kid-icarus/jira.nvim', requires = { - 'jcdickinson/http.nvim', + 'nvim-lua/plenary.nvim', } config = function () require'jira'.setup() -- see configuration section @@ -44,7 +44,7 @@ Using [lazygit.nvim](https://github.com/folke/lazy.nvim) { 'kid-icarus/jira.nvim', dependencies = { - 'jcdickinson/http.nvim', + 'nvim-lua/plenary.nvim', }, opts = {}, -- see configuration section } diff --git a/jira.nvim-scm-1.rockspec b/jira.nvim-scm-1.rockspec index cf76b8c..86e505c 100644 --- a/jira.nvim-scm-1.rockspec +++ b/jira.nvim-scm-1.rockspec @@ -15,6 +15,7 @@ description = { dependencies = { 'lua >= 5.1, < 5.4', + 'plenary.nvim', } source = { diff --git a/lua/jira/api_client.lua b/lua/jira/api_client.lua index cb09870..28bbf2a 100644 --- a/lua/jira/api_client.lua +++ b/lua/jira/api_client.lua @@ -1,6 +1,6 @@ -local http = require 'http' local utils = require 'jira.utils' local config = require 'jira.config' +local curl = require 'plenary.curl' local M = {} @@ -17,76 +17,60 @@ local get_base_url = function() return 'https://' .. jira_api_config.domain .. '/rest/api/3' end -M.get_issue = function(issue_id, callback) +M.get_issue = function(issue_id) local url = get_base_url() .. '/issue/' .. issue_id - http.request { - http.methods.GET, - url, - nil, - nil, + return curl.get(url, { headers = get_auth_headers(), - callback = callback, - } + }) end -M.get_transitions = function(issue_id, callback) +M.get_transitions = function(issue_id) local url = get_base_url() .. '/issue/' .. issue_id .. '/transitions' - http.request { - http.methods.GET, - url, - nil, - nil, + return curl.get(url, { 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 +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 + local transition_id + for _, transition in ipairs(transitions) do + if transition.name == transition_name then + transition_id = transition.id + break 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 + assert(transition_id, 'Transition not found: ' .. transition_name) + return M.transition_issue(issue_id, transition_id) end -- Transitions the issue to the given transition id -M.transition_issue = function(issue_id, transition_id, callback) +M.transition_issue = function(issue_id, transition_id) + assert(issue_id, 'Missing issue id') + assert(transition_id, 'Missing issue id') 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, + + return curl.post(url, { headers = get_auth_headers(), - callback = callback, - } + body = body, + }) end return M diff --git a/lua/jira/commands.lua b/lua/jira/commands.lua index f9555a9..61eccd2 100644 --- a/lua/jira/commands.lua +++ b/lua/jira/commands.lua @@ -51,44 +51,39 @@ function M.view_issue(issue_id) return end - api_client.get_issue(issue_id, function(err, response) - if err then - print('Error: ' .. err) - return - end - if response.code < 400 then - vim.schedule(function() - local data = vim.fn.json_decode(response.body) - local summary = data.fields.summary - local desc = utils.convert_adf_to_markdown(data.fields.description) - local buf = vim.api.nvim_create_buf(true, false) - vim.api.nvim_buf_set_option(buf, 'readonly', false) - vim.api.nvim_buf_set_option(buf, 'modifiable', true) - vim.api.nvim_buf_set_option(buf, 'buftype', 'nowrite') - vim.api.nvim_buf_set_lines(buf, 0, -1, true, { '# ' .. summary, '' }) - vim.api.nvim_buf_set_lines(buf, -1, -1, true, vim.split(desc, '\n')) - vim.api.nvim_buf_set_lines(buf, -1, -1, true, { '', '## Comments', '' }) - if data.fields.comment.total == 0 then - vim.api.nvim_buf_set_lines(buf, -1, -1, true, { 'No comments', '' }) - else - for _, comment in ipairs(data.fields.comment.comments) do - local author = comment.author.displayName - local timestamp = comment.updated - local body = utils.convert_adf_to_markdown(comment.body) - vim.api.nvim_buf_set_lines(buf, -1, -1, true, { '# ' .. author .. ' ' .. timestamp, '' }) - vim.api.nvim_buf_set_lines(buf, -1, -1, true, vim.split(body, '\n')) - end + local response = api_client.get_issue(issue_id) + if response.status < 400 then + vim.schedule(function() + local data = vim.fn.json_decode(response.body) + local summary = data.fields.summary + local desc = utils.convert_adf_to_markdown(data.fields.description) + local buf = vim.api.nvim_create_buf(true, false) + vim.api.nvim_buf_set_option(buf, 'readonly', false) + vim.api.nvim_buf_set_option(buf, 'modifiable', true) + vim.api.nvim_buf_set_option(buf, 'buftype', 'nowrite') + vim.api.nvim_buf_set_lines(buf, 0, -1, true, { '# ' .. summary, '' }) + vim.api.nvim_buf_set_lines(buf, -1, -1, true, vim.split(desc, '\n')) + vim.api.nvim_buf_set_lines(buf, -1, -1, true, { '', '## Comments', '' }) + if data.fields.comment.total == 0 then + vim.api.nvim_buf_set_lines(buf, -1, -1, true, { 'No comments', '' }) + else + for _, comment in ipairs(data.fields.comment.comments) do + local author = comment.author.displayName + local timestamp = comment.updated + local body = utils.convert_adf_to_markdown(comment.body) + vim.api.nvim_buf_set_lines(buf, -1, -1, true, { '# ' .. author .. ' ' .. timestamp, '' }) + vim.api.nvim_buf_set_lines(buf, -1, -1, true, vim.split(body, '\n')) end - vim.api.nvim_buf_set_option(buf, 'modifiable', false) - vim.api.nvim_buf_set_option(buf, 'filetype', 'markdown') - vim.cmd 'vsplit' - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_buf(win, buf) - end) - else - print('Non 200 response: ' .. response.code) - end - end) + end + vim.api.nvim_buf_set_option(buf, 'modifiable', false) + vim.api.nvim_buf_set_option(buf, 'filetype', 'markdown') + vim.cmd 'vsplit' + local win = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_buf(win, buf) + end) + else + print('Non 200 response: ' .. response.code) + end end -- @param issue_id string - the id of the issue to transition @@ -101,17 +96,14 @@ function M.transition_issue_name(issue_id, transition_name) 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) + local response = api_client.transition_issue_name(issue_id, transition_name) + if response and (response.exit ~= 0 or response.status ~= 204) then + vim.print 'Error making request' + end + if response and response.status == 204 then + vim.print 'Transitioned issue' + else + end end return M