Example #1
0
        public void InvalidPackage()
        {
            string packageId            = "invalidId";
            PackageNotFoundException ex = Assert.Throws <PackageNotFoundException>(() => this.IndexReader.DeletePackage(packageId));

            Assert.Equal(ex.PackageId, packageId);
        }
Example #2
0
        public void GetNonExistent()
        {
            PackageNotFoundException ex = Assert.Throws <PackageNotFoundException>(() => {
                using (Stream zipStream = this.IndexReader.GetPackageAsArchive("invalid id"))
                {
                    // do nothing, exception expected
                }
            });

            Assert.Equal("invalid id", ex.PackageId);
        }
Example #3
0
        private async void ProcessCompatibility(IEnumerable <PackageVersionPair> packageVersions,
                                                Dictionary <PackageVersionPair, TaskCompletionSource <PackageDetails> > compatibilityTaskCompletionSources)
        {
            var packageVersionsFound      = new HashSet <PackageVersionPair>();
            var packageVersionsWithErrors = new HashSet <PackageVersionPair>();

            var packageVersionsGroupedByPackageId = packageVersions
                                                    .GroupBy(pv => pv.PackageId)
                                                    .ToDictionary(pvGroup => pvGroup.Key, pvGroup => pvGroup.ToList());

            foreach (var groupedPackageVersions in packageVersionsGroupedByPackageId)
            {
                var packageToDownload = groupedPackageVersions.Key.ToLower();
                var fileToDownload    = GetDownloadFilePath(CompatibilityCheckerType, packageToDownload);

                try
                {
                    _logger.LogInformation("Downloading {0} from {1}", fileToDownload,
                                           CompatibilityCheckerType);
                    using var stream = await _httpService.DownloadS3FileAsync(fileToDownload);

                    using var gzipStream   = new GZipStream(stream, CompressionMode.Decompress);
                    using var streamReader = new StreamReader(gzipStream);
                    var data           = JsonConvert.DeserializeObject <PackageFromS3>(streamReader.ReadToEnd());
                    var packageDetails = data.Package ?? data.Namespaces;

                    if (packageDetails.Name == null || !string.Equals(packageDetails.Name.Trim(),
                                                                      packageToDownload.Trim(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        throw new PackageDownloadMismatchException(
                                  actualPackage: packageDetails.Name,
                                  expectedPackage: packageToDownload);
                    }

                    foreach (var packageVersion in groupedPackageVersions.Value)
                    {
                        if (compatibilityTaskCompletionSources.TryGetValue(packageVersion, out var taskCompletionSource))
                        {
                            taskCompletionSource.SetResult(packageDetails);
                            packageVersionsFound.Add(packageVersion);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("404"))
                    {
                        _logger.LogInformation($"Encountered {ex.GetType()} while downloading and parsing {fileToDownload} " +
                                               $"from {CompatibilityCheckerType}, but it was ignored. " +
                                               $"ErrorMessage: {ex.Message}.");
                    }
                    else
                    {
                        _logger.LogError("Failed when downloading and parsing {0} from {1}, {2}", fileToDownload, CompatibilityCheckerType, ex);
                    }

                    foreach (var packageVersion in groupedPackageVersions.Value)
                    {
                        if (compatibilityTaskCompletionSources.TryGetValue(packageVersion, out var taskCompletionSource))
                        {
                            taskCompletionSource.SetException(new PortingAssistantClientException(ExceptionMessage.PackageNotFound(packageVersion), ex));
                            packageVersionsWithErrors.Add(packageVersion);
                        }
                    }
                }
            }

            foreach (var packageVersion in packageVersions)
            {
                if (packageVersionsFound.Contains(packageVersion) || packageVersionsWithErrors.Contains(packageVersion))
                {
                    continue;
                }

                if (compatibilityTaskCompletionSources.TryGetValue(packageVersion, out var taskCompletionSource))
                {
                    var errorMessage = $"Could not find package {packageVersion} in external source; try checking an internal source.";
                    _logger.LogInformation(errorMessage);

                    var innerException = new PackageNotFoundException(errorMessage);
                    taskCompletionSource.TrySetException(new PortingAssistantClientException(ExceptionMessage.PackageNotFound(packageVersion), innerException));
                }
            }
        }
Example #4
0
        public static DialogViewModel CreateDialog(this PackageNotFoundException ex)
        {
            var headerVm = new DialogHeaderViewModel("Sorry, We couldn't find the package you were looking for.", $"Package ID: \"{ex.PackageId}\"", "crow", Accents.Warn);

            return(new DialogViewModel(headerVm));
        }
Example #5
0
        private async void ProcessCompatibility(IEnumerable <PackageVersionPair> packageVersions,
                                                Dictionary <PackageVersionPair, TaskCompletionSource <PackageDetails> > compatibilityTaskCompletionSources,
                                                string pathToSolution, bool isIncremental, bool incrementalRefresh)
        {
            var packageVersionsFound      = new HashSet <PackageVersionPair>();
            var packageVersionsWithErrors = new HashSet <PackageVersionPair>();

            var packageVersionsGroupedByPackageId = packageVersions
                                                    .GroupBy(pv => pv.PackageId)
                                                    .ToDictionary(pvGroup => pvGroup.Key, pvGroup => pvGroup.ToList());

            foreach (var groupedPackageVersions in packageVersionsGroupedByPackageId)
            {
                var packageToDownload = groupedPackageVersions.Key.ToLower();
                var fileToDownload    = GetDownloadFilePath(CompatibilityCheckerType, packageToDownload);

                try
                {
                    string         tempDirectoryPath = GetTempDirectory(pathToSolution);
                    PackageDetails packageDetails    = null;

                    if (isIncremental)
                    {
                        if (incrementalRefresh || !IsPackageInFile(fileToDownload, tempDirectoryPath))
                        {
                            _logger.LogInformation("Downloading {0} from {1}", fileToDownload, CompatibilityCheckerType);
                            packageDetails = await GetPackageDetailFromS3(fileToDownload, _httpService);

                            _logger.LogInformation("Caching {0} from {1} to Temp", fileToDownload, CompatibilityCheckerType);
                            CachePackageDetailsToFile(fileToDownload, packageDetails, tempDirectoryPath);
                        }
                        else
                        {
                            _logger.LogInformation("Fetching {0} from {1} from Temp", fileToDownload, CompatibilityCheckerType);
                            packageDetails = GetPackageDetailFromFile(fileToDownload, tempDirectoryPath);
                        }
                    }
                    else
                    {
                        packageDetails = await GetPackageDetailFromS3(fileToDownload, _httpService);
                    }

                    if (packageDetails.Name == null || !string.Equals(packageDetails.Name.Trim().ToLower(),
                                                                      packageToDownload.Trim().ToLower(), StringComparison.OrdinalIgnoreCase))
                    {
                        throw new PackageDownloadMismatchException(
                                  actualPackage: packageDetails.Name,
                                  expectedPackage: packageToDownload);
                    }

                    foreach (var packageVersion in groupedPackageVersions.Value)
                    {
                        if (compatibilityTaskCompletionSources.TryGetValue(packageVersion, out var taskCompletionSource))
                        {
                            taskCompletionSource.SetResult(packageDetails);
                            packageVersionsFound.Add(packageVersion);
                        }
                    }
                }
                catch (OutOfMemoryException ex)
                {
                    _logger.LogError("Failed when downloading and parsing {0} from {1}, {2}", fileToDownload, CompatibilityCheckerType, ex);
                    MemoryUtils.LogSolutiontSize(_logger, pathToSolution);
                    MemoryUtils.LogMemoryConsumption(_logger);
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("404"))
                    {
                        _logger.LogInformation($"Encountered {ex.GetType()} while downloading and parsing {fileToDownload} " +
                                               $"from {CompatibilityCheckerType}, but it was ignored. " +
                                               $"ErrorMessage: {ex.Message}.");
                        // filter all 404 errors
                        ex = null;
                    }
                    else
                    {
                        _logger.LogError("Failed when downloading and parsing {0} from {1}, {2}", fileToDownload, CompatibilityCheckerType, ex);
                    }

                    foreach (var packageVersion in groupedPackageVersions.Value)
                    {
                        if (compatibilityTaskCompletionSources.TryGetValue(packageVersion, out var taskCompletionSource))
                        {
                            taskCompletionSource.SetException(new PortingAssistantClientException(ExceptionMessage.PackageNotFound(packageVersion), ex));
                            packageVersionsWithErrors.Add(packageVersion);
                        }
                    }
                }
            }

            foreach (var packageVersion in packageVersions)
            {
                if (packageVersionsFound.Contains(packageVersion) || packageVersionsWithErrors.Contains(packageVersion))
                {
                    continue;
                }

                if (compatibilityTaskCompletionSources.TryGetValue(packageVersion, out var taskCompletionSource))
                {
                    var errorMessage = $"Could not find package {packageVersion} in external source; try checking an internal source.";
                    _logger.LogInformation(errorMessage);

                    var innerException = new PackageNotFoundException(errorMessage);
                    taskCompletionSource.TrySetException(new PortingAssistantClientException(ExceptionMessage.PackageNotFound(packageVersion), innerException));
                }
            }
        }
        /// <summary>
        /// Processes the package compatibility
        /// </summary>
        /// <param name="packageVersions">Collection of package versions to check</param>
        /// <param name="foundPackages">Collection of packages found</param>
        /// <param name="compatibilityTaskCompletionSources">The results of the compatibility check to process</param>
        private async void ProcessCompatibility(IEnumerable <PackageVersionPair> packageVersions,
                                                Dictionary <string, List <PackageVersionPair> > foundPackages,
                                                Dictionary <PackageVersionPair, TaskCompletionSource <PackageDetails> > compatibilityTaskCompletionSources)
        {
            var packageVersionsFound      = new HashSet <PackageVersionPair>();
            var packageVersionsWithErrors = new HashSet <PackageVersionPair>();

            foreach (var url in foundPackages)
            {
                try
                {
                    _logger.LogInformation("Downloading {0} from {1}", url.Key, CompatibilityCheckerType);
                    using var stream = await _httpService.DownloadS3FileAsync(url.Key);

                    using var gzipStream   = new GZipStream(stream, CompressionMode.Decompress);
                    using var streamReader = new StreamReader(gzipStream);
                    var packageFromS3 = JsonConvert.DeserializeObject <PackageFromS3>(streamReader.ReadToEnd());
                    packageFromS3.Package.Name = url.Value.First().PackageId;
                    foreach (var packageVersion in url.Value)
                    {
                        if (compatibilityTaskCompletionSources.TryGetValue(packageVersion, out var taskCompletionSource))
                        {
                            taskCompletionSource.SetResult(packageFromS3.Package);
                            packageVersionsFound.Add(packageVersion);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("404"))
                    {
                        _logger.LogInformation($"Encountered {ex.GetType()} while downloading and parsing {url.Key} " +
                                               $"from {CompatibilityCheckerType}, but it was ignored. " +
                                               $"ErrorMessage: {ex.Message}.");
                    }
                    else
                    {
                        _logger.LogError("Failed when downloading and parsing {0} from {1}, {2}", url.Key, CompatibilityCheckerType, ex);
                    }

                    foreach (var packageVersion in url.Value)
                    {
                        if (compatibilityTaskCompletionSources.TryGetValue(packageVersion, out var taskCompletionSource))
                        {
                            taskCompletionSource.SetException(new PortingAssistantClientException(ExceptionMessage.PackageNotFound(packageVersion), ex));
                            packageVersionsWithErrors.Add(packageVersion);
                        }
                    }
                }
            }

            foreach (var packageVersion in packageVersions)
            {
                if (packageVersionsFound.Contains(packageVersion) || packageVersionsWithErrors.Contains(packageVersion))
                {
                    continue;
                }

                if (compatibilityTaskCompletionSources.TryGetValue(packageVersion, out var taskCompletionSource))
                {
                    var errorMessage = $"Could not find package {packageVersion} in external source; try checking an internal source.";
                    _logger.LogInformation(errorMessage);

                    var innerException = new PackageNotFoundException(errorMessage);
                    taskCompletionSource.TrySetException(new PortingAssistantClientException(ExceptionMessage.PackageNotFound(packageVersion), innerException));
                }
            }
        }