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

Let GitHub Actions commit range detection crash gracefully on --force pushes #91

Merged
merged 1 commit into from
Sep 11, 2020
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
7 changes: 6 additions & 1 deletion hubploy/commitrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import json

from hubploy.utils import is_commit

def get_commit_range():
"""
Expand Down Expand Up @@ -36,4 +37,8 @@ def get_commit_range_github():

# push ref: https://developer.github.com/webhooks/event-payloads/#push
if 'before' in event:
return f"{event['before']}...HEAD"
if not is_commit(event['before']):
print(f"A GitHub Actions environment was detected, but the constructed commit range ({event['before']}...HEAD) was invalid. This can happen if a git push --force has been run.")
return None
else:
return f"{event['before']}...HEAD"
8 changes: 8 additions & 0 deletions hubploy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,11 @@ def path_touched(*paths, commit_range):
return subprocess.check_output([
'git', 'diff', '--name-only', commit_range, '--', *paths
]).decode('utf-8').strip() != ''


def is_commit(ref):
try:
subprocess.check_call(['git', 'cat-file', 'commit', ref])
return True
except subprocess.CalledProcessError:
return False