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

Open from clipboard url #1771

Merged
merged 69 commits into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
1de94ef
Add GitHub.OpenFromUrl command
jcansdale Jun 22, 2018
516128a
Update UriString to handle extended GitHub URLs
jcansdale Jun 22, 2018
7071b43
Support opening target from GitHub URL
jcansdale Jun 22, 2018
199a79f
Allow navigating to URL from clipboard
jcansdale Jun 25, 2018
134f6a8
Checkout or open using default path
jcansdale Jun 25, 2018
d5b833d
Don't open folder if current solution is inside it
jcansdale Jun 25, 2018
c097098
Add support for navigating to a PR URL
jcansdale Jun 25, 2018
12eb8d5
Open at line number from GitHub URL
jcansdale Jun 25, 2018
9399711
Add Open from GitHub command to File > Open
jcansdale Jun 25, 2018
89484dc
Merge branch 'master' into fixes/1757-open-from-url
jcansdale Jun 26, 2018
0aade59
Update UriString to allow http URLs with owner
jcansdale Jun 26, 2018
70d9d90
Find GitHub context from Chrome window title
jcansdale Jun 27, 2018
a82390f
Add support for opening from topmost browser
jcansdale Jun 27, 2018
c4cac4d
Support syncing the file open in topmost browser
jcansdale Jun 27, 2018
d1b0231
Ask user before cloning or opening a repository
jcansdale Jun 28, 2018
0d264d5
Factor GitHub URL parsing into FindContextFromUrl
jcansdale Jun 28, 2018
19249b0
Convert from GitHub to Visual Studio line numbers
jcansdale Jun 28, 2018
9b0fe66
Add support for file links that use a commit SHA
jcansdale Jun 28, 2018
7320235
Fix a few typos
jcansdale Jun 28, 2018
e900d7d
Update title regex to work with Firefox
jcansdale Jun 29, 2018
5019370
Detect repository home with no description
jcansdale Jul 2, 2018
90c0aa9
Use regex for matching line
jcansdale Jul 3, 2018
0bed9b2
Find line end from GitHub URL
jcansdale Jul 3, 2018
0aae4ca
Add support for navigating to a range of lines
jcansdale Jul 3, 2018
0fc10b7
Highlight line when single line is selected
jcansdale Jul 3, 2018
cfdf410
Select just the target lines
jcansdale Jul 3, 2018
55b1570
Stop the tuple madness
jcansdale Jul 3, 2018
cb7816c
Explicitly match CommitSha in blob URLs
jcansdale Jul 3, 2018
2243891
Add ResolvePath to resolve Treeish + BlobName
jcansdale Jul 3, 2018
39f3a54
Rename Treeish to TreeishPath
jcansdale Jul 4, 2018
d06ba40
First try opening in active repository
jcansdale Jul 4, 2018
d9ce6f5
Add separate `GitHub.OpenFromClipboard` command
jcansdale Jul 4, 2018
dbcb792
Simplify OpenFromClipboardCommand
jcansdale Jul 4, 2018
8e968cc
Show alert when no GitHub URL in clipboard
jcansdale Jul 4, 2018
b31b14f
Add ResolveGitObject to find local object from URL
jcansdale Jul 5, 2018
5b16030
Merge branch 'master' into fixes/1757-open-from-url
jcansdale Jul 5, 2018
92ffa39
Show message when there is no repo to navigate
jcansdale Jul 5, 2018
1f0f5fb
Return commitish and path from ResolveGitObject
jcansdale Jul 5, 2018
1d468d9
Let ResolveGitObject resolve commit without path
jcansdale Jul 5, 2018
5058564
Warn user when changes in local version of file
jcansdale Jul 5, 2018
bc780c4
If target blob has changed then show AnnotateFile
jcansdale Jul 6, 2018
c175d97
Don't throw when navigating to non-blob
jcansdale Jul 6, 2018
7784364
Rename ResolveGitObject to ResolveBlob
jcansdale Jul 6, 2018
6a3d8e4
Remove legacy ResolvePath method
jcansdale Jul 6, 2018
11866fb
Remove Open from GitHub... from File > Open menu
jcansdale Jul 9, 2018
c5c493d
Warn when user isn't in target repository
jcansdale Jul 9, 2018
36d6b0b
Merge branch 'master' into feature/open-from-clipboard-url
jcansdale Jul 9, 2018
3029ccd
Merge branch 'feature/open-from-clipboard-url' of https://github.com/…
jcansdale Jul 9, 2018
de5726f
Make ResolveBlob return if commitish is a SHA
jcansdale Jul 9, 2018
ab3e7e9
Change ResolveBlob to resolve remote refs
jcansdale Jul 9, 2018
19be672
Return commit SHA from ResolveBlob
jcansdale Jul 10, 2018
381806b
Add support for resolving tags
jcansdale Jul 10, 2018
f81fb36
Warn if URL owner is different to current owner
jcansdale Jul 10, 2018
e1bfa23
Allow repository names to differ in case
jcansdale Jul 10, 2018
fd0f835
Merge branch 'master' into feature/open-from-clipboard-url
jcansdale Jul 12, 2018
c429d76
Show dialog when attempting to open non-blob URL
jcansdale Jul 12, 2018
9ad3ba7
We don't support vpath in a URL
jcansdale Jul 12, 2018
81b6d8a
Lose the App form GitHub.App.Services
jcansdale Jul 12, 2018
f651b19
Clean up and xmldoc the AnnotateFile method
jcansdale Jul 12, 2018
f12ab09
Remove FindContextFromBrowser from hidden command
jcansdale Jul 12, 2018
057a77f
Add xmldocs for GitHubContext
jcansdale Jul 12, 2018
513b562
Add xmldocs for GitHubContextService
jcansdale Jul 12, 2018
b26b2a9
Merge branch 'master' into feature/open-from-clipboard-url
jcansdale Jul 13, 2018
83610bf
Only show command when in context of git repo
jcansdale Jul 13, 2018
bec5eca
Add more xmldocs
jcansdale Jul 13, 2018
ce68103
Lazy initialize the UIContext
jcansdale Jul 13, 2018
e88a3e4
Merge branch 'master' into feature/open-from-clipboard-url
grokys Jul 13, 2018
22ec886
Use <inheritdoc/> on members not classes!
jcansdale Jul 13, 2018
edbec49
Make <summary> xmldoc elements a single sentence
jcansdale Jul 13, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Explicitly match CommitSha in blob URLs
  • Loading branch information
jcansdale committed Jul 3, 2018
commit cb7816cd15c4b7b7b215098c0e64bbe73f3908ed
3 changes: 2 additions & 1 deletion src/GitHub.App/Services/GitHubContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class GitHubContext
public string Owner { get; set; }
public string RepositoryName { get; set; }
public string Host { get; set; }
public string Branch { get; set; }
public string BranchName { get; set; }
public string CommitSha { get; set; }
public int? PullRequest { get; set; }
public int? Issue { get; set; }
public string Path { get; set; }
Expand Down
60 changes: 35 additions & 25 deletions src/GitHub.App/Services/GitHubContextService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class GitHubContextService
static readonly Regex windowTitleBranchesRegex = new Regex($"Branches · {owner}/{repo}( · GitHub)? - ", RegexOptions.Compiled);

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

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

var (line, lineEnd) = FindLine(uri);
return new GitHubContext
var context = new GitHubContext
{
Host = uri.Host,
Owner = uri.Owner,
RepositoryName = uri.RepositoryName,
Path = FindPath(uri),
PullRequest = FindPullRequest(uri),
Line = line,
LineEnd = lineEnd
};

var repositoryPrefix = uri.ToRepositoryUrl().ToString() + "/";
if (!url.StartsWith(repositoryPrefix, StringComparison.OrdinalIgnoreCase))
{
return context;
}

var subpath = url.Substring(repositoryPrefix.Length);

(context.Line, context.LineEnd) = FindLine(subpath);

context.PullRequest = FindPullRequest(url);

var match = urlBlobCommitRegex.Match(subpath);
if (match.Success)
{
context.CommitSha = match.Groups["commit"].Value;
context.Path = match.Groups["path"].Value;
return context;
}

match = urlBlobBranchRegex.Match(subpath);
if (match.Success)
{
context.BranchName = match.Groups["branch"].Value;
context.Path = match.Groups["path"].Value;
return context;
}

return context;
}

public GitHubContext FindContextFromBrowser()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we'd decided not to put this feature in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature is on a back-burner, but the code and the hope is to eventually reactivate it.

The command that uses it GitHub.OpenFromUrl has been removed from the UI. It was previously surfaced via File > Open > Open from GitHub.... The command that is surfaced via GitHub > Open from clipboard is GitHub.OpenFromClipboard`.

It does make sense to remove it from GitHub.OpenFromUrl (which needs to be explicitly bound to a keyboard shortcut).

Expand Down Expand Up @@ -119,7 +146,7 @@ public GitHubContext FindContextFromWindowTitle(string windowTitle)
{
Owner = match.Groups["owner"].Value,
RepositoryName = match.Groups["repo"].Value,
Branch = match.Groups["branch"].Value,
BranchName = match.Groups["branch"].Value,
Path = match.Groups["path"].Value
};
}
Expand All @@ -141,7 +168,7 @@ public GitHubContext FindContextFromWindowTitle(string windowTitle)
{
Owner = match.Groups["owner"].Value,
RepositoryName = match.Groups["repo"].Value,
Branch = match.Groups["branch"].Value,
BranchName = match.Groups["branch"].Value,
};
}

Expand Down Expand Up @@ -256,23 +283,6 @@ IVsTextView OpenDocument(string fullPath)
return (null, null);
}

string FindPath(UriString uri)
{
var blob = FindSubPath(uri, "/blob/");
if (blob == null)
{
return null;
}

var pathIndex = blob.IndexOf('/');
if (pathIndex == -1)
{
return null;
}

return blob.Substring(pathIndex + 1);
}

static int? FindPullRequest(UriString gitHubUrl)
{
var pullRequest = FindSubPath(gitHubUrl, "/pull/")?.Split('/').First();
Expand Down
22 changes: 19 additions & 3 deletions test/GitHub.App.UnitTests/Services/GitHubContextServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ public void PullRequest(string url, int? expectPullRequest)
Assert.That(context?.PullRequest, Is.EqualTo(expectPullRequest));
}

[TestCase("https://github.com/github/VisualStudio/blob/master", null, null, null)]
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "master", null, "foo.cs")]
[TestCase("https://github.com/github/VisualStudio/blob/master/path/foo.cs", "master", null, "path/foo.cs")]
[TestCase("https://github.com/github/VisualStudio/blob/ee863ce265fc6217f589e66766125fed1b5b8256/path/foo.cs", null, "ee863ce265fc6217f589e66766125fed1b5b8256", "path/foo.cs")]
[TestCase("https://github.com/github/VisualStudio/blob/not_master/foo.cs", null, null, null, Description = "We currently only match SHA and master")]
public void Blob(string url, string expectBranch, string expectCommitSha, string expectPath)
{
var target = CreateGitHubContextService();

var context = target.FindContextFromUrl(url);

Assert.That(context.BranchName, Is.EqualTo(expectBranch));
Assert.That(context.CommitSha, Is.EqualTo(expectCommitSha));
Assert.That(context.Path, Is.EqualTo(expectPath));
}

[TestCase("https://github.com", null)]
[TestCase("https://github.com/github", null)]
[TestCase("https://github.com/github/VisualStudio", null)]
Expand Down Expand Up @@ -222,7 +238,7 @@ public void Branch(string branch, string expectBranch)

var context = target.FindContextFromWindowTitle(windowTitle);

Assert.That(context?.Branch, Is.EqualTo(expectBranch));
Assert.That(context?.BranchName, Is.EqualTo(expectBranch));
}

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

Assert.That(context.Owner, Is.EqualTo(expectOwner));
Assert.That(context.RepositoryName, Is.EqualTo(expectRepositoryName));
Assert.That(context.Branch, Is.EqualTo(expectBranch));
Assert.That(context.BranchName, Is.EqualTo(expectBranch));
}

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

Assert.That(context.Owner, Is.EqualTo(expectOwner));
Assert.That(context.RepositoryName, Is.EqualTo(expectRepositoryName));
Assert.That(context.Branch, Is.EqualTo(expectBranch));
Assert.That(context.BranchName, Is.EqualTo(expectBranch));
}

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