Something went wrong. Try again.
Neovim plugin for working with Github pull request comments
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182---@class GhPrGithub---@field pr_data PrData|nillocal GhPrGithub = {}
---@param repo_owner string---@param repo_name string---@param pr_number integer---@return stringlocal function construct_gh_comments_url(repo_owner, repo_name, pr_number) local comment_url = { "https://api.github.com/repos/", repo_owner, "/", repo_name, "/pulls/", pr_number, "/comments", } return table.concat(comment_url, "")end
---Fetches PR data from GitHub using gh CLI---Populates M.pr_data with PR informationGhPrGithub.get_pr_data = function() local cmd = "gh pr view --json number,url,headRepositoryOwner,headRepository,state" local res = vim.fn.system(cmd) local pr_result = vim.json.decode(res) local pr_number = pr_result['number'] local repo_name = pr_result['headRepository']['name'] local repo_owner = pr_result['headRepositoryOwner']['login'] GhPrGithub.pr_data = { number = pr_number, repo_name = repo_name, repo_owner = repo_owner, state = pr_result['state'], url = pr_result['url'], comments_url = construct_gh_comments_url(repo_owner, repo_name, pr_number) }end
---Fetches PR comments from GitHub API---@param callback fun(comments_data: table<string, Comment[]>?) Callback with comments dataGhPrGithub.get_pr_comments = function(callback) if GhPrGithub.pr_data == nil then GhPrGithub.get_pr_data() end
local utils = require('gh-pr-comments.utils') utils.http_get(GhPrGithub.pr_data.comments_url, function(body, err) if err then utils.err_notify("HTTP error: " .. err) callback(nil) return end local res_json = vim.json.decode(body) local comments_data = {} for _, comment in pairs(res_json) do local fp = comment['path'] local c_data = {} c_data.body = comment['body'] c_data.line = comment['line'] c_data.original_line = comment['original_line'] if comments_data[fp] ~= nil then comments_data[fp][#comments_data[fp] + 1] = c_data else comments_data[fp] = { c_data } end end
-- ensure comments are sorted by line numbers in ascending order for _, items in pairs(comments_data) do table.sort(items, function(a, b) return a.line < b.line end) end
callback(comments_data) end)end
return GhPrGithub