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

Commit 9b0fe66

Browse files
committed
Add support for file links that use a commit SHA
Assume that branch names don't contain a '/' (sic).
1 parent 19249b0 commit 9b0fe66

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public GitHubContext FindContextFromUrl(string url)
5353
Host = uri.Host,
5454
Owner = uri.Owner,
5555
RepositoryName = uri.RepositoryName,
56-
Path = FindSubPath(uri, "/blob/master/"),
56+
Path = FindPath(uri),
5757
PullRequest = FindPullRequest(uri),
5858
Line = FindLine(uri)
5959
};
@@ -170,6 +170,23 @@ public GitHubContext FindContextFromWindowTitle(string windowTitle)
170170
return lineNumber; // 1 based
171171
}
172172

173+
string FindPath(UriString uri)
174+
{
175+
var blob = FindSubPath(uri, "/blob/");
176+
if (blob == null)
177+
{
178+
return null;
179+
}
180+
181+
var pathIndex = blob.IndexOf('/');
182+
if (pathIndex == -1)
183+
{
184+
return null;
185+
}
186+
187+
return blob.Substring(pathIndex + 1);
188+
}
189+
173190
static int? FindPullRequest(UriString gitHubUrl)
174191
{
175192
var pullRequest = FindSubPath(gitHubUrl, "/pull/")?.Split('/').First();

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void PullRequest(string url, int? expectPullRequest)
6767
[TestCase("https://github.com/github/VisualStudio", null)]
6868
[TestCase("https://github.com/github/VisualStudio/blob/master/README.md", "README.md")]
6969
[TestCase("https://github.com/github/VisualStudio/blob/master/README.md#notices", "README.md")]
70+
[TestCase("https://github.com/github/VisualStudio/blob/0d264d50c57d701fa62d202f481075a6c6dbdce8/src/Code.cs#L86", "src/Code.cs")]
7071
public void Path(string url, string expectPath)
7172
{
7273
var target = new GitHubContextService();
@@ -76,12 +77,24 @@ public void Path(string url, string expectPath)
7677
Assert.That(context.Path, Is.EqualTo(expectPath));
7778
}
7879

80+
// HACK: We're assuming that branches don't contain a '/' (sic)
81+
[TestCase("https://github.com/github/VisualStudio/blob/fixes/branch/buggy.cs", "branch/buggy.cs")]
82+
public void ProblemPath(string url, string expectPath)
83+
{
84+
var target = new GitHubContextService();
85+
86+
var context = target.FindContextFromUrl(url);
87+
88+
Assert.That(context.Path, Is.EqualTo(expectPath));
89+
}
90+
7991
[TestCase("https://github.com", null)]
8092
[TestCase("https://github.com/github", null)]
8193
[TestCase("https://github.com/github/VisualStudio", null)]
8294
[TestCase("https://github.com/github/VisualStudio/blob/master/README.md", null)]
8395
[TestCase("https://github.com/github/VisualStudio/blob/master/README.md#notices", null)]
8496
[TestCase("https://github.com/github/VisualStudio/blob/master/src/GitHub.VisualStudio/GitHubPackage.cs#L38", 38)]
97+
[TestCase("https://github.com/github/VisualStudio/blob/0d264d50c57d701fa62d202f481075a6c6dbdce8/src/Code.cs#L86", 86)]
8598
public void Line(string url, int? expectLine)
8699
{
87100
var target = new GitHubContextService();

0 commit comments

Comments
 (0)