33using GitHub . Extensions ;
44using GitHub . Models ;
55using GitHub . Services ;
6+ using System . Threading . Tasks ;
7+ using GitHub . Api ;
8+ using NullGuard ;
9+ using System . Diagnostics ;
10+ using GitHub . Primitives ;
611
712namespace 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+ protected ISimpleApiClient simpleApiClient ;
24+
25+ [ AllowNull ]
26+ protected ISimpleApiClient SimpleApiClient
27+ {
28+ [ return : AllowNull ]
29+ get { return simpleApiClient ; }
30+ set
31+ {
32+ if ( simpleApiClient != value && value == null )
33+ apiFactory . ClearFromCache ( simpleApiClient ) ;
34+ simpleApiClient = value ;
35+ }
36+ }
37+
38+ protected ISimpleApiClientFactory ApiFactory => apiFactory ;
39+
1640 protected MenuBase ( )
1741 {
1842 }
1943
44+ protected MenuBase ( IServiceProvider serviceProvider , ISimpleApiClientFactory apiFactory )
45+ {
46+ this . serviceProvider = serviceProvider ;
47+ this . apiFactory = apiFactory ;
48+ }
49+
2050 protected MenuBase ( IServiceProvider serviceProvider )
2151 {
2252 this . serviceProvider = serviceProvider ;
@@ -41,10 +71,24 @@ void RefreshRepo()
4171 }
4272 }
4373
44- protected bool IsGitHubRepo ( )
74+ protected async Task < bool > IsGitHubRepo ( )
4575 {
4676 RefreshRepo ( ) ;
47- return ActiveRepo ? . CloneUrl ? . RepositoryName != null ;
77+
78+ var uri = ActiveRepo . CloneUrl ;
79+ if ( uri == null )
80+ return false ;
81+
82+ Debug . Assert ( apiFactory != null , "apiFactory cannot be null. Did you call the right constructor?" ) ;
83+ SimpleApiClient = apiFactory . Create ( uri ) ;
84+
85+ var isdotcom = HostAddress . IsGitHubDotComUri ( uri . ToRepositoryUrl ( ) ) ;
86+ if ( ! isdotcom )
87+ {
88+ var repo = await SimpleApiClient . GetRepository ( ) ;
89+ return ( repo . FullName == ActiveRepo . Name || repo . Id == 0 ) && SimpleApiClient . IsEnterprise ( ) ;
90+ }
91+ return isdotcom ;
4892 }
4993 }
5094}
0 commit comments