Neovim: Instantly Find The PR That Updated a Line in GitHub

Neovim: Instantly Find The PR That Updated a Line in GitHub
Photo by Marc-Olivier Jodoin / Unsplash

The GitLens extension in vscode makes it easy to get extra blame info for each line like author, associated PR, file diff, previews, etc. You can replicate most of its functionality in neovim via the gitsigns.nvim plugin except being able to jump to a line's associated PR.

I managed to do just that by leveraging the gh cli from GitHub and a bunch of awk commands. Here's a snippet you can copy-paste:

-- Searches for the PR that last updated the current line in github
-- Depends on the `gh` cli
vim.keymap.set('n', '<leader>pr', function()
  local lineNum = vim.api.nvim__buf_stats(0).current_lnum
  local fileName = vim.fn.expand('%')
  local commitHashCommand = 'git blame -L' .. lineNum .. ',' .. lineNum .. ' ' .. fileName .. " | awk '{print $1}'"

  vim.fn.system('gh search prs' ..
  ' `' .. commitHashCommand .. '`' .. " | awk '{print $2}' | xargs -I _ gh pr view --web _")
end, { desc = 'Search for PR in github' })
Neovim keymap for going to associated PR

You'll be able to magically go to a the PR that edited a line by hitting <leader>pr.