Skip to content

Commit

Permalink
write event.json to disk (#30)
Browse files Browse the repository at this point in the history
* Added basics to make the runner work with the payload

* Added some fixes to the basic code

* Added PR comments

* Added formatting.
  • Loading branch information
Marianna Sternefeld authored and TingluoHuang committed Jun 19, 2019
1 parent 410fec0 commit fd77921
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Runner.Worker/ActionRunner.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GitHub.DistributedTask.Expressions;
using GitHub.DistributedTask.ObjectTemplating.Tokens;
Expand Down Expand Up @@ -55,6 +57,21 @@ public async Task RunAsync()

IStepHost stepHost = HostContext.CreateService<IDefaultStepHost>();

// Makes directory for event_path data
var tempDirectory = HostContext.GetDirectory(WellKnownDirectory.Temp);
var workflowDirectory = Path.Combine(tempDirectory, "workflow");
Directory.CreateDirectory(workflowDirectory);

var gitHubEvent = ExecutionContext.GetGitHubContext("event");

// adds the GitHub event path/file if the event exists
if (gitHubEvent != null)
{
var workflowFile = Path.Combine(workflowDirectory, "event.json");
File.WriteAllText(workflowFile, gitHubEvent, new UTF8Encoding(false));
ExecutionContext.SetGitHubContext("event_path", workflowFile);
}

// Setup container stephost for running inside the container.
if (ExecutionContext.Container != null)
{
Expand Down
11 changes: 10 additions & 1 deletion src/Runner.Worker/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using GitHub.Runner.Common.Util;
using GitHub.Runner.Common;
using GitHub.Runner.Sdk;
using Newtonsoft.Json;
using System.Text;

namespace GitHub.Runner.Worker
Expand Down Expand Up @@ -329,7 +330,14 @@ public string GetGitHubContext(string name)
var githubContext = ExpressionValues["github"] as GitHubContext;
if (githubContext.TryGetValue(name, out var value))
{
return value as StringContextData;
if (value is StringContextData)
{
return value as StringContextData;
}
else
{
return value.ToJToken().ToString(Formatting.Indented);
}
}
else
{
Expand Down Expand Up @@ -569,6 +577,7 @@ public void InitializeJob(Pipelines.AgentJobRequestMessage message, Cancellation
{
var githubContext = new GitHubContext();
var ghDictionary = (DictionaryContextData)ExpressionValues["github"];
Trace.Info("Initialize GitHub context");
foreach (var pair in ghDictionary)
{
githubContext.Add(pair.Key, pair.Value);
Expand Down
14 changes: 14 additions & 0 deletions src/Sdk/DTPipelines/Pipelines/ContextData/ArrayContextData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Runtime.Serialization;
using GitHub.Services.WebApi.Internal;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Expressions = GitHub.DistributedTask.Expressions;

namespace GitHub.DistributedTask.Pipelines.ContextData
Expand Down Expand Up @@ -51,6 +52,19 @@ public override PipelineContextData Clone()
return result;
}

public override JToken ToJToken()
{
var result = new JArray();
if (m_items?.Count > 0)
{
foreach (var item in m_items)
{
result.Add(item.ToJToken());
}
}
return result;
}

public IEnumerator<PipelineContextData> GetEnumerator()
{
if (m_items?.Count > 0)
Expand Down
14 changes: 14 additions & 0 deletions src/Sdk/DTPipelines/Pipelines/ContextData/DictionaryContextData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.Serialization;
using GitHub.Services.WebApi.Internal;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Expressions = GitHub.DistributedTask.Expressions;

namespace GitHub.DistributedTask.Pipelines.ContextData
Expand Down Expand Up @@ -174,6 +175,19 @@ public override PipelineContextData Clone()
return result;
}

public override JToken ToJToken()
{
var json = new JObject();
if (m_list?.Count > 0)
{
foreach (var item in m_list)
{
json.Add(item.Key, item.Value.ToJToken());
}
}
return json;
}

public Boolean ContainsKey(String key)
{
return TryGetValue(key, out _);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.Serialization;
using GitHub.Services.WebApi.Internal;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace GitHub.DistributedTask.Pipelines.ContextData
{
Expand All @@ -24,5 +25,7 @@ protected PipelineContextData(Int32 type)
internal Int32 Type { get; }

public abstract PipelineContextData Clone();

public abstract JToken ToJToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.Serialization;
using GitHub.DistributedTask.Expressions;
using GitHub.Services.WebApi.Internal;
using Newtonsoft.Json.Linq;

namespace GitHub.DistributedTask.Pipelines.ContextData
{
Expand Down Expand Up @@ -35,6 +36,11 @@ public override PipelineContextData Clone()
return new StringContextData(m_value);
}

public override JToken ToJToken()
{
return (JToken) m_value;
}

String IString.GetString()
{
return Value;
Expand Down

0 comments on commit fd77921

Please sign in to comment.