Exemplo n.º 1
0
        public static CompatibilityResult GetCompatibilityResult(
            PackageDetailsWithApiIndices package,
            string apiMethodSignature,
            string packageVersion,
            string target           = "net6.0",
            bool checkLesserPackage = false)
        {
            var compatibilityResult = new CompatibilityResult
            {
                Compatibility      = Compatibility.UNKNOWN,
                CompatibleVersions = new List <string>()
            };

            // If necessary data to determine compatibility is missing, return unknown compatibility
            if (package == null ||
                apiMethodSignature == null ||
                !NuGetVersion.TryParse(packageVersion, out var validPackageVersion))
            {
                return(compatibilityResult);
            }

            if (package.PackageDetails.IsDeprecated)
            {
                compatibilityResult.Compatibility = Compatibility.DEPRECATED;
                return(compatibilityResult);
            }

            var apiDetails = GetApiDetails(package, apiMethodSignature);
            var compatiblePackageVersionsForTarget =
                GetCompatiblePackageVersionsForTarget(apiDetails, package, target, checkLesserPackage);

            // If package version is greater than the greatest compatible version, it is likely this latest version
            // has not been assessed and added to the compatibility datastore. If it has a lower version of the same
            // major that is compatible, then it will be marked as Compatible. It will be marked as Incompatible otherwise
            var maxCompatibleVersion = NugetVersionHelper.GetMaxVersion(compatiblePackageVersionsForTarget);

            if (maxCompatibleVersion != null &&
                !maxCompatibleVersion.IsZeroVersion() &&
                validPackageVersion.IsGreaterThan(maxCompatibleVersion))
            {
                compatibilityResult.Compatibility = validPackageVersion.HasSameMajorAs(maxCompatibleVersion)
                    ? Compatibility.COMPATIBLE
                    : Compatibility.INCOMPATIBLE;
            }
            // In all other cases, just check to see if the list of compatible versions for the target framework
            // contains the current package version
            else
            {
                compatibilityResult.Compatibility = validPackageVersion.HasLowerOrEqualCompatibleVersion(compatiblePackageVersionsForTarget)
                    ? Compatibility.COMPATIBLE
                    : Compatibility.INCOMPATIBLE;
            }

            // CompatibleVersions are recommended as potential upgrades from current version
            compatibilityResult.CompatibleVersions = validPackageVersion.FindGreaterCompatibleVersions(compatiblePackageVersionsForTarget).ToList();

            return(compatibilityResult);
        }
Exemplo n.º 2
0
        public static async Task <CompatibilityResult> IsCompatibleAsync(Task <PackageDetails> packageDetails, PackageVersionPair packageVersionPair, ILogger _logger, string target = "net6.0")
        {
            if (packageDetails == null || packageVersionPair == null)
            {
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }

            try
            {
                await packageDetails;
                if (!packageDetails.IsCompletedSuccessfully)
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.UNKNOWN,
                        CompatibleVersions = new List <string>()
                    });
                }

                var compatibleVersionsForTargetFramework = packageDetails.Result.Targets.GetValueOrDefault(target, null);
                if (compatibleVersionsForTargetFramework == null)
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.INCOMPATIBLE,
                        CompatibleVersions = new List <string>()
                    });
                }

                if (!NuGetVersion.TryParse(packageVersionPair.Version, out var version))
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.UNKNOWN,
                        CompatibleVersions = new List <string>()
                    });
                }

                var compatibleVersionsToRecommend = version.FindGreaterCompatibleVersions(compatibleVersionsForTargetFramework).ToList();
                compatibleVersionsToRecommend.Sort((a, b) => NuGetVersion.Parse(a).CompareTo(NuGetVersion.Parse(b)));

                Compatibility compatibility;
                var           maxCompatibleVersion = NugetVersionHelper.GetMaxVersion(compatibleVersionsForTargetFramework);
                if (maxCompatibleVersion != null &&
                    !maxCompatibleVersion.IsZeroVersion() &&
                    version.IsGreaterThan(maxCompatibleVersion))
                {
                    compatibility = version.HasSameMajorAs(maxCompatibleVersion)
                        ? Compatibility.COMPATIBLE
                        : Compatibility.INCOMPATIBLE;
                }
                else
                {
                    compatibility = version.HasLowerOrEqualCompatibleVersion(compatibleVersionsForTargetFramework)
                        ? Compatibility.COMPATIBLE
                        : Compatibility.INCOMPATIBLE;
                }

                return(new CompatibilityResult
                {
                    Compatibility = compatibility,
                    CompatibleVersions = compatibleVersionsToRecommend
                });
            }
            catch (OutOfMemoryException e)
            {
                _logger.LogError("parse package version {0} {1} with error {2}", packageVersionPair.PackageId, packageVersionPair.Version, e);
                MemoryUtils.LogMemoryConsumption(_logger);
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }
            catch (Exception e)
            {
                _logger.LogError("parse package version {0} {1} with error {2}", packageVersionPair.PackageId, packageVersionPair.Version, e);
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }
        }