Beispiel #1
0
    private static async Task Run(Options options)
    {
        // Set up NuGet stuff
        sourceCache      = new();
        sourceRepository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
        searchResource   = await sourceRepository.GetResourceAsync <PackageSearchResource>();

        updateResource = await sourceRepository.GetResourceAsync <PackageUpdateResource>();

        findByIdResource = await sourceRepository.GetResourceAsync <FindPackageByIdResource>();

        // Search for the nightly package IDs that were just built
        var localPackageIds = GetLocalNightlyPackageIds(options);

        // Search for the remotely published packages
        var remotePackageIds = await GetRemotePackageIds(options);

        // If there are differences, that means we removed/renmed something
        // In those cases, unist/deprecate all versions
        var toCompletelyUnlist = remotePackageIds.Except(localPackageIds).ToList();

        foreach (var packageId in toCompletelyUnlist)
        {
            await DeleteAllVersionsOfPackage(options, packageId);
        }

        // Now for each package that exists on remote, we want to unlish the nightly versions
        // These are packages that are both present locally and on remote
        var toUnlistNightly = localPackageIds.Intersect(remotePackageIds).ToList();

        foreach (var packageId in toUnlistNightly)
        {
            await DeleteAllNightlyVersionsOfPackage(options, packageId);
        }
    }
Beispiel #2
0
                /// <summary>
                /// Pushes a set of packages from .nupkg files that must exist in <see cref="CheckRepositoryInfo.ReleasesFolder"/>.
                /// </summary>
                /// <param name="pushes">The instances to push (that necessary target this feed).</param>
                /// <returns>The awaitable.</returns>
                public override async Task PushAsync(IEnumerable <ArtifactPush> pushes)
                {
                    string apiKey = null;

                    if (!_packageSource.IsLocal)
                    {
                        apiKey = ResolveAPIKey();
                        if (string.IsNullOrEmpty(apiKey))
                        {
                            Cake.Information($"Could not resolve API key. Push to '{Name}' => '{Url}' is skipped.");
                            return;
                        }
                    }
                    Cake.Information($"Pushing packages to '{Name}' => '{Url}'.");
                    ILogger logger = InitializeAndGetLogger(Cake);
                    PackageUpdateResource updater = await _updater;

                    foreach (ArtifactPush p in pushes)
                    {
                        string         packageString = p.Name + "." + p.Version.WithBuildMetaData(null).ToNormalizedString();
                        NormalizedPath fullPath      = ArtifactType.GlobalInfo.ReleasesFolder.AppendPart(packageString + ".nupkg");
                        await updater.Push(
                            fullPath,
                            string.Empty, // no Symbol source.
                            20,           //20 seconds timeout
                            disableBuffering : false,
                            getApiKey : endpoint => apiKey,
                            getSymbolApiKey : symbolsEndpoint => null,
                            noServiceEndpoint : false,
                            log : logger);
                    }
                    await OnAllArtifactsPushed(pushes);
                }
Beispiel #3
0
        private static async Task DeletePackageAsync()
        {
            try
            {
                // This code region is referenced by the NuGet docs. Please update the docs if you rename the region
                // or move it to a different file.
                #region DeletePackage
                ILogger           logger            = NullLogger.Instance;
                CancellationToken cancellationToken = CancellationToken.None;

                SourceCacheContext    cache      = new SourceCacheContext();
                SourceRepository      repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
                PackageUpdateResource resource   = await repository.GetResourceAsync <PackageUpdateResource>();

                string apiKey = "my-api-key";

                await resource.Delete(
                    "MyPackage",
                    "1.0.0-beta",
                    getApiKey : packageSource => apiKey,
                    confirm : packageSource => true,
                    noServiceEndpoint : false,
                    logger);

                #endregion
            }
            catch (Exception e)
            {
                Console.WriteLine($"Deleting package failed: {e}");
            }
        }
Beispiel #4
0
        private static async Task PushPackageAsync()
        {
            try
            {
                // This code region is referenced by the NuGet docs. Please update the docs if you rename the region
                // or move it to a different file.
                #region PushPackage
                ILogger           logger            = NullLogger.Instance;
                CancellationToken cancellationToken = CancellationToken.None;

                SourceCacheContext    cache      = new SourceCacheContext();
                SourceRepository      repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
                PackageUpdateResource resource   = await repository.GetResourceAsync <PackageUpdateResource>();

                string apiKey = "my-api-key";

                await resource.Push(
                    "MyPackage.nupkg",
                    symbolSource : null,
                    timeoutInSecond : 5 * 60,
                    disableBuffering : false,
                    getApiKey : packageSource => apiKey,
                    getSymbolApiKey : packageSource => null,
                    noServiceEndpoint : false,
                    skipDuplicate : false,
                    symbolPackageUpdateResource : null,
                    logger);

                #endregion
            }
            catch (Exception e)
            {
                Console.WriteLine($"Pushing package failed: {e}");
            }
        }
Beispiel #5
0
 private async Task PushPackagesAsync(PackageUpdateResource packageUpdateResource)
 {
     while (_packages.TryTake(out var package))
     {
         await PushPackageAsync(packageUpdateResource, package);
     }
 }
 private static async Task PushPackagesAsync(
     PackageUpdateResource packageUpdateResource,
     ConcurrentBag <PackageInfo> concurrentBag,
     string apiKey)
 {
     while (concurrentBag.TryTake(out var package))
     {
         await PushPackageAsync(packageUpdateResource, package, apiKey);
     }
 }
Beispiel #7
0
 NuGetFeed(NuGetArtifactType type, PackageSource s)
     : base(type)
 {
     _packageSource    = s;
     _sourceRepository = new SourceRepository(_packageSource, _providers);
     _updater          = new AsyncLazy <PackageUpdateResource>(async() =>
     {
         PackageUpdateResource r = await _sourceRepository.GetResourceAsync <PackageUpdateResource>();
         // TODO: Update for next NuGet version?
         // r.Settings = _settings;
         return(r);
     });
 }
 public static async Task UploadPackageAsync
 (
     PackageUpdateResource uploadResource,
     bool noServiceEndpoint,
     string packagePath,
     string apiKey,
     SymbolPackageUpdateResourceV3 symbolsResource
 )
 {
     if (symbolsResource is not null && IsValidSymbols(packagePath))
     {
         await uploadResource.Push
         (
             packagePath, symbolsResource.SourceUri.ToString(), 600, false, _ => apiKey, _ => apiKey,
             noServiceEndpoint, true, symbolsResource,
             NukeLoggerAdapter.Instance
         );
     }
        private static async Task PushPackageAsync(
            PackageUpdateResource packageUpdateResource,
            PackageInfo package,
            string apiKey)
        {
            for (var attempt = 1; attempt <= _maxRetryCount; attempt++)
            {
                // Fail fast if a parallel push operation has already failed
                _packagePushCancellationTokenSource.Token.ThrowIfCancellationRequested();

                Log.WriteInformation($"Attempting to publish package {package.Identity} (Attempt: {attempt})");

                try
                {
                    await packageUpdateResource.Push(
                        package.PackagePath,
                        symbolSource : null,
                        timeoutInSecond : (int)_packagePushTimeout.TotalSeconds,
                        disableBuffering : false,
                        getApiKey : _ => apiKey,
                        getSymbolApiKey : _ => null,
                        log : NullLogger.Instance);

                    Log.WriteInformation($"Done publishing package {package.Identity}");
                    return;
                }
                catch (Exception ex) when(attempt < _maxRetryCount)  // allow exception to be thrown at the last attempt
                {
                    // Write in a single call as multiple WriteLine statements can get interleaved causing
                    // confusion when reading logs.
                    Log.WriteInformation(
                        $"Attempt {attempt} failed to publish package {package.Identity}." +
                        Environment.NewLine +
                        ex.ToString() +
                        Environment.NewLine +
                        "Retrying...");
                }
                catch
                {
                    _packagePushCancellationTokenSource.Cancel();
                    throw;
                }
            }
        }
        public static async Task Run(
            ISettings settings,
            IPackageSourceProvider sourceProvider,
            string packagePath,
            string source,
            string apiKey,
            int timeoutSeconds,
            bool disableBuffering,
            bool noSymbols,
            ILogger logger)
        {
            source = CommandRunnerUtility.ResolveSource(sourceProvider, source);

            if (timeoutSeconds == 0)
            {
                timeoutSeconds = 5 * 60;
            }

            PackageUpdateResource packageUpdateResource = await CommandRunnerUtility.GetPackageUpdateResource(sourceProvider, source);

            // only push to SymbolSource when the actual package is being pushed to the official NuGet.org
            string symbolsSource = string.Empty;

            Uri sourceUri = packageUpdateResource.SourceUri;

            if (!noSymbols && !sourceUri.IsFile && sourceUri.IsAbsoluteUri)
            {
                if (sourceUri.Host.Equals(NuGetConstants.NuGetHostName, StringComparison.OrdinalIgnoreCase) || // e.g. nuget.org
                    sourceUri.Host.EndsWith("." + NuGetConstants.NuGetHostName, StringComparison.OrdinalIgnoreCase))    // *.nuget.org, e.g. www.nuget.org
                {
                    symbolsSource = NuGetConstants.DefaultSymbolServerUrl;
                }
            }

            await packageUpdateResource.Push(
                packagePath,
                symbolsSource,
                timeoutSeconds,
                disableBuffering,
                endpoint => CommandRunnerUtility.GetApiKey(settings, endpoint, apiKey),
                logger);
        }
Beispiel #11
0
        private async Task PushPackageAsync(PackageUpdateResource packageUpdateResource, PackageInfo package)
        {
            for (var attempt = 1; attempt <= _maxRetryCount; attempt++)
            {
                // Fail fast if a parallel push operation has already failed
                _packagePushCancellationTokenSource.Token.ThrowIfCancellationRequested();

                Log.LogMessage($"Attempting to publish package {package.Identity} (Attempt: {attempt})");

                try
                {
                    await packageUpdateResource.Push(
                        package.PackagePath,
                        symbolSource : null,
                        timeoutInSecond : TimeoutSeconds,
                        disableBuffering : false,
                        getApiKey : _ => ApiKey,
                        getSymbolApiKey : _ => null,
                        noServiceEndpoint : false,
                        log : NullLogger.Instance);

                    Log.LogMessage(MessageImportance.High, $"Published package {package.Identity}");

                    return;
                }
                catch (Exception ex) when(attempt < _maxRetryCount)  // allow exception to be thrown at the last attempt
                {
                    Log.LogMessage(
                        MessageImportance.High,
                        $"Attempt {attempt} failed to publish package {package.Identity}." +
                        Environment.NewLine +
                        ex +
                        Environment.NewLine +
                        "Retrying...");
                }
                catch
                {
                    _packagePushCancellationTokenSource.Cancel();
                    throw;
                }
            }
        }
Beispiel #12
0
        public static async Task Run(
            ISettings settings,
            IPackageSourceProvider sourceProvider,
            string packageId,
            string packageVersion,
            string source,
            string apiKey,
            bool nonInteractive,
            Func <string, bool> confirmFunc,
            ILogger logger)
        {
            source = CommandRunnerUtility.ResolveSource(sourceProvider, source);

            PackageUpdateResource packageUpdateResource = await CommandRunnerUtility.GetPackageUpdateResource(sourceProvider, source);

            await packageUpdateResource.Delete(
                packageId,
                packageVersion,
                (endpoint) => CommandRunnerUtility.GetApiKey(settings, endpoint, apiKey),
                (desc) => nonInteractive?true : confirmFunc(desc),
                logger);
        }
Beispiel #13
0
    private static async Task <int> PushAllAsync(string source, string symbolSource, IReadOnlyList <string> packages, string apiKey)
    {
        SourceRepository sourceRepository = ConfigureSourceRepository(source);

        PackageUpdateResource packageUpdateResource = await sourceRepository.GetResourceAsync <PackageUpdateResource>();

        Console.WriteLine($"Pushing Packages to: {packageUpdateResource.SourceUri}");

        SymbolPackageUpdateResourceV3?symbolPackageUpdateResource = await GetSymbolPackageUpdateSourceAsync(sourceRepository);

        PackageUpdateResource?symbolPackageUpdateResourceAsPackage = null;

        SourceRepository?symbolSourceRepository = null;

        if (!string.IsNullOrWhiteSpace(symbolSource) && symbolPackageUpdateResource == null)
        {
            symbolSourceRepository = ConfigureSourceRepository(symbolSource);

            PackageUpdateResource?resource = await symbolSourceRepository.GetResourceAsync <PackageUpdateResource>();

            if (resource?.SourceUri != null)
            {
                Console.WriteLine($"Pushing Symbol Packages to: {resource.SourceUri}");
                symbolPackageUpdateResourceAsPackage = resource;
            }
        }

        IReadOnlyList <string> symbolPackages    = ExtractSymbolPackages(packages);
        IReadOnlyList <string> nonSymbolPackages = ExtractProductionPackages(packages);

        IEnumerable <Task <(string package, bool success)> > tasks = BuildUploadTasks(symbolSourceRepository: symbolSourceRepository,
                                                                                      symbolPackageUpdateResource: symbolPackageUpdateResource,
                                                                                      symbolPackageUpdateResourceAsPackage: symbolPackageUpdateResourceAsPackage,
                                                                                      nonSymbolPackages: nonSymbolPackages,
                                                                                      symbolPackages: symbolPackages,
                                                                                      apiKey: apiKey,
                                                                                      packageUpdateResource: packageUpdateResource);

        (string package, bool success)[] results = await Task.WhenAll(tasks);
Beispiel #14
0
        public async override Task <Tuple <bool, INuGetResource> > TryCreate(
            SourceRepository source,
            CancellationToken token)
        {
            HttpSource            httpSource            = null;
            PackageUpdateResource packageUpdateResource = null;
            var sourceUri = source.PackageSource?.Source;

            if (!string.IsNullOrEmpty(sourceUri))
            {
                if (source.PackageSource.IsHttp)
                {
                    var httpSourceResource = await source.GetResourceAsync <HttpSourceResource>(token);

                    httpSource = httpSourceResource.HttpSource;
                }
                packageUpdateResource = new PackageUpdateResource(sourceUri, httpSource);
            }

            var result = new Tuple <bool, INuGetResource>(packageUpdateResource != null, packageUpdateResource);

            return(result);
        }
Beispiel #15
0
        public override async Task <Tuple <bool, INuGetResource> > TryCreate(
            SourceRepository source,
            CancellationToken token)
        {
            PackageUpdateResource packageUpdateResource = null;

            var serviceIndex = await source.GetResourceAsync <ServiceIndexResourceV3>(token);

            if (serviceIndex != null)
            {
                var baseUrl = serviceIndex[ServiceTypes.PackagePublish].FirstOrDefault();

                HttpSource httpSource = null;
                var        sourceUri  = baseUrl?.AbsoluteUri;
                if (!string.IsNullOrEmpty(sourceUri))
                {
                    if (!(new Uri(sourceUri)).IsFile)
                    {
                        var httpSourceResource = await source.GetResourceAsync <HttpSourceResource>(token);

                        httpSource = httpSourceResource.HttpSource;
                    }
                    packageUpdateResource = new PackageUpdateResource(sourceUri, httpSource);
                }
                else
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                      Strings.PackageServerEndpoint_NotSupported,
                                                                      source));
                }
            }

            var result = new Tuple <bool, INuGetResource>(packageUpdateResource != null, packageUpdateResource);

            return(result);
        }
Beispiel #16
0
        static async Task PushAsync(
            FindPackageByIdResource findPackageById,
            PackageUpdateResource packageUpdate,
            string apiKey,
            PackageIdentity identity,
            string nupkgPath,
            object pushedVersionsLock,
            Func <string, SemaphoreSlim> getIdSemaphore,
            Dictionary <string, Task <HashSet <NuGetVersion> > > pushedVersions,
            object consoleLock,
            bool allowRetry)
        {
            // Get the list of existing versions.
            Task <HashSet <NuGetVersion> > versionsTask;

            lock (pushedVersionsLock)
            {
                if (!pushedVersions.TryGetValue(identity.Id, out versionsTask))
                {
                    versionsTask = GetVersionsAsync(findPackageById, identity.Id, consoleLock);
                    pushedVersions.Add(identity.Id, versionsTask);
                }
            }

            var versions = await versionsTask;

            lock (pushedVersionsLock)
            {
                if (versions.Contains(identity.Version))
                {
                    return;
                }
            }

            Console.WriteLine($"  Pushing {identity.Id} {identity.Version.ToNormalizedString()}...");
            try
            {
                var idSemaphore = getIdSemaphore(identity.Id);
                await idSemaphore.WaitAsync();

                try
                {
                    await packageUpdate.Push(
                        nupkgPath,
                        symbolSource : string.Empty,
                        timeoutInSecond : 300,
                        disableBuffering : false,
                        getApiKey : _ => apiKey,
                        getSymbolApiKey : null,
                        noServiceEndpoint : false,
                        skipDuplicate : false,
                        symbolPackageUpdateResource : null,
                        log : NullLogger.Instance); // new ConsoleLogger());
                }
                finally
                {
                    idSemaphore.Release();
                }

                lock (pushedVersionsLock)
                {
                    versions.Add(identity.Version);
                }
            }
            catch (HttpRequestException ex) when(ex.Message.StartsWith("Response status code does not indicate success: 409 ") && allowRetry)
            {
                await PushAsync(
                    findPackageById,
                    packageUpdate,
                    apiKey,
                    identity,
                    nupkgPath,
                    pushedVersionsLock,
                    getIdSemaphore,
                    pushedVersions,
                    consoleLock,
                    allowRetry : false);
            }
            catch (Exception ex)
            {
                lock (consoleLock)
                {
                    Console.WriteLine($"  Push of {identity.Id} {identity.Version.ToNormalizedString()} ({new FileInfo(nupkgPath).Length} bytes) failed with exception:");
                    Console.WriteLine(ex);
                }
                return;
            }
        }