From c016f11bbd620458fbcdf3518d7f2b20a55442c7 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 21 Oct 2023 11:32:17 -0400 Subject: [PATCH] fix: remove key error when running on non pull request --- action/main.py | 5 +++-- tests/conftest.py | 17 +++++++++++++++++ tests/unit/test_main.py | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/action/main.py b/action/main.py index bf8aa42..7ed59a1 100644 --- a/action/main.py +++ b/action/main.py @@ -163,10 +163,11 @@ def get_push_event_details() -> dict: push_event = None github_event = get_github_event() - if github_event["pull_request"]: + try: # set sha to the head sha of the pull request github_sha = github_event["pull_request"]["head"]["sha"] - else: + except KeyError: + # not a pull request event github_sha = os.environ["GITHUB_SHA"] for event in response.json(): diff --git a/tests/conftest.py b/tests/conftest.py index e3ba7b6..4663d9f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -127,3 +127,20 @@ def latest_commit(github_token): commit = data[0]['sha'] os.environ['GITHUB_SHA'] = commit return commit + + +@pytest.fixture(scope='function', params=[True, False]) +def github_event_path(request): + # true is original file from GitHub context + # false is dummy file + + original_value = os.environ['GITHUB_EVENT_PATH'] + if request.param: + yield + else: + os.environ['GITHUB_EVENT_PATH'] = os.path.join( + os.path.dirname(os.path.dirname(__file__)), + 'dummy_github_event.json' + ) + yield + os.environ['GITHUB_EVENT_PATH'] = original_value diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index bf06b1d..b967d65 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -47,7 +47,7 @@ def test_get_repo_default_branch(github_token): assert main.get_repo_default_branch() == 'master' -def test_get_push_event_details(latest_commit): +def test_get_push_event_details(github_event_path, latest_commit): assert main.get_push_event_details()