Skip to content

Commit

Permalink
Merge pull request #241 from cdnjs/fix-algolia-use-aggregated
Browse files Browse the repository at this point in the history
Algolia: fetch existing versions from aggregated metadata
  • Loading branch information
xtuc authored Aug 19, 2021
2 parents 49bf02a + 28fdb10 commit 9b520bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions functions/algolia-pump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ var (
CF_ACCOUNT_ID = os.Getenv("CF_ACCOUNT_ID")
)

func getExistingVersions(p *packages.Package) ([]string, error) {
func getExistingVersionsFromAggregatedMetadata(p *packages.Package) ([]string, error) {
cfapi, err := cloudflare.NewWithAPIToken(KV_TOKEN, cloudflare.UsingAccount(CF_ACCOUNT_ID))
if err != nil {
return nil, errors.Wrap(err, "failed to create cloudflare API client")
}

versions, err := kv.GetVersions(cfapi, *p.Name)
versions, err := kv.GetVersionsFromAggregatedMetadata(cfapi, *p.Name)
if err != nil {
return nil, errors.Wrap(err, "failed to get verions")
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func Invoke(ctx context.Context, e gcp.GCSEvent) error {
return fmt.Errorf("could not decode config: %v", err)
}
// update package version with latest
versions, err := getExistingVersions(pkg)
versions, err := getExistingVersionsFromAggregatedMetadata(pkg)
if err != nil {
return fmt.Errorf("failed to retrieve existing versions: %s", err)
}
Expand Down
29 changes: 29 additions & 0 deletions kv/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ import (
cloudflare "github.com/cloudflare/cloudflare-go"
)

// GetVersionsFromAggregatedMetadata gets the list version for a particular package
// using the aggregated metadata endpoint.
//
// The aggregated metadata will only contain non-empty versions, so this is useful
// for updating Algolia.
func GetVersionsFromAggregatedMetadata(api *cloudflare.API, pckgname string) ([]string, error) {
aggPkg, err := getAggregatedMetadata(api, pckgname)
if err != nil {
switch err.(type) {
case KeyNotFoundError:
{
return nil, nil
}
default:
{
// api error
return nil, err
}
}
}

var versions []string
for _, asset := range aggPkg.Assets {
versions = append(versions, asset.Version)
}

return versions, nil
}

// RemoveVersionFromAggregatedMetadata will remove a particular version from
// a package's KV entry for aggregated metadata if it exists.
// This is useful for removing empty versions with no files.
Expand Down

0 comments on commit 9b520bb

Please sign in to comment.