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

Commit b47abfd

Browse files
committed
Add TryNavigateToContext to IGitHubContextService
Move context changing responsibilities away from RepositoryCloneService.
1 parent 2105d67 commit b47abfd

File tree

8 files changed

+39
-26
lines changed

8 files changed

+39
-26
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class GitHubContextService : IGitHubContextService
2525
{
2626
readonly IGitHubServiceProvider serviceProvider;
2727
readonly IGitService gitService;
28+
readonly IVSServices vsServices;
2829
readonly Lazy<IVsTextManager2> textManager;
2930

3031
// USERID_REGEX = /[a-z0-9][a-z0-9\-\_]*/i
@@ -61,13 +62,32 @@ public class GitHubContextService : IGitHubContextService
6162
static readonly Regex tempFileObjectishRegex = new Regex(@"\\TFSTemp\\[^\\]*[.](?<objectish>[a-z0-9]{8})[.][^.\\]*$", RegexOptions.Compiled);
6263

6364
[ImportingConstructor]
64-
public GitHubContextService(IGitHubServiceProvider serviceProvider, IGitService gitService)
65+
public GitHubContextService(IGitHubServiceProvider serviceProvider, IGitService gitService, IVSServices vsServices)
6566
{
6667
this.serviceProvider = serviceProvider;
6768
this.gitService = gitService;
69+
this.vsServices = vsServices;
6870
textManager = new Lazy<IVsTextManager2>(() => serviceProvider.GetService<SVsTextManager, IVsTextManager2>());
6971
}
7072

73+
/// <inheritdoc/>
74+
public void TryNavigateToContext(string repositoryDir, GitHubContext context)
75+
{
76+
if (context?.LinkType == LinkType.Blob)
77+
{
78+
// Navigate to file for /blob/ type URLs
79+
var (commitish, path, commitSha) = ResolveBlob(repositoryDir, context);
80+
81+
var hasChanges = HasChangesInWorkingDirectory(repositoryDir, commitish, path);
82+
if (hasChanges)
83+
{
84+
vsServices.ShowMessageBoxInfo(Resources.ChangesInWorkingDirectoryMessage + Environment.NewLine + commitish);
85+
}
86+
87+
TryOpenFile(repositoryDir, context);
88+
}
89+
}
90+
7191
/// <inheritdoc/>
7292
public GitHubContext FindContextFromClipboard()
7393
{

src/GitHub.App/Services/RepositoryCloneService.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Reactive.Linq;
66
using System.Threading.Tasks;
77
using GitHub.Api;
8-
using GitHub.Exports;
98
using GitHub.Extensions;
109
using GitHub.Helpers;
1110
using GitHub.Logging;
@@ -37,7 +36,6 @@ public class RepositoryCloneService : IRepositoryCloneService
3736
readonly ITeamExplorerServices teamExplorerServices;
3837
readonly IGraphQLClientFactory graphqlFactory;
3938
readonly IGitHubContextService gitHubContextService;
40-
readonly IVSServices vsServices;
4139
readonly IUsageTracker usageTracker;
4240
ICompiledQuery<ViewerRepositoriesModel> readViewerRepositories;
4341

@@ -48,15 +46,13 @@ public RepositoryCloneService(
4846
ITeamExplorerServices teamExplorerServices,
4947
IGraphQLClientFactory graphqlFactory,
5048
IGitHubContextService gitHubContextService,
51-
IVSServices vsServices,
5249
IUsageTracker usageTracker)
5350
{
5451
this.operatingSystem = operatingSystem;
5552
this.vsGitServices = vsGitServices;
5653
this.teamExplorerServices = teamExplorerServices;
5754
this.graphqlFactory = graphqlFactory;
5855
this.gitHubContextService = gitHubContextService;
59-
this.vsServices = vsServices;
6056
this.usageTracker = usageTracker;
6157

6258
defaultClonePath = GetLocalClonePathFromGitProvider(operatingSystem.Environment.GetUserRepositoriesPath());
@@ -160,19 +156,11 @@ public async Task CloneOrOpenRepository(
160156
// Give user a chance to choose a solution
161157
teamExplorerServices.ShowHomePage();
162158

159+
// Navigate to context for supported URL types (e.g. /blob/ URLs)
163160
var context = gitHubContextService.FindContextFromUrl(url);
164-
if (context?.LinkType == LinkType.Blob)
161+
if (context != null)
165162
{
166-
// Navigate to file for /blob/ type URLs
167-
var (commitish, path, isSha) = gitHubContextService.ResolveBlob(repositoryPath, context);
168-
169-
var hasChanges = gitHubContextService.HasChangesInWorkingDirectory(repositoryPath, commitish, path);
170-
if (hasChanges)
171-
{
172-
vsServices.ShowMessageBoxInfo(Resources.ChangesInWorkingDirectoryMessage);
173-
}
174-
175-
gitHubContextService.TryOpenFile(repositoryPath, context);
163+
gitHubContextService.TryNavigateToContext(repositoryPath, context);
176164
}
177165
}
178166

src/GitHub.Exports/Services/IGitHubContextService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ namespace GitHub.Services
88
/// </summary>
99
public interface IGitHubContextService
1010
{
11+
/// <summary>
12+
/// Attempt to navigate to the equivalent context inside Visual Studio.
13+
/// </summary>
14+
/// <param name="repositoryDir">The target repository</param>
15+
/// <param name="context">The context to open.</param>
16+
void TryNavigateToContext(string repositoryDir, GitHubContext context);
17+
1118
/// <summary>
1219
/// Find the context from a URL in the clipboard if any.
1320
/// </summary>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,9 @@ static GitHubContextService CreateGitHubContextService(string repositoryDir = nu
498498
{
499499
var sp = Substitute.For<IGitHubServiceProvider>();
500500
var gitService = Substitute.For<IGitService>();
501+
var vsServices = Substitute.For<IVSServices>();
501502
gitService.GetRepository(repositoryDir).Returns(repository);
502503

503-
return new GitHubContextService(sp, gitService);
504+
return new GitHubContextService(sp, gitService, vsServices);
504505
}
505506
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ public async Task UpdatesMetricsWhenRepositoryClonedAsync(string cloneUrl, int n
4141
var teamExplorerServices = Substitute.For<ITeamExplorerServices>();
4242
var graphqlFactory = Substitute.For<IGraphQLClientFactory>();
4343
var gitHubContextService = Substitute.For<IGitHubContextService>();
44-
var vsServices = Substitute.For<IVSServices>();
4544
var usageTracker = Substitute.For<IUsageTracker>();
4645
var cloneService = new RepositoryCloneService(operatingSystem, vsGitServices, teamExplorerServices,
47-
graphqlFactory, gitHubContextService, vsServices, usageTracker);
46+
graphqlFactory, gitHubContextService, usageTracker);
4847

4948
await cloneService.CloneRepository(cloneUrl, @"c:\dev\bar");
5049
var model = UsageModel.Create(Guid.NewGuid());
@@ -76,10 +75,9 @@ public async Task UpdatesMetricsWhenCloneOrOpenRepositoryAsync(string cloneUrl,
7675
var teamExplorerServices = Substitute.For<ITeamExplorerServices>();
7776
var graphqlFactory = Substitute.For<IGraphQLClientFactory>();
7877
var gitHubContextService = Substitute.For<IGitHubContextService>();
79-
var vsServices = Substitute.For<IVSServices>();
8078
var usageTracker = Substitute.For<IUsageTracker>();
8179
var cloneService = new RepositoryCloneService(operatingSystem, vsGitServices, teamExplorerServices,
82-
graphqlFactory, gitHubContextService, vsServices, usageTracker);
80+
graphqlFactory, gitHubContextService, usageTracker);
8381

8482
await cloneService.CloneOrOpenRepository(cloneDialogResult);
8583

@@ -99,10 +97,9 @@ public async Task UpdatesMetricsWhenDefaultClonePath(string targetPath, string d
9997
vsGitServices.GetLocalClonePathFromGitProvider().Returns(defaultPath);
10098
var graphqlFactory = Substitute.For<IGraphQLClientFactory>();
10199
var gitHubContextService = Substitute.For<IGitHubContextService>();
102-
var vsServices = Substitute.For<IVSServices>();
103100
var usageTracker = Substitute.For<IUsageTracker>();
104101
var cloneService = new RepositoryCloneService(operatingSystem, vsGitServices, teamExplorerServices,
105-
graphqlFactory, gitHubContextService, vsServices, usageTracker);
102+
graphqlFactory, gitHubContextService, usageTracker);
106103

107104
await cloneService.CloneRepository("https://github.com/foo/bar", targetPath);
108105
var model = UsageModel.Create(Guid.NewGuid());

test/GitHub.App.UnitTests/Substitutes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static IGitHubServiceProvider GetServiceProvider(
113113
var vsgit = IVSGitServices;
114114
var clone = cloneService ?? new RepositoryCloneService(os, vsgit, Substitute.For<ITeamExplorerServices>(),
115115
Substitute.For<IGraphQLClientFactory>(), Substitute.For<IGitHubContextService>(),
116-
Substitute.For<IVSServices>(), Substitute.For<IUsageTracker>());
116+
Substitute.For<IUsageTracker>());
117117
var create = creationService ?? new RepositoryCreationService(clone);
118118
avatarProvider = avatarProvider ?? Substitute.For<IAvatarProvider>();
119119
ret.GetService(typeof(IGitService)).Returns(gitservice);

test/GitHub.VisualStudio.UnitTests/Commands/OpenFromClipboardCommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static GitHubContext CreateGitHubContext(UriString uri = null, string owner = "g
169169
{
170170
uri = uri ?? new UriString($"https://github.com/{owner}/{repositoryName}/blob/{branch}/README.md");
171171

172-
return new GitHubContextService(null, null).FindContextFromUrl(uri);
172+
return new GitHubContextService(null, null, null).FindContextFromUrl(uri);
173173
}
174174

175175
static OpenFromClipboardCommand CreateOpenFromClipboardCommand(

test/GitHub.VisualStudio.UnitTests/Substitutes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static IGitHubServiceProvider GetServiceProvider(
112112
var os = OperatingSystem;
113113
var vsgit = IVSGitServices;
114114
var clone = cloneService ?? new RepositoryCloneService(os, vsgit, Substitute.For<ITeamExplorerServices>(),
115-
Substitute.For<IGraphQLClientFactory>(), Substitute.For<IGitHubContextService>(), Substitute.For<IVSServices>(),
115+
Substitute.For<IGraphQLClientFactory>(), Substitute.For<IGitHubContextService>(),
116116
Substitute.For<IUsageTracker>());
117117
var create = creationService ?? new RepositoryCreationService(clone);
118118
avatarProvider = avatarProvider ?? Substitute.For<IAvatarProvider>();

0 commit comments

Comments
 (0)