Files
Heimdall/scripts/ci/release-notes.py
T

48 lines
1.8 KiB
Python
Raw Normal View History

"""Generate release notes from pull requests merged since the previous version tag."""
import json
import os
from pathlib import Path
import urllib.error
import urllib.parse
import urllib.request
base = os.environ['BUILD_SERVER_URL'].rstrip('/') + '/api/v1/repos/' + os.environ['BUILD_REPOSITORY']
token = os.environ['GITEA_TOKEN']
before = os.environ['BEFORE_SHA']
current = os.environ['CURRENT_SHA']
def get(path):
request = urllib.request.Request(base + path, headers={
'Authorization': 'token ' + token,
'Accept': 'application/json',
})
try:
with urllib.request.urlopen(request, timeout=30) as response:
return json.load(response)
except urllib.error.HTTPError as error:
if error.code == 404:
return None
raise SystemExit(f'Gitea release notes API failed: HTTP {error.code}') from None
comparison = get('/compare/' + urllib.parse.quote(before, safe='') + '...' + urllib.parse.quote(current, safe=''))
if comparison is None:
raise SystemExit('Gitea could not compare the previous and current main commits')
pulls = {}
for commit in comparison.get('commits', []):
pull = get('/commits/' + urllib.parse.quote(commit['sha'], safe='') + '/pull')
if pull and pull.get('merged') and pull.get('number'):
pulls[pull['number']] = pull
lines = []
for number, pull in sorted(pulls.items()):
title = pull.get('title') or f'Pull request #{number}'
url = pull.get('html_url') or f"{base.removesuffix('/api/v1/repos/' + os.environ['BUILD_REPOSITORY'])}/pulls/{number}"
lines.append(f'- {title} ([#{number}]({url}))')
if not lines:
lines = [f'- Changes merged in commit `{current}`.']
Path('artifacts/release-notes.md').write_text('\n'.join(lines) + '\n')
print(f'Generated release notes for {len(pulls)} merged pull request(s).')