Skip to content

Commit 13df2bd

Browse files
authored
Merge pull request #307 from yansklyarenko/feature/GH-211-return-thread-object
2 parents cb05bf1 + 4dff960 commit 13df2bd

File tree

3 files changed

+87
-42
lines changed

3 files changed

+87
-42
lines changed

src/Cake.AzureDevOps.Tests/ExceptionAssertExtensions.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,21 @@
1010
public static class ExceptionAssertExtensions
1111
{
1212
/// <summary>
13-
/// Checks if an execption is of type <see cref="ArgumentNullException"/>.
13+
/// Checks if an exception is of type <typeparamref name="T"/>.
14+
/// </summary>
15+
/// <typeparam name="T">The <see cref="ArgumentException"/> or one of its descendants.</typeparam>
16+
/// <param name="exception">Exception to check.</param>
17+
/// <param name="expectedExceptionType">The expected type of the exception.</param>
18+
/// <param name="parameterName">Expected name of the parameter which has caused the exception.</param>
19+
public static void IsArgumentException<T>(this T exception, Type expectedExceptionType, string parameterName)
20+
where T : ArgumentException
21+
{
22+
Assert.IsType(expectedExceptionType, exception);
23+
Assert.Equal(parameterName, exception.ParamName);
24+
}
25+
26+
/// <summary>
27+
/// Checks if an exception is of type <see cref="ArgumentNullException"/>.
1428
/// </summary>
1529
/// <param name="exception">Exception to check.</param>
1630
/// <param name="parameterName">Expected name of the parameter which has caused the exception.</param>
@@ -21,7 +35,7 @@ public static void IsArgumentNullException(this Exception exception, string para
2135
}
2236

2337
/// <summary>
24-
/// Checks if an execption is of type <see cref="ArgumentOutOfRangeException"/>.
38+
/// Checks if an exception is of type <see cref="ArgumentOutOfRangeException"/>.
2539
/// </summary>
2640
/// <param name="exception">Exception to check.</param>
2741
/// <param name="parameterName">Expected name of the parameter which has caused the exception.</param>
@@ -32,7 +46,7 @@ public static void IsArgumentOutOfRangeException(this Exception exception, strin
3246
}
3347

3448
/// <summary>
35-
/// Checks if an execption is of type <see cref="ArgumentException"/>.
49+
/// Checks if an exception is of type <see cref="ArgumentException"/>.
3650
/// </summary>
3751
/// <param name="exception">Exception to check.</param>
3852
/// <param name="parameterName">Expected name of the parameter which has caused the exception.</param>
@@ -43,7 +57,7 @@ public static void IsArgumentException(this Exception exception, string paramete
4357
}
4458

4559
/// <summary>
46-
/// Checks if an execption is of type <see cref="InvalidOperationException"/>.
60+
/// Checks if an exception is of type <see cref="InvalidOperationException"/>.
4761
/// </summary>
4862
/// <param name="exception">Exception to check.</param>
4963
public static void IsInvalidOperationException(this Exception exception)
@@ -70,7 +84,7 @@ public static void IsAzureDevOpsPullRequestNotFoundException(this Exception exce
7084
}
7185

7286
/// <summary>
73-
/// Checks if an exception is of type <see cref="AzureDevOpsException"/>
87+
/// Checks if an exception is of type <see cref="AzureDevOpsException"/>.
7488
/// </summary>
7589
/// <param name="exception">Exception to check.</param>
7690
public static void IsAzureDevOpsException(this Exception exception)
@@ -79,7 +93,7 @@ public static void IsAzureDevOpsException(this Exception exception)
7993
}
8094

8195
/// <summary>
82-
/// Checks if an exception is of type <see cref="AzureDevOpsBranchNotFoundException"/>
96+
/// Checks if an exception is of type <see cref="AzureDevOpsBranchNotFoundException"/>.
8397
/// </summary>
8498
/// <param name="exception">Exception to check.</param>
8599
/// <param name="message">Exception message which should be checked.</param>

src/Cake.AzureDevOps.Tests/Repos/PullRequest/AzureDevOpsPullRequestTests.cs

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Cake.AzureDevOps.Tests.Repos.PullRequest
22
{
3+
using System;
34
using System.Collections.Generic;
45
using System.Linq;
56
using Cake.AzureDevOps.Repos.PullRequest;
@@ -767,97 +768,115 @@ public void Should_Return_Valid_Comment_Threads()
767768

768769
public sealed class TheCreateCommentMethod
769770
{
770-
[Fact]
771-
public void Should_Throw_If_Comment_Is_Null()
771+
[Theory]
772+
[InlineData((string)null, typeof(ArgumentNullException))]
773+
[InlineData("", typeof(ArgumentOutOfRangeException))]
774+
[InlineData(" ", typeof(ArgumentOutOfRangeException))]
775+
public void Should_Throw_If_Comment_Is_Null_Or_Empty_Or_Whitespace(string comment, Type expectedExceptionType)
772776
{
773777
// Given
774778
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsServerUrl, 100);
775779
var pullRequest = new AzureDevOpsPullRequest(fixture.Log, fixture.Settings, fixture.GitClientFactory);
776780

777781
// When
778-
var result = Record.Exception(() => pullRequest.CreateComment(null));
782+
var result = Record.Exception(() => pullRequest.CreateComment(comment)) as ArgumentException;
779783

780784
// Then
781-
result.IsArgumentNullException("comment");
785+
result.ShouldNotBeNull();
786+
result.IsArgumentException(expectedExceptionType, "comment");
782787
}
783788

784789
[Fact]
785-
public void Should_Throw_If_Comment_Is_Empty()
790+
public void Should_Return_Null_If_Null_Is_Returned_From_Git_Client()
786791
{
787792
// Given
788-
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsServerUrl, 100);
793+
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsServerUrl, 100)
794+
{
795+
GitClientFactory = new FakeNullForMethodsGitClientFactory(),
796+
};
789797
var pullRequest = new AzureDevOpsPullRequest(fixture.Log, fixture.Settings, fixture.GitClientFactory);
790798

791799
// When
792-
var result = Record.Exception(() => pullRequest.CreateComment(string.Empty));
800+
var thread = pullRequest.CreateComment("Foo");
793801

794802
// Then
795-
result.IsArgumentOutOfRangeException("comment");
803+
thread.ShouldBeNull();
796804
}
797805

798806
[Fact]
799-
public void Should_Throw_If_Comment_Is_Whitespace()
807+
public void Should_Create_Valid_Thread_With_One_Comment()
800808
{
801809
// Given
802810
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsServerUrl, 100);
803811
var pullRequest = new AzureDevOpsPullRequest(fixture.Log, fixture.Settings, fixture.GitClientFactory);
804812

805813
// When
806-
var result = Record.Exception(() => pullRequest.CreateComment(" "));
814+
var thread = pullRequest.CreateComment("Valid");
807815

808816
// Then
809-
result.IsArgumentOutOfRangeException("comment");
817+
thread.ShouldNotBeNull();
818+
thread.Status.ShouldBe(AzureDevOpsCommentThreadStatus.Active);
819+
thread.Comments.ShouldNotBeNull();
820+
thread.Comments.Count().ShouldBe(1);
821+
822+
var comment = thread.Comments.First();
823+
comment.CommentType.ShouldBe(AzureDevOpsCommentType.System);
824+
comment.IsDeleted.ShouldBeFalse();
825+
comment.Content.ShouldBe("Valid");
810826
}
827+
}
811828

829+
public sealed class TheCreateCommentThreadMethod
830+
{
812831
[Fact]
813-
public void Should_Not_Throw_If_Null_Is_Returned()
832+
public void Should_Throw_If_Input_Thread_Is_Null()
814833
{
815834
// Given
816-
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsServerUrl, 100)
817-
{
818-
GitClientFactory = new FakeNullForMethodsGitClientFactory(),
819-
};
835+
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsServerUrl, 100);
820836
var pullRequest = new AzureDevOpsPullRequest(fixture.Log, fixture.Settings, fixture.GitClientFactory);
821837

822838
// When
823-
pullRequest.CreateComment("Foo");
839+
var result = Record.Exception(() => pullRequest.CreateCommentThread(null));
824840

825841
// Then
826-
// ?? Nothing to validate here since the method returns void
842+
result.IsArgumentNullException("thread");
827843
}
828-
}
829844

830-
public sealed class TheCreateCommentThreadMethod
831-
{
832845
[Fact]
833-
public void Should_Throw_If_Input_Thread_Is_Null()
846+
public void Should_Return_Null_If_Pull_Request_Is_Invalid()
834847
{
835848
// Given
836-
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsServerUrl, 100);
849+
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsServerUrl, 100)
850+
{
851+
GitClientFactory = new FakeNullGitClientFactory(),
852+
};
853+
fixture.Settings.ThrowExceptionIfPullRequestCouldNotBeFound = false;
854+
837855
var pullRequest = new AzureDevOpsPullRequest(fixture.Log, fixture.Settings, fixture.GitClientFactory);
838856

839857
// When
840-
var result = Record.Exception(() => pullRequest.CreateCommentThread(null));
858+
var outThread = pullRequest.CreateCommentThread(new AzureDevOpsPullRequestCommentThread());
841859

842860
// Then
843-
result.IsArgumentNullException("thread");
861+
outThread.ShouldBeNull();
844862
}
845863

846864
[Fact]
847-
public void Should_Not_Throw_If_Null_Is_Returned()
865+
public void Should_Return_Null_If_Null_Is_Returned_From_Git_Client()
848866
{
849867
// Given
850868
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsServerUrl, 100)
851869
{
852870
GitClientFactory = new FakeNullForMethodsGitClientFactory(),
853871
};
872+
854873
var pullRequest = new AzureDevOpsPullRequest(fixture.Log, fixture.Settings, fixture.GitClientFactory);
855874

856875
// When
857-
pullRequest.CreateCommentThread(new AzureDevOpsPullRequestCommentThread());
876+
var outThread = pullRequest.CreateCommentThread(new AzureDevOpsPullRequestCommentThread());
858877

859878
// Then
860-
// ?? Nothing to validate here since the method returns void
879+
outThread.ShouldBeNull();
861880
}
862881

863882
[Fact]
@@ -866,12 +885,15 @@ public void Should_Create_Valid_Comment_Thread()
866885
// Given
867886
var fixture = new PullRequestFixture(BasePullRequestFixture.ValidAzureDevOpsUrl, 200);
868887
var pullRequest = new AzureDevOpsPullRequest(fixture.Log, fixture.Settings, fixture.GitClientFactory);
888+
var inThread = new AzureDevOpsPullRequestCommentThread { Id = 300, Status = AzureDevOpsCommentThreadStatus.Pending, FilePath = "/index.html" };
869889

870890
// When
871-
pullRequest.CreateCommentThread(new AzureDevOpsPullRequestCommentThread { Id = 300, Status = AzureDevOpsCommentThreadStatus.Pending, FilePath = "/index.html" });
891+
var outThread = pullRequest.CreateCommentThread(inThread);
872892

873893
// Then
874-
// ?? Nothing to validate here since the method returns void
894+
outThread.Id.ShouldBe(inThread.Id);
895+
outThread.Status.ShouldBe(inThread.Status);
896+
outThread.FilePath.ShouldBeEquivalentTo(inThread.FilePath);
875897
}
876898
}
877899

src/Cake.AzureDevOps/Repos/PullRequest/AzureDevOpsPullRequest.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,8 @@ public void ActivateCommentThread(int threadId)
577577
/// Creates a new comment thread with a single comment in the pull request.
578578
/// </summary>
579579
/// <param name="comment">Comment which should be added.</param>
580-
public void CreateComment(string comment)
580+
/// <returns>A newly created comment thread, or null if it can't be created.</returns>
581+
public AzureDevOpsPullRequestCommentThread CreateComment(string comment)
581582
{
582583
comment.NotNullOrWhiteSpace(nameof(comment));
583584

@@ -595,33 +596,41 @@ public void CreateComment(string comment)
595596
},
596597
};
597598

598-
this.CreateCommentThread(thread);
599+
return this.CreateCommentThread(thread);
599600
}
600601

601602
/// <summary>
602603
/// Creates a new comment thread in the pull request.
603604
/// </summary>
604605
/// <param name="thread">The instance of the thread.</param>
605-
public void CreateCommentThread(AzureDevOpsPullRequestCommentThread thread)
606+
/// <returns>A newly created comment thread, or null if it can't be created.</returns>
607+
public AzureDevOpsPullRequestCommentThread CreateCommentThread(AzureDevOpsPullRequestCommentThread thread)
606608
{
607609
thread.NotNull(nameof(thread));
608610

611+
AzureDevOpsPullRequestCommentThread resultingThread = null;
609612
if (!this.ValidatePullRequest())
610613
{
611-
return;
614+
return resultingThread;
612615
}
613616

614617
using (var gitClient = this.gitClientFactory.CreateGitClient(this.CollectionUrl, this.credentials))
615618
{
616-
gitClient
617-
.CreateThreadAsync(
619+
var newThread = gitClient.CreateThreadAsync(
618620
thread.InnerThread,
619621
this.RepositoryId,
620622
this.PullRequestId)
621623
.ConfigureAwait(false)
622624
.GetAwaiter()
623625
.GetResult();
626+
627+
if (newThread != null)
628+
{
629+
resultingThread = new AzureDevOpsPullRequestCommentThread(newThread);
630+
}
624631
}
632+
633+
return resultingThread;
625634
}
626635

627636
/// <summary>

0 commit comments

Comments
 (0)