@@ -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 ) ;
@@ -69,7 +61,15 @@ private async Task LogoutInternal(UriString host)
6961 public async Task CreateRepository ( NewRepository newRepository , Action < Octokit . Repository , Exception > callback , string organization = null )
7062 {
7163 Guard . ArgumentNotNull ( callback , "callback" ) ;
72- await CreateRepositoryInternal ( newRepository , callback , organization ) ;
64+ try
65+ {
66+ var repository = await CreateRepositoryInternal ( newRepository , organization ) ;
67+ callback ( repository , null ) ;
68+ }
69+ catch ( Exception e )
70+ {
71+ callback ( null , e ) ;
72+ }
7373 }
7474
7575 public async Task GetOrganizations ( Action < IList < Organization > > callback )
@@ -79,6 +79,13 @@ public async Task GetOrganizations(Action<IList<Organization>> callback)
7979 callback ( organizations ) ;
8080 }
8181
82+ public async Task LoadKeychain ( Action < bool > callback )
83+ {
84+ Guard . ArgumentNotNull ( callback , "callback" ) ;
85+ var hasLoadedKeys = await LoadKeychainInternal ( ) ;
86+ callback ( hasLoadedKeys ) ;
87+ }
88+
8289 public async Task GetCurrentUser ( Action < Octokit . User > callback )
8390 {
8491 Guard . ArgumentNotNull ( callback , "callback" ) ;
@@ -182,65 +189,38 @@ public async Task<bool> ContinueLoginAsync(LoginResult loginResult, Func<LoginRe
182189 return result . Code == LoginResultCodes . Success ;
183190 }
184191
185- private async Task < Octokit . Repository > GetRepositoryInternal ( )
192+ private async Task < Octokit . Repository > CreateRepositoryInternal ( NewRepository newRepository , string organization )
186193 {
187194 try
188195 {
189- if ( owner == null )
196+ logger . Trace ( "Creating repository" ) ;
197+
198+ if ( ! await LoadKeychainInternal ( ) )
190199 {
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- }
200+ throw new InvalidOperationException ( "The keychain did not load" ) ;
203201 }
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-
217- return repositoryCache ;
218- }
219-
220- private async Task CreateRepositoryInternal ( NewRepository newRepository , Action < Octokit . Repository , Exception > callback , string organization )
221- {
222- try
223- {
224- logger . Trace ( "Creating Repository" ) ;
225202
226203 Octokit . Repository repository ;
227- if ( organization != null )
204+ if ( ! string . IsNullOrEmpty ( organization ) )
228205 {
206+ logger . Trace ( "Creating repository for organization" ) ;
207+
229208 repository = await githubClient . Repository . Create ( organization , newRepository ) ;
230209 }
231210 else
232211 {
212+ logger . Trace ( "Creating repository for user" ) ;
213+
233214 repository = await githubClient . Repository . Create ( newRepository ) ;
234215 }
235216
236217 logger . Trace ( "Created Repository" ) ;
237-
238- callback ( repository , null ) ;
218+ return repository ;
239219 }
240220 catch ( Exception ex )
241221 {
242222 logger . Error ( ex , "Error Creating Repository" ) ;
243- callback ( null , ex ) ;
223+ throw ;
244224 }
245225 }
246226
@@ -250,6 +230,11 @@ private async Task<IList<Organization>> GetOrganizationInternal()
250230 {
251231 logger . Trace ( "Getting Organizations" ) ;
252232
233+ if ( ! await LoadKeychainInternal ( ) )
234+ {
235+ return new List < Organization > ( ) ;
236+ }
237+
253238 var organizations = await githubClient . Organization . GetAllForCurrent ( ) ;
254239
255240 logger . Trace ( "Obtained {0} Organizations" , organizations ? . Count . ToString ( ) ?? "NULL" ) ;
@@ -274,6 +259,11 @@ private async Task<IList<Organization>> GetOrganizationInternal()
274259 {
275260 logger . Trace ( "Getting Organizations" ) ;
276261
262+ if ( ! await LoadKeychainInternal ( ) )
263+ {
264+ return null ;
265+ }
266+
277267 userCache = await githubClient . User . Current ( ) ;
278268 }
279269 catch ( Exception ex )
@@ -285,6 +275,32 @@ private async Task<IList<Organization>> GetOrganizationInternal()
285275 return userCache ;
286276 }
287277
278+ private async Task < bool > LoadKeychainInternal ( )
279+ {
280+ logger . Trace ( "LoadKeychainInternal" ) ;
281+
282+ if ( keychain . HasKeys )
283+ {
284+ if ( ! keychain . NeedsLoad )
285+ {
286+ logger . Trace ( "LoadKeychainInternal: Has keys does not need load" ) ;
287+ return true ;
288+ }
289+
290+ logger . Trace ( "LoadKeychainInternal: Loading" ) ;
291+
292+ //TODO: ONE_USER_LOGIN This assumes only ever one user can login
293+ var uriString = keychain . Connections . First ( ) . Host ;
294+ var keychainAdapter = await keychain . Load ( uriString ) ;
295+
296+ return keychainAdapter . OctokitCredentials != Credentials . Anonymous ;
297+ }
298+
299+ logger . Trace ( "LoadKeychainInternal: No keys to load" ) ;
300+
301+ return false ;
302+ }
303+
288304 public async Task < bool > ValidateCredentials ( )
289305 {
290306 try
0 commit comments