Skip to content

Commit 26a6560

Browse files
authored
Merge pull request #32 from arran4/codex/add-support-for-self-hosted-gitlab-servers
Handle provider-specific GitServer defaults
2 parents 40273d9 + e4d1d5c commit 26a6560

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Configuration values can be supplied as environment variables, via a JSON config
7575
| `GBM_PROVIDER` | Git provider to use (`github` or `gitlab`). Defaults to `github`. |
7676
| `GBM_NAMESPACE` | Optional suffix added to the bookmarks repository name. |
7777
| `GBM_TITLE` | Overrides the page title shown in the browser. |
78-
| `GIT_SERVER` | Base URL for a self-hosted git provider, e.g. `https://gitlab.example.com` |
78+
| `GIT_SERVER` | Base URL for a self-hosted git provider, e.g. `https://gitlab.example.com`. Defaults to the public server for the selected provider. |
7979
| `GOBM_ENV_FILE` | Path to a file of `KEY=VALUE` pairs loaded before the environment. Defaults to `/etc/gobookmarks/gobookmarks.env`. |
8080
| `GOBM_CONFIG_FILE` | Path to the JSON config file. If unset the program uses `$XDG_CONFIG_HOME/gobookmarks/config.json` or `$HOME/.config/gobookmarks/config.json` for normal users and `/etc/gobookmarks/config.json` when run as root. |
8181

@@ -86,7 +86,7 @@ Use `--config <path>` or set `GOBM_CONFIG_FILE` to control which configuration f
8686

8787
The `--provider` command line flag or `GBM_PROVIDER` environment variable selects which git provider to use. By default the binary includes both GitHub and GitLab support. Use build tags `nogithub` or `nogitlab` to exclude either provider when building from source. If you specify an unknown provider the program will exit with an error listing the available options.
8888
The `--title` flag or `GBM_TITLE` environment variable sets the browser page title.
89-
Use `--git-server` or `GIT_SERVER` to point the application at a self-hosted git provider.
89+
Use `--git-server` or `GIT_SERVER` to override the base URL of the git provider (defaults to the provider's public server).
9090

9191
Running `gobookmarks --version` will print the version information along with the list of compiled-in providers.
9292
Use `--dump-config` to print the final configuration after merging the environment,

provider.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Provider interface {
3636
GetBookmarks(ctx context.Context, user, ref string, token *oauth2.Token) (string, string, error)
3737
UpdateBookmarks(ctx context.Context, user string, token *oauth2.Token, sourceRef, branch, text, expectSHA string) error
3838
CreateBookmarks(ctx context.Context, user string, token *oauth2.Token, branch, text string) error
39+
DefaultServer() string
3940
}
4041

4142
var providers = map[string]Provider{}
@@ -58,7 +59,14 @@ var ActiveProvider Provider
5859
// It returns true if the provider exists.
5960
func SetProviderByName(name string) bool {
6061
if p, ok := providers[name]; ok {
62+
prevDefault := ""
63+
if ActiveProvider != nil {
64+
prevDefault = ActiveProvider.DefaultServer()
65+
}
6166
ActiveProvider = p
67+
if GitServer == "" || GitServer == prevDefault {
68+
GitServer = p.DefaultServer()
69+
}
6270
return true
6371
}
6472
return false
@@ -67,5 +75,8 @@ func SetProviderByName(name string) bool {
6775
func init() {
6876
if p, ok := providers["github"]; ok {
6977
ActiveProvider = p
78+
if GitServer == "" {
79+
GitServer = p.DefaultServer()
80+
}
7081
}
7182
}

provider_github.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"context"
77
"encoding/base64"
88
"fmt"
9+
"strings"
910

1011
"github.com/google/go-github/v55/github"
1112
"golang.org/x/oauth2"
12-
"golang.org/x/oauth2/endpoints"
1313
)
1414

1515
// GitHubProvider implements Provider for GitHub.
@@ -19,18 +19,36 @@ func init() { RegisterProvider(GitHubProvider{}) }
1919

2020
func (GitHubProvider) Name() string { return "github" }
2121

22+
func (GitHubProvider) DefaultServer() string { return "https://github.com" }
23+
2224
func (GitHubProvider) OAuth2Config(clientID, clientSecret, redirectURL string) *oauth2.Config {
25+
server := strings.TrimRight(GitServer, "/")
26+
if server == "" {
27+
server = "https://github.com"
28+
}
2329
return &oauth2.Config{
2430
ClientID: clientID,
2531
ClientSecret: clientSecret,
2632
RedirectURL: redirectURL,
2733
Scopes: []string{"repo", "read:user", "user:email"},
28-
Endpoint: endpoints.GitHub,
34+
Endpoint: oauth2.Endpoint{
35+
AuthURL: server + "/login/oauth/authorize",
36+
TokenURL: server + "/login/oauth/access_token",
37+
},
2938
}
3039
}
3140

3241
func (GitHubProvider) client(ctx context.Context, token *oauth2.Token) *github.Client {
33-
return github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(token)))
42+
httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(token))
43+
server := strings.TrimRight(GitServer, "/")
44+
if server == "" || server == "https://github.com" {
45+
return github.NewClient(httpClient)
46+
}
47+
c, err := github.NewEnterpriseClient(server+"/api/v3/", server+"/upload/v3/", httpClient)
48+
if err != nil {
49+
return github.NewClient(httpClient)
50+
}
51+
return c
3452
}
3553

3654
func (p GitHubProvider) CurrentUser(ctx context.Context, token *oauth2.Token) (*User, error) {

provider_gitlab.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func init() { RegisterProvider(GitLabProvider{}) }
2222

2323
func (GitLabProvider) Name() string { return "gitlab" }
2424

25+
func (GitLabProvider) DefaultServer() string { return "https://gitlab.com" }
26+
2527
func (GitLabProvider) OAuth2Config(clientID, clientSecret, redirectURL string) *oauth2.Config {
2628
server := strings.TrimRight(GitServer, "/")
2729
if server == "" {

settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ var (
44
UseCssColumns bool
55
Namespace string
66
SiteTitle string
7-
GitServer string = "https://gitlab.com"
7+
GitServer string
88
)

0 commit comments

Comments
 (0)