Skip to content

Commit

Permalink
fix: don't update cache if ratelimited
Browse files Browse the repository at this point in the history
  • Loading branch information
sjurtf committed Nov 26, 2024
1 parent 1b26044 commit 63d87a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/terraform-registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ func gitHubRegistry(reg *registry.Registry) {
)
}
if reg.IsProviderEnabled {
logger.Debug("loading GitHub provider store cache")
logger.Debug("reloading GitHub provider store cache")
err := store.ReloadProviderCache(context.Background())
if err != nil {
logger.Error("failed to load GitHub provider store cache",
logger.Error("failed to reload GitHub provider store cache",
zap.Error(err),
)
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/store/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -213,6 +214,8 @@ func (s *GitHubStore) findAsset(ctx context.Context, owner string, repo string,
// Should be called at least once after initialisation and probably on regular
// intervals afterward to keep providerCache up-to-date.
func (s *GitHubStore) ReloadProviderCache(ctx context.Context) error {
var rateLimitErr *github.RateLimitError

repos, err := s.searchProviderRepositories(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -258,27 +261,39 @@ func (s *GitHubStore) ReloadProviderCache(ctx context.Context) error {

SHASums, SHASumURL, SHASumFileName, err := s.getSHA256Sums(ctx, owner, name, release.Assets)
if err != nil {
s.logger.Warn(fmt.Sprintf("not a valid release [%s/%s]- could not find SHA checksums: %s", nameKey, version, err))
if errors.As(err, &rateLimitErr) {
return err
}
s.logger.Warn(fmt.Sprintf("not a valid release [%s/%s] - could not find SHA checksums: %s", nameKey, version, err))
s.providerIgnoreCache.Store(cacheKey(nameKey, version), true)
continue
}

// not considered a valid release if a shasum file was not part of the release
if SHASumURL == "" {
if errors.As(err, &rateLimitErr) {
return err
}
s.logger.Warn(fmt.Sprintf("not a valid release [%s/%s] - could not find SHA checksums", nameKey, version))
s.providerIgnoreCache.Store(cacheKey(nameKey, version), true)
continue
}

providerProtocols, err := s.getProviderProtocols(ctx, owner, name, release.Assets)
if err != nil {
if errors.As(err, &rateLimitErr) {
return err
}
s.logger.Warn(fmt.Sprintf("not a valid release [%s/%s] - unable to identify provider protocol", nameKey, version))
s.providerIgnoreCache.Store(cacheKey(nameKey, version), true)
continue
}

keys, err := s.getGPGPublicKey(ctx, release, owner, name)
if err != nil || len(keys) != 1 {
if errors.As(err, &rateLimitErr) {
return err
}
s.logger.Warn(fmt.Sprintf("not a valid release [%s/%s] - unable to get GPG Public Key", nameKey, version))
s.providerIgnoreCache.Store(cacheKey(nameKey, version), true)
continue
Expand Down

0 comments on commit 63d87a0

Please sign in to comment.