From 9ceb3d481a7c7f3b89fdc5da8a77c97ec9c08bae Mon Sep 17 00:00:00 2001 From: Tingluo Huang Date: Fri, 11 Dec 2020 11:04:07 -0500 Subject: [PATCH] unset GTIHUB_ACTION_REPOSITORY and GITHUB_ACTION_REF for non-repo based actions. (#804) --- src/Runner.Worker/ActionRunner.cs | 9 ++++- src/Test/L0/Worker/ActionRunnerL0.cs | 60 ++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/Runner.Worker/ActionRunner.cs b/src/Runner.Worker/ActionRunner.cs index f2962c17f..3ea522ac8 100644 --- a/src/Runner.Worker/ActionRunner.cs +++ b/src/Runner.Worker/ActionRunner.cs @@ -142,6 +142,11 @@ namespace GitHub.Runner.Worker ExecutionContext.SetGitHubContext("action_repository", repoPathReferenceAction.Name); ExecutionContext.SetGitHubContext("action_ref", repoPathReferenceAction.Ref); } + else + { + ExecutionContext.SetGitHubContext("action_repository", null); + ExecutionContext.SetGitHubContext("action_ref", null); + } // Setup container stephost for running inside the container. if (ExecutionContext.Global.Container != null) @@ -250,11 +255,11 @@ namespace GitHub.Runner.Worker handler.PrintActionDetails(Stage); // Run the task. - try + try { await handler.RunAsync(Stage); } - finally + finally { fileCommandManager.ProcessFiles(ExecutionContext, ExecutionContext.Global.Container); } diff --git a/src/Test/L0/Worker/ActionRunnerL0.cs b/src/Test/L0/Worker/ActionRunnerL0.cs index 371a6253b..0efee2690 100644 --- a/src/Test/L0/Worker/ActionRunnerL0.cs +++ b/src/Test/L0/Worker/ActionRunnerL0.cs @@ -333,6 +333,66 @@ namespace GitHub.Runner.Common.Tests.Worker _ec.Verify(x => x.AddIssue(It.Is(s => s.Message.Contains("Unexpected input(s) 'invalid1', 'invalid2'")), It.IsAny()), Times.Once); } + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public async void SetGitHubContextActionRepoRef() + { + //Arrange + Setup(); + var actionId = Guid.NewGuid(); + var actionInputs = new MappingToken(null, null, null); + actionInputs.Add(new StringToken(null, null, null, "input1"), new StringToken(null, null, null, "test1")); + actionInputs.Add(new StringToken(null, null, null, "input2"), new StringToken(null, null, null, "test2")); + var action = new Pipelines.ActionStep() + { + Name = "action", + Id = actionId, + Reference = new Pipelines.RepositoryPathReference() + { + Name = "actions/test", + Ref = "master" + }, + Inputs = actionInputs + }; + + _actionRunner.Action = action; + + Dictionary finialInputs = new Dictionary(); + _handlerFactory.Setup(x => x.Create(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny>(), It.IsAny(), It.IsAny())) + .Callback((IExecutionContext executionContext, Pipelines.ActionStepDefinitionReference actionReference, IStepHost stepHost, ActionExecutionData data, Dictionary inputs, Dictionary environment, Variables runtimeVariables, string taskDirectory) => + { + finialInputs = inputs; + }) + .Returns(new Mock().Object); + + //Act + await _actionRunner.RunAsync(); + + //Assert + _ec.Verify(x => x.SetGitHubContext("action_repository", "actions/test"), Times.Once); + _ec.Verify(x => x.SetGitHubContext("action_ref", "master"), Times.Once); + + action = new Pipelines.ActionStep() + { + Name = "action", + Id = actionId, + Reference = new Pipelines.ScriptReference(), + Inputs = actionInputs + }; + _actionRunner.Action = action; + + _hc.EnqueueInstance(_defaultStepHost.Object); + _hc.EnqueueInstance(_fileCommandManager.Object); + + //Act + await _actionRunner.RunAsync(); + + //Assert + _ec.Verify(x => x.SetGitHubContext("action_repository", null), Times.Once); + _ec.Verify(x => x.SetGitHubContext("action_ref", null), Times.Once); + } + private void Setup([CallerMemberName] string name = "") { _ecTokenSource?.Dispose();