#!/usr/bin/env python3 """Read the current main head without placing an access token in arguments/logs.""" import json import os import sys import urllib.error import urllib.request url = os.environ['GITEA_SERVER_URL'].rstrip('/') + '/api/v1/repos/' + os.environ['CI_REPOSITORY'] + '/branches/main' request = urllib.request.Request(url, headers={'Authorization': 'token ' + os.environ['REGISTRY_TOKEN']}) try: with urllib.request.urlopen(request, timeout=30) as response: print(json.load(response)['commit']['id']) except (urllib.error.URLError, KeyError, ValueError) as error: print(f'Cannot verify current main head ({type(error).__name__})', file=sys.stderr) sys.exit(1)