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

[gha] deprecate set-output for determinator #5682

Merged
merged 1 commit into from
Dec 7, 2022
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
2 changes: 1 addition & 1 deletion .github/workflows/check-minimum-revision.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
FAILED=$?
set -e

echo "::set-output name=FAIL_MERGE_BASE::$FAILED"
echo "FAIL_MERGE_BASE=${FAILED}" >> $GITHUB_OUTPUT
MERGE_BASE="$(git merge-base origin/main ${{ env.GIT_SHA }})"

if [[ $FAILED == 1 ]]; then
Expand Down
19 changes: 18 additions & 1 deletion testsuite/determinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Generic, Optional, Sequence, Tuple, TypeVar, TypedDict

import click
import os


@dataclass
Expand Down Expand Up @@ -75,11 +76,26 @@ def main() -> None:

@dataclass
class GithubOutput:
"""
Represents a Github Output string
It should be written separately to $GITHUB_OUTPUT in the action: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
"""

key: str
value: str

def format(self) -> str:
return f"::set-output name={self.key}::{self.value}"
return f"{self.key}={self.value}"


def write_github_output(output: GithubOutput) -> None:
try:
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
f.write(f"{output.format()}\n")
except KeyError:
raise Exception(
"GITHUB_OUTPUT not set, not writing output. This may be an error with the action setup."
)


@main.command()
Expand All @@ -101,6 +117,7 @@ def changed_files(

if github_output_key:
output = GithubOutput(github_output_key, "true" if verdict.verdict else "false")
write_github_output(output)
print(output.format())
else:
if not verdict.verdict:
Expand Down
16 changes: 14 additions & 2 deletions testsuite/determinator_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import unittest
import tempfile
import os

from click.testing import CliRunner
from determinator import main, ChangedFilesPredicate, ChangedFilesContext


# If this test is run from Github Actions, $GITHUB_OUTPUT will be set
# Otherwise, create a tempfile for it, which exists for the duration of the test
if "GITHUB_OUTPUT" not in os.environ:
temp_github_output = tempfile.NamedTemporaryFile(
prefix="determinator_github_output_"
)
os.environ["GITHUB_OUTPUT"] = temp_github_output.name
print("GITHUB_OUTPUT set to", os.environ["GITHUB_OUTPUT"])


class ChangedFilesPredicateTestCase(unittest.TestCase):
def test_changed_files_passes(self) -> None:
context: ChangedFilesContext = {"changed_files": ["asdf"]}
Expand Down Expand Up @@ -33,7 +45,7 @@ def test_determinator_from_github(self) -> None:
)
self.assertEqual(
result.output,
"FAILED because Matched files: []\n" "::set-output name=BANANA::false\n",
"FAILED because Matched files: []\n" "BANANA=false\n",
)
self.assertEqual(result.exit_code, 0)

Expand All @@ -55,6 +67,6 @@ def test_determinator_from_github_passes(self) -> None:
result.output,
"PASSED because Matched files: "
"['testsuite/fixtures/helm/banana.ts']\n"
"::set-output name=BANANA::true\n",
"BANANA=true\n",
)
self.assertEqual(result.exit_code, 0)