Skip to content

Commit

Permalink
feat: add octo review browse to browse a pull request
Browse files Browse the repository at this point in the history
Add a new command `Octo review browse` which opens the review layout for
a PR but does not start a review.

This is handy when you want to look over the changes in a PR but do not
necessary want to review it.

When you are in a Review layout initiated by `Octo review browse` you
can intuitively convert it into an active review session with `Octo
review start`.

Its invalid to call `Octo review browse` after `Octo review start` and
the user must either submit or discard the active review to enter the
browse state again.

Closes: pwntester#829

Signed-off-by: ldelossa <[email protected]>
  • Loading branch information
ldelossa committed Jan 29, 2025
1 parent 876e6f2 commit 18cdd1a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ function M.setup()
end,
},
review = {
browse = function()
reviews.browse_review()
end,
start = function()
reviews.start_review()
end,
Expand Down
58 changes: 55 additions & 3 deletions lua/octo/reviews/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ function Review:create(callback)
}
end

-- Get review threads without start a review.
function Review:populate_threads(callback)
local query =
graphql("review_threads_query", self.pull_request.owner, self.pull_request.name, self.pull_request.number)
-- print query to file /tmp/query
local file = io.open("/tmp/query", "w")
file:write(query)
file:close()

gh.run {
args = { "api", "graphql", "-f", string.format("query=%s", query) },
cb = function(output, stderr)
if stderr and not utils.is_blank(stderr) then
utils.error(stderr)
elseif output then
local resp = vim.fn.json_decode(output)
callback(resp)
end
end,
}
end

function Review:browse()
self:populate_threads(function(resp)
local threads = resp.data.repository.pullRequest.reviewThreads.nodes
self:update_threads(threads)
self:initiate()
end)
end

-- Starts a new review
function Review:start()
self:create(function(resp)
Expand Down Expand Up @@ -573,12 +603,34 @@ local function get_pr_from_buffer_or_current_branch(cb)
cb(pull_request)
else
pull_request = utils.get_pull_request_for_current_branch(cb)
cb(pull_request)
end
end

function M.browse_review()
local current_review = M.get_current_review()

if current_review and current_review.id ~= -1 then
vim.notify("Cannot browse when a review has been started", vim.log.levels.ERROR)
return
end

get_pr_from_buffer_or_current_branch(function(pull_request)
current_review = Review:new(pull_request)
current_review:browse()
end)
end

function M.start_review()
-- its possible we are already browsing a review with 'Octo review browse'
local current_review = M.get_current_review()
if current_review then
current_review:start()
return
end

get_pr_from_buffer_or_current_branch(function(pull_request)
local current_review = Review:new(pull_request)
current_review = Review:new(pull_request)
current_review:start()
end)
end
Expand All @@ -599,7 +651,7 @@ end

function M.discard_review()
local current_review = M.get_current_review()
if current_review then
if current_review and current_review.id ~= -1 then
current_review:discard()
else
utils.error "Please start or resume a review first"
Expand All @@ -608,7 +660,7 @@ end

function M.submit_review()
local current_review = M.get_current_review()
if current_review then
if current_review and current_review.id ~= -1 then
current_review:collect_submit_info()
else
utils.error "Please start or resume a review first"
Expand Down

0 comments on commit 18cdd1a

Please sign in to comment.