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

chore: rewrite api client to use plenary. http dep is dead #22

Merged
merged 1 commit into from
May 25, 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
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ jobs:
test_interpreters: |
neovim-stable
neovim-nightly
dependencies: |
plenary.nvim
env:
LUAROCKS_API_KEY: ${{ secrets.LUAROCKS_API_KEY }}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions jira.nvim-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ description = {

dependencies = {
'lua >= 5.1, < 5.4',
'plenary.nvim',
}

source = {
Expand Down
84 changes: 34 additions & 50 deletions lua/jira/api_client.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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
88 changes: 40 additions & 48 deletions lua/jira/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Loading