Skip to content

Commit

Permalink
feat(plugin): retry checksum fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Feb 10, 2023
1 parent 4dc6cc0 commit 702e4de
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ require (
github.com/aws/smithy-go v1.13.5
github.com/go-chi/chi/v5 v5.0.8
github.com/google/go-github/v50 v50.0.0
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-retryablehttp v0.7.2
github.com/kelseyhightower/envconfig v1.4.0
github.com/migueleliasweb/go-github-mock v0.0.15
github.com/patrickmn/go-cache v2.1.0+incompatible
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQL
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/felixge/httpsnoop v1.0.2/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
Expand Down Expand Up @@ -666,7 +667,11 @@ github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.12.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v0.12.2 h1:F1fdYblUEsxKiailtkhCCG2g4bipEgaHiDc8vffNpD4=
github.com/hashicorp/go-hclog v0.12.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-immutable-radix v1.2.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
Expand All @@ -676,6 +681,8 @@ github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHh
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.7.2 h1:AcYqCvkpalPnPF2pn0KamgwamS42TqUDDYFRKq/RAd0=
github.com/hashicorp/go-retryablehttp v0.7.2/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
Expand Down Expand Up @@ -785,13 +792,15 @@ github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
Expand Down
20 changes: 16 additions & 4 deletions internal/plugin/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@ import (
"net/http"
"regexp"
"strings"
"sync"
"time"

"github.com/Masterminds/semver/v3"
"github.com/go-semantic-release/plugin-registry/pkg/registry"
"github.com/google/go-github/v50/github"
"github.com/hashicorp/go-retryablehttp"
)

var httpClient = &http.Client{
Timeout: time.Minute,
var (
defaultRetryableClient *retryablehttp.Client
defaultRetryableClientInit sync.Once
)

func getDefaultRetryableClient() *retryablehttp.Client {
defaultRetryableClientInit.Do(func() {
defaultRetryableClient = retryablehttp.NewClient()
defaultRetryableClient.Logger = nil
defaultRetryableClient.HTTPClient.Timeout = time.Minute
})
return defaultRetryableClient
}

func getOwnerRepo(fullRepo string) (string, string) {
Expand Down Expand Up @@ -81,11 +93,11 @@ func getGitHubRelease(ctx context.Context, ghClient *github.Client, fullRepo, ta

func fetchChecksumFile(ctx context.Context, url string) (map[string]string, error) {
ret := make(map[string]string)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
req, err := retryablehttp.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
res, err := httpClient.Do(req)
res, err := getDefaultRetryableClient().Do(req)
if err != nil {
return nil, err
}
Expand Down
22 changes: 19 additions & 3 deletions internal/plugin/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,30 @@ c3703969 plugin_v1.0.0_linux_arm
cacce75a plugin_v1.0.0_linux_arm64
`

func getCheckSumServer() *httptest.Server {
func getCheckSumServer(failingRequests int) *httptest.Server {
cnt := 0
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cnt++
if cnt <= failingRequests {
w.WriteHeader(http.StatusInternalServerError)
return
}
_, _ = io.WriteString(w, testChecksumFile)
}))
}

func TestFetchChecksumFile(t *testing.T) {
ts := getCheckSumServer()
ts := getCheckSumServer(0)
defer ts.Close()
checksums, err := fetchChecksumFile(context.Background(), ts.URL)
require.NoError(t, err)
require.Len(t, checksums, 6)
require.Equal(t, "0911f3dd", checksums["plugin_v1.0.0_windows_amd64.exe"])
require.Equal(t, "cacce75a", checksums["plugin_v1.0.0_linux_arm64"])
}

func TestFetchChecksumFileRetry(t *testing.T) {
ts := getCheckSumServer(1)
defer ts.Close()
checksums, err := fetchChecksumFile(context.Background(), ts.URL)
require.NoError(t, err)
Expand All @@ -84,7 +100,7 @@ func TestFetchChecksumFile(t *testing.T) {
}

func TestGetPluginAssets(t *testing.T) {
checksumServer := getCheckSumServer()
checksumServer := getCheckSumServer(0)
defer checksumServer.Close()
dlURL := github.String(checksumServer.URL)
ghReleaseAssets := []*github.ReleaseAsset{
Expand Down

0 comments on commit 702e4de

Please sign in to comment.