Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 245d9f0

Browse files
authored
Merge pull request #424 from gazab/fixes/generate-link-startline-endline-order
Fix url generation with startLine and endLine
2 parents 9ed5631 + 5e4eefd commit 245d9f0

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/GitHub.Exports/Models/SimpleRepositoryModel.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ public UriString GenerateUrl(string path = null, int startLine = -1, int endLine
8484
path = path.Substring(LocalPath.Length + 1);
8585
}
8686

87+
if (startLine > 0 && endLine > 0 && startLine > endLine)
88+
{
89+
// if startLine is greater than endLine and both are set, swap them
90+
var temp = startLine;
91+
startLine = endLine;
92+
endLine = temp;
93+
}
94+
95+
if (startLine == endLine)
96+
{
97+
// if startLine is the same as endLine don't generate a range link
98+
endLine = -1;
99+
}
100+
87101
return new UriString(GenerateUrl(CloneUrl.ToRepositoryUrl().AbsoluteUri, sha, path, startLine, endLine));
88102
}
89103

src/UnitTests/GitHub.Exports/SimpleRepositoryModelTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ void SetupRepository(string sha)
2828
[Theory]
2929
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
3030
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")]
31+
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, 1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")]
3132
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1-L2")]
33+
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 2, 1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1-L2")]
3234
[InlineData(false, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
3335
[InlineData(false, "https://github.com/foo/bar", "123123", "", 1, 2, "https://github.com/foo/bar/commit/123123")]
3436
[InlineData(false, "https://github.com/foo/bar", "", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar")]
3537
[InlineData(false, "https://github.com/foo/bar", null, null, -1, -1, "https://github.com/foo/bar")]
3638
[InlineData(false, null, "123123", @"src\dir\file1.cs", 1, 2, null)]
3739
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
3840
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")]
41+
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, 1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1")]
3942
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1-L2")]
43+
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", 2, 1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs#L1-L2")]
4044
[InlineData(true, "https://github.com/foo/bar", "123123", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
4145
[InlineData(true, "https://github.com/foo/bar", "", @"src\dir\file1.cs", -1, 2, "https://github.com/foo/bar")]
4246
[InlineData(true, null, "123123", @"src\dir\file1.cs", 1, 2, null)]

0 commit comments

Comments
 (0)