Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 507750d

Browse files
Merge branch 'master' into force-setting-encoding-option
2 parents d26c9d8 + 21cdc4e commit 507750d

File tree

3 files changed

+131
-113
lines changed

3 files changed

+131
-113
lines changed

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ private async Task LogoutInternal(UriString host)
6161
public async Task CreateRepository(NewRepository newRepository, Action<Octokit.Repository, Exception> callback, string organization = null)
6262
{
6363
Guard.ArgumentNotNull(callback, "callback");
64-
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+
}
6573
}
6674

6775
public async Task GetOrganizations(Action<IList<Organization>> callback)
@@ -71,6 +79,13 @@ public async Task GetOrganizations(Action<IList<Organization>> callback)
7179
callback(organizations);
7280
}
7381

82+
public async Task LoadKeychain(Action<bool> callback)
83+
{
84+
Guard.ArgumentNotNull(callback, "callback");
85+
var hasLoadedKeys = await LoadKeychainInternal();
86+
callback(hasLoadedKeys);
87+
}
88+
7489
public async Task GetCurrentUser(Action<Octokit.User> callback)
7590
{
7691
Guard.ArgumentNotNull(callback, "callback");
@@ -174,16 +189,15 @@ public async Task<bool> ContinueLoginAsync(LoginResult loginResult, Func<LoginRe
174189
return result.Code == LoginResultCodes.Success;
175190
}
176191

177-
private async Task CreateRepositoryInternal(NewRepository newRepository, Action<Octokit.Repository, Exception> callback, string organization)
192+
private async Task<Octokit.Repository> CreateRepositoryInternal(NewRepository newRepository, string organization)
178193
{
179194
try
180195
{
181196
logger.Trace("Creating repository");
182197

183-
if (!await EnsureKeychainLoaded())
198+
if (!await LoadKeychainInternal())
184199
{
185-
callback(null, new Exception("Keychain Not Loaded"));
186-
return;
200+
throw new InvalidOperationException("The keychain did not load");
187201
}
188202

189203
Octokit.Repository repository;
@@ -201,13 +215,12 @@ private async Task CreateRepositoryInternal(NewRepository newRepository, Action<
201215
}
202216

203217
logger.Trace("Created Repository");
204-
205-
callback(repository, null);
218+
return repository;
206219
}
207220
catch (Exception ex)
208221
{
209222
logger.Error(ex, "Error Creating Repository");
210-
callback(null, ex);
223+
throw;
211224
}
212225
}
213226

@@ -217,9 +230,9 @@ private async Task<IList<Organization>> GetOrganizationInternal()
217230
{
218231
logger.Trace("Getting Organizations");
219232

220-
if (!await EnsureKeychainLoaded())
233+
if (!await LoadKeychainInternal())
221234
{
222-
return null;
235+
return new List<Organization>();
223236
}
224237

225238
var organizations = await githubClient.Organization.GetAllForCurrent();
@@ -246,7 +259,7 @@ private async Task<IList<Organization>> GetOrganizationInternal()
246259
{
247260
logger.Trace("Getting Organizations");
248261

249-
if (!await EnsureKeychainLoaded())
262+
if (!await LoadKeychainInternal())
250263
{
251264
return null;
252265
}
@@ -262,27 +275,28 @@ private async Task<IList<Organization>> GetOrganizationInternal()
262275
return userCache;
263276
}
264277

265-
private async Task<bool> EnsureKeychainLoaded()
278+
private async Task<bool> LoadKeychainInternal()
266279
{
267-
logger.Trace("EnsureKeychainLoaded");
280+
logger.Trace("LoadKeychainInternal");
268281

269282
if (keychain.HasKeys)
270283
{
271284
if (!keychain.NeedsLoad)
272285
{
273-
logger.Trace("EnsureKeychainLoaded: Has keys does not need load");
286+
logger.Trace("LoadKeychainInternal: Has keys does not need load");
274287
return true;
275288
}
276289

277-
logger.Trace("EnsureKeychainLoaded: Loading");
290+
logger.Trace("LoadKeychainInternal: Loading");
278291

292+
//TODO: ONE_USER_LOGIN This assumes only ever one user can login
279293
var uriString = keychain.Connections.First().Host;
280294
var keychainAdapter = await keychain.Load(uriString);
281295

282296
return keychainAdapter.OctokitCredentials != Credentials.Anonymous;
283297
}
284298

285-
logger.Trace("EnsureKeychainLoaded: No keys to load");
299+
logger.Trace("LoadKeychainInternal: No keys to load");
286300

287301
return false;
288302
}

src/GitHub.Api/Application/IApiClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ interface IApiClient
1717
Task<bool> ValidateCredentials();
1818
Task Logout(UriString host);
1919
Task GetCurrentUser(Action<Octokit.User> callback);
20+
Task LoadKeychain(Action<bool> callback);
2021
}
2122
}
Lines changed: 100 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Linq;
3+
using System.Threading.Tasks;
34
using Octokit;
5+
using Rackspace.Threading;
46
using UnityEditor;
57
using UnityEngine;
68

@@ -13,6 +15,10 @@ class PublishView : Subview
1315
private const string PublicRepoMessage = "Anyone can see this repository. You choose who can commit";
1416
private const string PublishViewCreateButton = "Publish";
1517
private const string OwnersDefaultText = "Select a user or org";
18+
private const string SelectedOwnerLabel = "Owner";
19+
private const string RepositoryNameLabel = "Repository Name";
20+
private const string DescriptionLabel = "Description";
21+
private const string CreatePrivateRepositoryLabel = "Create as a private repository";
1622

1723
[SerializeField] private string username;
1824
[SerializeField] private string[] owners = { OwnersDefaultText };
@@ -60,37 +66,34 @@ private void PopulateView()
6066
try
6167
{
6268
var keychainConnections = Platform.Keychain.Connections;
69+
//TODO: ONE_USER_LOGIN This assumes only ever one user can login
6370
if (keychainConnections.Any())
6471
{
6572
Logger.Trace("GetCurrentUser");
6673

67-
Client.GetCurrentUser(user => {
68-
if (user == null)
74+
isBusy = true;
75+
76+
Client.LoadKeychain(hasKeys => {
77+
if (!hasKeys)
6978
{
7079
Logger.Warning("Unable to get current user");
80+
isBusy = false;
7181
return;
7282
}
7383

74-
owners = owners.Union(new[] { user.Login }).ToArray();
75-
username = user.Login;
76-
77-
Logger.Trace("GetOrganizations");
78-
79-
Client.GetOrganizations(organizations =>
80-
{
81-
if (organizations == null)
82-
{
83-
Logger.Warning("Unable to get list of organizations");
84-
return;
85-
}
84+
//TODO: ONE_USER_LOGIN This assumes only ever one user can login
85+
username = keychainConnections.First().Username;
8686

87+
Client.GetOrganizations(organizations => {
8788
Logger.Trace("Loaded {0} organizations", organizations.Count);
8889

8990
var organizationLogins = organizations
9091
.OrderBy(organization => organization.Login)
9192
.Select(organization => organization.Login);
9293

93-
owners = owners.Union(organizationLogins).ToArray();
94+
owners = new[] { username }.Union(organizationLogins).ToArray();
95+
96+
isBusy = false;
9497
});
9598
});
9699
}
@@ -128,111 +131,111 @@ public override void OnGUI()
128131

129132
GUILayout.Space(Styles.PublishViewSpacingHeight);
130133

131-
GUILayout.BeginHorizontal();
134+
EditorGUI.BeginDisabledGroup(isBusy);
132135
{
133-
GUILayout.BeginVertical();
136+
GUILayout.BeginHorizontal();
134137
{
135-
GUILayout.Label("Owner");
138+
GUILayout.BeginVertical();
139+
{
140+
GUILayout.Label(SelectedOwnerLabel);
136141

137-
GUI.enabled = !isBusy;
138-
selectedOwner = EditorGUILayout.Popup(selectedOwner, owners);
139-
GUI.enabled = true;
140-
}
141-
GUILayout.EndVertical();
142+
selectedOwner = EditorGUILayout.Popup(selectedOwner, owners);
143+
}
144+
GUILayout.EndVertical();
142145

143-
GUILayout.BeginVertical(GUILayout.Width(8));
144-
{
145-
GUILayout.Space(20);
146-
GUILayout.Label("/");
147-
}
148-
GUILayout.EndVertical();
146+
GUILayout.BeginVertical(GUILayout.Width(8));
147+
{
148+
GUILayout.Space(20);
149+
GUILayout.Label("/");
150+
}
151+
GUILayout.EndVertical();
149152

150-
GUILayout.BeginVertical();
151-
{
152-
GUILayout.Label("Repository Name");
153-
GUI.enabled = !isBusy;
154-
repoName = EditorGUILayout.TextField(repoName);
155-
GUI.enabled = true;
153+
GUILayout.BeginVertical();
154+
{
155+
GUILayout.Label(RepositoryNameLabel);
156+
repoName = EditorGUILayout.TextField(repoName);
157+
}
158+
GUILayout.EndVertical();
156159
}
157-
GUILayout.EndVertical();
158-
}
159-
GUILayout.EndHorizontal();
160+
GUILayout.EndHorizontal();
160161

161-
GUILayout.Label("Description");
162-
GUI.enabled = !isBusy;
163-
repoDescription = EditorGUILayout.TextField(repoDescription);
164-
GUI.enabled = true;
165-
GUILayout.Space(Styles.PublishViewSpacingHeight);
162+
GUILayout.Label(DescriptionLabel);
163+
repoDescription = EditorGUILayout.TextField(repoDescription);
164+
GUILayout.Space(Styles.PublishViewSpacingHeight);
166165

167-
GUILayout.BeginVertical();
168-
{
169-
GUILayout.BeginHorizontal();
166+
GUILayout.BeginVertical();
170167
{
171-
GUI.enabled = !isBusy;
172-
togglePrivate = GUILayout.Toggle(togglePrivate, "Create as a private repository");
173-
GUI.enabled = true;
174-
}
175-
GUILayout.EndHorizontal();
168+
GUILayout.BeginHorizontal();
169+
{
170+
togglePrivate = GUILayout.Toggle(togglePrivate, CreatePrivateRepositoryLabel);
171+
}
172+
GUILayout.EndHorizontal();
176173

177-
GUILayout.BeginHorizontal();
178-
{
179-
GUILayout.Space(Styles.PublishViewSpacingHeight);
180-
var repoPrivacyExplanation = togglePrivate ? PrivateRepoMessage : PublicRepoMessage;
181-
GUILayout.Label(repoPrivacyExplanation, Styles.LongMessageStyle);
174+
GUILayout.BeginHorizontal();
175+
{
176+
GUILayout.Space(Styles.PublishViewSpacingHeight);
177+
var repoPrivacyExplanation = togglePrivate ? PrivateRepoMessage : PublicRepoMessage;
178+
GUILayout.Label(repoPrivacyExplanation, Styles.LongMessageStyle);
179+
}
180+
GUILayout.EndHorizontal();
182181
}
183-
GUILayout.EndHorizontal();
184-
}
185-
GUILayout.EndVertical();
186-
182+
GUILayout.EndVertical();;
187183

188-
GUILayout.Space(Styles.PublishViewSpacingHeight);
184+
GUILayout.Space(Styles.PublishViewSpacingHeight);
189185

190-
if (error != null)
191-
GUILayout.Label(error, Styles.ErrorLabel);
186+
if (error != null)
187+
GUILayout.Label(error, Styles.ErrorLabel);
192188

193-
GUILayout.FlexibleSpace();
194-
195-
GUILayout.BeginHorizontal();
196-
{
197189
GUILayout.FlexibleSpace();
198-
GUI.enabled = !string.IsNullOrEmpty(repoName) && !isBusy && selectedOwner != 0;
199-
if (GUILayout.Button(PublishViewCreateButton))
200-
{
201-
isBusy = true;
202-
203-
var organization = owners[selectedOwner] == username ? null : owners[selectedOwner];
204190

205-
Client.CreateRepository(new NewRepository(repoName)
206-
{
207-
Private = togglePrivate,
208-
}, (repository, ex) =>
191+
GUILayout.BeginHorizontal();
192+
{
193+
GUILayout.FlexibleSpace();
194+
EditorGUI.BeginDisabledGroup(!IsFormValid);
195+
if (GUILayout.Button(PublishViewCreateButton))
209196
{
210-
Logger.Trace("Create Repository Callback");
197+
isBusy = true;
211198

212-
if (ex != null)
213-
{
214-
error = ex.Message;
215-
isBusy = false;
216-
return;
217-
}
199+
var organization = owners[selectedOwner] == username ? null : owners[selectedOwner];
218200

219-
if (repository == null)
201+
Client.CreateRepository(new NewRepository(repoName)
220202
{
221-
Logger.Warning("Returned Repository is null");
222-
isBusy = false;
223-
return;
224-
}
203+
Private = togglePrivate,
204+
}, (repository, ex) =>
205+
{
206+
Logger.Trace("Create Repository Callback");
225207

226-
GitClient.RemoteAdd("origin", repository.CloneUrl)
227-
.Then(GitClient.Push("origin", Repository.CurrentBranch.Value.Name))
228-
.ThenInUI(Parent.Finish)
229-
.Start();
230-
}, organization);
208+
if (ex != null)
209+
{
210+
error = ex.Message;
211+
isBusy = false;
212+
return;
213+
}
214+
215+
if (repository == null)
216+
{
217+
Logger.Warning("Returned Repository is null");
218+
isBusy = false;
219+
return;
220+
}
221+
222+
GitClient.RemoteAdd("origin", repository.CloneUrl)
223+
.Then(GitClient.Push("origin", Repository.CurrentBranch.Value.Name))
224+
.ThenInUI(Finish)
225+
.Start();
226+
}, organization);
227+
}
228+
EditorGUI.EndDisabledGroup();
231229
}
232-
GUI.enabled = true;
230+
GUILayout.EndHorizontal();
231+
GUILayout.Space(10);
233232
}
234-
GUILayout.EndHorizontal();
235-
GUILayout.Space(10);
233+
EditorGUI.EndDisabledGroup();
234+
}
235+
236+
private bool IsFormValid
237+
{
238+
get { return !string.IsNullOrEmpty(repoName) && !isBusy && selectedOwner != 0; }
236239
}
237240
}
238241
}

0 commit comments

Comments
 (0)