175 lines
5.5 KiB
Lua
175 lines
5.5 KiB
Lua
local config = require("gitea.config")
|
|
local auth = require("gitea.auth")
|
|
local curl = require("plenary.curl")
|
|
|
|
local M = {}
|
|
|
|
local function get_base_url()
|
|
return config.values.server_url
|
|
end
|
|
|
|
local function get_auth_header()
|
|
local token = auth.get_token()
|
|
if not token or token == "" then
|
|
error("[gitea.nvim] Missing Gitea token.")
|
|
end
|
|
return "token " .. token
|
|
end
|
|
|
|
local function request(method, endpoint, opts)
|
|
opts = opts or {}
|
|
local url = get_base_url() .. endpoint
|
|
local headers = opts.headers or {}
|
|
headers["Authorization"] = get_auth_header()
|
|
headers["Content-Type"] = "application/json"
|
|
local result = curl.request({
|
|
url = url,
|
|
method = method,
|
|
headers = headers,
|
|
timeout = 10000,
|
|
body = opts.body and vim.json.encode(opts.body) or nil,
|
|
query = opts.query,
|
|
})
|
|
return result
|
|
end
|
|
|
|
-- Issues
|
|
function M.list_issues(owner, repo, opts)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/issues", owner, repo)
|
|
local result = request("GET", endpoint, { query = opts })
|
|
if result and result.status == 200 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
function M.get_issue(owner, repo, number)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/issues/%d", owner, repo, number)
|
|
local result = request("GET", endpoint)
|
|
if result and result.status == 200 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
function M.create_issue(owner, repo, data)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/issues", owner, repo)
|
|
local result = request("POST", endpoint, { body = data })
|
|
if result and result.status == 201 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
function M.edit_issue(owner, repo, number, data)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/issues/%d", owner, repo, number)
|
|
local result = request("PATCH", endpoint, { body = data })
|
|
if result and result.status == 200 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
function M.close_issue(owner, repo, number)
|
|
return M.edit_issue(owner, repo, number, { state = "closed" })
|
|
end
|
|
|
|
function M.reopen_issue(owner, repo, number)
|
|
return M.edit_issue(owner, repo, number, { state = "open" })
|
|
end
|
|
|
|
function M.comment_issue(owner, repo, number, body)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/issues/%d/comments", owner, repo, number)
|
|
local result = request("POST", endpoint, { body = { body = body } })
|
|
if result and result.status == 201 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
-- ADDED: edit_issue_comment
|
|
-- Gitea supports: PATCH /repos/{owner}/{repo}/issues/comments/{id}
|
|
function M.edit_issue_comment(owner, repo, number, comment_id, body)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/issues/comments/%d", owner, repo, comment_id)
|
|
local result = request("PATCH", endpoint, { body = { body = body } })
|
|
if result and result.status == 200 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
-- ADDED: get_issue_comments
|
|
function M.get_issue_comments(owner, repo, number)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/issues/%d/comments", owner, repo, number)
|
|
local result = request("GET", endpoint)
|
|
if result and result.status == 200 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
-- PR
|
|
function M.list_pull_requests(owner, repo, opts)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/pulls", owner, repo)
|
|
local result = request("GET", endpoint, { query = opts })
|
|
if result and result.status == 200 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
function M.get_pull_request(owner, repo, number)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/pulls/%d", owner, repo, number)
|
|
local result = request("GET", endpoint)
|
|
if result and result.status == 200 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
function M.create_pull_request(owner, repo, data)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/pulls", owner, repo)
|
|
local result = request("POST", endpoint, { body = data })
|
|
if result and result.status == 201 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
function M.edit_pull_request(owner, repo, number, data)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/pulls/%d", owner, repo, number)
|
|
local result = request("PATCH", endpoint, { body = data })
|
|
if result and result.status == 200 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
function M.merge_pull_request(owner, repo, number, merge_style, merge_title, merge_message)
|
|
local endpoint = string.format("/api/v1/repos/%s/%s/pulls/%d/merge", owner, repo, number)
|
|
local result = request("POST", endpoint, {
|
|
body = {
|
|
Do = merge_style or "merge",
|
|
MergeTitleField = merge_title or "",
|
|
MergeMessageField = merge_message or "",
|
|
}
|
|
})
|
|
if result and result.status == 200 then
|
|
return vim.json.decode(result.body)
|
|
end
|
|
return nil, result and result.status
|
|
end
|
|
|
|
function M.close_pull_request(owner, repo, number)
|
|
return M.edit_pull_request(owner, repo, number, { state = "closed" })
|
|
end
|
|
|
|
function M.reopen_pull_request(owner, repo, number)
|
|
return M.edit_pull_request(owner, repo, number, { state = "open" })
|
|
end
|
|
|
|
function M.comment_pull_request(owner, repo, number, body)
|
|
return M.comment_issue(owner, repo, number, body)
|
|
end
|
|
|
|
return M |