From b8933dfb32a31f65e3291e207a41c09f3a12d274 Mon Sep 17 00:00:00 2001 From: whywaita Date: Tue, 9 Nov 2021 16:44:39 +0900 Subject: [PATCH] Use conditional GET by httpcache --- go.mod | 1 + go.sum | 2 ++ pkg/gh/github.go | 8 +++++--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 6c6749d..507cd4d 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/go-sql-driver/mysql v1.5.0 github.com/google/go-cmp v0.5.6 github.com/google/go-github/v35 v35.2.0 + github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 github.com/hashicorp/go-plugin v1.4.0 github.com/hashicorp/go-version v1.3.0 github.com/jmoiron/sqlx v1.2.0 diff --git a/go.sum b/go.sum index f7221ba..330f4fa 100644 --- a/go.sum +++ b/go.sum @@ -107,6 +107,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA= +github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/hashicorp/go-hclog v0.14.1 h1:nQcJDQwIAGnmoUWp8ubocEX40cCml/17YkF6csQLReU= github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= github.com/hashicorp/go-plugin v1.4.0 h1:b0O7rs5uiJ99Iu9HugEzsM67afboErkHUWddUSpUO3A= diff --git a/pkg/gh/github.go b/pkg/gh/github.go index 877a07d..430bbaa 100644 --- a/pkg/gh/github.go +++ b/pkg/gh/github.go @@ -10,6 +10,8 @@ import ( "strings" "time" + "github.com/gregjones/httpcache" + "github.com/bradleyfalzon/ghinstallation/v2" "github.com/google/go-github/v35/github" "github.com/patrickmn/go-cache" @@ -50,7 +52,7 @@ func NewClient(ctx context.Context, personalToken, gheDomain string) (*github.Cl // header is "Authorization: Bearer YOUR_JWT" // docs: https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-a-github-app func NewClientGitHubApps(gheDomain string, appID int64, appPEM []byte) (*github.Client, error) { - tr := http.DefaultTransport + tr := httpcache.NewMemoryCacheTransport() itr, err := ghinstallation.NewAppsTransport(tr, appID, appPEM) if err != nil { return nil, fmt.Errorf("failed to create Apps transport: %w", err) @@ -71,7 +73,7 @@ func NewClientGitHubApps(gheDomain string, appID int64, appPEM []byte) (*github. // 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) { - tr := http.DefaultTransport + tr := httpcache.NewMemoryCacheTransport() itr, err := ghinstallation.New(tr, appID, installationID, appPEM) if err != nil { return nil, fmt.Errorf("failed to create Apps transport: %w", err) @@ -93,7 +95,7 @@ func CheckSignature(installationID int64) error { appID := config.Config.GitHub.AppID pem := config.Config.GitHub.PEMByte - tr := http.DefaultTransport + tr := httpcache.NewMemoryCacheTransport() _, err := ghinstallation.New(tr, appID, installationID, pem) if err != nil { return fmt.Errorf("failed to create GitHub installation: %w", err)