Skip to content

Commit

Permalink
add json cli flag for act compat
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX committed Nov 29, 2024
1 parent be0c2a7 commit cee58eb
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 18 deletions.
21 changes: 21 additions & 0 deletions src/Runner.Client/ActionResultStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Runtime.Serialization;

namespace Runner.Client
{
[DataContract]
public enum ActionResultStatus
{
[EnumMember]
Success = 0,

[EnumMember]
Failure = 2,

[EnumMember]
Cancelled = 3,

[EnumMember]
Skipped = 4,
}

}
29 changes: 29 additions & 0 deletions src/Runner.Client/ActionResultStatusConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Runtime.Serialization;
using GitHub.DistributedTask.WebApi;

namespace Runner.Client
{
public static class ActionResultStatusConverter {
public static ActionResultStatus FromPipelines(TaskResult pipelineStatus) {
ActionResultStatus result = ActionResultStatus.Failure;
switch(pipelineStatus) {
case TaskResult.Failed:
case TaskResult.Abandoned:
result = ActionResultStatus.Failure;
break;
case TaskResult.Canceled:
result = ActionResultStatus.Cancelled;
break;
case TaskResult.Succeeded:
case TaskResult.SucceededWithIssues:
result = ActionResultStatus.Success;
break;
case TaskResult.Skipped:
result = ActionResultStatus.Skipped;
break;
}
return result;
}
}
}

75 changes: 64 additions & 11 deletions src/Runner.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class Job {

public bool Finished {get;set;}
public JobCompletedEvent JobCompletedEvent {get;set;}
public string Matrix { get; set; }
}

private class TimeLineEvent {
Expand Down Expand Up @@ -167,7 +168,7 @@ private class Parameters {
public bool Interactive { get; internal set; }
public string[] LocalRepositories { get; set; }
public bool Trace { get; set; }

public bool Json { get; set; }
public Parameters ShallowCopy()
{
return (Parameters) this.MemberwiseClone();
Expand Down Expand Up @@ -975,6 +976,9 @@ static int Main(string[] args)
var traceOpt = new Option<bool>(
new[] {"--trace"},
"Client Trace of console log events, to debug missing live logs");
var jsonOpt = new Option<bool>(
new[] {"--json"},
"Experimental json logging");
var quietOpt = new Option<bool>(
new[] {"-q", "--quiet"},
"Display no progress in the cli");
Expand Down Expand Up @@ -1104,6 +1108,7 @@ static int Main(string[] args)
watchOpt,
interactiveOpt,
traceOpt,
jsonOpt,
quietOpt,
privilegedOpt,
usernsOpt,
Expand Down Expand Up @@ -2083,6 +2088,9 @@ await Task.WhenAny(Task.Run(() => {
});
}
}
if(rec.WorkflowName == null && rec.TimeLine?.FirstOrDefault()?.RecordType == "workflow") {
rec.WorkflowName = rec.TimeLine?.FirstOrDefault()?.RefName;
}
return Task.CompletedTask;
};
Func<JobCompletedEvent, Task> printFinishJob = async ev => {
Expand All @@ -2096,7 +2104,11 @@ await Task.WhenAny(Task.Run(() => {
} catch {

}
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"Job Completed with Status: {ev.Result.ToString()}");
if(parameters.Json) {
Console.WriteLine(JsonConvert.SerializeObject(new { dryrun = false, workflowName = rec.WorkflowName, job = rec.TimeLine[0].Name, jobId = rec.TimeLine[0].RefName, level = "info", matrix = new {}, jobResult = ActionResultStatusConverter.FromPipelines(ev.Result).ToString().ToLower(), msg = $"Job Completed with Status: {ev.Result.ToString()}", time = System.DateTime.Now }));
} else {
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"Job Completed with Status: {ev.Result.ToString()}");
}
};
var eventstream = resp.Content.ReadAsStream();

Expand Down Expand Up @@ -2134,8 +2146,12 @@ await Task.WhenAny(Task.Run(() => {
if(record == null || !record.Result.HasValue) {
rec.Pending.Add(e);
continue;
}
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"{record.Result.Value.ToString()}: {record.Name}");
}
if(parameters.Json) {
Console.WriteLine(JsonConvert.SerializeObject(new { dryrun = false, workflowName = rec.WorkflowName, job = rec.TimeLine[0].Name, jobId = rec.TimeLine[0].RefName, level = "info", matrix = JArray.Parse(jobs.FirstOrDefault(j => j.JobId == rec.TimeLine[0].Id)?.Matrix ?? "[{}]").LastOrDefault() ?? new JObject(), stepResult = ActionResultStatusConverter.FromPipelines(record.Result.Value).ToString().ToLower(), msg = $"{record.Result.Value.ToString()}: {record.Name}", stage = "Main", step = rec.TimeLine.Find(r => r.Id == e.record.StepId)?.Name, stepId = new [] { rec.RecordId.ToString() }, time = System.DateTime.Now }));
} else {
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"{record.Result.Value.ToString()}: {record.Name}");
}
}
rec.RecordId = e.record.StepId;
if(rec.TimeLine != null) {
Expand All @@ -2150,11 +2166,19 @@ await Task.WhenAny(Task.Run(() => {
} catch {

}
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"Running: {record.Name}");
if(parameters.Json) {
Console.WriteLine(JsonConvert.SerializeObject(new { dryrun = false, workflowName = rec.WorkflowName, job = rec.TimeLine[0].Name, jobId = rec.TimeLine[0].RefName, level = "info", matrix = JArray.Parse(jobs.FirstOrDefault(j => j.JobId == rec.TimeLine[0].Id)?.Matrix ?? "[{}]").LastOrDefault() ?? new JObject(), msg = $"Running: {record.Name}", stage = "Main", step = rec.TimeLine.Find(r => r.Id == e.record.StepId)?.Name, stepId = new [] { rec.RecordId.ToString() }, time = System.DateTime.Now }));
} else {
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null && rec.TimeLine[0].RecordType != "workflow" ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"Running: {record.Name}");
}
}
}
foreach (var webconsoleline in e.record.Value) {
WriteLogLine((int)rec.Color, webconsoleline);
if(parameters.Json) {
Console.WriteLine(JsonConvert.SerializeObject(new { dryrun = false, workflowName = rec.WorkflowName, job = rec.TimeLine[0].Name, jobId = rec.TimeLine[0].RefName, level = "info", matrix = JArray.Parse(jobs.FirstOrDefault(j => j.JobId == rec.TimeLine[0].Id)?.Matrix ?? "[{}]").LastOrDefault() ?? new JObject(), msg = webconsoleline, stage = "Main", step = rec.TimeLine.Find(r => r.Id == e.record.StepId)?.Name, stepId = new [] { rec.RecordId.ToString() }, time = System.DateTime.Now }));
} else {
WriteLogLine((int)rec.Color, webconsoleline);
}
}
}
if(line == "event: timeline") {
Expand All @@ -2174,7 +2198,11 @@ await Task.WhenAny(Task.Run(() => {
break;
}
var rec = timelineRecords[e.timelineId];
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"{record.Result.Value.ToString()}: {record.Name}");
if(parameters.Json) {
Console.WriteLine(JsonConvert.SerializeObject(new { dryrun = false, workflowName = rec.WorkflowName, job = rec.TimeLine[0].Name, jobId = rec.TimeLine[0].RefName, level = "info", matrix = JArray.Parse(jobs.FirstOrDefault(j => j.JobId == rec.TimeLine[0].Id)?.Matrix ?? "[{}]").LastOrDefault() ?? new JObject(), stepResult = ActionResultStatusConverter.FromPipelines(record.Result.Value).ToString().ToLower(), msg = $"{record.Result.Value.ToString()}: {record.Name}", stage = "Main", step = rec.TimeLine.Find(r => r.Id == e2.record.StepId)?.Name, stepId = new [] { rec.RecordId.ToString() }, time = System.DateTime.Now }));
} else {
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"{record.Result.Value.ToString()}: {record.Name}");
}
}
timelineRecords[e.timelineId].RecordId = e2.record.StepId;
if(timelineRecords[e.timelineId].TimeLine != null) {
Expand All @@ -2189,11 +2217,20 @@ await Task.WhenAny(Task.Run(() => {
} catch {

}
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"Running: {record.Name}");
if(parameters.Json) {
Console.WriteLine(JsonConvert.SerializeObject(new { dryrun = false, workflowName = rec.WorkflowName, job = rec.TimeLine[0].Name, jobId = rec.TimeLine[0].RefName, level = "info", matrix = JArray.Parse(jobs.FirstOrDefault(j => j.JobId == rec.TimeLine[0].Id)?.Matrix ?? "[{}]").LastOrDefault() ?? new JObject(), msg = $"Running: {record.Name}", stage = "Main", step = rec.TimeLine.Find(r => r.Id == e2.record.StepId)?.Name, stepId = new [] { rec.RecordId.ToString() }, time = System.DateTime.Now }));
} else {
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"Running: {record.Name}");
}
}
}
foreach (var webconsoleline in e2.record.Value) {
WriteLogLine((int)timelineRecords[e.timelineId].Color, webconsoleline);
if(parameters.Json) {
var rec = timelineRecords[e.timelineId];
Console.WriteLine(JsonConvert.SerializeObject(new { dryrun = false, workflowName = rec.WorkflowName, job = rec.TimeLine[0].Name, jobId = rec.TimeLine[0].RefName, level = "info", matrix = JArray.Parse(jobs.FirstOrDefault(j => j.JobId == rec.TimeLine[0].Id)?.Matrix ?? "[{}]").LastOrDefault() ?? new JObject(), msg = webconsoleline, stage = "Main", step = rec.TimeLine.Find(r => r.Id == e2.record.StepId)?.Name, stepId = new [] { rec.RecordId.ToString() }, time = System.DateTime.Now }));
} else {
WriteLogLine((int)timelineRecords[e.timelineId].Color, webconsoleline);
}
}
timelineRecords[e.timelineId].Pending.RemoveAt(0);
}
Expand All @@ -2202,7 +2239,11 @@ await Task.WhenAny(Task.Run(() => {
if(record != null && record.Result.HasValue) {
var rec = timelineRecords[e.timelineId];
rec.RecordId = Guid.Empty;
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"{record.Result.Value.ToString()}: {record.Name}");
if(parameters.Json) {
Console.WriteLine(JsonConvert.SerializeObject(new { dryrun = false, workflowName = rec.WorkflowName, job = rec.TimeLine[0].Name, jobId = rec.TimeLine[0].RefName, level = "info", matrix = JArray.Parse(jobs.FirstOrDefault(j => j.JobId == rec.TimeLine[0].Id)?.Matrix ?? "[{}]").LastOrDefault() ?? new JObject(), stepResult = ActionResultStatusConverter.FromPipelines(record.Result.Value).ToString().ToLower(), msg = $"{record.Result.Value.ToString()}: {record.Name}", stage = "Main", step = rec.TimeLine.Find(r => r.Id == rec.RecordId)?.Name, stepId = new [] { rec.RecordId.ToString() }, time = System.DateTime.Now }));
} else {
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"{record.Result.Value.ToString()}: {record.Name}");
}
}
}
}
Expand Down Expand Up @@ -2334,7 +2375,11 @@ await Task.WhenAny(Task.Run(() => {
result = TaskResult.Succeeded;
}
if(rec != null) {
WriteLogLine((int)rec.Color, $"{(rec.WorkflowName != null ? $"{rec.WorkflowName} / " : "")}{rec.TimeLine[0].Name}", $"Workflow {_workflow.runid} Completed with Status: {result.ToString()}");
if(parameters.Json) {
Console.WriteLine(JsonConvert.SerializeObject(new { dryrun = false, workflowName = rec.WorkflowName, level = "info", workflowResult = ActionResultStatusConverter.FromPipelines(result).ToString().ToLower(), msg = $"Workflow {_workflow.runid} Completed with Status: {result.ToString()}", time = System.DateTime.Now }));
} else {
WriteLogLine((int)rec.Color, $"{rec.TimeLine[0].Name}", $"Workflow {_workflow.runid} Completed with Status: {result.ToString()}");
}
} else {
Console.WriteLine($"Workflow {_workflow.runid} finished with status {result.ToString()}");
}
Expand All @@ -2345,6 +2390,13 @@ await Task.WhenAny(Task.Run(() => {
var job = JsonConvert.DeserializeObject<Job>(data);
jobs.Add(job);
}
if(line == "event: job_update") {
var job = JsonConvert.DeserializeObject<Job>(data);
var idx = jobs.FindIndex(j => j.JobId == job.JobId);
if(idx != -1) {
jobs[idx] = job;
}
}
if(line == "event: finish") {
var ev = JsonConvert.DeserializeObject<JobCompletedEvent>(data);
await printFinishJob(ev);
Expand Down Expand Up @@ -2485,6 +2537,7 @@ await Task.WhenAny(Task.Run(() => {
parameters.Watch = bindingContext.ParseResult.GetValueForOption(watchOpt);
parameters.Interactive = bindingContext.ParseResult.GetValueForOption(interactiveOpt);
parameters.Trace = bindingContext.ParseResult.GetValueForOption(traceOpt);
parameters.Json = bindingContext.ParseResult.GetValueForOption(jsonOpt);
parameters.Quiet = bindingContext.ParseResult.GetValueForOption(quietOpt);
parameters.Privileged = bindingContext.ParseResult.GetValueForOption(privilegedOpt);
parameters.Userns = bindingContext.ParseResult.GetValueForOption(usernsOpt);
Expand Down
Loading

0 comments on commit cee58eb

Please sign in to comment.