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

Commit e9db741

Browse files
Structures to capture the output from OctorunTask
1 parent 6843085 commit e9db741

File tree

4 files changed

+117
-57
lines changed

4 files changed

+117
-57
lines changed

script

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -241,24 +241,18 @@ private async Task<GitHubRepository> CreateRepositoryInternal(NewRepository newR
241241
.Configure(processManager);
242242

243243
var ret = await octorunTask.StartAwait();
244-
245-
if (ret.Count == 0)
244+
if (ret.IsSuccess && ret.Output.Length == 2)
246245
{
247-
throw new ApiClientException("Publish failed");
248-
}
249-
250-
if (ret[0] == "success")
251-
{
252-
return new GitHubRepository()
246+
return new GitHubRepository
253247
{
254-
Name = ret[1],
255-
CloneUrl = ret[2],
248+
Name = ret.Output[0],
249+
CloneUrl = ret.Output[1]
256250
};
257251
}
258252

259-
if (ret.Count > 3)
253+
if (ret.Output.Any())
260254
{
261-
throw new ApiClientException(ret[3]);
255+
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
262256
}
263257

264258
throw new ApiClientException("Publish failed");
@@ -287,30 +281,27 @@ private async Task GetOrganizationInternal(Action<Organization[]> onSuccess, Act
287281
.Configure(processManager);
288282

289283
var ret = await octorunTask.StartAsAsync();
290-
291-
logger.Trace("Return: {0}", string.Join(";", ret.ToArray()));
292-
293-
if (ret.Count == 0)
294-
{
295-
throw new ApiClientException("Error getting organizations");
296-
}
297-
298-
if (ret[0] == "success")
284+
if (ret.IsSuccess)
299285
{
300286
var organizations = new List<Organization>();
301-
for (var i = 1; i < ret.Count; i = i + 2)
287+
for (var i = 0; i < ret.Output.Length; i = i + 2)
302288
{
303289
organizations.Add(new Organization
304290
{
305-
Name = ret[i],
306-
Login = ret[i + 1]
291+
Name = ret.Output[i],
292+
Login = ret.Output[i + 1]
307293
});
308294
}
309295

310296
onSuccess(organizations.ToArray());
311297
return;
312298
}
313299

300+
if (ret.Output.Any())
301+
{
302+
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
303+
}
304+
314305
throw new ApiClientException("Error getting organizations");
315306
}
316307
catch (Exception ex)
@@ -337,17 +328,20 @@ private async Task<GitHubUser> GetCurrentUserInternal()
337328
.Configure(processManager);
338329

339330
var ret = await octorunTask.StartAsAsync();
340-
341-
logger.Trace("Return: {0}", string.Join(";", ret.ToArray()));
342-
343-
if (ret[0] == "success")
331+
if (ret.IsSuccess)
344332
{
345-
return new GitHubUser {
346-
Name = ret[1],
347-
Login = ret[2]
333+
return new GitHubUser
334+
{
335+
Name = ret.Output[0],
336+
Login = ret.Output[1]
348337
};
349338
}
350339

340+
if (ret.Output.Any())
341+
{
342+
throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
343+
}
344+
351345
throw new ApiClientException("Error validating current user");
352346
}
353347
catch (KeychainEmptyException)

src/GitHub.Api/Authentication/LoginManager.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Net;
34
using System.Threading.Tasks;
45
using Octokit;
@@ -272,29 +273,24 @@ string password
272273
proc.StandardInput.Close();
273274
};
274275

275-
var ret = (await loginTask.StartAwait());
276+
var ret = await loginTask.StartAwait();
276277

277-
if (ret.Count == 0)
278+
if (ret.IsSuccess)
278279
{
279-
throw new Exception("Authentication failed");
280-
}
281-
282-
if (ret[0] == "success")
283-
{
284-
auth = new ApplicationAuthorization(ret[1]);
280+
auth = new ApplicationAuthorization(ret.Output[0]);
285281
return auth;
286282
}
287283

288-
if (ret[0] == "2fa")
284+
if (ret.IsCutom && ret.Status == "2fa")
289285
{
290-
keychain.SetToken(host, ret[1]);
286+
keychain.SetToken(host, ret.Output[0]);
291287
await keychain.Save(host);
292288
throw new TwoFactorRequiredException(TwoFactorType.Unknown);
293289
}
294290

295-
if (ret.Count > 2)
291+
if (ret.Output.Any())
296292
{
297-
throw new Exception(ret[3]);
293+
throw new Exception(string.Join(Environment.NewLine, ret.Output));
298294
}
299295

300296
throw new Exception("Authentication failed");
@@ -329,22 +325,17 @@ string code
329325
proc.StandardInput.Close();
330326
};
331327

332-
var ret = (await loginTask.StartAwait());
333-
334-
if (ret.Count == 0)
335-
{
336-
throw new Exception("Authentication failed");
337-
}
328+
var ret = await loginTask.StartAwait();
338329

339-
if (ret[0] == "success")
330+
if (ret.IsSuccess)
340331
{
341-
auth = new ApplicationAuthorization(ret[1]);
332+
auth = new ApplicationAuthorization(ret.Output[0]);
342333
return auth;
343334
}
344335

345-
if (ret.Count > 2)
336+
if (ret.Output.Any())
346337
{
347-
throw new Exception(ret[3]);
338+
throw new Exception(string.Join(Environment.NewLine, ret.Output));
348339
}
349340

350341
throw new Exception("Authentication failed");

src/GitHub.Api/Tasks/OctorunTask.cs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,78 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Threading;
5+
using GitHub.Logging;
46

57
namespace GitHub.Unity
68
{
7-
class OctorunTask : SimpleListProcessTask
9+
class OctorunTaskOutputProcessor : BaseOutputProcessor<OctorunResult>
10+
{
11+
private int lineCount;
12+
private string status;
13+
private List<string> output = new List<string>();
14+
15+
public override void LineReceived(string line)
16+
{
17+
if (line == null)
18+
{
19+
OctorunResult octorunResult;
20+
if (lineCount == 0)
21+
{
22+
octorunResult = new OctorunResult();
23+
}
24+
else
25+
{
26+
octorunResult = new OctorunResult(status, output.ToArray());
27+
}
28+
RaiseOnEntry(octorunResult);
29+
return;
30+
}
31+
32+
if (lineCount == 0)
33+
{
34+
status = line;
35+
}
36+
else if (status == "error")
37+
{
38+
if (lineCount > 1)
39+
{
40+
output.Add(line);
41+
}
42+
}
43+
else
44+
{
45+
output.Add(line);
46+
}
47+
48+
lineCount++;
49+
}
50+
}
51+
52+
class OctorunTask : ProcessTask<OctorunResult>
853
{
954
private readonly string clientId;
1055
private readonly string clientSecret;
1156
private readonly string user;
1257
private readonly string userToken;
1358

59+
private readonly NPath pathToNodeJs;
60+
private readonly string arguments;
61+
1462
public OctorunTask(CancellationToken token, NPath pathToNodeJs, NPath pathToOctorunJs, string arguments,
1563
string clientId = null,
1664
string clientSecret = null,
1765
string user = null,
1866
string userToken = null,
19-
IOutputProcessor<string, List<string>> processor = null)
20-
: base(token, pathToNodeJs, $"{pathToOctorunJs} {arguments}", processor)
67+
IOutputProcessor<OctorunResult> processor = null)
68+
: base(token, processor ?? new OctorunTaskOutputProcessor())
2169
{
2270
this.clientId = clientId;
2371
this.clientSecret = clientSecret;
2472
this.user = user;
2573
this.userToken = userToken;
74+
this.pathToNodeJs = pathToNodeJs;
75+
this.arguments = $"{pathToOctorunJs} {arguments}";
2676
}
2777

2878
public override void Configure(ProcessStartInfo psi)
@@ -49,5 +99,30 @@ public override void Configure(ProcessStartInfo psi)
4999
psi.EnvironmentVariables.Add("OCTORUN_TOKEN", userToken);
50100
}
51101
}
102+
103+
public override string ProcessName => pathToNodeJs;
104+
public override string ProcessArguments => arguments;
105+
}
106+
107+
class OctorunResult
108+
{
109+
public string Status { get; }
110+
public string[] Output { get; }
111+
112+
public OctorunResult()
113+
{
114+
Status = "error";
115+
Output = new string[0];
116+
}
117+
118+
public OctorunResult(string status, string[] output)
119+
{
120+
Status = status;
121+
Output = output;
122+
}
123+
124+
public bool IsSuccess => Status.Equals("success", StringComparison.InvariantCultureIgnoreCase);
125+
public bool IsError => Status.Equals("error", StringComparison.InvariantCultureIgnoreCase);
126+
public bool IsCutom => !IsSuccess && !IsError;
52127
}
53128
}

0 commit comments

Comments
 (0)