Skip to content

Commit

Permalink
Now correctly using the API base URL, when defined, addressing #16 (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Sérgio Moura <[email protected]>
  • Loading branch information
sergiomarqmoura and Sérgio Moura authored Jan 12, 2022
1 parent 9ac9875 commit b55f8aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
31 changes: 27 additions & 4 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package github
import (
"context"
"errors"
"net/http"
"os"
"strings"

"github.com/bitrise-io/go-utils/log"
gogithub "github.com/google/go-github/v33/github"
"golang.org/x/oauth2"
)
Expand All @@ -16,17 +19,37 @@ type GithubClient struct {

func NewClient(accessToken string) *GithubClient {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
tc := NewAuthTokenClient(accessToken)

return &GithubClient{
ctx,
gogithub.NewClient(tc),
}
}

func NewEnterpriseClient(baseURL string, accessToken string) *GithubClient {
ctx := context.Background()
tc := NewAuthTokenClient(accessToken)
enterpriseClient, err := gogithub.NewEnterpriseClient(baseURL, baseURL, tc)
if err != nil {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}

return &GithubClient{
ctx,
enterpriseClient,
}
}

func NewAuthTokenClient(accessToken string) *http.Client {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
return oauth2.NewClient(ctx, ts)
}

func (c *GithubClient) CreateComment(owner, repo string, issueNumber int, body string) (*gogithub.IssueComment, error) {
commentToCreate := gogithub.IssueComment{Body: &body}
comment, _, err := c.Issues.CreateComment(c.Context, owner, repo, issueNumber, &commentToCreate)
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ func main() {
owner, repo := ownerAndRepo(conf.RepositoryURL)
commentBody := conf.Body

githubClient := github.NewClient(string(conf.AuthToken))
var githubClient *github.GithubClient

if conf.APIBaseURL == "" {
githubClient = github.NewClient(string(conf.AuthToken))
} else {
githubClient = github.NewEnterpriseClient(conf.APIBaseURL, string(conf.AuthToken))
}

// if tag is set, try to find and update existing comment
if conf.UpdateCommentTag != "" {
Expand Down

0 comments on commit b55f8aa

Please sign in to comment.