Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: multiline_output for github actions #983

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pontos/github/actions/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#

import os
import uuid
from contextlib import contextmanager
from io import TextIOWrapper
from pathlib import Path
Expand Down Expand Up @@ -287,6 +288,37 @@ def output(name: str, value: SupportsStr):
with Path(output_filename).open("a", encoding="utf8") as f:
f.write(f"{name}={value}\n")

@staticmethod
def multiline_output(name: str, value: SupportsStr):
"""
Set an multiline action output

An action output can be consumed by another job

Example:
.. code-block:: python

from pontos.github.actions import ActionIO

ActionIO.output("foo", "bar")

Args:
name: Name of the output variable
value: Value of the output variable
"""
output_filename = os.environ.get("GITHUB_OUTPUT")
if not output_filename:
raise GitHubActionsError(
"GITHUB_OUTPUT environment variable not set. Can't write "
"action output."
)

with Path(output_filename).open("a", encoding="utf8") as f:
delimiter = uuid.uuid1()
f.write(f"{name}<<{delimiter}")
f.write(f"{value}")
f.write(str(delimiter))

@staticmethod
def input(name: str, default: Optional[str] = None) -> Optional[str]:
"""
Expand Down
21 changes: 21 additions & 0 deletions tests/github/actions/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ def test_output(self):

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

@patch("uuid.uuid1")
def test_multiline_output(self, uuid_mock):
deadbeef = "deadbeef"
name = "foo"
ml_string = """bar
baz
boing"""
expected_output = f"{name}<<{deadbeef}{ml_string}{deadbeef}"
uuid_mock.return_value = deadbeef
with temp_directory() as temp_dir:
file_path = temp_dir / "github.output"

with patch.dict(
"os.environ", {"GITHUB_OUTPUT": str(file_path)}, clear=True
):
ActionIO.multiline_output("foo", ml_string)

output = file_path.read_text(encoding="utf8")

self.assertEqual(output, expected_output)

@patch.dict("os.environ", {}, clear=True)
def test_output_no_env(self):
with self.assertRaises(GitHubActionsError):
Expand Down
Loading