Skip to content

Commit e52e391

Browse files
authored
Merge branch 'main' into codex/improve-error-logging-and-reporting
2 parents 4d8e587 + 26a6560 commit e52e391

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
@@ -7,10 +7,10 @@ import (
77
"encoding/base64"
88
"fmt"
99
"log"
10+
"strings"
1011

1112
"github.com/google/go-github/v55/github"
1213
"golang.org/x/oauth2"
13-
"golang.org/x/oauth2/endpoints"
1414
)
1515

1616
// GitHubProvider implements Provider for GitHub.
@@ -20,18 +20,36 @@ func init() { RegisterProvider(GitHubProvider{}) }
2020

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

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

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

3755
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
@@ -24,6 +24,8 @@ func init() { RegisterProvider(GitLabProvider{}) }
2424

2525
func (GitLabProvider) Name() string { return "gitlab" }
2626

27+
func (GitLabProvider) DefaultServer() string { return "https://gitlab.com" }
28+
2729
func (GitLabProvider) OAuth2Config(clientID, clientSecret, redirectURL string) *oauth2.Config {
2830
server := strings.TrimRight(GitServer, "/")
2931
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)