Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsBuild"/> class.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="settings">Settings for accessing AzureDevOps.</param>
        /// <param name="build">The build.</param>
        internal AzureDevOpsBuild(ICakeLog log, AzureDevOpsBuildsSettings settings, Build build)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));
            build.NotNull(nameof(build));

            this.log   = log;
            this.build = build;
            this.buildClientFactory = new BuildClientFactory();
            this.testClientFactory  = new TestManagementClientFactory();
            this.credentials        = settings.Credentials;
            this.CollectionUrl      = settings.CollectionUrl;
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsBuild"/> class.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="settings">Settings for accessing AzureDevOps.</param>
        /// <param name="buildClientFactory">A factory to communicate with build client.</param>
        /// <param name="testManagementClientFactory">A factory to communicate with test management client.</param>
        /// <exception cref="AzureDevOpsBuildNotFoundException">If <see cref="AzureDevOpsBuildSettings.ThrowExceptionIfBuildCouldNotBeFound"/>
        /// is set to <c>true</c> and no build could be found.</exception>
        internal AzureDevOpsBuild(
            ICakeLog log,
            AzureDevOpsBuildSettings settings,
            IBuildClientFactory buildClientFactory,
            ITestManagementClientFactory testManagementClientFactory)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));
            buildClientFactory.NotNull(nameof(buildClientFactory));
            testManagementClientFactory.NotNull(nameof(testManagementClientFactory));

            this.log = log;
            this.buildClientFactory = buildClientFactory;
            this.testClientFactory  = testManagementClientFactory;
            this.credentials        = settings.Credentials;
            this.CollectionUrl      = settings.CollectionUrl;
            this.throwExceptionIfBuildCouldNotBeFound = settings.ThrowExceptionIfBuildCouldNotBeFound;

            using (var buildClient = this.buildClientFactory.CreateBuildClient(settings.CollectionUrl, settings.Credentials, out var authorizedIdenity))
            {
                this.log.Verbose(
                    "Authorized Identity:\n  Id: {0}\n  DisplayName: {1}",
                    authorizedIdenity.Id,
                    authorizedIdenity.DisplayName);

                try
                {
                    if (settings.ProjectGuid != Guid.Empty)
                    {
                        this.log.Verbose("Read build with ID {0} from project with ID {1}", settings.BuildId, settings.ProjectGuid);
                        this.build =
                            buildClient
                            .GetBuildAsync(
                                settings.ProjectGuid,
                                settings.BuildId)
                            .ConfigureAwait(false)
                            .GetAwaiter()
                            .GetResult();
                    }
                    else if (!string.IsNullOrWhiteSpace(settings.ProjectName))
                    {
                        this.log.Verbose("Read build with ID {0} from project with name {1}", settings.BuildId, settings.ProjectName);
                        this.build =
                            buildClient
                            .GetBuildAsync(
                                settings.ProjectName,
                                settings.BuildId)
                            .ConfigureAwait(false)
                            .GetAwaiter()
                            .GetResult();
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException(
                                  nameof(settings),
                                  "Either ProjectGuid or ProjectName needs to be set");
                    }
                }
                catch (BuildNotFoundException ex)
                {
                    if (this.throwExceptionIfBuildCouldNotBeFound)
                    {
                        throw new AzureDevOpsBuildNotFoundException("Build not found", ex);
                    }

                    this.log.Warning("Could not find build");
                    return;
                }
            }

            this.log.Verbose(
                "Build information:\n  Id: {0}\n  BuildNumber: {1}",
                this.build.Id,
                this.build.BuildNumber);
        }