initial commit
This commit is contained in:
168
lua/gitea/api.lua
Normal file
168
lua/gitea/api.lua
Normal file
@@ -0,0 +1,168 @@
|
||||
local config = require("gitea.config")
|
||||
local auth = require("gitea.auth")
|
||||
local curl = require("plenary.curl")
|
||||
|
||||
local M = {}
|
||||
|
||||
local function get_base_url()
|
||||
local server = config.values.server_url
|
||||
if not server or server == "" then
|
||||
-- fallback
|
||||
server = "https://gitea.example.com"
|
||||
end
|
||||
return server
|
||||
end
|
||||
|
||||
local function get_auth_header()
|
||||
local token = auth.get_token()
|
||||
if not token or token == "" then
|
||||
error("[gitea.nvim] Missing Gitea token. Please run :Gitea again or restart after entering a 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 = 5000, -- we can also read config.values.timeout
|
||||
body = opts.body and vim.json.encode(opts.body) or nil,
|
||||
})
|
||||
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)
|
||||
-- data = { title = "", body = "", labels = {"bug"}, etc. }
|
||||
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)
|
||||
-- Gitea: state -> "closed"
|
||||
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
|
||||
|
||||
-------------------------------------------------------
|
||||
-- PULL REQUESTS
|
||||
-------------------------------------------------------
|
||||
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)
|
||||
-- data = { head = "branch", base = "master", title = "My PR", body = "..." }
|
||||
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)
|
||||
-- merge_style: "merge"|"rebase"|"squash" (in Gitea it's "merge"|"rebase"|"rebase-merge"|"squash" -
|
||||
-- see Gitea docs for specifics; we can map the user’s choice to Gitea’s).
|
||||
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", -- e.g. "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)
|
||||
-- same endpoint as issues for Gitea. A PR is an issue with is_pull = true
|
||||
return M.comment_issue(owner, repo, number, body)
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user