From 4adaf9c1e6371ad48698c13c0a793b561a1d47da Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Wed, 2 Nov 2022 09:40:19 -0400 Subject: [PATCH] Use Global.Variables instead of JobContext and include action path/ref in the message. (#2214) * Use Global.Variables instead of JobContext and include action path/ref in the message. * encoding * . --- .editorconfig | 1 + .../Handlers/NodeScriptActionHandler.cs | 29 ++++++++++++++----- src/Runner.Worker/JobRunner.cs | 6 ++-- src/Runner.Worker/Variables.cs | 10 +++++-- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.editorconfig b/.editorconfig index 37c44b8cf..76e6ed4e6 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,7 @@ # https://editorconfig.org/ [*] +charset = utf-8 # Set default charset to utf-8 insert_final_newline = true # ensure all files end with a single newline trim_trailing_whitespace = true # attempt to remove trailing whitespace on save diff --git a/src/Runner.Worker/Handlers/NodeScriptActionHandler.cs b/src/Runner.Worker/Handlers/NodeScriptActionHandler.cs index fc743f1b1..fb77137d9 100644 --- a/src/Runner.Worker/Handlers/NodeScriptActionHandler.cs +++ b/src/Runner.Worker/Handlers/NodeScriptActionHandler.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; @@ -7,8 +8,8 @@ using GitHub.DistributedTask.Pipelines; using GitHub.DistributedTask.Pipelines.ContextData; using GitHub.DistributedTask.WebApi; using GitHub.Runner.Common; -using GitHub.Runner.Sdk; using GitHub.Runner.Common.Util; +using GitHub.Runner.Sdk; using GitHub.Runner.Worker.Container; using GitHub.Runner.Worker.Container.ContainerHooks; @@ -137,13 +138,25 @@ namespace GitHub.Runner.Worker.Handlers if (Data.NodeVersion == "node12" && (ExecutionContext.Global.Variables.GetBoolean(Constants.Runner.Features.Node12Warning) ?? false)) { - if (!ExecutionContext.JobContext.ContainsKey("Node12ActionsWarnings")) - { - ExecutionContext.JobContext["Node12ActionsWarnings"] = new ArrayContextData(); - } var repoAction = Action as RepositoryPathReference; - var actionDisplayName = new StringContextData(repoAction.Name ?? repoAction.Path); // local actions don't have a 'Name' - ExecutionContext.JobContext["Node12ActionsWarnings"].AssertArray("Node12ActionsWarnings").Add(actionDisplayName); + var warningActions = new HashSet(); + if (ExecutionContext.Global.Variables.TryGetValue("Node12ActionsWarnings", out var node12Warnings)) + { + warningActions = StringUtil.ConvertFromJson>(node12Warnings); + } + + var repoActionFullName = ""; + if (string.IsNullOrEmpty(repoAction.Name)) + { + repoActionFullName = repoAction.Path; // local actions don't have a 'Name' + } + else + { + repoActionFullName = $"{repoAction.Name}/{repoAction.Path ?? string.Empty}".TrimEnd('/') + $"@{repoAction.Ref}"; + } + + warningActions.Add(repoActionFullName); + ExecutionContext.Global.Variables.Set("Node12ActionsWarnings", StringUtil.ConvertToJson(warningActions)); } using (var stdoutManager = new OutputManager(ExecutionContext, ActionCommandManager)) diff --git a/src/Runner.Worker/JobRunner.cs b/src/Runner.Worker/JobRunner.cs index ac2ebd42a..562ef9068 100644 --- a/src/Runner.Worker/JobRunner.cs +++ b/src/Runner.Worker/JobRunner.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -258,9 +258,9 @@ namespace GitHub.Runner.Worker } } - if (jobContext.JobContext.ContainsKey("Node12ActionsWarnings")) + if (jobContext.Global.Variables.TryGetValue("Node12ActionsWarnings", out var node12Warnings)) { - var actions = string.Join(", ", jobContext.JobContext["Node12ActionsWarnings"].AssertArray("Node12ActionsWarnings").Select(action => action.ToString())); + var actions = string.Join(", ", StringUtil.ConvertFromJson>(node12Warnings)); jobContext.Warning(string.Format(Constants.Runner.Node12DetectedAfterEndOfLife, actions)); } diff --git a/src/Runner.Worker/Variables.cs b/src/Runner.Worker/Variables.cs index 3284bb7e1..2969bf610 100644 --- a/src/Runner.Worker/Variables.cs +++ b/src/Runner.Worker/Variables.cs @@ -1,10 +1,10 @@ -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; -using GitHub.DistributedTask.WebApi; using GitHub.DistributedTask.Logging; using GitHub.DistributedTask.Pipelines.ContextData; +using GitHub.DistributedTask.WebApi; using GitHub.Runner.Common; using GitHub.Runner.Common.Util; using GitHub.Runner.Sdk; @@ -136,6 +136,12 @@ namespace GitHub.Runner.Worker return null; } + public void Set(string name, string val) + { + ArgUtil.NotNullOrEmpty(name, nameof(name)); + _variables[name] = new Variable(name, val, false); + } + public bool TryGetValue(string name, out string val) { Variable variable;