Skip to content

Commit

Permalink
Merge pull request #120 from whywaita/fix/more-conditional-get
Browse files Browse the repository at this point in the history
Use httpcache in gh.NewClient
  • Loading branch information
whywaita authored Nov 25, 2021
2 parents de869db + d79454f commit 4034fca
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pkg/gh/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"strings"
"time"

"github.com/gregjones/httpcache"

"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/v35/github"
"github.com/gregjones/httpcache"
"github.com/patrickmn/go-cache"
"github.com/whywaita/myshoes/internal/config"
"github.com/whywaita/myshoes/pkg/logger"
Expand All @@ -35,17 +34,22 @@ func init() {

// NewClient create a client of GitHub
func NewClient(ctx context.Context, personalToken, gheDomain string) (*github.Client, error) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: personalToken,
})
tc := oauth2.NewClient(ctx, ts)
oauth2Transport := &oauth2.Transport{
Source: oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: personalToken},
),
}
transport := &httpcache.Transport{
Transport: oauth2Transport,
Cache: httpcache.NewMemoryCache(),
MarkCachedResponses: true,
}

if gheDomain == "" {
return github.NewClient(tc), nil
return github.NewClient(&http.Client{Transport: transport}), nil
}

return github.NewEnterpriseClient(gheDomain, gheDomain, tc)
return github.NewEnterpriseClient(gheDomain, gheDomain, &http.Client{Transport: transport})
}

// NewClientGitHubApps create a client of GitHub using Private Key from GitHub Apps
Expand All @@ -69,7 +73,7 @@ func NewClientGitHubApps(gheDomain string, appID int64, appPEM []byte) (*github.
return github.NewEnterpriseClient(gheDomain, gheDomain, &http.Client{Transport: itr})
}

// NewClientInstallation create a client of Github using installation ID from GitHub Apps
// NewClientInstallation create a client of GitHub using installation ID from GitHub Apps
// header is "Authorization: token YOUR_INSTALLATION_ACCESS_TOKEN"
// docs: https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-an-installation
func NewClientInstallation(gheDomain string, installationID int64, appID int64, appPEM []byte) (*github.Client, error) {
Expand Down Expand Up @@ -104,7 +108,7 @@ func CheckSignature(installationID int64) error {
return nil
}

// ExistGitHubRepository check exist of github repository
// ExistGitHubRepository check exist of GitHub repository
func ExistGitHubRepository(scope, gheDomain string, githubPersonalToken string) error {
repoURL, err := getRepositoryURL(scope, gheDomain)
if err != nil {
Expand Down Expand Up @@ -132,7 +136,7 @@ func ExistGitHubRepository(scope, gheDomain string, githubPersonalToken string)
return fmt.Errorf("invalid response code (%d)", resp.StatusCode)
}

// ExistGitHubRunner check exist registered of github runner
// ExistGitHubRunner check exist registered of GitHub runner
func ExistGitHubRunner(ctx context.Context, client *github.Client, owner, repo, runnerName string) (*github.Runner, error) {
runners, err := ListRunners(ctx, client, owner, repo)
if err != nil {
Expand Down

0 comments on commit 4034fca

Please sign in to comment.