@@ -29,7 +29,6 @@ public static IApiClient Create(UriString repositoryUrl, IKeychain keychain)
29
29
private readonly ILoginManager loginManager ;
30
30
private static readonly SemaphoreSlim sem = new SemaphoreSlim ( 1 ) ;
31
31
32
- Octokit . Repository repositoryCache = new Octokit . Repository ( ) ;
33
32
IList < Organization > organizationsCache ;
34
33
Octokit . User userCache ;
35
34
@@ -49,13 +48,6 @@ public ApiClient(UriString hostUrl, IKeychain keychain, IGitHubClient githubClie
49
48
loginManager = new LoginManager ( keychain , ApplicationInfo . ClientId , ApplicationInfo . ClientSecret ) ;
50
49
}
51
50
52
- public async Task GetRepository ( Action < Octokit . Repository > callback )
53
- {
54
- Guard . ArgumentNotNull ( callback , "callback" ) ;
55
- var repo = await GetRepositoryInternal ( ) ;
56
- callback ( repo ) ;
57
- }
58
-
59
51
public async Task Logout ( UriString host )
60
52
{
61
53
await LogoutInternal ( host ) ;
@@ -182,45 +174,18 @@ public async Task<bool> ContinueLoginAsync(LoginResult loginResult, Func<LoginRe
182
174
return result . Code == LoginResultCodes . Success ;
183
175
}
184
176
185
- private async Task < Octokit . Repository > GetRepositoryInternal ( )
177
+ private async Task CreateRepositoryInternal ( NewRepository newRepository , Action < Octokit . Repository , Exception > callback , string organization )
186
178
{
187
179
try
188
180
{
189
- if ( owner == null )
181
+ logger . Trace ( "Creating repository" ) ;
182
+
183
+ if ( ! await EnsureKeychainLoaded ( ) )
190
184
{
191
- var ownerLogin = OriginalUrl . Owner ;
192
- var repositoryName = OriginalUrl . RepositoryName ;
193
-
194
- if ( ownerLogin != null && repositoryName != null )
195
- {
196
- var repo = await githubClient . Repository . Get ( ownerLogin , repositoryName ) ;
197
- if ( repo != null )
198
- {
199
- repositoryCache = repo ;
200
- }
201
- owner = ownerLogin ;
202
- }
185
+ callback ( null , new Exception ( "Keychain Not Loaded" ) ) ;
186
+ return ;
203
187
}
204
- }
205
- // it'll throw if it's private or an enterprise instance requiring authentication
206
- catch ( ApiException apiex )
207
- {
208
- if ( ! HostAddress . IsGitHubDotCom ( OriginalUrl . ToRepositoryUri ( ) ) )
209
- isEnterprise = apiex . IsGitHubApiException ( ) ;
210
- }
211
- catch { }
212
- finally
213
- {
214
- sem . Release ( ) ;
215
- }
216
188
217
- return repositoryCache ;
218
- }
219
-
220
- private async Task CreateRepositoryInternal ( NewRepository newRepository , Action < Octokit . Repository , Exception > callback , string organization )
221
- {
222
- try
223
- {
224
189
Octokit . Repository repository ;
225
190
if ( ! string . IsNullOrEmpty ( organization ) )
226
191
{
@@ -252,6 +217,11 @@ private async Task<IList<Organization>> GetOrganizationInternal()
252
217
{
253
218
logger . Trace ( "Getting Organizations" ) ;
254
219
220
+ if ( ! await EnsureKeychainLoaded ( ) )
221
+ {
222
+ return null ;
223
+ }
224
+
255
225
var organizations = await githubClient . Organization . GetAllForCurrent ( ) ;
256
226
257
227
logger . Trace ( "Obtained {0} Organizations" , organizations ? . Count . ToString ( ) ?? "NULL" ) ;
@@ -276,6 +246,11 @@ private async Task<IList<Organization>> GetOrganizationInternal()
276
246
{
277
247
logger . Trace ( "Getting Organizations" ) ;
278
248
249
+ if ( ! await EnsureKeychainLoaded ( ) )
250
+ {
251
+ return null ;
252
+ }
253
+
279
254
userCache = await githubClient . User . Current ( ) ;
280
255
}
281
256
catch ( Exception ex )
@@ -287,6 +262,31 @@ private async Task<IList<Organization>> GetOrganizationInternal()
287
262
return userCache ;
288
263
}
289
264
265
+ private async Task < bool > EnsureKeychainLoaded ( )
266
+ {
267
+ logger . Trace ( "EnsureKeychainLoaded" ) ;
268
+
269
+ if ( keychain . HasKeys )
270
+ {
271
+ if ( ! keychain . NeedsLoad )
272
+ {
273
+ logger . Trace ( "EnsureKeychainLoaded: Has keys does not need load" ) ;
274
+ return true ;
275
+ }
276
+
277
+ logger . Trace ( "EnsureKeychainLoaded: Loading" ) ;
278
+
279
+ var uriString = keychain . Connections . First ( ) . Host ;
280
+ var keychainAdapter = await keychain . Load ( uriString ) ;
281
+
282
+ return keychainAdapter . OctokitCredentials != Credentials . Anonymous ;
283
+ }
284
+
285
+ logger . Trace ( "EnsureKeychainLoaded: No keys to load" ) ;
286
+
287
+ return false ;
288
+ }
289
+
290
290
public async Task < bool > ValidateCredentials ( )
291
291
{
292
292
try
0 commit comments