Skip to content

Commit bece6c4

Browse files
authored
(GH-188) Add IIssue.FileLink and pass FileLink settings to issue providers (#192)
* (GH-188) Add possibility to pass file link settings while reading issues * (GH-188) Add IIssue.FileLink
1 parent ba8d445 commit bece6c4

32 files changed

+750
-23
lines changed

src/Cake.Issues.Testing/BaseIssueProviderFixture.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public abstract class BaseIssueProviderFixture<T>
1919
protected BaseIssueProviderFixture()
2020
{
2121
this.Log = new FakeLog { Verbosity = Verbosity.Normal };
22-
this.RepositorySettings = new RepositorySettings(@"c:\repo");
22+
this.ReadIssuesSettings = new ReadIssuesSettings(@"c:\repo");
2323
}
2424

2525
/// <summary>
@@ -30,7 +30,7 @@ protected BaseIssueProviderFixture()
3030
/// <summary>
3131
/// Gets or sets the repository settings.
3232
/// </summary>
33-
public RepositorySettings RepositorySettings { get; set; }
33+
public ReadIssuesSettings ReadIssuesSettings { get; set; }
3434

3535
/// <summary>
3636
/// Calls <see cref="BaseIssueProvider.ReadIssues()"/>.
@@ -67,12 +67,12 @@ private T CreateIssueProvider()
6767
typeof(T),
6868
this.GetCreateIssueProviderArguments().ToArray());
6969

70-
if (this.RepositorySettings == null)
70+
if (this.ReadIssuesSettings == null)
7171
{
72-
throw new InvalidOperationException("No repository settings set.");
72+
throw new InvalidOperationException("No settings for reading issues set.");
7373
}
7474

75-
provider.Initialize(this.RepositorySettings);
75+
provider.Initialize(this.ReadIssuesSettings);
7676
return provider;
7777
}
7878
}

src/Cake.Issues.Testing/FakeConfigurableIssueProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public FakeConfigurableIssueProvider(
5959
public override string ProviderName => "Fake Issue Provider";
6060

6161
/// <inheritdoc/>
62-
protected override IEnumerable<IIssue> InternalReadIssues()
62+
protected override IEnumerable<IIssue> InternalReadIssues(FileLinkSettings fileLinkSettings)
6363
{
6464
return this.issues;
6565
}

src/Cake.Issues.Testing/FakeIssueProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public FakeIssueProvider(ICakeLog log, IEnumerable<IIssue> issues)
4848
public override string ProviderName => "Fake Issue Provider";
4949

5050
/// <inheritdoc/>
51-
protected override IEnumerable<IIssue> InternalReadIssues()
51+
protected override IEnumerable<IIssue> InternalReadIssues(FileLinkSettings fileLinkSettings)
5252
{
5353
return this.issues;
5454
}

src/Cake.Issues.Testing/IssueChecker.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static void Check(
5050
expectedIssue.EndLine,
5151
expectedIssue.Column,
5252
expectedIssue.EndColumn,
53+
expectedIssue.FileLink,
5354
expectedIssue.MessageText,
5455
expectedIssue.MessageHtml,
5556
expectedIssue.MessageMarkdown,
@@ -81,6 +82,8 @@ public static void Check(
8182
/// <c>null</c> if the issue is not expected to be related to a file or specific column.</param>
8283
/// <param name="endColumn">Expected end of column range.
8384
/// <c>null</c> if the issue is not expected to be related to a file, specific column or range of columns.</param>
85+
/// <param name="fileLink">Expected file link.
86+
/// <c>null</c> if the issue is not expected to have a file link.</param>
8487
/// <param name="messageText">Expected message in plain text format.</param>
8588
/// <param name="messageHtml">Expected message in HTML format.</param>
8689
/// <param name="messageMarkdown">Expected message in Markdown format.</param>
@@ -105,6 +108,7 @@ public static void Check(
105108
int? endLine,
106109
int? column,
107110
int? endColumn,
111+
Uri fileLink,
108112
string messageText,
109113
string messageHtml,
110114
string messageMarkdown,
@@ -215,6 +219,12 @@ public static void Check(
215219
$"Expected issue.EndColumn to be '{endColumn}' but was '{issue.EndColumn}'.");
216220
}
217221

222+
if (issue.FileLink?.ToString() != fileLink?.ToString())
223+
{
224+
throw new Exception(
225+
$"Expected issue.FileLink to be '{fileLink}' but was '{issue.FileLink}'.");
226+
}
227+
218228
if (issue.MessageText != messageText)
219229
{
220230
throw new Exception(

src/Cake.Issues.Tests/BaseMultiFormatIssueProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void Should_Read_Issues_From_Format()
110110
"Foo".ToByteArray(),
111111
format);
112112
var provider = new FakeMultiFormatIssueProvider(log, settings);
113-
provider.Initialize(new RepositorySettings(@"c:\repo"));
113+
provider.Initialize(new ReadIssuesSettings(@"c:\repo"));
114114

115115
// When
116116
var result = provider.ReadIssues();

src/Cake.Issues.Tests/IIssueComparerTests.cs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,41 @@ public void Should_Return_False_If_Column_Is_Different(int? column1, int? column
155155
CompareIssues(issue1, issue2, false);
156156
}
157157

158+
[Theory]
159+
[InlineData("http://foo", "http://bar")]
160+
[InlineData("http://foo", null)]
161+
[InlineData(null, "http://foo")]
162+
public void Should_Return_False_If_FileLink_Is_Different(string fileLink1, string fileLink2)
163+
{
164+
// Given
165+
var issueBuilder =
166+
IssueBuilder
167+
.NewIssue("message", "providerType", "providerName");
168+
if (!string.IsNullOrEmpty(fileLink1))
169+
{
170+
issueBuilder =
171+
issueBuilder
172+
.WithFileLink(new Uri(fileLink1));
173+
}
174+
175+
var issue1 = issueBuilder.Create();
176+
177+
issueBuilder =
178+
IssueBuilder
179+
.NewIssue("message", "providerType", "providerName");
180+
if (!string.IsNullOrEmpty(fileLink2))
181+
{
182+
issueBuilder =
183+
issueBuilder
184+
.WithFileLink(new Uri(fileLink2));
185+
}
186+
187+
var issue2 = issueBuilder.Create();
188+
189+
// When / Then
190+
CompareIssues(issue1, issue2, false);
191+
}
192+
158193
[Fact]
159194
public void Should_Return_False_If_MessageText_Is_Different()
160195
{
@@ -532,6 +567,41 @@ public void Should_Return_True_If_Column_Is_Same(int? column1, int? column2)
532567
CompareIssues(issue1, issue2, true);
533568
}
534569

570+
[Theory]
571+
[InlineData("http://foo", "http://foo")]
572+
[InlineData("http://foo", "http://Foo")]
573+
[InlineData(null, null)]
574+
public void Should_Return_True_If_FileLink_Is_Same(string fileLink1, string fileLink2)
575+
{
576+
// Given
577+
var issueBuilder =
578+
IssueBuilder
579+
.NewIssue("message", "providerType", "providerName");
580+
if (!string.IsNullOrEmpty(fileLink1))
581+
{
582+
issueBuilder =
583+
issueBuilder
584+
.WithFileLink(new Uri(fileLink1));
585+
}
586+
587+
var issue1 = issueBuilder.Create();
588+
589+
issueBuilder =
590+
IssueBuilder
591+
.NewIssue("message", "providerType", "providerName");
592+
if (!string.IsNullOrEmpty(fileLink2))
593+
{
594+
issueBuilder =
595+
issueBuilder
596+
.WithFileLink(new Uri(fileLink2));
597+
}
598+
599+
var issue2 = issueBuilder.Create();
600+
601+
// When / Then
602+
CompareIssues(issue1, issue2, true);
603+
}
604+
535605
[Fact]
536606
public void Should_Return_True_If_MessageText_Is_Same()
537607
{
@@ -1322,6 +1392,76 @@ public void Should_Return_True_If_Column_Is_Same(int? column1, int? column2)
13221392
CompareIssues(issue1, issue2, true);
13231393
}
13241394

1395+
[Theory]
1396+
[InlineData("http://foo", "http://bar")]
1397+
[InlineData("http://foo", null)]
1398+
[InlineData(null, "http://foo")]
1399+
public void Should_Return_True_If_FileLink_Is_Different(string fileLink1, string fileLink2)
1400+
{
1401+
// Given
1402+
var issueBuilder =
1403+
IssueBuilder
1404+
.NewIssue("message", "providerType", "providerName");
1405+
if (!string.IsNullOrEmpty(fileLink1))
1406+
{
1407+
issueBuilder =
1408+
issueBuilder
1409+
.WithFileLink(new Uri(fileLink1));
1410+
}
1411+
1412+
var issue1 = issueBuilder.Create();
1413+
1414+
issueBuilder =
1415+
IssueBuilder
1416+
.NewIssue("message", "providerType", "providerName");
1417+
if (!string.IsNullOrEmpty(fileLink2))
1418+
{
1419+
issueBuilder =
1420+
issueBuilder
1421+
.WithFileLink(new Uri(fileLink2));
1422+
}
1423+
1424+
var issue2 = issueBuilder.Create();
1425+
1426+
// When / Then
1427+
CompareIssues(issue1, issue2, true);
1428+
}
1429+
1430+
[Theory]
1431+
[InlineData("http://foo", "http://foo")]
1432+
[InlineData("http://foo", "http://Foo")]
1433+
[InlineData(null, null)]
1434+
public void Should_Return_True_If_FileLink_Is_Same(string fileLink1, string fileLink2)
1435+
{
1436+
// Given
1437+
var issueBuilder =
1438+
IssueBuilder
1439+
.NewIssue("message", "providerType", "providerName");
1440+
if (!string.IsNullOrEmpty(fileLink1))
1441+
{
1442+
issueBuilder =
1443+
issueBuilder
1444+
.WithFileLink(new Uri(fileLink1));
1445+
}
1446+
1447+
var issue1 = issueBuilder.Create();
1448+
1449+
issueBuilder =
1450+
IssueBuilder
1451+
.NewIssue("message", "providerType", "providerName");
1452+
if (!string.IsNullOrEmpty(fileLink2))
1453+
{
1454+
issueBuilder =
1455+
issueBuilder
1456+
.WithFileLink(new Uri(fileLink2));
1457+
}
1458+
1459+
var issue2 = issueBuilder.Create();
1460+
1461+
// When / Then
1462+
CompareIssues(issue1, issue2, true);
1463+
}
1464+
13251465
[Fact]
13261466
public void Should_Return_True_If_MessageText_Is_Same()
13271467
{

src/Cake.Issues.Tests/IIssueExtensionsTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ public void Should_Throw_If_Issue_Is_Null()
311311
[InlineData("foo {EndLine} bar", "foo 420 bar")]
312312
[InlineData("foo {Column} bar", "foo 23 bar")]
313313
[InlineData("foo {EndColumn} bar", "foo 230 bar")]
314+
[InlineData("foo {FileLink} bar", "foo https://github.com/myorg/myrepo/blob/develop/src/foo.cs#L10-L12 bar")]
314315
[InlineData("foo {Rule} bar", "foo Rule Foo bar")]
315316
[InlineData("foo {RuleUrl} bar", "foo https://google.com/ bar")]
316317
[InlineData("foo {MessageText} bar", "foo MessageText Foo bar")]
@@ -327,6 +328,7 @@ public void Should_Replace_Tokens(string pattern, string expectedResult)
327328
.WithMessageInMarkdownFormat("MessageMarkdown Foo")
328329
.InFile(@"src/Cake.Issues/foo.cs", 42, 420, 23, 230)
329330
.InProject(@"src/Cake.Issues/Cake.Issues.csproj", "Cake.Issues")
331+
.WithFileLink(new Uri("https://github.com/myorg/myrepo/blob/develop/src/foo.cs#L10-L12"))
330332
.OfRule("Rule Foo", new Uri("https://google.com"))
331333
.WithPriority(IssuePriority.Error)
332334
.Create();

src/Cake.Issues.Tests/IssueBuilderTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,38 @@ public void Should_Set_EndColumn(int endColumn)
16041604
}
16051605
}
16061606

1607+
public sealed class TheWithFileLinkMethod
1608+
{
1609+
[Fact]
1610+
public void Should_Throw_If_FileLink_Is_Null()
1611+
{
1612+
// Given
1613+
var fixture = new IssueBuilderFixture();
1614+
Uri fileLink = null;
1615+
1616+
// When
1617+
var result = Record.Exception(() =>
1618+
fixture.IssueBuilder.WithFileLink(fileLink));
1619+
1620+
// Then
1621+
result.IsArgumentNullException("fileLink");
1622+
}
1623+
1624+
[Theory]
1625+
[InlineData("https://google.com/")]
1626+
public void Should_Set_FileLink(string fileLink)
1627+
{
1628+
// Given
1629+
var fixture = new IssueBuilderFixture();
1630+
1631+
// When
1632+
var issue = fixture.IssueBuilder.WithFileLink(new Uri(fileLink)).Create();
1633+
1634+
// Then
1635+
issue.FileLink.ToString().ShouldBe(fileLink);
1636+
}
1637+
}
1638+
16071639
public sealed class TheWithPriorityMethod
16081640
{
16091641
[Fact]

src/Cake.Issues.Tests/IssueReaderTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,59 @@ public void Should_Set_Run_Property()
330330
issues.ShouldContain(issue2);
331331
issue2.Run.ShouldBe(run);
332332
}
333+
334+
[Fact]
335+
public void Should_Set_FileLink_Property()
336+
{
337+
// Given
338+
var filePath1 = @"src\Cake.Issues.Tests\Foo.cs";
339+
var line1 = 10;
340+
var issue1 =
341+
IssueBuilder
342+
.NewIssue("Foo", "ProviderTypeFoo", "ProviderNameFoo")
343+
.InFile(filePath1, line1)
344+
.OfRule("Foo")
345+
.WithPriority(IssuePriority.Warning)
346+
.Create();
347+
var filePath2 = @"src\Cake.Issues.Tests\Bar.cs";
348+
var line2 = 12;
349+
var issue2 =
350+
IssueBuilder
351+
.NewIssue("Bar", "ProviderTypeBar", "ProviderNameBar")
352+
.InFile(filePath2, line2)
353+
.OfRule("Bar")
354+
.WithPriority(IssuePriority.Warning)
355+
.Create();
356+
var fixture = new IssuesFixture();
357+
fixture.IssueProviders.Clear();
358+
fixture.IssueProviders.Add(
359+
new FakeIssueProvider(
360+
fixture.Log,
361+
new List<IIssue>
362+
{
363+
issue1,
364+
issue2,
365+
}));
366+
var repoUrl = "https://github.com/cake-contrib/Cake.Issues.Website";
367+
var branch = "develop";
368+
fixture.Settings.FileLinkSettings =
369+
FileLinkSettings.GitHub(
370+
new System.Uri(repoUrl),
371+
branch,
372+
null);
373+
374+
// When
375+
var issues = fixture.ReadIssues().ToList();
376+
377+
// Then
378+
issues.Count.ShouldBe(2);
379+
issues.ShouldContain(issue1);
380+
issue1.FileLink.ToString()
381+
.ShouldBe($"{repoUrl}/blob/{branch}/{filePath1.Replace(@"\", "/")}#L{line1}");
382+
issues.ShouldContain(issue2);
383+
issue2.FileLink.ToString()
384+
.ShouldBe($"{repoUrl}/blob/{branch}/{filePath2.Replace(@"\", "/")}#L{line2}");
385+
}
333386
}
334387
}
335388
}

0 commit comments

Comments
 (0)