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

fix: remove key error when running on non pull request #6

Merged
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
5 changes: 3 additions & 2 deletions action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down