Skip to content

Commit 0f67464

Browse files
authored
Merge pull request #6 from github/skarim/ghes-custom-hostname-support
support custom hostnames
2 parents e7fdf6d + 865b470 commit 0f67464

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

cmd/utils_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ func TestParsePRURL(t *testing.T) {
249249
{"standard URL", "https://github.com/owner/repo/pull/42", 42, true},
250250
{"with trailing slash", "https://github.com/owner/repo/pull/42/", 42, true},
251251
{"with files tab", "https://github.com/owner/repo/pull/42/files", 42, true},
252+
{"GHES URL", "https://ghes.example.com/owner/repo/pull/99", 99, true},
253+
{"GHES URL with trailing slash", "https://ghes.example.com/owner/repo/pull/7/", 7, true},
252254
{"not a PR URL", "https://github.com/owner/repo/issues/42", 0, false},
253255
{"plain number", "42", 0, false},
254256
{"branch name", "feat-1", 0, false},

cmd/view.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
tea "github.com/charmbracelet/bubbletea"
1313
"github.com/github/gh-stack/internal/config"
1414
"github.com/github/gh-stack/internal/git"
15+
ghapi "github.com/github/gh-stack/internal/github"
1516
"github.com/github/gh-stack/internal/stack"
1617
"github.com/github/gh-stack/internal/tui/stackview"
1718
"github.com/spf13/cobra"
@@ -65,8 +66,9 @@ func runView(cfg *config.Config, opts *viewOptions) error {
6566
}
6667

6768
func viewShort(cfg *config.Config, s *stack.Stack, currentBranch string) error {
68-
var repoOwner, repoName string
69+
var repoHost, repoOwner, repoName string
6970
if repo, err := cfg.Repo(); err == nil {
71+
repoHost = repo.Host
7072
repoOwner = repo.Owner
7173
repoName = repo.Name
7274
}
@@ -81,7 +83,7 @@ func viewShort(cfg *config.Config, s *stack.Stack, currentBranch string) error {
8183
}
8284

8385
indicator := branchStatusIndicator(cfg, s, b)
84-
prSuffix := shortPRSuffix(cfg, b, repoOwner, repoName)
86+
prSuffix := shortPRSuffix(cfg, b, repoHost, repoOwner, repoName)
8587
if b.Branch == currentBranch {
8688
cfg.Outf("» %s%s%s %s\n", cfg.ColorBold(b.Branch), indicator, prSuffix, cfg.ColorCyan("(current)"))
8789
} else if merged {
@@ -187,13 +189,13 @@ func viewJSON(cfg *config.Config, s *stack.Stack, currentBranch string) error {
187189
return err
188190
}
189191

190-
func shortPRSuffix(cfg *config.Config, b stack.BranchRef, owner, repo string) string {
192+
func shortPRSuffix(cfg *config.Config, b stack.BranchRef, host, owner, repo string) string {
191193
if b.PullRequest == nil || b.PullRequest.Number == 0 {
192194
return ""
193195
}
194196
url := b.PullRequest.URL
195197
if url == "" && owner != "" && repo != "" {
196-
url = fmt.Sprintf("https://github.com/%s/%s/pull/%d", owner, repo, b.PullRequest.Number)
198+
url = ghapi.PRURL(host, owner, repo, b.PullRequest.Number)
197199
}
198200
prNum := cfg.PRLink(b.PullRequest.Number, url)
199201
colorFn := cfg.ColorSuccess // green for open
@@ -251,9 +253,10 @@ func viewFullTUI(cfg *config.Config, s *stack.Stack, currentBranch string) error
251253
func viewFullStatic(cfg *config.Config, s *stack.Stack, currentBranch string) error {
252254
client, clientErr := cfg.GitHubClient()
253255

254-
var repoOwner, repoName string
256+
var repoHost, repoOwner, repoName string
255257
repo, repoErr := cfg.Repo()
256258
if repoErr == nil {
259+
repoHost = repo.Host
257260
repoOwner = repo.Owner
258261
repoName = repo.Name
259262
}
@@ -285,7 +288,7 @@ func viewFullStatic(cfg *config.Config, s *stack.Stack, currentBranch string) er
285288
} else if clientErr == nil && repoErr == nil {
286289
pr, err := client.FindPRForBranch(b.Branch)
287290
if err == nil && pr != nil {
288-
prInfo = fmt.Sprintf(" https://github.com/%s/%s/pull/%d", repoOwner, repoName, pr.Number)
291+
prInfo = " " + ghapi.PRURL(repoHost, repoOwner, repoName, pr.Number)
289292
}
290293
}
291294

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,5 @@ func (c *Config) GitHubClient() (ghapi.ClientOps, error) {
121121
if err != nil {
122122
return nil, fmt.Errorf("determining repository: %w", err)
123123
}
124-
return ghapi.NewClient(repo.Owner, repo.Name)
124+
return ghapi.NewClient(repo.Host, repo.Owner, repo.Name)
125125
}

internal/github/github.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,47 @@ type PullRequest struct {
2424
type Client struct {
2525
gql *api.GraphQLClient
2626
rest *api.RESTClient
27+
host string
2728
owner string
2829
repo string
2930
slug string
3031
}
3132

3233
// NewClient creates a new GitHub API client for the given repository.
33-
func NewClient(owner, repo string) (*Client, error) {
34-
gql, err := api.DefaultGraphQLClient()
34+
// The host parameter specifies the GitHub hostname (e.g. "github.com" or a
35+
// GHES hostname like "github.mycompany.com"). If empty, it defaults to
36+
// "github.com".
37+
func NewClient(host, owner, repo string) (*Client, error) {
38+
if host == "" {
39+
host = "github.com"
40+
}
41+
opts := api.ClientOptions{Host: host}
42+
gql, err := api.NewGraphQLClient(opts)
3543
if err != nil {
3644
return nil, fmt.Errorf("creating GraphQL client: %w", err)
3745
}
38-
rest, err := api.DefaultRESTClient()
46+
rest, err := api.NewRESTClient(opts)
3947
if err != nil {
4048
return nil, fmt.Errorf("creating REST client: %w", err)
4149
}
4250
return &Client{
4351
gql: gql,
4452
rest: rest,
53+
host: host,
4554
owner: owner,
4655
repo: repo,
4756
slug: owner + "/" + repo,
4857
}, nil
4958
}
5059

60+
// PRURL constructs the web URL for a pull request on the given host.
61+
func PRURL(host, owner, repo string, number int) string {
62+
if host == "" {
63+
host = "github.com"
64+
}
65+
return fmt.Sprintf("https://%s/%s/%s/pull/%d", host, owner, repo, number)
66+
}
67+
5168
// FindPRForBranch finds an open PR by head branch name.
5269
func (c *Client) FindPRForBranch(branch string) (*PullRequest, error) {
5370
var query struct {

internal/github/github_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package github
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPRURL(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
host string
13+
owner string
14+
repo string
15+
number int
16+
want string
17+
}{
18+
{"github.com", "github.com", "owner", "repo", 42, "https://github.com/owner/repo/pull/42"},
19+
{"GHES host", "ghes.example.com", "myorg", "myrepo", 99, "https://ghes.example.com/myorg/myrepo/pull/99"},
20+
{"empty host defaults to github.com", "", "owner", "repo", 1, "https://github.com/owner/repo/pull/1"},
21+
}
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
got := PRURL(tt.host, tt.owner, tt.repo, tt.number)
25+
assert.Equal(t, tt.want, got)
26+
})
27+
}
28+
}

0 commit comments

Comments
 (0)