versionMatchesSemver checks if the provided version matches the given version semver range. The range string has the following format: https://github.com/blang/semver#ranges.
(ctx *gcp.Context, versionRange string, version string)
| 160 | return false, err |
| 161 | } |
| 162 | // If the version contains a wildcard, we will replace with 0 for the semver comparison. |
| 163 | v = strings.ReplaceAll(v, "*", "0") |
| 164 | |
| 165 | return versionMatchesSemver(ctx, ">=3.13.0-0", v) |
| 166 | } |
| 167 | |
| 168 | // versionMatchesSemver checks if the provided version matches the given version semver range. |
| 169 | // The range string has the following format: https://github.com/blang/semver#ranges. |
| 170 | func versionMatchesSemver(ctx *gcp.Context, versionRange string, version string) (bool, error) { |
| 171 | if version == "" { |
| 172 | return false, nil |
| 173 | } |
| 174 | if isSupportedUnstablePythonVersion(version) { |
| 175 | // The format of Python pre-release version e.g. 3.14.0rc1 doesn't follow the semver rule |
| 176 | // that requires a hyphen before the identifier "rc". |
| 177 | if strings.Contains(version, "rc") && !strings.Contains(version, "-rc") { |
| 178 | version = strings.Replace(version, "rc", "-rc", 1) |
| 179 | } |
| 180 | } |
| 181 | constraint, err := semver.NewConstraint(versionRange) |
| 182 | if err != nil { |
| 183 | return false, fmt.Errorf("invalid version range %q: %w", versionRange, err) |
| 184 | } |
| 185 | v, err := semver.NewVersion(version) |
| 186 | if err != nil { |
| 187 | return false, fmt.Errorf("invalid version %q: %w", version, err) |
| 188 | } |
| 189 | if !constraint.Check(v) { |