Example #1
0
        /// <summary>
        /// Creates a new comment thread in the pull request.
        /// </summary>
        /// <param name="thread">The instance of the thread.</param>
        /// <returns>A newly created comment thread, or null if it can't be created.</returns>
        public AzureDevOpsPullRequestCommentThread CreateCommentThread(AzureDevOpsPullRequestCommentThread thread)
        {
            thread.NotNull(nameof(thread));

            AzureDevOpsPullRequestCommentThread resultingThread = null;
            if (!this.ValidatePullRequest())
            {
                return resultingThread;
            }

            using (var gitClient = this.gitClientFactory.CreateGitClient(this.CollectionUrl, this.credentials))
            {
                var newThread = gitClient.CreateThreadAsync(
                        thread.InnerThread,
                        this.RepositoryId,
                        this.PullRequestId)
                    .ConfigureAwait(false)
                    .GetAwaiter()
                    .GetResult();

                if (newThread != null)
                {
                    resultingThread = new AzureDevOpsPullRequestCommentThread(newThread);
                }
            }

            return resultingThread;
        }
Example #2
0
        private bool AddThreadProperties(
            AzureDevOpsPullRequestCommentThread thread,
            IEnumerable <AzureDevOpsPullRequestIterationChange> changes,
            IIssue issue,
            int iterationId,
            string commentSource)
        {
            thread.NotNull(nameof(thread));
            changes.NotNull(nameof(changes));
            issue.NotNull(nameof(issue));

            var properties = new Dictionary <string, object>();

            if (issue.AffectedFileRelativePath != null)
            {
                if (this.azureDevOpsPullRequest.CodeReviewId > 0)
                {
                    var changeTrackingId =
                        this.TryGetCodeFlowChangeTrackingId(changes, issue.AffectedFileRelativePath);
                    if (changeTrackingId < 0)
                    {
                        // Don't post comment if we couldn't determine the change.
                        return(false);
                    }

                    AddCodeFlowProperties(issue, iterationId, changeTrackingId, properties);
                }
                else
                {
                    throw new NotSupportedException("Legacy code reviews are not supported.");
                }
            }

            // An Azure DevOps UI extension will recognize this and format the comments differently.
            properties.Add("CodeAnalysisThreadType", "CodeAnalysisIssue");

            thread.Properties = properties;

            // Add a custom property to be able to distinguish all comments created this way.
            thread.SetCommentSource(commentSource);

            // Add custom property for identifying the comment for subsequent runs
            thread.SetCommentIdentifier(issue.Identifier);

            // Add a custom property to be able to distinguish all comments by provider type later on
            thread.SetProviderType(issue.ProviderType);

            // Add a custom property to be able to return issue message from existing threads,
            // without any formatting done by this addin, back to Cake.Issues.PullRequests.
            thread.SetIssueMessage(issue.MessageText);

            return(true);
        }
Example #3
0
        /// <summary>
        /// Converts a <see cref="AzureDevOpsPullRequestCommentThread"/> from Azure DevOps to a <see cref="IPullRequestDiscussionThread"/> as used in this addin.
        /// </summary>
        /// <param name="thread">Azure DevOps thread to convert.</param>
        /// <returns>Converted thread.</returns>
        public static IPullRequestDiscussionThread ToPullRequestDiscussionThread(this AzureDevOpsPullRequestCommentThread thread)
        {
            thread.NotNull(nameof(thread));

            return(new PullRequestDiscussionThread(
                       thread.Id,
                       thread.Status.ToPullRequestDiscussionStatus(),
                       thread.FilePath,
                       thread.Comments.Select(x => x.ToPullRequestDiscussionComment()))
            {
                CommentSource = thread.GetCommentSource(),
                Resolution = thread.Status.ToPullRequestDiscussionResolution(),
            });
        }
        /// <summary>
        /// Creates a new comment thread in the pull request.
        /// </summary>
        /// <param name="thread">The instance of the thread.</param>
        public void CreateCommentThread(AzureDevOpsPullRequestCommentThread thread)
        {
            thread.NotNull(nameof(thread));

            if (!this.ValidatePullRequest())
            {
                return;
            }

            using (var gitClient = this.gitClientFactory.CreateGitClient(this.CollectionUrl, this.credentials))
            {
                gitClient.CreateThreadAsync(
                    thread.InnerThread,
                    this.RepositoryId,
                    this.PullRequestId,
                    null,
                    CancellationToken.None).Wait();
            }
        }
        /// <summary>
        /// Sets the comment identifier value used to identify the issue for which the comment was created.
        /// </summary>
        /// <param name="thread">Thread for which the value should be set.</param>
        /// <param name="value">Value to set as comment identifier.</param>
        public static void SetCommentIdentifier(this AzureDevOpsPullRequestCommentThread thread, string value)
        {
            thread.NotNull(nameof(thread));

            thread.SetValue(CommentIdentifierPropertyName, value);
        }
        /// <summary>
        /// Gets the comment identifier to identify the issue for which the comment was created.
        /// </summary>
        /// <param name="thread">Thread to get the value from.</param>
        /// <returns>Comment identifier value.</returns>
        public static string GetCommentIdentifier(this AzureDevOpsPullRequestCommentThread thread)
        {
            thread.NotNull(nameof(thread));

            return(thread.GetValue <string>(CommentIdentifierPropertyName));
        }
        /// <summary>
        /// Checks if the custom comment source value used to decorate comments created by this addin
        /// has a specific value.
        /// </summary>
        /// <param name="thread">Thread to check.</param>
        /// <param name="value">Value to check for.</param>
        /// <returns><c>True</c> if the value is identical, <c>False</c> otherwise.</returns>
        public static bool IsCommentSource(this AzureDevOpsPullRequestCommentThread thread, string value)
        {
            thread.NotNull(nameof(thread));

            return(thread.GetCommentSource() == value);
        }
        /// <summary>
        /// Sets the provider type value used to identify specific provider origins later on when reading back existing issues.
        /// </summary>
        /// <param name="thread">Thread for which the value should be set.</param>
        /// <param name="value">Value to set as comment source.</param>
        public static void SetProviderType(this AzureDevOpsPullRequestCommentThread thread, string value)
        {
            thread.NotNull(nameof(thread));

            thread.SetValue(ProviderTypePropertyName, value);
        }
        /// <summary>
        /// Gets the provider type value used to identify specific provider origins later on when reading back existing issues.
        /// </summary>
        /// <param name="thread">Thread to get the value from.</param>
        /// <returns>Comment source value.</returns>
        public static string GetProviderType(this AzureDevOpsPullRequestCommentThread thread)
        {
            thread.NotNull(nameof(thread));

            return(thread.GetValue <string>(ProviderTypePropertyName));
        }
        /// <summary>
        /// Sets the original message of the issue as provided by Cake.Issues.PullRequests.
        /// </summary>
        /// <param name="thread">Thread for which the value should be set.</param>
        /// <param name="value">Value to set as the original message.</param>
        public static void SetIssueMessage(this AzureDevOpsPullRequestCommentThread thread, string value)
        {
            thread.NotNull(nameof(thread));

            thread.SetValue(IssueMessagePropertyName, value);
        }
        /// <summary>
        /// Gets the original message of the issue as provided by Cake.Issues.PullRequests,
        /// without any formatting done by this addin.
        /// </summary>
        /// <param name="thread">Thread to get the value from.</param>
        /// <returns>Original message of the issue.</returns>
        public static string GetIssueMessage(this AzureDevOpsPullRequestCommentThread thread)
        {
            thread.NotNull(nameof(thread));

            return(thread.GetValue <string>(IssueMessagePropertyName));
        }