Skip to content

Instantly share code, notes, and snippets.

@cherryramatisdev
Last active October 12, 2023 23:55
Show Gist options
  • Save cherryramatisdev/9e1ed6c7d7bf406bbee6a4d929fb681d to your computer and use it in GitHub Desktop.
Save cherryramatisdev/9e1ed6c7d7bf406bbee6a4d929fb681d to your computer and use it in GitHub Desktop.
Simple module to manage pull request creation within neovim
local M = {}
---@return 'main' | 'develop'
local function decide_base_branch()
if vim.fn.system 'git branch --contains develop' == 'error: malformed object name develop' then
return 'main'
end
return 'develop'
end
---@return string
local function return_project_root()
return vim.fn.substitute(vim.fn.system 'git rev-parse --show-toplevel', '\n', '', '')
end
function M.open_pr_template()
local template_path = return_project_root() .. '/.github/pull_request_template.md'
vim.cmd('tabe ' .. vim.fn.tempname())
if vim.fn.filereadable(template_path) then
vim.fn.setline(1, vim.fn.readfile(template_path))
vim.bo.filetype = 'markdown'
end
end
function M.create_pr()
local filetype = vim.fn.expand '%'
local base_branch = decide_base_branch()
local reviewers = vim.fn.getenv 'REVIEWERS'
local gh_pr_cmd = string.format('gh pr -a @me create -B %s -r %s -F "%s"', base_branch, reviewers, filetype)
vim.cmd('!tmux neww ' .. gh_pr_cmd)
end
function M.open_pr()
local cmd = 'gh pr view -w'
vim.cmd('!' .. cmd)
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment