Repository navigation
feat: GitHub Enterprise Server and GHE.com #29
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/devblac/prw/internal/config" | ||
| "github.com/devblac/prw/internal/forge" | ||
| "github.com/devblac/prw/internal/github" | ||
| ) | ||
|
|
||
| func TestClientFor_Enterprise(t *testing.T) { | ||
| var gotPath, gotAuth string | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| gotPath, gotAuth = r.URL.Path, r.Header.Get("Authorization") | ||
| fmt.Fprint(w, `{"number": 9, "title": "Add SSO", "state": "open", "head": {"sha": "abc"}}`) | ||
| })) | ||
| defer srv.Close() | ||
| old := newEnterpriseClient | ||
| newEnterpriseClient = func(webURL, token string) (*github.Client, error) { | ||
| c, err := github.NewEnterpriseClient(webURL, token) | ||
| if err == nil { | ||
| c.BaseURL = srv.URL + "/api/v3" | ||
| } | ||
| return c, err | ||
| } | ||
| t.Cleanup(func() { newEnterpriseClient = old }) | ||
| t.Setenv("GH_ENTERPRISE_TOKEN", "ghe-token") | ||
| t.Setenv("GITHUB_ENTERPRISE_TOKEN", "") | ||
|
|
||
| ref, err := forge.ParsePRURL("https://github.acme.com/team/api/pull/9") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := clientFor(&config.Config{}, ref.Host); err == nil || !strings.Contains(err.Error(), "github_enterprise_url") { | ||
| t.Errorf("unconfigured host: err = %v", err) | ||
| } | ||
| if _, err := clientFor(&config.Config{GitHubEnterpriseURL: "https://other.example.com"}, ref.Host); err == nil { | ||
| t.Error("a host other than the configured one must be refused") | ||
| } | ||
|
|
||
| cfg := &config.Config{GitHubEnterpriseURL: "https://github.acme.com"} | ||
| client, err := clientFor(cfg, ref.Host) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| pr, err := client.GetPullRequest(context.Background(), ref.Owner, ref.Repo, ref.Number) | ||
| if err != nil || pr.Title != "Add SSO" { | ||
| t.Fatalf("pr = %+v, %v", pr, err) | ||
| } | ||
| if gotPath != "/api/v3/repos/team/api/pulls/9" || gotAuth != "Bearer ghe-token" { | ||
| t.Errorf("request to %s with %q", gotPath, gotAuth) | ||
| } | ||
|
|
||
| t.Setenv("GH_ENTERPRISE_TOKEN", "") | ||
| old2 := config.GHAuthTokenForHost | ||
| config.GHAuthTokenForHost = func(string) (string, error) { return "", fmt.Errorf("not logged in") } | ||
| t.Cleanup(func() { config.GHAuthTokenForHost = old2 }) | ||
| if _, err := clientFor(cfg, ref.Host); err == nil || !strings.Contains(err.Error(), "gh auth login --hostname github.acme.com") { | ||
| t.Errorf("missing token: err = %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestConfigSet_EnterpriseURL(t *testing.T) { | ||
| cfg := &config.Config{} | ||
| if err := applyConfigSet(cfg, "github_enterprise_url", "https://github.acme.com/"); err != nil || cfg.GitHubEnterpriseURL != "https://github.acme.com" { | ||
| t.Errorf("set: %v, %q", err, cfg.GitHubEnterpriseURL) | ||
| } | ||
| for _, bad := range []string{"https://github.com", "bitbucket.org", "ftp://x.example.com"} { | ||
| if err := applyConfigSet(cfg, "github_enterprise_url", bad); err == nil { | ||
| t.Errorf("github_enterprise_url %q should be refused", bad) | ||
| } | ||
| } | ||
| if err := applyConfigUnset(cfg, "github_enterprise_url"); err != nil || cfg.GitHubEnterpriseURL != "" { | ||
| t.Errorf("unset: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestNeedsGitHub(t *testing.T) { | ||
| t.Setenv("BITBUCKET_TOKEN", "") | ||
| t.Setenv("BITBUCKET_EMAIL", "") | ||
| mine := []string{config.AutoWatchMine} | ||
| ghPR := []config.WatchedPR{{Owner: "o", Repo: "r", Number: 1}} | ||
| bbPR := []config.WatchedPR{{Host: forge.Bitbucket, Owner: "o", Repo: "r", Number: 1}} | ||
| ghe := "https://github.acme.com" | ||
|
|
||
| for _, tt := range []struct { | ||
| name string | ||
| cfg config.Config | ||
| want bool | ||
| }{ | ||
| {"nothing configured", config.Config{}, true}, | ||
| {"auto-watch with no other account", config.Config{AutoWatch: mine}, true}, | ||
| {"github.com PR always needs it", config.Config{WatchedPRs: ghPR, BitbucketToken: "t"}, true}, | ||
| {"only Bitbucket PRs", config.Config{WatchedPRs: bbPR}, false}, | ||
| {"Bitbucket auto-watch, empty list", config.Config{AutoWatch: mine, BitbucketToken: "t"}, false}, | ||
| {"Enterprise auto-watch, empty list", config.Config{AutoWatch: mine, GitHubEnterpriseURL: ghe}, false}, | ||
| {"Enterprise account, empty list", config.Config{GitHubEnterpriseURL: ghe}, false}, | ||
| } { | ||
| cfg := tt.cfg | ||
| if got := needsGitHub(&cfg); got != tt.want { | ||
| t.Errorf("%s: needsGitHub = %v, want %v", tt.name, got, tt.want) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a configured Enterprise instance, this finder calls
github.Client.SearchPullRequests, but that method still decodes each result usinggithub.ParsePRURL(internal/github/client.go:403), whose regex only acceptsgithub.com. Consequently, results with URLs such ashttps://github.acme.com/team/api/pull/9are silently skipped, so Enterprise auto-watch never discovers PRs. The new dashboard source uses the same method and likewise displays no Enterprise PRs. Update the search-result decoder to accept Enterprise URLs as well.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a real bug: the search decoder only accepted github.com links, so Enterprise auto-watch and the dashboard found nothing. Results are now decoded with
forge.ParsePRURL, which accepts Enterprise hosts. Fixed in 2707b3b, with a test of an Enterprise search result.