Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command similar to :GBrowse, that will open a Github page with existing pull request for the current branch? #2158

Open
vitaly-zdanevich opened this issue Apr 26, 2023 · 2 comments

Comments

@vitaly-zdanevich
Copy link

Looks like currently there is no such functionality?

@vitaly-zdanevich vitaly-zdanevich changed the title Add command similar to :GBrowse, that will open a PR for the current branch? Add command similar to :GBrowse, that will open a Github page with existing pull request for the current branch? Apr 26, 2023
@benelan
Copy link
Sponsor

benelan commented Jul 15, 2023

That functionality is built into the gh cli. You could make your own vim command, e.g.

if executable("gh")
    command! -nargs=* GBrowsePR exe "!gh pr view --web <args>"
endif

@benelan
Copy link
Sponsor

benelan commented Jul 16, 2023

Here it is with fugitive's object completion for specifying the branch. It will still open the PR for the current branch without an argument. If attached to tmux, it will open the PR in a new window for people who use CLI browsers.

function! s:warn(msg)
  echohl WarningMsg | echom a:msg | echohl None
endfunction

function! s:browse_github_pr(args) abort
    if !executable("gh")
        return s:warn("gh cli required: https://cli.github.com/")
    endif

    let s:gh_pr_cmd = (exists("$TMUX")
            \   ? "!tmux new-window -n github_pr "
            \   : "!"
            \ ) . "gh pr view --web --"

    execute s:gh_pr_cmd a:args
endfunction

command! -nargs=? -complete=customlist,fugitive#CompleteObject 
            \ GBrowsePR call s:browse_github_pr(<q-args>)

Edit

Here it is with GBrowse's bang to copy functionality:

function! s:warn(msg)
  echohl WarningMsg | echom a:msg | echohl None
endfunction

function! s:browse_github_pr(bang, args) abort
    if !executable("gh")
        return s:warn("gh cli required: https://cli.github.com")
    endif

    let s:url = substitute(
                \   system("gh pr view --json url --jq '.url' -- " . a:args),
                \   '\n\+$', '', ''
                \ )
    echom s:url

    if a:bang && has('clipboard')
        let @+ = s:url
    else
        if !exists('g:loaded_netrw')
            runtime! autoload/netrw.vim
        endif

        if exists('*netrw#BrowseX')
            call netrw#BrowseX(s:url, 0)
        else
            return s:warn("netrw required to open github pull request")
        endif
    endif
endfunction

command! -bang -nargs=? -complete=customlist,fugitive#CompleteObject
            \ GBrowsePR call s:browse_github_pr(<bang>0, <q-args>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants