/// <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));
        }
        /// <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));
            }
        }