From b4a939b78cb8295d84042e14007536afb265e11b Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Sun, 2 Aug 2015 16:55:14 -0400 Subject: [PATCH] Support for GitHub enterprise Completely untested. --- README.markdown | 5 +++++ plugin/rhubarb.vim | 35 +++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/README.markdown b/README.markdown index 7257191..777f512 100644 --- a/README.markdown +++ b/README.markdown @@ -38,6 +38,11 @@ your netrc if you fetch it by hand: echo 'machine api.github.com login password x-oauth-basic' \ >> ~/.netrc +If you are using GitHub Enterprise, repeat those steps for each domain (omit +the `api.` portion). You'll also need to tell Rhubarb the root URLs: + + let g:github_enterprise_urls = ['https://example.com'] + ## FAQ > How do I turn off that preview window that shows the issue body? diff --git a/plugin/rhubarb.vim b/plugin/rhubarb.vim index 2c898ab..df5b8b2 100644 --- a/plugin/rhubarb.vim +++ b/plugin/rhubarb.vim @@ -32,23 +32,32 @@ function! s:shellesc(arg) abort endif endfunction +function! s:homepage_for_url(url) abort + let domain_pattern = 'github\.com' + let domains = get(g:, 'github_enterprise_urls', get(g:, 'fugitive_github_domains', [])) + call map(copy(domains), 'substitute(v:val, "/$", "", "")') + for domain in domains + let domain_pattern .= '\|' . escape(split(domain, '://')[-1], '.') + endfor + let repo = fugitive#buffer().repo() + let base = matchstr(a:url, '^\%(https\=://\|git://\|git@\)\=\zs\('.domain_pattern.'\)[/:].\{-\}\ze\%(\.git\)\=$') + if index(domains, 'http://' . matchstr(base, '^[^:/]*')) >= 0 + return 'http://' . tr(base, ':', '/') + elseif !empty(base) + return 'https://' . tr(base, ':', '/') + endif + call s:throw('not a GitHub repository: '.a:url) +endfunction + function! s:repo_homepage() abort if !exists('b:rhubarb_homepage') let repo = fugitive#buffer().repo() let url = repo.config('remote.origin.url') - let name = matchstr(url, 'github\.com[:/]\zs[^/]*/[^/]\{-\}\ze\%(\.git\)\=$') - if empty(name) - call s:throw('origin is not a GitHub repository: '.url) - endif - let b:rhubarb_homepage = 'https://github.com/'.name + let b:rhubarb_homepage = s:homepage_for_url(url) endif return b:rhubarb_homepage endfunction -function! s:repo_name() abort - return matchstr(s:repo_homepage(), '://[^/]*/\zs.*') -endfunction - " }}}1 " HTTP {{{1 @@ -143,7 +152,13 @@ function! rhubarb#request(path, ...) abort endfunction function! rhubarb#repo_request(...) abort - return rhubarb#request('/repos/' . s:repo_name() . (a:0 && a:1 !=# '' ? '/' . a:1 : ''), a:0 > 1 ? a:2 : {}) + let base = s:repo_homepage() + if base =~# '//github\.com/' + let base = substitute(base, '//github\.com/', '//api.github.com/repos/', '') + else + let base = substitute(base, '//[^/]\+/\zs', 'api/v3/repos/', '') + endif + return rhubarb#request(base . (a:0 && a:1 !=# '' ? '/' . a:1 : ''), a:0 > 1 ? a:2 : {}) endfunction " }}}1