// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Net.Http; using System.Threading.Tasks; using Microsoft.Build.Framework; using Newtonsoft.Json.Linq; using Task = Microsoft.Build.Utilities.Task; namespace Microsoft.DotNet.Cli.Build.UploadToLinuxPackageRepository { public class UploadToLinuxPackageRepository : Task { /// /// The Azure repository service user name. /// [Required] public string Username { get; set; } /// /// The Azure repository service Password. /// [Required] public string Password { get; set; } /// /// The Azure repository service URL ex: "tux-devrepo.corp.microsoft.com". /// [Required] public string Server { get; set; } [Required] public string RepositoryId { get; set; } [Required] public string PathOfPackageToUpload { get; set; } [Required] public string PackageNameInLinuxPackageRepository { get; set; } [Required] public string PackageVersionInLinuxPackageRepository { get; set; } public override bool Execute() { ExecuteAsyncWithRetry().GetAwaiter().GetResult(); return true; } private async System.Threading.Tasks.Task ExecuteAsyncWithRetry() { await ExponentialRetry.ExecuteWithRetry( UploadAndAddpackageAndEnsureItIsReady, s => s == "", maxRetryCount: 3, timer: () => ExponentialRetry.Timer(ExponentialRetry.Intervals), taskDescription: $"running {nameof(UploadAndAddpackageAndEnsureItIsReady)}"); } private async Task UploadAndAddpackageAndEnsureItIsReady() { try { Log.LogMessage( MessageImportance.High, "Begin uploading Linux Package to feed service, RepositoryId {0}, Server {1}, Package to upload {2}.", RepositoryId, Server, PathOfPackageToUpload); var linuxPackageRepositoryDestiny = new LinuxPackageRepositoryDestiny(Username, Password, Server, RepositoryId); var uploadResponse = await new LinuxPackageRepositoryHttpPrepare( linuxPackageRepositoryDestiny, new FileUploadStrategy(PathOfPackageToUpload)).RemoteCall(); var idInRepositoryService = new IdInRepositoryService(JObject.Parse(uploadResponse)["id"].ToString()); var addPackageResponse = await new LinuxPackageRepositoryHttpPrepare( linuxPackageRepositoryDestiny, new AddPackageStrategy( idInRepositoryService, PackageNameInLinuxPackageRepository, PackageVersionInLinuxPackageRepository, linuxPackageRepositoryDestiny.RepositoryId)).RemoteCall(); var queueResourceLocation = new QueueResourceLocation(addPackageResponse); Func> pullQueuedPackageStatus = new LinuxPackageRepositoryHttpPrepare( linuxPackageRepositoryDestiny, new PullQueuedPackageStatus(queueResourceLocation)).RemoteCall; await ExponentialRetry.ExecuteWithRetry( pullQueuedPackageStatus, s => s == "fileReady", 5, () => ExponentialRetry.Timer(ExponentialRetry.Intervals), $"PullQueuedPackageStatus location: {queueResourceLocation.Location}"); Log.LogMessage( MessageImportance.High, "Upload to feed service is completed, queue resource location {0}", queueResourceLocation.Location); return ""; } catch (FailedToAddPackageToPackageRepositoryException e) { return e.ToString(); } catch (HttpRequestException e) { return e.ToString(); } } } }