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

Commit cb7816c

Browse files
committed
Explicitly match CommitSha in blob URLs
1 parent 55b1570 commit cb7816c

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

src/GitHub.App/Services/GitHubContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public class GitHubContext
77
public string Owner { get; set; }
88
public string RepositoryName { get; set; }
99
public string Host { get; set; }
10-
public string Branch { get; set; }
10+
public string BranchName { get; set; }
11+
public string CommitSha { get; set; }
1112
public int? PullRequest { get; set; }
1213
public int? Issue { get; set; }
1314
public string Path { get; set; }

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class GitHubContextService
4343
static readonly Regex windowTitleBranchesRegex = new Regex($"Branches · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
4444

4545
static readonly Regex urlLineRegex = new Regex($"#L(?<line>[0-9]+)(-L(?<lineEnd>[0-9]+))?$", RegexOptions.Compiled);
46+
static readonly Regex urlBlobCommitRegex = new Regex($"blob/(?<commit>[a-z0-9]{{40}})/(?<path>[^#]*)", RegexOptions.Compiled);
47+
static readonly Regex urlBlobBranchRegex = new Regex($"blob/(?<branch>master)/(?<path>[^#]*)", RegexOptions.Compiled);
4648

4749
[ImportingConstructor]
4850
public GitHubContextService([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
@@ -63,17 +65,42 @@ public GitHubContext FindContextFromUrl(string url)
6365
return null;
6466
}
6567

66-
var (line, lineEnd) = FindLine(uri);
67-
return new GitHubContext
68+
var context = new GitHubContext
6869
{
6970
Host = uri.Host,
7071
Owner = uri.Owner,
7172
RepositoryName = uri.RepositoryName,
72-
Path = FindPath(uri),
73-
PullRequest = FindPullRequest(uri),
74-
Line = line,
75-
LineEnd = lineEnd
7673
};
74+
75+
var repositoryPrefix = uri.ToRepositoryUrl().ToString() + "/";
76+
if (!url.StartsWith(repositoryPrefix, StringComparison.OrdinalIgnoreCase))
77+
{
78+
return context;
79+
}
80+
81+
var subpath = url.Substring(repositoryPrefix.Length);
82+
83+
(context.Line, context.LineEnd) = FindLine(subpath);
84+
85+
context.PullRequest = FindPullRequest(url);
86+
87+
var match = urlBlobCommitRegex.Match(subpath);
88+
if (match.Success)
89+
{
90+
context.CommitSha = match.Groups["commit"].Value;
91+
context.Path = match.Groups["path"].Value;
92+
return context;
93+
}
94+
95+
match = urlBlobBranchRegex.Match(subpath);
96+
if (match.Success)
97+
{
98+
context.BranchName = match.Groups["branch"].Value;
99+
context.Path = match.Groups["path"].Value;
100+
return context;
101+
}
102+
103+
return context;
77104
}
78105

79106
public GitHubContext FindContextFromBrowser()
@@ -119,7 +146,7 @@ public GitHubContext FindContextFromWindowTitle(string windowTitle)
119146
{
120147
Owner = match.Groups["owner"].Value,
121148
RepositoryName = match.Groups["repo"].Value,
122-
Branch = match.Groups["branch"].Value,
149+
BranchName = match.Groups["branch"].Value,
123150
Path = match.Groups["path"].Value
124151
};
125152
}
@@ -141,7 +168,7 @@ public GitHubContext FindContextFromWindowTitle(string windowTitle)
141168
{
142169
Owner = match.Groups["owner"].Value,
143170
RepositoryName = match.Groups["repo"].Value,
144-
Branch = match.Groups["branch"].Value,
171+
BranchName = match.Groups["branch"].Value,
145172
};
146173
}
147174

@@ -256,23 +283,6 @@ IVsTextView OpenDocument(string fullPath)
256283
return (null, null);
257284
}
258285

259-
string FindPath(UriString uri)
260-
{
261-
var blob = FindSubPath(uri, "/blob/");
262-
if (blob == null)
263-
{
264-
return null;
265-
}
266-
267-
var pathIndex = blob.IndexOf('/');
268-
if (pathIndex == -1)
269-
{
270-
return null;
271-
}
272-
273-
return blob.Substring(pathIndex + 1);
274-
}
275-
276286
static int? FindPullRequest(UriString gitHubUrl)
277287
{
278288
var pullRequest = FindSubPath(gitHubUrl, "/pull/")?.Split('/').First();

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ public void PullRequest(string url, int? expectPullRequest)
6363
Assert.That(context?.PullRequest, Is.EqualTo(expectPullRequest));
6464
}
6565

66+
[TestCase("https://github.com/github/VisualStudio/blob/master", null, null, null)]
67+
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "master", null, "foo.cs")]
68+
[TestCase("https://github.com/github/VisualStudio/blob/master/path/foo.cs", "master", null, "path/foo.cs")]
69+
[TestCase("https://github.com/github/VisualStudio/blob/ee863ce265fc6217f589e66766125fed1b5b8256/path/foo.cs", null, "ee863ce265fc6217f589e66766125fed1b5b8256", "path/foo.cs")]
70+
[TestCase("https://github.com/github/VisualStudio/blob/not_master/foo.cs", null, null, null, Description = "We currently only match SHA and master")]
71+
public void Blob(string url, string expectBranch, string expectCommitSha, string expectPath)
72+
{
73+
var target = CreateGitHubContextService();
74+
75+
var context = target.FindContextFromUrl(url);
76+
77+
Assert.That(context.BranchName, Is.EqualTo(expectBranch));
78+
Assert.That(context.CommitSha, Is.EqualTo(expectCommitSha));
79+
Assert.That(context.Path, Is.EqualTo(expectPath));
80+
}
81+
6682
[TestCase("https://github.com", null)]
6783
[TestCase("https://github.com/github", null)]
6884
[TestCase("https://github.com/github/VisualStudio", null)]
@@ -222,7 +238,7 @@ public void Branch(string branch, string expectBranch)
222238

223239
var context = target.FindContextFromWindowTitle(windowTitle);
224240

225-
Assert.That(context?.Branch, Is.EqualTo(expectBranch));
241+
Assert.That(context?.BranchName, Is.EqualTo(expectBranch));
226242
}
227243

228244
[TestCase("github/VisualStudio: GitHub Extension for Visual Studio - Google Chrome", "github", "VisualStudio", null)]
@@ -241,7 +257,7 @@ public void OwnerRepositoryBranch(string windowTitle, string expectOwner, string
241257

242258
Assert.That(context.Owner, Is.EqualTo(expectOwner));
243259
Assert.That(context.RepositoryName, Is.EqualTo(expectRepositoryName));
244-
Assert.That(context.Branch, Is.EqualTo(expectBranch));
260+
Assert.That(context.BranchName, Is.EqualTo(expectBranch));
245261
}
246262

247263
[TestCase("github/VisualStudio at build/appveyor-fixes - Google Chrome", "github", "VisualStudio", "build/appveyor-fixes", Description = "Chrome")]
@@ -254,7 +270,7 @@ public void TreeBranch(string windowTitle, string expectOwner, string expectRepo
254270

255271
Assert.That(context.Owner, Is.EqualTo(expectOwner));
256272
Assert.That(context.RepositoryName, Is.EqualTo(expectRepositoryName));
257-
Assert.That(context.Branch, Is.EqualTo(expectBranch));
273+
Assert.That(context.BranchName, Is.EqualTo(expectBranch));
258274
}
259275

260276
[TestCase("Branches · github/VisualStudio - Google Chrome", "github", "VisualStudio", Description = "Chrome")]

0 commit comments

Comments
 (0)