private async Task <LicensedPackage> GetLicenseFromProject(AnalyzedPackage package, Uri?projectUrl)
        {
            var(license, _) = await this.TryGetLicenseFromProject(projectUrl);

            if (license != null)
            {
                return(package.Attach(license));
            }

            return(package
                   .With(AnalysisState.Error, $"Package has no valid license URL and the license could not be guessed.")
                   .Attach(License.NonEvaluated));
        }
        private async Task <LicensedPackage> GetLicenseAsync(AnalyzedPackage package)
        {
            if (this.licenseProvider.TryGetLicense(package.Id, out string explicitLicense))
            {
                return(package.With(AnalysisState.Ok, "Given by config.").Attach(new License(explicitLicense)));
            }

            if (package.State == AnalysisState.Error)
            {
                return(package.Attach(License.NonEvaluated));
            }

            string path = string.Format(this.licensePathFormatStr, package.Id, package.Version);

            if (DiskCache.TryGetValue(path, this.config.DiskCache.ResolvedLicenses, out ResolvedLicense? cachedLicense))
            {
                return(new LicensedPackage(package.Id, package.Version, package.OriginProject, cachedLicense.License, cachedLicense.State, cachedLicense.Messages));
            }

            var details = await this.getDetails(package);

            if (details.Status == PackageDetailsResultEnum.InternalPackage)
            {
                return(package
                       .With(AnalysisState.Ok, "Internal package. No explicit license given.")
                       .Attach(License.Internal));
            }
            else if (details.Status == PackageDetailsResultEnum.NoPackageFound)
            {
                Log.Error($"Could not find information on package {package}");
                return(package
                       .With(AnalysisState.Error, $"Could not find information on package")
                       .Attach(License.NonEvaluated));
            }

            var packageDetails = details.Package !;

            if (packageDetails.License != null)
            {
                return(new LicensedPackage(package.Id, package.Version, package.OriginProject, packageDetails.License));
            }

            string deprecated = "https://aka.ms/deprecateLicenseUrl";

            if (packageDetails.LicenseUrl == null || packageDetails.LicenseUrl.ToString() == deprecated)
            {
                Log.Information($"{package} has no valid license URL (will try to fetch from repository)");
                return(await GetLicenseFromProject(package, packageDetails.PackageUrl));
            }

            if (this.licenseParsing.TryGetLicenseFromKnownUrl(packageDetails.LicenseUrl, out License? license))
            {
                return(package.Attach(license));
            }

            Uri licenseUrl = ChangeGithubLicenseUrl(packageDetails.LicenseUrl);

            string error;

            (license, error) = await this.licenseParsing.TryGetLicenseFromLicenseFile(licenseUrl);

            if (license != null)
            {
                return(package.Attach(license));
            }

            Log.Debug($"Could not find license for package {package}: " + error);
            return(package.With(AnalysisState.Ok, error ?? string.Empty).Attach(License.UnknownLicense));
        }