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

Commit 0bed9b2

Browse files
committed
Find line end from GitHub URL
1 parent 90c0aa9 commit 0bed9b2

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/GitHub.App/Services/GitHubContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public class GitHubContext
1212
public int? Issue { get; set; }
1313
public string Path { get; set; }
1414
public int? Line { get; set; }
15+
public int? LineEnd { get; set; }
1516
}
1617
}

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class GitHubContextService
3535
static readonly Regex windowTitlePathRegex = new Regex($"{path} at {branch} · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
3636
static readonly Regex windowTitleBranchesRegex = new Regex($"Branches · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
3737

38-
static readonly Regex urlLineRegex = new Regex($"#L(?<line>[0-9]+)$", RegexOptions.Compiled);
38+
static readonly Regex urlLineRegex = new Regex($"#L(?<line>[0-9]+)(-L(?<lineEnd>[0-9]+))?$", RegexOptions.Compiled);
3939

4040
public GitHubContext FindContextFromUrl(string url)
4141
{
@@ -50,14 +50,16 @@ public GitHubContext FindContextFromUrl(string url)
5050
return null;
5151
}
5252

53+
var (line, lineEnd) = FindLine(uri);
5354
return new GitHubContext
5455
{
5556
Host = uri.Host,
5657
Owner = uri.Owner,
5758
RepositoryName = uri.RepositoryName,
5859
Path = FindPath(uri),
5960
PullRequest = FindPullRequest(uri),
60-
Line = FindLine(uri)
61+
Line = line,
62+
LineEnd = lineEnd
6163
};
6264
}
6365

@@ -158,18 +160,26 @@ public GitHubContext FindContextFromWindowTitle(string windowTitle)
158160
}
159161

160162

161-
static int? FindLine(UriString gitHubUrl)
163+
static (int? lineStart, int? lineEnd) FindLine(UriString gitHubUrl)
162164
{
163165
var url = gitHubUrl.ToString();
164166

165167
var match = urlLineRegex.Match(url);
166168
if (match.Success)
167169
{
168170
int.TryParse(match.Groups["line"].Value, out int line);
169-
return line;
171+
172+
var lineEndGroup = match.Groups["lineEnd"];
173+
if (string.IsNullOrEmpty(lineEndGroup.Value))
174+
{
175+
return (line, null);
176+
}
177+
178+
int.TryParse(lineEndGroup.Value, out int lineEnd);
179+
return (line, lineEnd);
170180
}
171181

172-
return null;
182+
return (null, null);
173183
}
174184

175185
string FindPath(UriString uri)

test/GitHub.App.UnitTests/Services/GitHubContextServiceTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ public void Line(string url, int? expectLine)
104104
Assert.That(context.Line, Is.EqualTo(expectLine));
105105
}
106106

107+
[TestCase("https://github.com/github/VisualStudio", null, null)]
108+
[TestCase("https://github.com/github/VisualStudio/blob/master/Code.cs#L115", 115, null)]
109+
[TestCase("https://github.com/github/VisualStudio/blob/master/Code.cs#L115-L116", 115, 116)]
110+
public void LineEnd(string url, int? expectLine, int? expectLineEnd)
111+
{
112+
var target = new GitHubContextService();
113+
114+
var context = target.FindContextFromUrl(url);
115+
116+
Assert.That(context.Line, Is.EqualTo(expectLine));
117+
Assert.That(context.LineEnd, Is.EqualTo(expectLineEnd));
118+
}
119+
107120
[TestCase("foo", true)]
108121
[TestCase("ssh://[email protected]:443/benstraub/libgit2", true)]
109122
[TestCase("https://github.com/github/VisualStudio", false)]

0 commit comments

Comments
 (0)