From db793d8f51b3f30a457a3914a26b0406d3ad7004 Mon Sep 17 00:00:00 2001 From: nexustar Date: Mon, 10 Jan 2022 17:09:42 +0800 Subject: [PATCH] playground: fix cannot find nightly version (#1715) --- pkg/repository/v1_repository.go | 62 +++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/pkg/repository/v1_repository.go b/pkg/repository/v1_repository.go index f57a2a9354..f2b22d202d 100644 --- a/pkg/repository/v1_repository.go +++ b/pkg/repository/v1_repository.go @@ -799,6 +799,30 @@ func (r *V1Repository) LocalComponentVersion(id, ver string, includeYanked bool) return vi, nil } +func findVersionFromManifest(id, constraint, platform string, manifest *v1manifest.Component) (string, error) { + cons, err := utils.NewConstraint(constraint) + if err != nil { + return "", err + } + versions := manifest.VersionList(platform) + verList := make([]string, 0, len(versions)) + for v := range versions { + if v == manifest.Nightly { + continue + } + verList = append(verList, v) + } + sort.Slice(verList, func(p, q int) bool { + return semver.Compare(verList[p], verList[q]) > 0 + }) + for _, v := range verList { + if cons.Check(v) { + return v, nil + } + } + return "", errors.Annotatef(ErrUnknownVersion, "version %s on %s for component %s not found", constraint, platform, id) +} + // ResolveComponentVersionWithPlatform resolves the latest version of a component that satisfies the constraint func (r *V1Repository) ResolveComponentVersionWithPlatform(id, constraint, platform string) (utils.Version, error) { manifest, err := r.LocalComponentManifest(id, false) @@ -814,40 +838,24 @@ func (r *V1Repository) ResolveComponentVersionWithPlatform(id, constraint, platf } ver = v.String() case utils.NightlyVersionAlias: - if !manifest.HasNightly(platform) { - return "", errors.Annotatef(ErrUnknownVersion, "component %s does not have nightly on %s", id, platform) - } - ver = manifest.Nightly - default: - cons, err := utils.NewConstraint(constraint) + v, _, err := r.LatestNightlyVersion(id) if err != nil { return "", err } - versions := manifest.VersionList(platform) - verList := make([]string, 0, len(versions)) - for v := range versions { - if v == manifest.Nightly { - continue + ver = v.String() + default: + ver, err = findVersionFromManifest(id, constraint, platform, manifest) + if err != nil { + manifest, err = r.FetchComponentManifest(id, false) + if err != nil { + return "", err } - verList = append(verList, v) - } - sort.Slice(verList, func(p, q int) bool { - return semver.Compare(verList[p], verList[q]) > 0 - }) - for _, v := range verList { - if cons.Check(v) { - ver = v - break + ver, err = findVersionFromManifest(id, constraint, platform, manifest) + if err != nil { + return "", errors.Annotatef(ErrUnknownVersion, "version %s on %s for component %s not found", constraint, platform, id) } } } - if ver == "" { - return "", fmt.Errorf(`no version on %s for component %s satisfies constraint "%s"`, platform, id, constraint) - } - vi := manifest.VersionItem(platform, ver, false) - if vi == nil { - return "", errors.Annotatef(ErrUnknownVersion, "version %s on %s for component %s not found", ver, platform, id) - } return utils.Version(ver), nil }