Beispiel #1
0
        public sealed override bool Execute()
        {
            try
            {
                HelixApi     = GetHelixApi();
                AnonymousApi = ApiFactory.GetAnonymous(BaseUri);
                System.Threading.Tasks.Task.Run(() => ExecuteCore(_cancel.Token)).GetAwaiter().GetResult();
            }
            catch (HttpOperationException ex) when(ex.Response.StatusCode == HttpStatusCode.Unauthorized)
            {
                Log.LogError(FailureCategory.Build, "Helix operation returned 'Unauthorized'. Did you forget to set HelixAccessToken?");
            }
            catch (HttpOperationException ex) when(ex.Response.StatusCode == HttpStatusCode.Forbidden)
            {
                Log.LogError(FailureCategory.Build, "Helix operation returned 'Forbidden'.");
            }
            catch (OperationCanceledException ocex) when(ocex.CancellationToken == _cancel.Token)
            {
                // Canceled
                return(false);
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(FailureCategory.Helix, ex, true, true, null);
            }

            return(!Log.HasLoggedErrors);
        }
Beispiel #2
0
    protected HelixTestBase(string helixType) : base()
    {
        var helixSource = GetEnvironmentVariable("MONO_HELIX_SOURCE");

        if (helixSource.StartsWith("pr/"))
        {
            // workaround for https://github.com/dotnet/arcade/issues/1392
            var storage      = new Storage((HelixApi)_api);
            var anonymousApi = ApiFactory.GetAnonymous();
            typeof(HelixApi).GetProperty("Storage").SetValue(anonymousApi, storage, null);
            _api = anonymousApi;
        }

        var build = _api.Job.Define()
                    .WithSource(helixSource)
                    .WithType(helixType)
                    .WithBuild(GetEnvironmentVariable("MONO_HELIX_BUILD_MONIKER"));

        _job = build
               .WithTargetQueue(GetEnvironmentVariable("MONO_HELIX_TARGET_QUEUE"))
               .WithCreator(GetEnvironmentVariable("MONO_HELIX_CREATOR"))
               .WithCorrelationPayloadDirectory(GetEnvironmentVariable("MONO_HELIX_TEST_PAYLOAD_DIRECTORY"))
               .WithCorrelationPayloadFiles(GetEnvironmentVariable("MONO_HELIX_XUNIT_REPORTER_PATH"))
               // these are well-known properties used by Mission Control
               .WithProperty("architecture", GetEnvironmentVariable("MONO_HELIX_ARCHITECTURE"))
               .WithProperty("operatingSystem", GetEnvironmentVariable("MONO_HELIX_OPERATINGSYSTEM"));
    }
Beispiel #3
0
 protected Task <T> RetryAsync <T>(Func <Task <T> > function)
 {
     // Grab the retry logic from the helix api client
     return(ApiFactory.GetAnonymous().RetryAsync(
                async() => await function(),
                ex => Log.LogMessage(MessageImportance.Low, $"Azure Dev Ops Operation failed: {ex}\nRetrying..."),
                CancellationToken.None));
 }
Beispiel #4
0
        private IHelixApi GetHelixApi()
        {
            if (string.IsNullOrEmpty(AccessToken))
            {
                Log.LogMessage(MessageImportance.Low, "No AccessToken provided, using anonymous access to helix api.");
                return(ApiFactory.GetAnonymous(BaseUri));
            }

            Log.LogMessage(MessageImportance.Low, "Authenticating to helix api using provided AccessToken");
            return(ApiFactory.GetAuthenticated(BaseUri, AccessToken));
        }
Beispiel #5
0
 protected Task RetryAsync(Func <Task> function)
 {
     // Grab the retry logic from the helix api client
     return(ApiFactory.GetAnonymous()
            .RetryAsync(
                async() =>
     {
         await function();
         return false;         // the retry function requires a return, give it one
     },
                ex => Log.LogMessage(MessageImportance.Low, $"Azure Dev Ops Operation failed: {ex}\nRetrying...")));
 }
Beispiel #6
0
        /// <summary>
        /// Get the HelixApi based on the settings of this pool provider
        /// </summary>
        /// <returns>For now, just an unauthenticated api client</returns>
        private IHelixApi GetHelixApi(bool isAnonymous)
        {
            IHelixApi api;

            if (isAnonymous)
            {
                api = ApiFactory.GetAnonymous(_configuration.HelixEndpoint);
            }
            else
            {
                api = ApiFactory.GetAuthenticated(_configuration.HelixEndpoint, _configuration.ApiAuthorizationPat);
            }
            return(api);
        }
        public static async Task <TestParameters> GetAsync()
        {
            IConfiguration userSecrets = new ConfigurationBuilder()
                                         .AddUserSecrets <TestParameters>()
                                         .Build();

            string maestroBaseUri    = Environment.GetEnvironmentVariable("MAESTRO_BASEURI") ?? userSecrets["MAESTRO_BASEURI"] ?? "https://maestro-int.westus2.cloudapp.azure.com";
            string maestroToken      = Environment.GetEnvironmentVariable("MAESTRO_TOKEN") ?? userSecrets["MAESTRO_TOKEN"];
            string githubToken       = Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? userSecrets["GITHUB_TOKEN"];
            string darcPackageSource = Environment.GetEnvironmentVariable("DARC_PACKAGE_SOURCE");
            string azdoToken         = Environment.GetEnvironmentVariable("AZDO_TOKEN") ?? userSecrets["AZDO_TOKEN"];

            var testDir = TemporaryDirectory.Get();
            var testDirSharedWrapper = Shareable.Create(testDir);

            IMaestroApi maestroApi = maestroToken == null
                ? ApiFactory.GetAnonymous(maestroBaseUri)
                : ApiFactory.GetAuthenticated(maestroBaseUri, maestroToken);

            string darcVersion = await maestroApi.Assets.GetDarcVersionAsync();

            string dotnetExe = await TestHelpers.Which("dotnet");

            var toolInstallArgs = new List <string>
            {
                "tool", "install",
                "--tool-path", testDirSharedWrapper.Peek() !.Directory,
                "--version", darcVersion,
                "Microsoft.DotNet.Darc",
            };

            if (!string.IsNullOrEmpty(darcPackageSource))
            {
                toolInstallArgs.Add("--add-source");
                toolInstallArgs.Add(darcPackageSource);
            }
            await TestHelpers.RunExecutableAsync(dotnetExe, toolInstallArgs.ToArray());

            string darcExe = Path.Join(testDirSharedWrapper.Peek() !.Directory, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "darc.exe" : "darc");

            Assembly assembly  = typeof(TestParameters).Assembly;
            var      githubApi =
                new GitHubClient(
                    new ProductHeaderValue(assembly.GetName().Name, assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion),
                    new InMemoryCredentialStore(new Credentials(githubToken)));
            var azDoClient =
                new Microsoft.DotNet.DarcLib.AzureDevOpsClient(await TestHelpers.Which("git"), azdoToken, new NUnitLogger(), testDirSharedWrapper.TryTake() !.Directory);

            return(new TestParameters(darcExe, await TestHelpers.Which("git"), maestroBaseUri, maestroToken !, githubToken, maestroApi, githubApi, azDoClient, testDir, azdoToken));
        }
Beispiel #8
0
        /// <summary>
        /// Get the HelixApi based on the settings of this pool provider
        /// </summary>
        /// <returns>For now, just an unauthenticated api client</returns>
        private IHelixApi GetHelixApi(bool isAnonymous)
        {
            IHelixApi api;

            if (isAnonymous)
            {
                api = ApiFactory.GetAnonymous();
            }
            else
            {
                api = ApiFactory.GetAuthenticated(_configuration.ApiAuthorizationPat);
            }
            // Alter the base URI based on configuration.  It's also useful to note that in the current version of the API, the endpoint isn't
            // defaulted to https, and so unless this is done every request will fail.
            api.BaseUri = new Uri(_configuration.HelixEndpoint);
            return(api);
        }
Beispiel #9
0
        public sealed override bool Execute()
        {
            try
            {
                HelixApi     = GetHelixApi();
                AnonymousApi = ApiFactory.GetAnonymous(BaseUri);
                System.Threading.Tasks.Task.Run(() => ExecuteCore(_cancel.Token)).GetAwaiter().GetResult();
            }
            catch (RestApiException ex) when(ex.Response.Status == (int)HttpStatusCode.Unauthorized)
            {
                Log.LogError(FailureCategory.Build, "Helix operation returned 'Unauthorized'. Did you forget to set HelixAccessToken?");
            }
            catch (RestApiException ex) when(ex.Response.Status == (int)HttpStatusCode.Forbidden)
            {
                Log.LogError(FailureCategory.Build, "Helix operation returned 'Forbidden'.");
            }
            catch (OperationCanceledException ocex) when(ocex.CancellationToken == _cancel.Token)
            {
                // Canceled
                return(false);
            }
            catch (ArgumentException argEx) when(argEx.Message.StartsWith("Helix API does not contain an entry "))
            {
                if (FailOnMissingTargetQueue)
                {
                    Log.LogError(FailureCategory.Build, argEx.Message);
                }
                else
                {
                    Log.LogWarning($"{argEx.Message} (FailOnMissingTargetQueue is false, so this is just a warning.)");
                }
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(FailureCategory.Helix, ex, true, true, null);
            }

            return(!Log.HasLoggedErrors);
        }
        public override bool Execute()
        {
            try
            {
                HelixApi     = GetHelixApi();
                AnonymousApi = ApiFactory.GetAnonymous(BaseUri);

                if (!InAzurePipeline)
                {
                    Log.LogError("This task must be run inside an Azure Pipelines Build");
                }
                else
                {
                    using (var client = CreateHttpClient())
                    {
                        ExecuteCoreAsync(client, _cancel.Token).GetAwaiter().GetResult();
                    }
                }
            }
            catch (HttpOperationException ex) when(ex.Response.StatusCode == HttpStatusCode.Unauthorized)
            {
                Log.LogError("Helix operation returned 'Unauthorized'. Did you forget to set HelixAccessToken?");
            }
            catch (HttpOperationException ex) when(ex.Response.StatusCode == HttpStatusCode.Forbidden)
            {
                Log.LogError("Helix operation returned 'Forbidden'.");
            }
            catch (OperationCanceledException ocex) when(ocex.CancellationToken == _cancel.Token)
            {
                // Canceled
                return(false);
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex, true, true, null);
            }

            return(!Log.HasLoggedErrors);
        }
Beispiel #11
0
        public sealed override bool Execute()
        {
            try
            {
                HelixApi     = GetHelixApi();
                AnonymousApi = ApiFactory.GetAnonymous(BaseUri);
                System.Threading.Tasks.Task.Run(ExecuteCore).GetAwaiter().GetResult();
            }
            catch (HttpOperationException ex) when(ex.Response.StatusCode == HttpStatusCode.Unauthorized)
            {
                Log.LogError("Helix operation returned 'Unauthorized'. Did you forget to set HelixAccessToken?");
            }
            catch (HttpOperationException ex) when(ex.Response.StatusCode == HttpStatusCode.Forbidden)
            {
                Log.LogError("Helix operation returned 'Forbidden'.");
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex, true, true, null);
            }

            return(!Log.HasLoggedErrors);
        }
Beispiel #12
0
        public async ValueTask GetHelixPayloads(string jobId, List <string> workItems, string downloadDir)
        {
            if (!Path.IsPathFullyQualified(downloadDir))
            {
                downloadDir = Path.Combine(Environment.CurrentDirectory, downloadDir);
            }

            IHelixApi  helixApi   = _token.IsNone ? ApiFactory.GetAnonymous() : ApiFactory.GetAuthenticated(_token.Token);
            JobDetails jobDetails = await helixApi.Job.DetailsAsync(jobId).ConfigureAwait(false);

            string?jobListFile = jobDetails.JobList;

            if (string.IsNullOrEmpty(jobListFile))
            {
                throw new ArgumentException($"Couldn't find job list for job {jobId}, if it is an internal job, please use a helix access token from https://helix.dot.net/Account/Tokens");
            }

            using MemoryStream memoryStream = await _client.DownloadFileAsync(jobListFile).ConfigureAwait(false);

            using StreamReader reader = new StreamReader(memoryStream);
            string jobListJson = await reader.ReadToEndAsync().ConfigureAwait(false);

            WorkItemInfo[] workItemsInfo = JsonConvert.DeserializeObject <WorkItemInfo[]>(jobListJson);

            if (workItemsInfo.Length > 0)
            {
                Directory.CreateDirectory(downloadDir);
                string correlationDir = Path.Combine(downloadDir, "correlation-payload");
                Directory.CreateDirectory(correlationDir);

                // download correlation payload
                JObject correlationPayload = workItemsInfo[0].CorrelationPayloadUrisWithDestinations ?? new JObject();
                foreach (JProperty property in correlationPayload.Children())
                {
                    string url             = property.Name;
                    Uri    uri             = new Uri(url);
                    string fileName        = uri.Segments[^ 1];