Skip to content

Commit

Permalink
Add: Update Actions output due to changes in GitHub
Browse files Browse the repository at this point in the history
The Actions outputs are moved from parsing the process output into
name/value pairs within a specific file. The file name is read from an
environment variable.

See https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
for full details.
  • Loading branch information
bjoernricks committed Oct 25, 2022
1 parent 4db4de0 commit 796bdba
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
13 changes: 12 additions & 1 deletion pontos/github/actions/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

import contextlib
import os
from pathlib import Path
from typing import Optional

from .errors import GitHubActionsError


def _to_options(
name: str = None,
Expand Down Expand Up @@ -208,7 +211,15 @@ def output(name: str, value: str):
name: Name of the output variable
value: Value of the output variable
"""
print(f"::set-output name={name}::{value}")
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:
f.write(f"{name}={value}\n")

@staticmethod
def input(name: str, default: Optional[str] = None) -> str:
Expand Down
22 changes: 22 additions & 0 deletions pontos/github/actions/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (C) 2022 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


class GitHubActionsError(Exception):
"""
A GitHub Actions related error has occurred
"""
32 changes: 27 additions & 5 deletions tests/github/actions/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import unittest
from unittest.mock import MagicMock, patch
from unittest.mock import patch

from pontos.github.actions.core import ActionIO, Console
from pontos.github.actions.errors import GitHubActionsError
from pontos.testing import temp_directory


@patch("builtins.print")
Expand Down Expand Up @@ -103,8 +105,28 @@ def test_input(self):
self.assertEqual(ActionIO.input("FOO_BAR"), "2345")
self.assertEqual(ActionIO.input("FoO BaR"), "2345")

@patch("builtins.print")
def test_output(self, print_mock: MagicMock):
ActionIO.output("foo", "bar")
def test_output(self):
with temp_directory() as temp_dir:
file_path = temp_dir / "github.output"

print_mock.assert_called_once_with("::set-output name=foo::bar")
with patch.dict(
"os.environ", {"GITHUB_OUTPUT": str(file_path)}, clear=True
):
ActionIO.output("foo", "bar")
ActionIO.output("lorem", "ipsum")

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

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

def test_output_no_env(self):
with patch.dict("os.environ", {}, clear=True), self.assertRaises(
GitHubActionsError
):
ActionIO.output("foo", "bar")

def test_output_empty_env(self):
with patch.dict(
"os.environ", {"GITHUB_OUTPUT": ""}, clear=True
), self.assertRaises(GitHubActionsError):
ActionIO.output("foo", "bar")

0 comments on commit 796bdba

Please sign in to comment.