Skip to content

Commit f59fd50

Browse files
y0urselfgreenbonebot
authored andcommitted
Add: multiline_output for github actions
1 parent 5b44838 commit f59fd50

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

pontos/github/actions/core.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55

66
import os
7+
import uuid
78
from contextlib import contextmanager
89
from io import TextIOWrapper
910
from pathlib import Path
@@ -287,6 +288,37 @@ def output(name: str, value: SupportsStr):
287288
with Path(output_filename).open("a", encoding="utf8") as f:
288289
f.write(f"{name}={value}\n")
289290

291+
@staticmethod
292+
def multiline_output(name: str, value: SupportsStr):
293+
"""
294+
Set an multiline action output
295+
296+
An action output can be consumed by another job
297+
298+
Example:
299+
.. code-block:: python
300+
301+
from pontos.github.actions import ActionIO
302+
303+
ActionIO.output("foo", "bar")
304+
305+
Args:
306+
name: Name of the output variable
307+
value: Value of the output variable
308+
"""
309+
output_filename = os.environ.get("GITHUB_OUTPUT")
310+
if not output_filename:
311+
raise GitHubActionsError(
312+
"GITHUB_OUTPUT environment variable not set. Can't write "
313+
"action output."
314+
)
315+
316+
with Path(output_filename).open("a", encoding="utf8") as f:
317+
delimiter = uuid.uuid1()
318+
f.write(f"{name}<<{delimiter}")
319+
f.write(f"{value}")
320+
f.write(str(delimiter))
321+
290322
@staticmethod
291323
def input(name: str, default: Optional[str] = None) -> Optional[str]:
292324
"""

tests/github/actions/test_core.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ def test_output(self):
107107

108108
self.assertEqual(output, "foo=bar\nlorem=ipsum\n")
109109

110+
@patch("uuid.uuid1")
111+
def test_multiline_output(self, uuid_mock):
112+
deadbeef = "deadbeef"
113+
name = "foo"
114+
ml_string = """bar
115+
baz
116+
boing"""
117+
expected_output = f"{name}<<{deadbeef}{ml_string}{deadbeef}"
118+
uuid_mock.return_value = deadbeef
119+
with temp_directory() as temp_dir:
120+
file_path = temp_dir / "github.output"
121+
122+
with patch.dict(
123+
"os.environ", {"GITHUB_OUTPUT": str(file_path)}, clear=True
124+
):
125+
ActionIO.multiline_output("foo", ml_string)
126+
127+
output = file_path.read_text(encoding="utf8")
128+
129+
self.assertEqual(output, expected_output)
130+
110131
@patch.dict("os.environ", {}, clear=True)
111132
def test_output_no_env(self):
112133
with self.assertRaises(GitHubActionsError):

0 commit comments

Comments
 (0)