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

Commit e900d7d

Browse files
committed
Update title regex to work with Firefox
Search for Chrome then Firefox windows. Need to find a way to search in order of height.
1 parent 7320235 commit e900d7d

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public class GitHubContextService
2828

2929
static readonly string path = $"^{repo}/(?<path>[^ ]+)";
3030

31-
static readonly Regex windowTitleRepositoryRegex = new Regex($"^{owner}/{repo}: ", RegexOptions.Compiled);
32-
static readonly Regex windowTitleBranchRegex = new Regex($"^{owner}/{repo} at {branch} ", RegexOptions.Compiled);
33-
static readonly Regex windowTitlePullRequestRegex = new Regex($" · Pull Request #{pull} · {owner}/{repo} - ", RegexOptions.Compiled);
34-
static readonly Regex windowTitleIssueRegex = new Regex($" · Issue #{issue} · {owner}/{repo} - ", RegexOptions.Compiled);
35-
static readonly Regex windowTitlePathRegex = new Regex($"{path} at {branch} · {owner}/{repo} - ", RegexOptions.Compiled);
36-
static readonly Regex windowTitleBranchesRegex = new Regex($"Branches · {owner}/{repo} - ", RegexOptions.Compiled);
31+
static readonly Regex windowTitleRepositoryRegex = new Regex($"^(GitHub - )?{owner}/{repo}: ", RegexOptions.Compiled);
32+
static readonly Regex windowTitleBranchRegex = new Regex($"^(GitHub - )?{owner}/{repo} at {branch} ", RegexOptions.Compiled);
33+
static readonly Regex windowTitlePullRequestRegex = new Regex($" · Pull Request #{pull} · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
34+
static readonly Regex windowTitleIssueRegex = new Regex($" · Issue #{issue} · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
35+
static readonly Regex windowTitlePathRegex = new Regex($"{path} at {branch} · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
36+
static readonly Regex windowTitleBranchesRegex = new Regex($"Branches · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);
3737

3838
public GitHubContext FindContextFromUrl(string url)
3939
{
@@ -61,10 +61,14 @@ public GitHubContext FindContextFromUrl(string url)
6161

6262
public GitHubContext FindContextFromBrowser()
6363
{
64-
return FindWindowTitlesForClass("Chrome_WidgetWin_1").Select(FindContextFromWindowTitle).Where(x => x != null).FirstOrDefault();
64+
return
65+
FindWindowTitlesForClass("Chrome_WidgetWin_1") // Chrome
66+
.Concat(FindWindowTitlesForClass("MozillaWindowClass")) // Firefox
67+
.Select(FindContextFromWindowTitle).Where(x => x != null)
68+
.FirstOrDefault();
6569
}
6670

67-
public IEnumerable<string> FindWindowTitlesForClass(string className = "Chrome_WidgetWin_1")
71+
public IEnumerable<string> FindWindowTitlesForClass(string className = "MozillaWindowClass")
6872
{
6973
IntPtr handleWin = IntPtr.Zero;
7074
while (IntPtr.Zero != (handleWin = User32.FindWindowEx(IntPtr.Zero, handleWin, className, IntPtr.Zero)))

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,33 @@ public void OwnerRepositoryBranch(string windowTitle, string expectOwner, string
230230
Assert.That(context.Branch, Is.EqualTo(expectBranch));
231231
}
232232

233-
[TestCase("[spike] Open from GitHub URL by jcansdale · Pull Request #1763 · github/VisualStudio - Google Chrome", 1763)]
233+
[TestCase("github/VisualStudio at build/appveyor-fixes - Google Chrome", "github", "VisualStudio", "build/appveyor-fixes", Description = "Chrome")]
234+
[TestCase("GitHub - github/VisualStudio at refactor/pr-list - Mozilla Firefox", "github", "VisualStudio", "refactor/pr-list", Description = "Firefox")]
235+
public void TreeBranch(string windowTitle, string expectOwner, string expectRepositoryName, string expectBranch)
236+
{
237+
var target = new GitHubContextService();
238+
239+
var context = target.FindContextFromWindowTitle(windowTitle);
240+
241+
Assert.That(context.Owner, Is.EqualTo(expectOwner));
242+
Assert.That(context.RepositoryName, Is.EqualTo(expectRepositoryName));
243+
Assert.That(context.Branch, Is.EqualTo(expectBranch));
244+
}
245+
246+
[TestCase("Branches · github/VisualStudio - Google Chrome", "github", "VisualStudio", Description = "Chrome")]
247+
[TestCase("Branches · github/VisualStudio · GitHub - Mozilla Firefox", "github", "VisualStudio", Description = "Firefox")]
248+
public void Branches(string windowTitle, string expectOwner, string expectRepositoryName)
249+
{
250+
var target = new GitHubContextService();
251+
252+
var context = target.FindContextFromWindowTitle(windowTitle);
253+
254+
Assert.That(context.Owner, Is.EqualTo(expectOwner));
255+
Assert.That(context.RepositoryName, Is.EqualTo(expectRepositoryName));
256+
}
257+
258+
[TestCase("Description · Pull Request #1763 · github/VisualStudio - Google Chrome", 1763)]
259+
[TestCase("Description · Pull Request #1763 · github/VisualStudio · GitHub - Mozilla Firefox", 1763, Description = "Firefox")]
234260
public void PullRequest(string windowTitle, int expectPullRequest)
235261
{
236262
var target = new GitHubContextService();
@@ -241,6 +267,8 @@ public void PullRequest(string windowTitle, int expectPullRequest)
241267
}
242268

243269
[TestCase("Consider adding C# code style preferences to editorconfig · Issue #1750 · github/VisualStudio - Google Chrome", 1750)]
270+
[TestCase("Scrape browser titles · Issue #4 · jcansdale/VisualStudio · GitHub - Mozilla Firefox", 4, Description = "Firefox")]
271+
244272
public void Issue(string windowTitle, int expectIssue)
245273
{
246274
var target = new GitHubContextService();
@@ -252,6 +280,7 @@ public void Issue(string windowTitle, int expectIssue)
252280

253281
[TestCase("VisualStudio/mark_github.xaml at master · github/VisualStudio - Google Chrome", "mark_github.xaml")]
254282
[TestCase("VisualStudio/src/GitHub.VisualStudio/Resources/icons at master · github/VisualStudio - Google Chrome", "src/GitHub.VisualStudio/Resources/icons")]
283+
[TestCase("VisualStudio/README.md at master · jcansdale/VisualStudio · GitHub - Mozilla Firefox", "README.md", Description = "Firefox")]
255284
public void Path(string windowTitle, string expectPath)
256285
{
257286
var target = new GitHubContextService();
@@ -260,5 +289,17 @@ public void Path(string windowTitle, string expectPath)
260289

261290
Assert.That(context?.Path, Is.EqualTo(expectPath));
262291
}
292+
293+
[TestCase("jcansdale/VisualStudio: GitHub Extension for Visual Studio - Google Chrome", "jcansdale", "VisualStudio", Description = "Chrome")]
294+
[TestCase("GitHub - jcansdale/VisualStudio: GitHub Extension for Visual Studio - Mozilla Firefox", "jcansdale", "VisualStudio", Description = "Firefox")]
295+
public void RepositoryHome(string windowTitle, string expectOwner, string expectRepositoryName)
296+
{
297+
var target = new GitHubContextService();
298+
299+
var context = target.FindContextFromWindowTitle(windowTitle);
300+
301+
Assert.That(context.Owner, Is.EqualTo(expectOwner));
302+
Assert.That(context.RepositoryName, Is.EqualTo(expectRepositoryName));
303+
}
263304
}
264305
}

0 commit comments

Comments
 (0)