@@ -29,7 +29,6 @@ public static IApiClient Create(UriString repositoryUrl, IKeychain keychain)
2929 private readonly ILoginManager loginManager ;
3030 private static readonly SemaphoreSlim sem = new SemaphoreSlim ( 1 ) ;
3131
32- Octokit . Repository repositoryCache = new Octokit . Repository ( ) ;
3332 IList < Organization > organizationsCache ;
3433 Octokit . User userCache ;
3534
@@ -49,13 +48,6 @@ public ApiClient(UriString hostUrl, IKeychain keychain, IGitHubClient githubClie
4948 loginManager = new LoginManager ( keychain , ApplicationInfo . ClientId , ApplicationInfo . ClientSecret ) ;
5049 }
5150
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-
5951 public async Task Logout ( UriString host )
6052 {
6153 await LogoutInternal ( host ) ;
@@ -182,45 +174,18 @@ public async Task<bool> ContinueLoginAsync(LoginResult loginResult, Func<LoginRe
182174 return result . Code == LoginResultCodes . Success ;
183175 }
184176
185- private async Task < Octokit . Repository > GetRepositoryInternal ( )
177+ private async Task CreateRepositoryInternal ( NewRepository newRepository , Action < Octokit . Repository , Exception > callback , string organization )
186178 {
187179 try
188180 {
189- if ( owner == null )
181+ logger . Trace ( "Creating repository" ) ;
182+
183+ if ( ! await EnsureKeychainLoaded ( ) )
190184 {
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 ;
203187 }
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- }
216188
217- return repositoryCache ;
218- }
219-
220- private async Task CreateRepositoryInternal ( NewRepository newRepository , Action < Octokit . Repository , Exception > callback , string organization )
221- {
222- try
223- {
224189 Octokit . Repository repository ;
225190 if ( ! string . IsNullOrEmpty ( organization ) )
226191 {
@@ -252,6 +217,11 @@ private async Task<IList<Organization>> GetOrganizationInternal()
252217 {
253218 logger . Trace ( "Getting Organizations" ) ;
254219
220+ if ( ! await EnsureKeychainLoaded ( ) )
221+ {
222+ return null ;
223+ }
224+
255225 var organizations = await githubClient . Organization . GetAllForCurrent ( ) ;
256226
257227 logger . Trace ( "Obtained {0} Organizations" , organizations ? . Count . ToString ( ) ?? "NULL" ) ;
@@ -276,6 +246,11 @@ private async Task<IList<Organization>> GetOrganizationInternal()
276246 {
277247 logger . Trace ( "Getting Organizations" ) ;
278248
249+ if ( ! await EnsureKeychainLoaded ( ) )
250+ {
251+ return null ;
252+ }
253+
279254 userCache = await githubClient . User . Current ( ) ;
280255 }
281256 catch ( Exception ex )
@@ -287,6 +262,31 @@ private async Task<IList<Organization>> GetOrganizationInternal()
287262 return userCache ;
288263 }
289264
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+
290290 public async Task < bool > ValidateCredentials ( )
291291 {
292292 try
0 commit comments