Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gha] deprecate set-output for determinator
Browse files Browse the repository at this point in the history
rustielin committed Nov 22, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d2e02f1 commit a1f9d3d
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-minimum-revision.yaml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 18 additions & 1 deletion testsuite/determinator.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
from typing import Generic, Optional, Sequence, Tuple, TypeVar, TypedDict

import click
import os


@dataclass
@@ -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()
@@ -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:
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"]}
@@ -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)

@@ -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)

0 comments on commit a1f9d3d

Please sign in to comment.