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

Commit dc74482

Browse files
author
Jasmine
committed
used existing functionality to check if repo is a github repo
1 parent 8c67752 commit dc74482

File tree

7 files changed

+74
-24
lines changed

7 files changed

+74
-24
lines changed

src/GitHub.Exports/Services/IMenuHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23

34
namespace GitHub.VisualStudio
45
{

src/GitHub.VisualStudio/Base/MenuBase.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,47 @@
33
using GitHub.Extensions;
44
using GitHub.Models;
55
using GitHub.Services;
6+
using System.Threading.Tasks;
7+
using GitHub.Api;
8+
using GitHub.Primitives;
9+
using NullGuard;
10+
using System.Diagnostics;
611

712
namespace GitHub.VisualStudio
813
{
914
public abstract class MenuBase
1015
{
1116
readonly IServiceProvider serviceProvider;
17+
readonly ISimpleApiClientFactory apiFactory;
18+
1219
protected IServiceProvider ServiceProvider { get { return serviceProvider; } }
1320

1421
protected ISimpleRepositoryModel ActiveRepo { get; private set; }
1522

23+
ISimpleApiClient simpleApiClient;
24+
[AllowNull]
25+
public ISimpleApiClient SimpleApiClient
26+
{
27+
[return: AllowNull]
28+
get { return simpleApiClient; }
29+
set
30+
{
31+
if (simpleApiClient != value && value == null)
32+
apiFactory.ClearFromCache(simpleApiClient);
33+
simpleApiClient = value;
34+
}
35+
}
36+
37+
protected ISimpleApiClientFactory ApiFactory => apiFactory;
38+
1639
protected MenuBase()
1740
{
1841
}
1942

20-
protected MenuBase(IServiceProvider serviceProvider)
43+
protected MenuBase(IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
2144
{
2245
this.serviceProvider = serviceProvider;
46+
this.apiFactory = apiFactory;
2347
}
2448

2549
void RefreshRepo()
@@ -41,10 +65,24 @@ void RefreshRepo()
4165
}
4266
}
4367

44-
protected bool IsGitHubRepo()
68+
protected async Task<bool> IsGitHubRepo()
4569
{
4670
RefreshRepo();
47-
return ActiveRepo?.CloneUrl?.RepositoryName != null && ActiveRepo?.CloneUrl?.IsGithubUri == true;
71+
72+
var uri = ActiveRepo.CloneUrl;
73+
if (uri == null)
74+
return false;
75+
76+
Debug.Assert(apiFactory != null, "apiFactory cannot be null. Did you call the right constructor?");
77+
SimpleApiClient = apiFactory.Create(uri);
78+
79+
var isdotcom = HostAddress.IsGitHubDotComUri(uri.ToRepositoryUrl());
80+
if (!isdotcom)
81+
{
82+
var repo = await SimpleApiClient.GetRepository();
83+
return (repo.FullName == ActiveRepo.Name || repo.Id == 0) && SimpleApiClient.IsEnterprise();
84+
}
85+
return isdotcom;
4886
}
4987
}
5088
}

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Reflection;
66
using System.Runtime.InteropServices;
7+
using SysTask = System.Threading.Tasks;
78
using GitHub.Extensions;
89
using GitHub.Models;
910
using GitHub.Services;

src/GitHub.VisualStudio/Menus/AddConnection.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using GitHub.Services;
55
using GitHub.UI;
66
using GitHub.Extensions;
7+
using NullGuard;
8+
using GitHub.Api;
79

810
namespace GitHub.VisualStudio.Menus
911
{
@@ -12,8 +14,8 @@ namespace GitHub.VisualStudio.Menus
1214
public class AddConnection: MenuBase, IMenuHandler
1315
{
1416
[ImportingConstructor]
15-
public AddConnection([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
16-
: base(serviceProvider)
17+
public AddConnection([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
18+
: base(serviceProvider, apiFactory)
1719
{
1820
}
1921

@@ -24,11 +26,5 @@ public void Activate()
2426
{
2527
StartFlow(UIControllerFlow.Authentication);
2628
}
27-
28-
void StartFlow(UIControllerFlow controllerFlow)
29-
{
30-
var uiProvider = ServiceProvider.GetExportedValue<IUIProvider>();
31-
uiProvider.RunUI(controllerFlow, null);
32-
}
3329
}
3430
}

src/GitHub.VisualStudio/Menus/CopyLink.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using GitHub.Extensions;
66
using GitHub.Services;
77
using GitHub.VisualStudio.UI;
8+
using System.Threading.Tasks;
9+
using GitHub.Api;
810

911
namespace GitHub.VisualStudio.Menus
1012
{
@@ -13,17 +15,18 @@ namespace GitHub.VisualStudio.Menus
1315
public class CopyLink : LinkMenuBase, IDynamicMenuHandler
1416
{
1517
[ImportingConstructor]
16-
public CopyLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
17-
: base(serviceProvider)
18+
public CopyLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
19+
: base(serviceProvider, apiFactory)
1820
{
1921
}
2022

2123
public Guid Guid => GuidList.guidContextMenuSet;
2224
public int CmdId => PkgCmdIDList.copyLinkCommand;
2325

24-
public void Activate()
26+
public async void Activate([AllowNull]object data = null)
2527
{
26-
if (!IsGitHubRepo())
28+
var isgithub = await IsGitHubRepo();
29+
if (!isgithub)
2730
return;
2831

2932
var link = GenerateLink();
@@ -44,7 +47,11 @@ public void Activate()
4447

4548
public bool CanShow()
4649
{
47-
return IsGitHubRepo();
50+
return System.Threading.Tasks.Task.Run(() =>
51+
{
52+
return IsGitHubRepo();
53+
}).Result;
4854
}
55+
4956
}
5057
}

src/GitHub.VisualStudio/Menus/LinkMenuBase.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
using GitHub.Extensions;
1+
using GitHub.Api;
2+
using GitHub.Extensions;
23
using GitHub.Primitives;
34
using System;
45

56
namespace GitHub.VisualStudio.Menus
67
{
78
public class LinkMenuBase: MenuBase
89
{
9-
public LinkMenuBase(IServiceProvider serviceProvider)
10-
: base(serviceProvider)
10+
public LinkMenuBase(IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
11+
: base(serviceProvider, apiFactory)
1112
{
1213
}
1314

src/GitHub.VisualStudio/Menus/OpenLink.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Microsoft.VisualStudio.Shell;
44
using System;
55
using System.ComponentModel.Composition;
6+
using System.Threading.Tasks;
7+
using GitHub.Api;
68

79
namespace GitHub.VisualStudio.Menus
810
{
@@ -11,17 +13,18 @@ namespace GitHub.VisualStudio.Menus
1113
public class OpenLink: LinkMenuBase, IDynamicMenuHandler
1214
{
1315
[ImportingConstructor]
14-
public OpenLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
15-
: base(serviceProvider)
16+
public OpenLink([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, ISimpleApiClientFactory apiFactory)
17+
: base(serviceProvider, apiFactory)
1618
{
1719
}
1820

1921
public Guid Guid => GuidList.guidContextMenuSet;
2022
public int CmdId => PkgCmdIDList.openLinkCommand;
2123

22-
public void Activate()
24+
public async void Activate([AllowNull]object data = null)
2325
{
24-
if (!IsGitHubRepo())
26+
var isgithub = await IsGitHubRepo();
27+
if (!isgithub)
2528
return;
2629

2730
var link = GenerateLink();
@@ -33,7 +36,10 @@ public void Activate()
3336

3437
public bool CanShow()
3538
{
36-
return IsGitHubRepo();
39+
return System.Threading.Tasks.Task.Run(() =>
40+
{
41+
return IsGitHubRepo();
42+
}).Result;
3743
}
3844
}
3945
}

0 commit comments

Comments
 (0)