import json
import os
import re
import subprocess
import sys
import dateutil.parser
github = json.loads(sys.argv[1])
commit = None
timestamp = None
ref = github['ref']
if github['event_name'] == 'pull_request':
pr = github['event']['number']
commit = github['event']['pull_request']['head']['sha']
r = subprocess.run(['git', 'show', '--no-patch', '--format=%s', 'HEAD'],
capture_output=True, text=True, check=True)
m = re.fullmatch('Merge [0-9a-f]+ into ([0-9a-f]+)', r.stdout)
if m:
parent = m.group(1)
else:
parent = github['event']['pull_request']['base']['sha']
elif github['event_name'] == 'push':
commit = github['sha']
parent = github['event']['before']
timestamp = dateutil.parser.isoparse(
github['event']['head_commit']['timestamp']).timestamp()
m = re.search(r'\(#(\d+)\)', github['event']['head_commit']['message'])
if m:
pr = m.group(1)
else:
pr = 0
if commit is not None:
env = os.environ.get('GITHUB_ENV')
assert env
with open(env, 'at') as out:
print(f'GH_EVENT_PR={pr}', file=out)
print(f'GH_EVENT_HASH={commit}', file=out)
print(f'GH_EVENT_PARENT={parent}', file=out)
print(f'GH_EVENT_REF={ref}', file=out)
if timestamp:
print(f'GH_EVENT_TIMESTAMP={timestamp}', file=out)