Skip to content

Commit

Permalink
Merge pull request cake-contrib#42 from cake-contrib/feature/cake-con…
Browse files Browse the repository at this point in the history
…tribgh-2

(cake-contribGH-2) Support issues not related to a file
  • Loading branch information
pascalberger authored Sep 6, 2018
2 parents 85c56a0 + b208b7e commit 50e6c22
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,6 @@ public void Should_Throw_If_Thread_Is_Null()
result.IsArgumentNullException("thread");
}

[Fact]
public void Should_Throw_If_ThreadContext_Is_Null()
{
// Given
var thread =
new GitPullRequestCommentThread
{
Id = 123,
Status = CommentThreadStatus.Active,
ThreadContext = null,
Comments = new List<Comment>(),
Properties = new PropertiesCollection()
};

// When
var result = Record.Exception(() => thread.ToPullRequestDiscussionThread());

// Then
result.IsInvalidOperationException("ThreadContext is not created.");
}

[Fact]
public void Should_Throw_If_Comments_Are_Null()
{
Expand Down Expand Up @@ -183,6 +162,27 @@ public void Should_Set_Correct_FilePath(string filePath, string expectedResult)
result.AffectedFileRelativePath.ToString().ShouldBe(expectedResult);
}

[Fact]
public void Should_Set_Correct_FilePath_If_ThreadContext_Is_Null()
{
// Given
var thread =
new GitPullRequestCommentThread
{
Id = 123,
Status = CommentThreadStatus.Active,
ThreadContext = null,
Comments = new List<Comment>(),
Properties = new PropertiesCollection()
};

// When
var result = thread.ToPullRequestDiscussionThread();

// Then
result.AffectedFileRelativePath.ShouldBeNull();
}

[Fact]
public void Should_Set_Correct_Comments()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Linq;
using Cake.Core.IO;
using Microsoft.TeamFoundation.SourceControl.WebApi;

/// <summary>
Expand All @@ -21,20 +22,21 @@ public static IPullRequestDiscussionThread ToPullRequestDiscussionThread(this Gi
{
thread.NotNull(nameof(thread));

if (thread.ThreadContext == null)
if (thread.Comments == null)
{
throw new InvalidOperationException("ThreadContext is not created.");
throw new InvalidOperationException("Comments list is not created.");
}

if (thread.Comments == null)
FilePath filePath = null;
if (thread.ThreadContext != null && thread.ThreadContext.FilePath != null)
{
throw new InvalidOperationException("Comments list is not created.");
filePath = thread.ThreadContext.FilePath.TrimStart('/');
}

return new PullRequestDiscussionThread(
thread.Id,
thread.Status.ToPullRequestDiscussionStatus(),
thread.ThreadContext.FilePath?.TrimStart('/'),
filePath,
thread.Comments.Select(x => x.ToPullRequestDiscussionComment()))
{
CommentSource = thread.GetCommentSource(),
Expand Down
37 changes: 22 additions & 15 deletions src/Cake.Issues.PullRequests.Tfs/TfsPullRequestSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,14 @@ private IEnumerable<GitPullRequestCommentThread> CreateDiscussionThreads(
changes = this.GetCodeFlowChanges(gitClient, iterationId);
}

// Filter isues not related to a file.
if (!this.settings.ReportIssuesNotRelatedToAFile)
{
issues = issues.Where(x => x.AffectedFileRelativePath != null);
}

// ReSharper disable once PossibleMultipleEnumeration
// We currenty don't support issues not related to a file.
// See https://github.com/cake-contrib/Cake.Issues.PullRequests.Tfs/issues/2
foreach (var issue in issues.Where(x => x.AffectedFileRelativePath != null))
foreach (var issue in issues)
{
this.Log.Information(
"Creating a discussion comment for the issue at line {0} from {1}",
Expand Down Expand Up @@ -258,21 +262,24 @@ private bool AddThreadProperties(

var properties = new PropertiesCollection();

if (this.tfsPullRequest.CodeReviewId > 0)
if (issue.AffectedFileRelativePath != null)
{
var changeTrackingId =
this.TryGetCodeFlowChangeTrackingId(changes, issue.AffectedFileRelativePath);
if (changeTrackingId < 0)
if (this.tfsPullRequest.CodeReviewId > 0)
{
// Don't post comment if we couldn't determine the change.
return false;
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.");
}

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

// A VSTS UI extension will recognize this and format the comments differently.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,12 @@ public TfsPullRequestSystemSettings(TfsPullRequestSettings settings)
/// Default value is <c>true</c>.
/// </summary>
public bool FilterModifiedFiles { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether issues not related to a file should be posted
/// as comments or not.
/// Default value is <c>false</c>.
/// </summary>
public bool ReportIssuesNotRelatedToAFile { get; set; } = false;
}
}

0 comments on commit 50e6c22

Please sign in to comment.