Skip to content

Commit

Permalink
Support GitHub Enterprise Server hosts
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
rajbos authored Mar 24, 2023
1 parent 0c090b3 commit b443372
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
26 changes: 23 additions & 3 deletions clients/githubrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/google/go-github/v38/github"
Expand Down Expand Up @@ -127,7 +129,7 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD

// URI implements RepoClient.URI.
func (client *Client) URI() string {
return fmt.Sprintf("github.com/%s/%s", client.repourl.owner, client.repourl.repo)
return fmt.Sprintf("%s/%s/%s", client.repourl.host, client.repourl.owner, client.repourl.repo)
}

// LocalPath implements RepoClient.LocalPath.
Expand Down Expand Up @@ -245,8 +247,26 @@ func CreateGithubRepoClientWithTransport(ctx context.Context, rt http.RoundTripp
httpClient := &http.Client{
Transport: rt,
}
client := github.NewClient(httpClient)
graphClient := githubv4.NewClient(httpClient)

var client *github.Client
var graphClient *githubv4.Client
// check if GITHUB_API_URL is set
if githubAPIURL := os.Getenv("GITHUB_API_URL"); githubAPIURL != "https://api.github.com" {
// create a new enterprise client with custom URLs
ghithubSERVERURL := os.Getenv("GITHUB_SERVER_URL")
if ghithubSERVERURL == "" {
// load the server url from the api url
ghithubSERVERURL = strings.TrimSuffix(githubAPIURL, "/api/v3")
}
githubGRAPHQLURL := fmt.Sprintf("%s/api/graphql", ghithubSERVERURL)

client, _ = github.NewEnterpriseClient(githubAPIURL, githubAPIURL, httpClient)
graphClient = githubv4.NewEnterpriseClient(githubGRAPHQLURL, httpClient)
} else {
// use the defaul url values from the github client
client = github.NewClient(httpClient)
graphClient = githubv4.NewClient(httpClient)
}

return &Client{
ctx: ctx,
Expand Down
6 changes: 0 additions & 6 deletions clients/githubrepo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ func (r *repoURL) Org() clients.Repo {

// IsValid implements Repo.IsValid.
func (r *repoURL) IsValid() error {
switch r.host {
case "github.com":
default:
return sce.WithMessage(sce.ErrorUnsupportedHost, r.host)
}

if strings.TrimSpace(r.owner) == "" || strings.TrimSpace(r.repo) == "" {
return sce.WithMessage(sce.ErrorInvalidURL,
fmt.Sprintf("%v. Expected the full repository url", r.URI()))
Expand Down

0 comments on commit b443372

Please sign in to comment.