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

Commit bb3debc

Browse files
Merge branch 'master' into fixes/settings-view-code-cleanup
2 parents e770b88 + 793f493 commit bb3debc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1693
-920
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Have you read GitHub for Unity's Code of Conduct? By filing an Issue, you are ex
99
- Be sure to run with tracing enabled to capture runtime details in the log file
1010
- Include the log file in the PR.
1111
- On Windows, the extension log file is at `%LOCALAPPDATA%\GitHubUnity\github-unity.log`
12-
- On macOS, the extension log file is at `~/.local/share/GitHubUnity/github-unity.log`
12+
- On macOS, the extension log file is at `~/Library/Application Support/GitHubUnity/github-unity.log`
1313

1414
### Description
1515

common/SolutionInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
namespace System
3232
{
3333
internal static class AssemblyVersionInformation {
34-
internal const string Version = "0.16.0.0";
34+
internal const string Version = "0.19.0.0";
3535
}
3636
}

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,24 @@ protected void Initialize()
3939
Logging.TracingEnabled = UserSettings.Get(Constants.TraceLoggingKey, false);
4040
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, CancellationToken);
4141
Platform.Initialize(ProcessManager, TaskManager);
42-
if (Environment.GitExecutablePath != null)
43-
{
44-
GitClient = new GitClient(Environment, ProcessManager, Platform.CredentialManager, TaskManager);
45-
}
42+
GitClient = new GitClient(Environment, ProcessManager, Platform.CredentialManager, TaskManager);
4643
SetupMetrics();
4744
}
4845

49-
public virtual async Task Run(bool firstRun)
46+
public void Run(bool firstRun)
47+
{
48+
new ActionTask(SetupGit())
49+
.Then(RestartRepository)
50+
.ThenInUI(InitializeUI)
51+
.Start();
52+
}
53+
54+
private async Task SetupGit()
5055
{
5156
Logger.Trace("Run - CurrentDirectory {0}", NPath.CurrentDirectory);
5257

5358
if (Environment.GitExecutablePath == null)
5459
{
55-
GitClient = new GitClient(Environment, ProcessManager, Platform.CredentialManager, TaskManager);
5660
Environment.GitExecutablePath = await DetermineGitExecutablePath();
5761

5862
Logger.Trace("Environment.GitExecutablePath \"{0}\" Exists:{1}", Environment.GitExecutablePath, Environment.GitExecutablePath.FileExists());
@@ -67,10 +71,6 @@ public virtual async Task Run(bool firstRun)
6771
}
6872
}
6973

70-
RestartRepository();
71-
InitializeUI();
72-
73-
new ActionTask(new Task(() => LoadKeychain().Start())).Start();
7474
}
7575

7676
public ITask InitializeRepository()
@@ -79,7 +79,10 @@ public ITask InitializeRepository()
7979

8080
var targetPath = NPath.CurrentDirectory;
8181

82-
var unityYamlMergeExec = Environment.UnityApplication.Parent.Combine("Tools", "UnityYAMLMerge");
82+
var unityYamlMergeExec = Environment.IsWindows
83+
? Environment.UnityApplication.Parent.Combine("Data", "Tools", "UnityYAMLMerge.exe")
84+
: Environment.UnityApplication.Combine("Contents", "Tools", "UnityYAMLMerge");
85+
8386
var yamlMergeCommand = Environment.IsWindows
8487
? $@"'{unityYamlMergeExec}' merge -p ""$BASE"" ""$REMOTE"" ""$LOCAL"" ""$MERGED"""
8588
: $@"'{unityYamlMergeExec}' merge -p '$BASE' '$REMOTE' '$LOCAL' '$MERGED'";
@@ -104,41 +107,27 @@ public ITask InitializeRepository()
104107
}))
105108
.Then(GitClient.Add(filesForInitialCommit))
106109
.Then(GitClient.Commit("Initial commit", null))
107-
.Then(RestartRepository)
110+
.Then(_ =>
111+
{
112+
Environment.InitializeRepository();
113+
RestartRepository();
114+
})
108115
.ThenInUI(InitializeUI);
109116
return task;
110117
}
111118

112119
public void RestartRepository()
113120
{
114-
Environment.InitializeRepository();
115121
if (Environment.RepositoryPath != null)
116122
{
117123
repositoryManager = Unity.RepositoryManager.CreateInstance(Platform, TaskManager, UsageTracker, GitClient, Environment.RepositoryPath);
118-
Environment.Repository = new Repository(GitClient, repositoryManager, Environment.RepositoryPath);
119124
repositoryManager.Initialize();
120-
Environment.Repository.Initialize();
125+
Environment.Repository.Initialize(repositoryManager);
121126
repositoryManager.Start();
122127
Logger.Trace($"Got a repository? {Environment.Repository}");
123128
}
124129
}
125130

126-
private async Task LoadKeychain()
127-
{
128-
Logger.Trace("Loading Keychain");
129-
130-
var firstConnection = Platform.Keychain.Hosts.FirstOrDefault();
131-
if (firstConnection == null)
132-
{
133-
Logger.Trace("No Host Found");
134-
}
135-
else
136-
{
137-
Logger.Trace("Loading Connection to Host:\"{0}\"", firstConnection);
138-
await Platform.Keychain.Load(firstConnection).SafeAwait();
139-
}
140-
}
141-
142131
private async Task<NPath> DetermineGitExecutablePath(ProgressReport progress = null)
143132
{
144133
var gitExecutablePath = SystemSettings.Get(Constants.GitInstallPathKey)?.ToNPath();

src/GitHub.Api/Application/IApiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ interface IApiClient
99
{
1010
HostAddress HostAddress { get; }
1111
UriString OriginalUrl { get; }
12-
Task GetRepository(Action<Octokit.Repository> callback);
1312
Task CreateRepository(NewRepository newRepository, Action<Octokit.Repository, Exception> callback, string organization = null);
1413
Task GetOrganizations(Action<IList<Organization>> callback);
1514
Task Login(string username, string password, Action<LoginResult> need2faCode, Action<bool, string> result);
@@ -18,5 +17,6 @@ interface IApiClient
1817
Task<bool> ValidateCredentials();
1918
Task Logout(UriString host);
2019
Task GetCurrentUser(Action<Octokit.User> callback);
20+
Task LoadKeychain(Action<bool> callback);
2121
}
2222
}

src/GitHub.Api/Application/IApplicationManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ interface IApplicationManager : IDisposable
1717
ITaskManager TaskManager { get; }
1818
IGitClient GitClient { get; }
1919
IUsageTracker UsageTracker { get; }
20+
21+
void Run(bool firstRun);
2022
void RestartRepository();
2123
ITask InitializeRepository();
2224
}

src/GitHub.Api/Authentication/IKeychain.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface IKeychain
1515
Connection[] Connections { get; }
1616
IList<UriString> Hosts { get; }
1717
bool HasKeys { get; }
18+
bool NeedsLoad { get; }
1819
void SetToken(UriString host, string token);
1920
}
2021
}

src/GitHub.Api/Authentication/Keychain.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,7 @@ public void UpdateToken(UriString host, string token)
230230
public IList<UriString> Hosts => connectionCache.Keys.ToArray();
231231

232232
public bool HasKeys => connectionCache.Any();
233+
234+
public bool NeedsLoad => HasKeys && FindOrCreateAdapter(connectionCache.First().Value.Host).OctokitCredentials == Credentials.Anonymous;
233235
}
234236
}

src/GitHub.Api/Git/GitClient.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,18 @@ private Task<NPath> LookForPortableGit()
134134

135135
private async Task<NPath> LookForSystemGit()
136136
{
137-
if (environment.IsMac)
137+
NPath path = null;
138+
if (!environment.IsWindows)
138139
{
139-
var path = "/usr/local/bin/git".ToNPath();
140-
if (path.FileExists())
141-
return path;
140+
var p = new NPath("/usr/local/bin/git");
141+
if (p.FileExists())
142+
path = p;
142143
}
143-
return await new FindExecTask("git", taskManager.Token).StartAwait();
144+
145+
if (path == null)
146+
path = await new FindExecTask("git", taskManager.Token).StartAwait();
147+
148+
return path;
144149
}
145150

146151
public bool ValidateGitInstall(NPath path)

0 commit comments

Comments
 (0)