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

Commit 1d1be86

Browse files
authored
Merge branch 'master' into fixes/settings
2 parents 12afcce + 9cec196 commit 1d1be86

35 files changed

+773
-252
lines changed

build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ fi
3434

3535
xbuild GitHub.Unity.sln /property:Configuration=$Configuration /target:$Target
3636

37+
cp -r unity/PackageProject/Assets/Editor/GitHub ../github-unity-test/GitHubExtensionProject/Assets/Editor || true
38+
3739
rm -f unity/PackageProject/Assets/Editor/GitHub/deleteme*
3840
rm -f unity/PackageProject/Assets/Editor/GitHub/deleteme*
3941
rm -f unity/PackageProject/Assets/Editor/GitHub/*.xml

src/GitHub.Api/Api/ApiClient.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,39 @@ namespace GitHub.Unity
77
{
88
class ApiClient : IApiClient
99
{
10+
public static IApiClient Create(UriString repositoryUrl, IKeychain keychain, IAppConfiguration appConfiguration)
11+
{
12+
var credentialStore = keychain.Connect(repositoryUrl);
13+
var hostAddress = HostAddress.Create(repositoryUrl);
14+
15+
return new ApiClient(repositoryUrl, keychain,
16+
new GitHubClient(appConfiguration.ProductHeader, credentialStore, hostAddress.ApiUri));
17+
}
18+
1019
private static readonly Unity.ILogging logger = Unity.Logging.GetLogger<ApiClient>();
1120
public HostAddress HostAddress { get; }
1221
public UriString OriginalUrl { get; }
1322

23+
private readonly IKeychain keychain;
1424
private readonly IGitHubClient githubClient;
15-
private readonly IGitHubClient githubAppClient;
16-
private readonly ICredentialManager credentialManager;
1725
private readonly ILoginManager loginManager;
1826
private static readonly SemaphoreSlim sem = new SemaphoreSlim(1);
1927

2028
Octokit.Repository repositoryCache = new Octokit.Repository();
2129
string owner;
2230
bool? isEnterprise;
2331

24-
public ApiClient(UriString hostUrl, ICredentialManager credentialManager, IGitHubClient githubClient, IGitHubClient githubAppClient)
32+
public ApiClient(UriString hostUrl, IKeychain keychain, IGitHubClient githubClient)
2533
{
2634
Guard.ArgumentNotNull(hostUrl, nameof(hostUrl));
27-
Guard.ArgumentNotNull(credentialManager, nameof(credentialManager));
35+
Guard.ArgumentNotNull(keychain, nameof(keychain));
2836
Guard.ArgumentNotNull(githubClient, nameof(githubClient));
2937

3038
HostAddress = HostAddress.Create(hostUrl);
3139
OriginalUrl = hostUrl;
40+
this.keychain = keychain;
3241
this.githubClient = githubClient;
33-
this.githubAppClient = githubAppClient;
34-
this.credentialManager = credentialManager;
35-
loginManager = new LoginManager(credentialManager, ApplicationInfo.ClientId, ApplicationInfo.ClientSecret);
42+
loginManager = new LoginManager(keychain, ApplicationInfo.ClientId, ApplicationInfo.ClientSecret);
3643
}
3744

3845
public async void GetRepository(Action<Octokit.Repository> callback)
@@ -54,6 +61,7 @@ public async Task Login(string username, string password, Action<LoginResult> ne
5461
}
5562
catch (Exception ex)
5663
{
64+
logger.Warning(ex);
5765
result(false, ex.Message);
5866
return;
5967
}
@@ -176,10 +184,12 @@ public async Task<bool> ValidateCredentials()
176184
{
177185
try
178186
{
179-
var credential = await credentialManager.Load(OriginalUrl);
180-
if (credential != null)
187+
var store = keychain.Connect(OriginalUrl);
188+
189+
if (store.OctokitCredentials != Credentials.Anonymous)
181190
{
182-
await githubAppClient.Authorization.CheckApplicationAuthentication(ApplicationInfo.ClientId, credential.Token);
191+
var credential = store.Credential;
192+
await githubClient.Authorization.CheckApplicationAuthentication(ApplicationInfo.ClientId, credential.Token);
183193
}
184194
}
185195
catch

src/GitHub.Api/Api/ApiClientFactory.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/GitHub.Api/Api/IApiClientFactory.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/GitHub.Api/ApplicationManagerBase.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ApplicationManagerBase : IApplicationManager
88
{
99
protected static readonly ILogging logger = Logging.GetLogger<IApplicationManager>();
1010

11+
private AppConfiguration appConfiguration;
1112
private RepositoryLocator repositoryLocator;
1213
private RepositoryManager repositoryManager;
1314

@@ -40,8 +41,7 @@ protected void Initialize(IUIDispatcher uiDispatcher)
4041

4142
Platform = new Platform(Environment, FileSystem, uiDispatcher);
4243
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, CancellationToken);
43-
Platform.Initialize(ProcessManager);
44-
ApiClientFactory.Instance = new ApiClientFactory(new AppConfiguration(), Platform.CredentialManager);
44+
Platform.Initialize(Environment, ProcessManager);
4545
}
4646

4747
public virtual Task Run()
@@ -129,9 +129,28 @@ private async Task RunInternal()
129129
logger.Trace("Environment.GitExecutablePath \"{0}\" Exists:{1}", gitSetup.GitExecutablePath, gitSetup.GitExecutablePath.FileExists());
130130

131131
await RestartRepository();
132-
}
133132

133+
if (Environment.IsWindows)
134+
{
135+
string credentialHelper = null;
136+
var gitConfigGetTask = new GitConfigGetTask(Environment, ProcessManager,
137+
new TaskResultDispatcher<string>(s => {
138+
credentialHelper = s;
139+
}), "credential.helper", GitConfigSource.Global);
140+
141+
142+
await gitConfigGetTask.RunAsync(CancellationToken.None);
134143

144+
if (string.IsNullOrEmpty(credentialHelper))
145+
{
146+
var gitConfigSetTask = new GitConfigSetTask(Environment, ProcessManager,
147+
new TaskResultDispatcher<string>(s => { }), "credential.helper", "wincred",
148+
GitConfigSource.Global);
149+
150+
await gitConfigSetTask.RunAsync(CancellationToken.None);
151+
}
152+
}
153+
}
135154

136155
private async Task<string> LookForGitInstallationPath()
137156
{
@@ -169,6 +188,14 @@ public void Dispose()
169188
Dispose(true);
170189
}
171190

191+
public AppConfiguration AppConfiguration
192+
{
193+
get
194+
{
195+
return appConfiguration ?? (appConfiguration = new AppConfiguration());
196+
}
197+
}
198+
172199
public virtual IEnvironment Environment { get; set; }
173200
public IFileSystem FileSystem { get; protected set; }
174201
public IPlatform Platform { get; protected set; }

src/GitHub.Api/Authentication/AppCredentialStore.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/GitHub.Api/Authentication/CredentialStore.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Threading.Tasks;
2+
3+
namespace GitHub.Unity
4+
{
5+
interface IKeychain
6+
{
7+
KeychainAdapter Connect(UriString host);
8+
Task<KeychainAdapter> Load(UriString host);
9+
void Clear(UriString host);
10+
void Clear();
11+
Task Flush(UriString host);
12+
void UpdateToken(UriString host, string token);
13+
void Save(ICredential credential);
14+
void Initialize();
15+
bool HasKeys { get; }
16+
}
17+
}

src/GitHub.Api/Authentication/ILoginManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ interface ILoginManager
2626
/// Logs out of GitHub server.
2727
/// </summary>
2828
/// <param name="hostAddress">The address of the server.</param>
29-
Task Logout(UriString hostAddress, IGitHubClient client);
29+
Task Logout(UriString hostAddress);
3030
}
3131
}

src/GitHub.Api/Authentication/IPlatform.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ namespace GitHub.Unity
55
{
66
interface IPlatform
77
{
8-
Task<IPlatform> Initialize(IProcessManager processManager);
8+
Task<IPlatform> Initialize(IEnvironment environment, IProcessManager processManager);
99
IProcessEnvironment GitEnvironment { get; }
1010
ICredentialManager CredentialManager { get; }
1111
IEnvironment Environment { get; }
1212
IProcessManager ProcessManager { get; }
1313
IUIDispatcher UIDispatcher { get; }
14+
IKeychain Keychain { get; }
1415
}
1516
}

0 commit comments

Comments
 (0)