From b372f3081e80c72721a62cbf71f9d42cc851ee47 Mon Sep 17 00:00:00 2001
From: Damian Hickey <57436+damianh@users.noreply.github.com>
Date: Tue, 5 Nov 2024 11:31:27 +0100
Subject: [PATCH] Better support multi-line run

---
 libs/github/src/Actions.Workflow/Step.cs      | 18 +++++++++--
 .../Actions.Workflow.Tests/JobStepTests.cs    | 30 ++++++++++++++++++-
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/libs/github/src/Actions.Workflow/Step.cs b/libs/github/src/Actions.Workflow/Step.cs
index 2b4759b2..1c5e56e2 100644
--- a/libs/github/src/Actions.Workflow/Step.cs
+++ b/libs/github/src/Actions.Workflow/Step.cs
@@ -1,4 +1,5 @@
-using YamlDotNet.Core.Events;
+using YamlDotNet.Core;
+using YamlDotNet.Core.Events;
 using YamlDotNet.RepresentationModel;
 
 namespace Logicality.GitHub.Actions.Workflow;
@@ -173,8 +174,19 @@ internal void Build(YamlMappingNode node, SequenceStyle sequenceStyle)
 
         if (!string.IsNullOrWhiteSpace(_run))
         {
-            node.Add("run", _run);
-            
+            if (_run.Contains(Environment.NewLine))
+            {
+                var runNode = new YamlScalarNode(_run)
+                {
+                    Style = ScalarStyle.Literal
+                };
+                node.Add("run", runNode);
+            }
+            else
+            {
+                node.Add("run", _run);
+            }
+
             if (!string.IsNullOrWhiteSpace(_workingDirectory))
             {
                 node.Add("working-directory", _workingDirectory);
diff --git a/libs/github/tests/Actions.Workflow.Tests/JobStepTests.cs b/libs/github/tests/Actions.Workflow.Tests/JobStepTests.cs
index d4b2b016..1a39023c 100644
--- a/libs/github/tests/Actions.Workflow.Tests/JobStepTests.cs
+++ b/libs/github/tests/Actions.Workflow.Tests/JobStepTests.cs
@@ -73,7 +73,35 @@ public void Step_Run()
 
         actual.ShouldBe(expected.ReplaceLineEndings());;
     }
-    
+
+    [Fact]
+    public void Step_Run_Scalar()
+    {
+        var actual = new Workflow("workflow")
+            .Job("build")
+            .Step("step")
+            .Run("""
+                 sudo apt-get update
+                 sudo apt-get install -y ca-certificates
+                 """)
+            .Workflow
+            .GetYaml();
+
+        var expected = Workflow.Header + @"
+
+name: workflow
+jobs:
+  build:
+    steps:
+    - id: step
+      run: |-
+        sudo apt-get update
+        sudo apt-get install -y ca-certificates
+";
+
+        actual.ShouldBe(expected.ReplaceLineEndings()); ;
+    }
+
     [Fact]
     public void Step_WorkingDirectory_With_Run()
     {