/// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsBuildSettings"/> class using environment variables
        /// as set by an Azure Pipelines build.
        /// </summary>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        public AzureDevOpsBuildSettings(IAzureDevOpsCredentials credentials)
        {
            credentials.NotNull(nameof(credentials));

            this.Credentials   = credentials;
            this.CollectionUrl = EnvironmentVariableHelper.GetSystemTeamFoundationCollectionUri();
            this.ProjectName   = EnvironmentVariableHelper.GetSystemTeamProject();

            var buildId = Environment.GetEnvironmentVariable("BUILD_BUILDID", EnvironmentVariableTarget.Process);

            if (string.IsNullOrWhiteSpace(buildId))
            {
                throw new InvalidOperationException(
                          "Failed to read the BUILD_BUILDID environment variable. Make sure you are running in an Azure Pipelines build.");
            }

            if (!int.TryParse(buildId, out int buildIdValue))
            {
                throw new InvalidOperationException("BUILD_BUILDID environment variable should contain integer value");
            }

            if (buildIdValue <= 0)
            {
                throw new InvalidOperationException("BUILD_BUILDID environment variable should contain integer value and it should be greater than zero");
            }

            this.BuildId = buildIdValue;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseAzureDevOpsCollectionSettings"/> class.
        /// </summary>
        /// <param name="collectionUrl">Full URL of the Azure DevOps collection,
        /// eg. <code>http://myserver:8080/defaultcollection</code>.</param>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        protected BaseAzureDevOpsCollectionSettings(Uri collectionUrl, IAzureDevOpsCredentials credentials)
            : base(credentials)
        {
            collectionUrl.NotNull(nameof(collectionUrl));

            this.CollectionUrl = collectionUrl;
        }
 public GitHttpClient CreateGitClient(Uri collectionUrl, IAzureDevOpsCredentials credentials, out Identity identity)
 {
     identity = new Identity {
         ProviderDisplayName = "FakeUser", Id = Guid.NewGuid(), IsActive = true
     };
     return(this.CreateGitClient(collectionUrl, credentials));
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseAzureDevOpsPullRequestSettings"/> class.
        /// </summary>
        /// <param name="repositoryUrl">Full URL of the Git repository,
        /// eg. <code>http://myserver:8080/defaultcollection/myproject/_git/myrepository</code>.
        /// Supported URL schemes are HTTP, HTTPS and SSH.
        /// URLs using SSH scheme are converted to HTTPS.</param>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        protected BaseAzureDevOpsPullRequestSettings(Uri repositoryUrl, IAzureDevOpsCredentials credentials)
            : base(credentials)
        {
            repositoryUrl.NotNull(nameof(repositoryUrl));

            this.RepositoryUrl = repositoryUrl;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsBuildSettings"/> class using environment variables
        /// as set by an Azure Pipelines build.
        /// </summary>
        /// <param name="buildId">ID of the build.</param>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        public AzureDevOpsBuildSettings(int buildId, IAzureDevOpsCredentials credentials)
            : base(credentials)
        {
            buildId.NotNegativeOrZero(nameof(buildId));

            this.BuildId = buildId;
        }
Example #6
0
        /// <summary>
        /// Returns the <see cref="VssCredentials"/> corresponding to a <see cref="IAzureDevOpsCredentials"/> object.
        /// </summary>
        /// <param name="credentials"><see cref="IAzureDevOpsCredentials"/> credential instance.</param>
        /// <returns><see cref="VssCredentials"/> instance.</returns>
        public static VssCredentials ToVssCredentials(this IAzureDevOpsCredentials credentials)
        {
            credentials.NotNull(nameof(credentials));

            switch (credentials.GetType().Name)
            {
            case nameof(AzureDevOpsNtlmCredentials):
                return(new VssCredentials());

            case nameof(AzureDevOpsBasicCredentials):
                var basicCredentials = (AzureDevOpsBasicCredentials)credentials;
                return(new VssBasicCredential(basicCredentials.UserName, basicCredentials.Password));

            case nameof(AzureDevOpsOAuthCredentials):
                var oAuthCredentials = (AzureDevOpsOAuthCredentials)credentials;
                return(new VssOAuthAccessTokenCredential(oAuthCredentials.AccessToken));

            case nameof(AzureDevOpsAadCredentials):
                var aadCredentials = (AzureDevOpsAadCredentials)credentials;
                return(new VssAadCredential(aadCredentials.UserName, aadCredentials.Password));

            default:
                throw new AzureDevOpsException("Unknown credential type.");
            }
        }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsPullRequestSettings"/> class.
        /// </summary>
        /// <param name="repositoryUrl">Full URL of the Git repository,
        /// eg. <code>http://myserver:8080/defaultcollection/myproject/_git/myrepository</code>.
        /// Supported URL schemes are HTTP, HTTPS and SSH.
        /// URLs using SSH scheme are converted to HTTPS.</param>
        /// <param name="pullRequestId">ID of the pull request.</param>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        public AzureDevOpsPullRequestSettings(Uri repositoryUrl, int pullRequestId, IAzureDevOpsCredentials credentials)
            : base(repositoryUrl, credentials)
        {
            pullRequestId.NotNegativeOrZero(nameof(pullRequestId));

            this.PullRequestId = pullRequestId;
        }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsBuildsSettings"/> class using environment variables
        /// as set by an Azure Pipelines build.
        /// </summary>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        public AzureDevOpsBuildsSettings(IAzureDevOpsCredentials credentials)
        {
            credentials.NotNull(nameof(credentials));

            this.Credentials   = credentials;
            this.CollectionUrl = EnvironmentVariableHelper.GetSystemTeamFoundationCollectionUri();
            this.ProjectName   = EnvironmentVariableHelper.GetSystemTeamProject();
        }
Example #9
0
        public static IPullRequestSystem AzureDevOpsPullRequests(
            this ICakeContext context,
            IAzureDevOpsCredentials credentials)
        {
            context.NotNull(nameof(context));
            credentials.NotNull(nameof(credentials));

            return(context.AzureDevOpsPullRequests(new AzureDevOpsPullRequestSystemSettings(credentials)));
        }
Example #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsBuildsSettings"/> class.
        /// </summary>
        /// <param name="collectionUrl">Full URL of the Azure DevOps collection,
        /// eg. <code>http://myserver:8080/defaultcollection</code>.</param>
        /// <param name="projectName">Name of the project.</param>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        public AzureDevOpsBuildsSettings(Uri collectionUrl, string projectName, IAzureDevOpsCredentials credentials)
        {
            collectionUrl.NotNull(nameof(collectionUrl));
            projectName.NotNullOrWhiteSpace(nameof(projectName));
            credentials.NotNull(nameof(credentials));

            this.CollectionUrl = collectionUrl;
            this.ProjectName   = projectName;
            this.Credentials   = credentials;
        }
                public void Should_Throw_If_Credentials_Are_Null()
                {
                    // Given
                    IAzureDevOpsCredentials credentials = null;

                    // When
                    var result = Record.Exception(() => new BaseAzureDevOpsCollectionSettingsImpl(credentials));

                    // Then
                    result.IsArgumentNullException("credentials");
                }
Example #12
0
            public void Should_Throw_If_Credentials_Are_Null()
            {
                // Given
                IAzureDevOpsCredentials creds = null;

                // When
                var result = Record.Exception(() => new AzureDevOpsBuildsSettings(creds));

                // Then
                result.IsArgumentNullException("credentials");
            }
                public void Should_Throw_If_Credentials_Are_Null()
                {
                    // Given
                    var collectionUrl = new Uri("http://example.com/collection");
                    IAzureDevOpsCredentials credentials = null;

                    // When
                    var result = Record.Exception(() => new BaseAzureDevOpsCollectionSettingsImpl(collectionUrl, credentials));

                    // Then
                    result.IsArgumentNullException("credentials");
                }
Example #14
0
        public static IPullRequestSystem AzureDevOpsPullRequests(
            this ICakeContext context,
            Uri repositoryUrl,
            int pullRequestId,
            IAzureDevOpsCredentials credentials)
        {
            context.NotNull(nameof(context));
            repositoryUrl.NotNull(nameof(repositoryUrl));
            credentials.NotNull(nameof(credentials));

            return(context.AzureDevOpsPullRequests(new AzureDevOpsPullRequestSystemSettings(repositoryUrl, pullRequestId, credentials)));
        }
Example #15
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;
        }
Example #16
0
            public void Should_Throw_If_Credentials_Are_Null()
            {
                // Given
                var collectionUrl = new Uri("http://example.com/collection");
                var projectGuid   = Guid.NewGuid();
                IAzureDevOpsCredentials credentials = null;

                // When
                var result = Record.Exception(() => new AzureDevOpsBuildsSettings(collectionUrl, projectGuid, credentials));

                // Then
                result.IsArgumentNullException("credentials");
            }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseAzureDevOpsPullRequestSettings"/> class using environment variables
        /// as set by an Azure Pipelines build.
        /// </summary>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        protected BaseAzureDevOpsPullRequestSettings(IAzureDevOpsCredentials credentials)
            : base(credentials)
        {
            var repositoryUrl = Environment.GetEnvironmentVariable("BUILD_REPOSITORY_URI", EnvironmentVariableTarget.Process);

            if (string.IsNullOrWhiteSpace(repositoryUrl))
            {
                throw new InvalidOperationException(
                          "Failed to read the BUILD_REPOSITORY_URI environment variable. Make sure you are running in an Azure Pipelines build.");
            }

            this.RepositoryUrl = new Uri(repositoryUrl);
        }
Example #18
0
            public void Should_Throw_If_Credentials_Are_Null()
            {
                // Given
                var repositoryUrl = new Uri("http://example.com");
                var sourceBranch  = "foo";
                IAzureDevOpsCredentials credentials = null;

                // When
                var result = Record.Exception(() => new AzureDevOpsPullRequestSettings(repositoryUrl, sourceBranch, credentials));

                // Then
                result.IsArgumentNullException("credentials");
            }
Example #19
0
        public static IPullRequestSystem AzureDevOpsPullRequests(
            this ICakeContext context,
            Uri repositoryUrl,
            string sourceBranch,
            IAzureDevOpsCredentials credentials)
        {
            context.NotNull(nameof(context));
            repositoryUrl.NotNull(nameof(repositoryUrl));
            sourceBranch.NotNullOrWhiteSpace(nameof(sourceBranch));
            credentials.NotNull(nameof(credentials));

            return(context.AzureDevOpsPullRequests(new AzureDevOpsPullRequestSystemSettings(repositoryUrl, sourceBranch, credentials)));
        }
Example #20
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsBuildsSettings"/> class.
        /// </summary>
        /// <param name="collectionUrl">Full URL of the Azure DevOps collection,
        /// eg. <code>http://myserver:8080/defaultcollection</code>.</param>
        /// <param name="projectGuid">ID of the project.</param>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        public AzureDevOpsBuildsSettings(Uri collectionUrl, Guid projectGuid, IAzureDevOpsCredentials credentials)
        {
            collectionUrl.NotNull(nameof(collectionUrl));
            credentials.NotNull(nameof(credentials));

            if (projectGuid == Guid.Empty)
            {
                throw new ArgumentOutOfRangeException(nameof(projectGuid));
            }

            this.Credentials   = credentials;
            this.ProjectGuid   = projectGuid;
            this.CollectionUrl = collectionUrl;
        }
Example #21
0
        public override GitHttpClient CreateGitClient(Uri collectionUrl, IAzureDevOpsCredentials credentials)
        {
            var mock = new Mock <GitHttpClient>(MockBehavior.Loose, collectionUrl, credentials.ToVssCredentials());

            mock.Setup(arg => arg.GetPullRequestAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), null, null, null, null, null, null, default(CancellationToken)))
            .ReturnsAsync(() => null);

            mock.Setup(arg => arg.GetPullRequestsAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <GitPullRequestSearchCriteria>(), null, null, 1, null, default(CancellationToken)))
            .ReturnsAsync(() => new List <GitPullRequest>());

            mock = this.Setup(mock);

            return(mock.Object);
        }
Example #22
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsCreatePullRequestSettings"/> class.
        /// </summary>
        /// <param name="repositoryUrl">Full URL of the Git repository,
        /// eg. <code>http://myserver:8080/defaultcollection/myproject/_git/myrepository</code>.
        /// Supported URL schemes are HTTP, HTTPS and SSH.
        /// URLs using SSH scheme are converted to HTTPS.</param>
        /// <param name="sourceRefName">Branch for which the pull request is made.</param>
        /// <param name="targetRefName">Target branch of the pull request.
        /// If <see langword="null"/> or <see cref="string.Empty"/> the default branch of the repository will be used.</param>
        /// <param name="title">Title of the pull request.</param>
        /// <param name="description">Description of the pull request.</param>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        public AzureDevOpsCreatePullRequestSettings(
            Uri repositoryUrl,
            string sourceRefName,
            string targetRefName,
            string title,
            string description,
            IAzureDevOpsCredentials credentials)
            : base(repositoryUrl, sourceRefName, credentials)
        {
            title.NotNullOrWhiteSpace(nameof(title));
            description.NotNull(nameof(description));

            this.TargetRefName = targetRefName;
            this.Title         = title;
            this.Description   = description;
        }
Example #23
0
        /// <summary>
        /// Creates a client object for communicating with Azure DevOps.
        /// </summary>
        /// <param name="collectionUrl">The URL of the Azure DevOps team project collection.</param>
        /// <param name="credentials">The credentials to connect to Azure DevOps.</param>
        /// <param name="authorizedIdentity">Returns identity which is authorized.</param>
        /// <returns>Client object for communicating with Azure DevOps.</returns>
        public BuildHttpClient CreateBuildClient(Uri collectionUrl, IAzureDevOpsCredentials credentials, out Identity authorizedIdentity)
        {
            var connection =
                new VssConnection(collectionUrl, credentials.ToVssCredentials());

            authorizedIdentity = connection.AuthorizedIdentity;

            var buildClient = connection.GetClient <BuildHttpClient>();

            if (buildClient == null)
            {
                throw new AzureDevOpsException("Could not retrieve the BuildHttpClient object");
            }

            return(buildClient);
        }
Example #24
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsBuildSettings"/> class using environment variables
        /// as set by a Azure Pipelines build.
        /// </summary>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        public AzureDevOpsBuildSettings(IAzureDevOpsCredentials credentials)
        {
            credentials.NotNull(nameof(credentials));

            this.Credentials = credentials;

            var collectionUrl = Environment.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI", EnvironmentVariableTarget.Process);

            if (string.IsNullOrWhiteSpace(collectionUrl))
            {
                throw new InvalidOperationException(
                          "Failed to read the SYSTEM_TEAMFOUNDATIONCOLLECTIONURI environment variable. Make sure you are running in an Azure Pipelines build.");
            }

            this.CollectionUrl = new Uri(collectionUrl);

            var projectName = Environment.GetEnvironmentVariable("SYSTEM_TEAMPROJECT", EnvironmentVariableTarget.Process);

            if (string.IsNullOrWhiteSpace(projectName))
            {
                throw new InvalidOperationException(
                          "Failed to read the SYSTEM_TEAMPROJECT environment variable. Make sure you are running in an Azure Pipelines build.");
            }

            this.ProjectName = projectName;

            var buildId = Environment.GetEnvironmentVariable("BUILD_BUILDID", EnvironmentVariableTarget.Process);

            if (string.IsNullOrWhiteSpace(buildId))
            {
                throw new InvalidOperationException(
                          "Failed to read the BUILD_BUILDID environment variable. Make sure you are running in an Azure Pipelines build.");
            }

            if (!int.TryParse(buildId, out int buildIdValue))
            {
                throw new InvalidOperationException("BUILD_BUILDID environment variable should contain integer value");
            }

            if (buildIdValue <= 0)
            {
                throw new InvalidOperationException("BUILD_BUILDID environment variable should contain integer value and it should be greater than zero");
            }

            this.BuildId = buildIdValue;
        }
            public void Should_Throw_If_Credentials_Are_Null()
            {
                // Given
                var repositoryUrl = new Uri("http://example.com");
                var sourceRefName = "foo";
                var targetRefName = "master";
                var title         = "foo";
                var description   = "bar";
                IAzureDevOpsCredentials credentials = null;

                // When
                var result =
                    Record.Exception(() =>
                                     new AzureDevOpsCreatePullRequestSettings(repositoryUrl, sourceRefName, targetRefName, title, description, credentials));

                // Then
                result.IsArgumentNullException("credentials");
            }
Example #26
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsPullRequestSettings"/> class using environment variables
        /// as set by an Azure Pipelines build.
        /// </summary>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        /// <exception cref="InvalidOperationException">If build is not running in Azure Pipelines,
        /// or build is not for a pull request.</exception>
        public AzureDevOpsPullRequestSettings(IAzureDevOpsCredentials credentials)
            : base(credentials)
        {
            var pullRequestId = Environment.GetEnvironmentVariable("SYSTEM_PULLREQUEST_PULLREQUESTID", EnvironmentVariableTarget.Process);

            if (string.IsNullOrWhiteSpace(pullRequestId))
            {
                throw new InvalidOperationException(
                          "Failed to read the SYSTEM_PULLREQUEST_PULLREQUESTID environment variable. Make sure you are running in an Azure Pipelines build.");
            }

            if (!int.TryParse(pullRequestId, out int pullRequestIdValue))
            {
                throw new InvalidOperationException("SYSTEM_PULLREQUEST_PULLREQUESTID environment variable should contain integer value");
            }

            if (pullRequestIdValue <= 0)
            {
                throw new InvalidOperationException("SYSTEM_PULLREQUEST_PULLREQUESTID environment variable should contain integer value and it should be greater than zero");
            }

            this.PullRequestId = pullRequestIdValue;
        }
        public override GitHttpClient CreateGitClient(Uri collectionUrl, IAzureDevOpsCredentials credentials)
        {
            var mock = new Mock <GitHttpClient>(MockBehavior.Loose, collectionUrl, credentials.ToVssCredentials());

            mock.Setup(arg => arg.GetPullRequestAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), null, null, null, null, null, null, default(CancellationToken)))
            .ReturnsAsync((string project1, string repoId1, int prId, int i1, int i2, int i3, bool b1, bool b2, object o1, CancellationToken c1) => new GitPullRequest
            {
                PullRequestId = prId,
                Status        = PullRequestStatus.Active,
                Repository    = new GitRepository
                {
                    Id   = Guid.NewGuid(),
                    Name = repoId1,
                },
                SourceRefName         = "foo",
                TargetRefName         = "master",
                CodeReviewId          = 123,
                LastMergeSourceCommit = new GitCommitRef {
                    CommitId = "4a92b977"
                },
                LastMergeTargetCommit = new GitCommitRef {
                    CommitId = "78a3c113"
                },
            });

            mock.Setup(arg => arg.GetPullRequestsAsync(
                           It.IsAny <string>(),
                           It.IsAny <string>(),
                           It.IsAny <GitPullRequestSearchCriteria>(),
                           null,
                           null,
                           1,
                           null,
                           default(CancellationToken)))
            .ReturnsAsync((string project2, string repoId2, GitPullRequestSearchCriteria sc, int j1, int j2, int top, object o2, CancellationToken c2)
                          => new List <GitPullRequest>(new[]
            {
                new GitPullRequest
                {
                    PullRequestId = 777,
                    Status        = PullRequestStatus.Active,
                    Repository    = new GitRepository
                    {
                        Id   = Guid.NewGuid(),
                        Name = repoId2,
                    },
                    SourceRefName         = sc.SourceRefName,
                    TargetRefName         = "master",
                    CodeReviewId          = 123,
                    LastMergeSourceCommit = new GitCommitRef {
                        CommitId = "4a92b977"
                    },
                    LastMergeTargetCommit = new GitCommitRef {
                        CommitId = "78a3c113"
                    },
                },
            }));

            mock = this.Setup(mock);

            return(mock.Object);
        }
        /// <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);
        }
Example #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AzureDevOpsPullRequestSettings"/> class.
 /// </summary>
 /// <param name="repositoryUrl">Full URL of the Git repository,
 /// eg. <code>http://myserver:8080/defaultcollection/myproject/_git/myrepository</code>.
 /// Supported URL schemes are HTTP, HTTPS and SSH.
 /// URLs using SSH scheme are converted to HTTPS.</param>
 /// <param name="sourceBranch">Branch for which the pull request is made.</param>
 /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
 public AzureDevOpsPullRequestSettings(Uri repositoryUrl, string sourceBranch, IAzureDevOpsCredentials credentials)
     : base(repositoryUrl, sourceBranch, credentials)
 {
 }
Example #30
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureDevOpsBuildSettings"/> class.
        /// </summary>
        /// <param name="collectionUrl">Full URL of the Azure DevOps collection,
        /// eg. <code>http://myserver:8080/defaultcollection</code>.</param>
        /// <param name="projectName">Name of the project.</param>
        /// <param name="buildId">ID of the build.</param>
        /// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
        public AzureDevOpsBuildSettings(Uri collectionUrl, string projectName, int buildId, IAzureDevOpsCredentials credentials)
        {
            collectionUrl.NotNull(nameof(collectionUrl));
            projectName.NotNullOrWhiteSpace(nameof(projectName));
            buildId.NotNegativeOrZero(nameof(buildId));
            credentials.NotNull(nameof(credentials));

            this.CollectionUrl = collectionUrl;
            this.ProjectName   = projectName;
            this.BuildId       = buildId;
            this.Credentials   = credentials;
        }