/// <summary>
        /// Returns the build definition for the <paramref name="settings"/>.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="settings">Settings for accessing AzureDevOps.</param>
        /// <returns>The build definition for the BuildDefinitionName on <paramref name="settings"/>.
        /// <c>null</c> if the BuildDefinitionName was not set or no build definition was found.</returns>
        private static AzureDevOpsBuildDefinition GetBuildDefinition(
            ICakeLog log,
            AzureDevOpsBuildsSettings settings)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));

            var buildDefinitions =
                AzureDevOpsBuildsDefinitionHelper
                .GetAzureDevOpsBuildDefinitions(
                    log,
                    settings);

            var buildDefinition =
                buildDefinitions
                .SingleOrDefault(x => x.Name.Equals(settings.BuildDefinitionName, StringComparison.InvariantCultureIgnoreCase));

            if (buildDefinition == null)
            {
                log.Verbose(
                    "Build definition '{0}' not found",
                    settings.BuildDefinitionName);
            }

            log.Verbose(
                "Build definition '{0}' found",
                settings.BuildDefinitionName);

            return(buildDefinition);
        }
        /// <summary>
        /// Create a pull request.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="settings">Settings for accessing AzureDevOps.</param>
        /// <returns>Instance of the created pull request.</returns>
        public static AzureDevOpsPullRequest Create(ICakeLog log, AzureDevOpsCreatePullRequestSettings settings)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));

            return(Create(log, new GitClientFactory(), settings));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Orchestrator"/> class.
        /// </summary>
        /// <param name="log">Cake log instance.</param>
        /// <param name="pullRequestSystem">Object for accessing pull request system.
        /// <c>null</c> if only issues should be read.</param>
        /// <param name="settings">Settings.</param>
        public Orchestrator(
            ICakeLog log,
            IPullRequestSystem pullRequestSystem,
            ReportIssuesToPullRequestSettings settings)
        {
#pragma warning disable SA1123 // Do not place regions within elements
            #region DupFinder Exclusion
#pragma warning restore SA1123 // Do not place regions within elements

            log.NotNull(nameof(log));
            pullRequestSystem.NotNull(nameof(pullRequestSystem));
            settings.NotNull(nameof(settings));

            this.log = log;
            this.pullRequestSystem = pullRequestSystem;
            this.settings          = settings;

            #endregion

            // Initialize pull request system.
            this.log.Verbose("Initialize pull request system...");
            this.pullRequestSystemInitialized = this.pullRequestSystem.Initialize(this.settings);
            if (!this.pullRequestSystemInitialized)
            {
                this.log.Warning("Error initializing the pull request system.");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BasePullRequestSystemCapability{T}"/> class.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="pullRequestSystem">Pull request system to which this capability belongs.</param>
        public BasePullRequestSystemCapability(ICakeLog log, T pullRequestSystem)
        {
            log.NotNull(nameof(log));
            pullRequestSystem.NotNull(nameof(pullRequestSystem));

            this.Log = log;
            this.PullRequestSystem = pullRequestSystem;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IssueReportCreator"/> class.
        /// </summary>
        /// <param name="log">Cake log instance.</param>
        /// <param name="settings">Settings to use.</param>
        public IssueReportCreator(
            ICakeLog log,
            CreateIssueReportSettings settings)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));

            this.log      = log;
            this.settings = settings;
        }
Exemplo n.º 6
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.credentials        = settings.Credentials;
            this.CollectionUrl      = settings.CollectionUrl;
        }
        /// <summary>
        /// Returns the build definition for the <paramref name="settings"/>.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="buildHttpClient">The Http build client.</param>
        /// <param name="settings">Settings for accessing AzureDevOps.</param>
        /// <returns>The build definition for the BuildDefinitionName on <paramref name="settings"/>.
        /// <c>null</c> if the BuildDefinitionName was not set or no build definition was found.</returns>
        private static BuildDefinitionReference GetBuildDefinition(
            ICakeLog log,
            BuildHttpClient buildHttpClient,
            AzureDevOpsBuildsSettings settings)
        {
            log.NotNull(nameof(log));
            buildHttpClient.NotNull(nameof(buildHttpClient));
            settings.NotNull(nameof(settings));

            List <BuildDefinitionReference> buildDefinitions = null;

            if (settings.ProjectGuid != Guid.Empty)
            {
                buildDefinitions =
                    buildHttpClient
                    .GetDefinitionsAsync(settings.ProjectGuid)
                    .ConfigureAwait(false)
                    .GetAwaiter()
                    .GetResult();
            }
            else if (!string.IsNullOrWhiteSpace(settings.ProjectName))
            {
                buildDefinitions =
                    buildHttpClient
                    .GetDefinitionsAsync(settings.ProjectName)
                    .ConfigureAwait(false)
                    .GetAwaiter()
                    .GetResult();
            }
            else
            {
                throw new ArgumentOutOfRangeException(
                          nameof(settings),
                          "Either ProjectGuid or ProjectName needs to be set");
            }

            var buildDefinition =
                buildDefinitions
                .SingleOrDefault(x => x.Name.Equals(settings.BuildDefinitionName, StringComparison.InvariantCultureIgnoreCase));

            if (buildDefinition == null)
            {
                log.Verbose(
                    "Build definition '{0}' not found",
                    settings.BuildDefinitionName);
            }

            log.Verbose(
                "Build definition '{0}' found",
                settings.BuildDefinitionName);

            return(buildDefinition);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IssueFilterer"/> class.
        /// </summary>
        /// <param name="log">The Cake log instance.</param>
        /// <param name="pullRequestSystem">Pull request system to use.</param>
        /// <param name="settings">Settings to use.</param>
        public IssueFilterer(
            ICakeLog log,
            IPullRequestSystem pullRequestSystem,
            ReportIssuesToPullRequestSettings settings)
        {
            log.NotNull(nameof(log));
            pullRequestSystem.NotNull(nameof(pullRequestSystem));
            settings.NotNull(nameof(settings));

            this.log = log;
            this.pullRequestSystem = pullRequestSystem;
            this.settings          = settings;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Orchestrator"/> class.
        /// </summary>
        /// <param name="log">Cake log instance.</param>
        /// <param name="pullRequestSystem">Object for accessing pull request system.
        /// <c>null</c> if only issues should be read.</param>
        public Orchestrator(
            ICakeLog log,
            IPullRequestSystem pullRequestSystem)
        {
#pragma warning disable SA1123 // Do not place regions within elements
            #region DupFinder Exclusion
#pragma warning restore SA1123 // Do not place regions within elements

            log.NotNull(nameof(log));
            pullRequestSystem.NotNull(nameof(pullRequestSystem));

            this.log = log;
            this.pullRequestSystem = pullRequestSystem;

            #endregion
        }
Exemplo n.º 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IssuesReader"/> class.
        /// </summary>
        /// <param name="log">Cake log instance.</param>
        /// <param name="issueProviders">List of issue providers to use.</param>
        /// <param name="settings">Settings to use.</param>
        public IssuesReader(
            ICakeLog log,
            IEnumerable <IIssueProvider> issueProviders,
            RepositorySettings settings)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));

            // ReSharper disable once PossibleMultipleEnumeration
            issueProviders.NotNullOrEmptyOrEmptyElement(nameof(issueProviders));

            this.log      = log;
            this.settings = settings;

            // ReSharper disable once PossibleMultipleEnumeration
            this.issueProviders.AddRange(issueProviders);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Returns the build definitions for the <paramref name="settings"/>.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="settings">Settings for accessing Azure DevOps.</param>
        /// <returns>The build definitions for the the <paramref name="settings"/>.</returns>
        internal static IEnumerable <AzureDevOpsBuildDefinition> GetAzureDevOpsBuildDefinitions(
            ICakeLog log,
            AzureDevOpsBuildsSettings settings)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));

            List <BuildDefinitionReference> buildDefinitions = null;

            using (var buildHttpClient = new BuildClientFactory().CreateBuildClient(settings.CollectionUrl, settings.Credentials))
            {
                if (settings.ProjectGuid != Guid.Empty)
                {
                    buildDefinitions =
                        buildHttpClient
                        .GetDefinitionsAsync(settings.ProjectGuid)
                        .ConfigureAwait(false)
                        .GetAwaiter()
                        .GetResult();
                }
                else if (!string.IsNullOrWhiteSpace(settings.ProjectName))
                {
                    buildDefinitions =
                        buildHttpClient
                        .GetDefinitionsAsync(settings.ProjectName)
                        .ConfigureAwait(false)
                        .GetAwaiter()
                        .GetResult();
                }
                else
                {
                    throw new ArgumentOutOfRangeException(
                              nameof(settings),
                              "Either ProjectGuid or ProjectName needs to be set");
                }

                log.Verbose(
                    "{0} Build definitions found",
                    buildDefinitions.Count);

                return(buildDefinitions
                       .Select(x => x.ToAzureDevOpsBuildDefinition())
                       .ToList());
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IssueFilterer"/> class.
        /// </summary>
        /// <param name="log">The Cake log instance.</param>
        /// <param name="pullRequestSystem">Pull request system to use.</param>
        /// <param name="settings">Settings to use.</param>
        public IssueFilterer(
            ICakeLog log,
            IPullRequestSystem pullRequestSystem,
            IReportIssuesToPullRequestSettings settings)
        {
#pragma warning disable SA1123 // Do not place regions within elements
            #region DupFinder Exclusion
#pragma warning restore SA1123 // Do not place regions within elements

            log.NotNull(nameof(log));
            pullRequestSystem.NotNull(nameof(pullRequestSystem));
            settings.NotNull(nameof(settings));

            this.log = log;
            this.pullRequestSystem = pullRequestSystem;
            this.settings          = settings;

            #endregion
        }
Exemplo n.º 13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Orchestrator"/> class.
        /// </summary>
        /// <param name="log">Cake log instance.</param>
        /// <param name="codeAnalysisProviders">List of code analysis issue providers to use.</param>
        /// <param name="pullRequestSystem">Object for accessing pull request system.
        /// <c>null</c> if only issues should be read.</param>
        /// <param name="settings">Settings.</param>
        public Orchestrator(
            ICakeLog log,
            IEnumerable <ICodeAnalysisProvider> codeAnalysisProviders,
            IPullRequestSystem pullRequestSystem,
            ReportIssuesToPullRequestSettings settings)
        {
            log.NotNull(nameof(log));
            pullRequestSystem.NotNull(nameof(pullRequestSystem));
            settings.NotNull(nameof(settings));

            // ReSharper disable once PossibleMultipleEnumeration
            codeAnalysisProviders.NotNullOrEmptyOrEmptyElement(nameof(codeAnalysisProviders));

            this.log = log;
            this.pullRequestSystem = pullRequestSystem;
            this.settings          = settings;

            // ReSharper disable once PossibleMultipleEnumeration
            this.codeAnalysisProviders.AddRange(codeAnalysisProviders);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Initiates the creation of a new <see cref="FakePullRequestSystem"/>.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <returns>Builder class for creating a new pull request system.</returns>
        public static FakePullRequestSystemBuilder NewPullRequestSystem(ICakeLog log)
        {
            log.NotNull(nameof(log));

            return(new FakePullRequestSystemBuilder(log));
        }
        /// <summary>
        /// Create a pull request.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="gitClientFactory">Git client factory.</param>
        /// <param name="settings">Settings for accessing AzureDevOps.</param>
        /// <returns>Instance of the created pull request.</returns>
        internal static AzureDevOpsPullRequest Create(ICakeLog log, IGitClientFactory gitClientFactory, AzureDevOpsCreatePullRequestSettings settings)
        {
            log.NotNull(nameof(log));
            gitClientFactory.NotNull(nameof(gitClientFactory));
            settings.NotNull(nameof(settings));

            var repositoryDescription = new RepositoryDescription(settings.RepositoryUrl);

            using (var gitClient = gitClientFactory.CreateGitClient(repositoryDescription.CollectionUrl, settings.Credentials))
            {
                var repository =
                    gitClient
                    .GetRepositoryAsync(repositoryDescription.ProjectName, repositoryDescription.RepositoryName)
                    .ConfigureAwait(false)
                    .GetAwaiter()
                    .GetResult();

                if (repository == null)
                {
                    throw new AzureDevOpsException("Could not read repository.");
                }

                var targetBranchName = settings.TargetRefName;
                if (targetBranchName == null)
                {
                    targetBranchName = repository.DefaultBranch;
                }

                var refs =
                    gitClient
                    .GetRefsAsync(
                        repositoryDescription.ProjectName,
                        repositoryDescription.RepositoryName,
                        filter: targetBranchName.Replace("refs/", string.Empty))
                    .ConfigureAwait(false)
                    .GetAwaiter()
                    .GetResult();

                if (refs == null)
                {
                    throw new AzureDevOpsBranchNotFoundException(targetBranchName);
                }

                var targetBranch = refs.SingleOrDefault();

                if (targetBranch == null)
                {
                    throw new AzureDevOpsBranchNotFoundException(targetBranchName);
                }

                var pullRequest = new GitPullRequest()
                {
                    SourceRefName = settings.SourceRefName,
                    TargetRefName = targetBranch.Name,
                    Title         = settings.Title,
                    Description   = settings.Description,
                };

                var createdPullRequest =
                    gitClient
                    .CreatePullRequestAsync(
                        pullRequest,
                        repositoryDescription.ProjectName,
                        repositoryDescription.RepositoryName)
                    .ConfigureAwait(false)
                    .GetAwaiter()
                    .GetResult();

                var pullRequestReadSettings =
                    new AzureDevOpsPullRequestSettings(
                        settings.RepositoryUrl,
                        createdPullRequest.PullRequestId,
                        settings.Credentials);

                return(new AzureDevOpsPullRequest(log, pullRequestReadSettings, gitClientFactory));
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseIssueComponent{T}"/> class.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        protected BaseIssueComponent(ICakeLog log)
        {
            log.NotNull(nameof(log));

            this.Log = log;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsPullRequest"/> class.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="settings">Settings for accessing AzureDevOps.</param>
        /// <param name="gitClientFactory">A factory to communicate with Git client.</param>
        /// <exception cref="AzureDevOpsPullRequestNotFoundException">If <see cref="AzureDevOpsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/>
        /// is set to <c>true</c> and no pull request could be found.</exception>
        internal AzureDevOpsPullRequest(ICakeLog log, AzureDevOpsPullRequestSettings settings, IGitClientFactory gitClientFactory)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));
            gitClientFactory.NotNull(nameof(gitClientFactory));

            this.log = log;
            this.gitClientFactory = gitClientFactory;
            this.credentials      = settings.Credentials;
            this.throwExceptionIfPullRequestCouldNotBeFound = settings.ThrowExceptionIfPullRequestCouldNotBeFound;

            this.repositoryDescription = new RepositoryDescription(settings.RepositoryUrl);

            this.log.Verbose(
                "Repository information:\n  CollectionName: {0}\n  CollectionUrl: {1}\n  ProjectName: {2}\n  RepositoryName: {3}",
                this.repositoryDescription.CollectionName,
                this.repositoryDescription.CollectionUrl,
                this.repositoryDescription.ProjectName,
                this.repositoryDescription.RepositoryName);

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

                if (settings.PullRequestId.HasValue)
                {
                    this.log.Verbose("Read pull request with ID {0}", settings.PullRequestId.Value);
                    this.pullRequest =
                        gitClient
                        .GetPullRequestAsync(
                            this.repositoryDescription.ProjectName,
                            this.repositoryDescription.RepositoryName,
                            settings.PullRequestId.Value)
                        .ConfigureAwait(false)
                        .GetAwaiter()
                        .GetResult();
                }
                else if (!string.IsNullOrWhiteSpace(settings.SourceRefName))
                {
                    this.log.Verbose("Read pull request for branch {0}", settings.SourceRefName);

                    var pullRequestSearchCriteria =
                        new GitPullRequestSearchCriteria()
                    {
                        Status        = Microsoft.TeamFoundation.SourceControl.WebApi.PullRequestStatus.Active,
                        SourceRefName = settings.SourceRefName,
                    };

                    this.pullRequest =
                        gitClient
                        .GetPullRequestsAsync(
                            this.repositoryDescription.ProjectName,
                            this.repositoryDescription.RepositoryName,
                            pullRequestSearchCriteria,
                            top: 1)
                        .ConfigureAwait(false)
                        .GetAwaiter()
                        .GetResult()
                        .SingleOrDefault();
                }
                else
                {
                    throw new ArgumentOutOfRangeException(
                              nameof(settings),
                              "Either PullRequestId or SourceRefName needs to be set");
                }
            }

            if (this.pullRequest == null)
            {
                if (this.throwExceptionIfPullRequestCouldNotBeFound)
                {
                    throw new AzureDevOpsPullRequestNotFoundException("Pull request not found");
                }

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

            this.log.Verbose(
                "Pull request information:\n  PullRequestId: {0}\n  RepositoryId: {1}\n  RepositoryName: {2}\n  SourceRefName: {3}",
                this.pullRequest.PullRequestId,
                this.pullRequest.Repository.Id,
                this.pullRequest.Repository.Name,
                this.pullRequest.SourceRefName);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PullRequestSystem"/> class.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        protected PullRequestSystem(ICakeLog log)
        {
            log.NotNull(nameof(log));

            this.Log = log;
        }
Exemplo n.º 19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseLogFileFormat{TIssueProvider, TSettings}"/> class.
        /// </summary>
        /// <param name="log">The Cake log instance.</param>
        protected BaseLogFileFormat(ICakeLog log)
        {
            log.NotNull(nameof(log));

            this.Log = log;
        }
Exemplo n.º 20
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>
        /// <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)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));
            buildClientFactory.NotNull(nameof(buildClientFactory));

            this.log = log;
            this.buildClientFactory = buildClientFactory;
            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);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="IssueReportCreator"/> class.
        /// </summary>
        /// <param name="log">Cake log instance.</param>
        public IssueReportCreator(ICakeLog log)
        {
            log.NotNull(nameof(log));

            this.log = log;
        }
Exemplo n.º 22
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CodeAnalysisProvider"/> class.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        protected CodeAnalysisProvider(ICakeLog log)
        {
            log.NotNull(nameof(log));

            this.Log = log;
        }
        /// <summary>
        /// Gets the builds for the parameter <paramref name="settings"/>.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        /// <param name="settings">Settings for getting the build.</param>
        /// <returns>The builds or an empty list of builds if no builds were found for the <paramref name="settings"/>.</returns>
        /// <exception cref="InvalidOperationException">If no build definition was found for the <paramref name="settings"/>.</exception>
        internal static IEnumerable <AzureDevOpsBuild> GetAzureDevOpsBuilds(
            ICakeLog log,
            AzureDevOpsBuildsSettings settings)
        {
            log.NotNull(nameof(log));
            settings.NotNull(nameof(settings));

            var azureDevOpsBuilds = new List <AzureDevOpsBuild>();

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

                AzureDevOpsBuildDefinition buildDefinition = null;

                if (!string.IsNullOrEmpty(settings.BuildDefinitionName))
                {
                    buildDefinition = GetBuildDefinition(log, settings);
                    if (buildDefinition == null)
                    {
                        throw new InvalidOperationException($"Build definition '{settings.BuildDefinitionName}' not found");
                    }
                }

                List <int> buildsDefinitionIds =
                    buildDefinition == null ? null : new List <int>()
                {
                    buildDefinition.Id
                };

                List <Build> builds = null;

                if (settings.ProjectGuid != Guid.Empty)
                {
                    builds =
                        buildHttpClient
                        .GetBuildsAsync(
                            settings.ProjectGuid,
                            definitions: buildsDefinitionIds,
                            statusFilter: settings.BuildStatus?.ToBuildStatus(),
                            resultFilter: settings.BuildResult?.ToBuildResult(),
                            queryOrder: settings.BuildQueryOrder?.ToBuildQueryOrder(),
                            branchName: settings.BranchName,
                            top: settings.Top,
                            maxBuildsPerDefinition: settings.MaxBuildsPerDefinition)
                        .ConfigureAwait(false)
                        .GetAwaiter()
                        .GetResult();
                }
                else if (!string.IsNullOrWhiteSpace(settings.ProjectName))
                {
                    builds =
                        buildHttpClient
                        .GetBuildsAsync(
                            settings.ProjectName,
                            definitions: buildsDefinitionIds,
                            statusFilter: settings.BuildStatus?.ToBuildStatus(),
                            resultFilter: settings.BuildResult?.ToBuildResult(),
                            queryOrder: settings.BuildQueryOrder?.ToBuildQueryOrder(),
                            branchName: settings.BranchName,
                            top: settings.Top,
                            maxBuildsPerDefinition: settings.MaxBuildsPerDefinition)
                        .ConfigureAwait(false)
                        .GetAwaiter()
                        .GetResult();
                }
                else
                {
                    throw new ArgumentOutOfRangeException(
                              nameof(settings),
                              "Either ProjectGuid or ProjectName needs to be set");
                }

                azureDevOpsBuilds.AddRange(builds.Select(x => new AzureDevOpsBuild(log, settings, x)));
            }

            log.Verbose(
                "{0} builds found",
                azureDevOpsBuilds.Count);

            return(azureDevOpsBuilds);
        }
Exemplo n.º 24
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FakePullRequestSystemBuilder"/> class.
        /// </summary>
        /// <param name="log">The Cake log context.</param>
        private FakePullRequestSystemBuilder(ICakeLog log)
        {
            log.NotNull(nameof(log));

            this.log = log;
        }