2018-11-04 02:35:33 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-11-04 10:26:55 +01:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
2018-11-04 02:35:33 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import urllib.request
|
|
|
|
import http.client
|
|
|
|
from urllib.error import HTTPError
|
|
|
|
|
|
|
|
|
|
|
|
def parse_changelog(tag: str) -> Optional[str]:
|
|
|
|
release_changelog: str = ''
|
|
|
|
with open('CHANGELOG.md', 'r') as f:
|
|
|
|
in_target: bool = False
|
|
|
|
done: bool = False
|
|
|
|
for line in f.readlines():
|
|
|
|
if in_target:
|
|
|
|
if f'<!-- END RELEASE {tag} -->' in line:
|
|
|
|
done = True
|
|
|
|
break
|
|
|
|
release_changelog += line
|
|
|
|
elif f'<!-- BEGIN RELEASE {tag} -->' in line:
|
|
|
|
in_target = True
|
|
|
|
continue
|
|
|
|
if not done:
|
|
|
|
return None
|
|
|
|
return release_changelog
|
|
|
|
|
|
|
|
|
2018-11-04 10:26:55 +01:00
|
|
|
def fetch_job_ids(project_id: int, pipeline_id: int, api_token: str) -> Dict[str, str]:
|
|
|
|
url: str = f'https://gitlab.com/api/v4/projects/{project_id}/pipelines/{pipeline_id}/jobs'
|
|
|
|
headers: Dict[str, str] = {
|
|
|
|
'Private-Token': api_token
|
|
|
|
}
|
|
|
|
req = urllib.request.Request(url, headers=headers)
|
|
|
|
try:
|
|
|
|
resp: http.client.HTTPResponse = urllib.request.urlopen(req)
|
|
|
|
except HTTPError as e:
|
|
|
|
print(e.read().decode())
|
|
|
|
sys.exit(1)
|
|
|
|
resp_data: bytes = resp.read()
|
2018-11-04 10:43:50 +01:00
|
|
|
joblist: List[Dict[str, Any]] = json.loads(resp_data.decode())
|
2018-11-04 10:26:55 +01:00
|
|
|
|
|
|
|
jobidmap: Dict[str, str] = {}
|
|
|
|
for job in joblist:
|
|
|
|
name: str = job['name']
|
2018-11-04 10:43:50 +01:00
|
|
|
job_id: str = job['id']
|
|
|
|
jobidmap[name] = job_id
|
2018-11-04 10:26:55 +01:00
|
|
|
return jobidmap
|
|
|
|
|
|
|
|
|
2018-11-04 02:35:33 +01:00
|
|
|
def fetch_single_shafile(url: str) -> str:
|
|
|
|
req = urllib.request.Request(url)
|
|
|
|
try:
|
|
|
|
resp: http.client.HTTPResponse = urllib.request.urlopen(req)
|
|
|
|
except HTTPError as e:
|
|
|
|
print(e.read().decode())
|
|
|
|
sys.exit(1)
|
|
|
|
resp_data: bytes = resp.readline()
|
|
|
|
shafile: str = resp_data.decode()
|
|
|
|
filename: str = shafile.strip().split(' ')[-1].strip()
|
|
|
|
return filename
|
|
|
|
|
|
|
|
|
2018-11-04 10:26:55 +01:00
|
|
|
def fetch_wheel_url(base_url: str, job_ids: Dict[str, str]) -> Optional[Tuple[str, str]]:
|
|
|
|
mybase: str = f'{base_url}/jobs/{job_ids["build_wheel"]}/artifacts/raw'
|
|
|
|
wheel_sha_url: str = f'{mybase}/dist/SHA256SUMS'
|
2018-11-04 02:35:33 +01:00
|
|
|
wheel_filename: str = fetch_single_shafile(wheel_sha_url)
|
2018-11-04 10:26:55 +01:00
|
|
|
wheel_url: str = f'{mybase}/dist/{wheel_filename}'
|
2018-11-04 02:35:33 +01:00
|
|
|
return wheel_url, wheel_sha_url
|
|
|
|
|
|
|
|
|
2018-11-04 10:26:55 +01:00
|
|
|
def fetch_debian_url(base_url: str, job_ids: Dict[str, str]) -> Optional[Tuple[str, str]]:
|
|
|
|
mybase: str = f'{base_url}/jobs/{job_ids["build_debian"]}/artifacts/raw'
|
|
|
|
debian_sha_url: str = f'{mybase}/package/debian/SHA256SUMS'
|
2018-11-04 02:35:33 +01:00
|
|
|
debian_filename: str = fetch_single_shafile(debian_sha_url)
|
2018-11-04 10:26:55 +01:00
|
|
|
debian_url: str = f'{mybase}/package/debian/{debian_filename}'
|
2018-11-04 02:35:33 +01:00
|
|
|
return debian_url, debian_sha_url
|
|
|
|
|
|
|
|
|
2018-11-04 10:26:55 +01:00
|
|
|
def fetch_arch_url(base_url: str, job_ids: Dict[str, str]) -> Optional[Tuple[str, str]]:
|
|
|
|
mybase: str = f'{base_url}/jobs/{job_ids["build_archlinux"]}/artifacts/raw'
|
|
|
|
arch_sha_url: str = f'{mybase}/package/archlinux/SHA256SUMS'
|
2018-11-04 02:35:33 +01:00
|
|
|
arch_filename: str = fetch_single_shafile(arch_sha_url)
|
2018-11-04 10:26:55 +01:00
|
|
|
arch_url: str = f'{mybase}/package/archlinux/{arch_filename}'
|
2018-11-04 02:35:33 +01:00
|
|
|
return arch_url, arch_sha_url
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
api_token: Optional[str] = os.getenv('GITLAB_API_TOKEN')
|
|
|
|
release_tag: Optional[str] = os.getenv('CI_COMMIT_TAG')
|
|
|
|
project_name: Optional[str] = os.getenv('CI_PROJECT_PATH')
|
|
|
|
project_id: Optional[str] = os.getenv('CI_PROJECT_ID')
|
2018-11-04 10:26:55 +01:00
|
|
|
pipeline_id: Optional[str] = os.getenv('CI_PIPELINE_ID')
|
2018-11-04 02:35:33 +01:00
|
|
|
if api_token is None:
|
|
|
|
print('GITLAB_API_TOKEN is not set.', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
if release_tag is None:
|
|
|
|
print('CI_COMMIT_TAG is not set.', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
if project_name is None:
|
|
|
|
print('CI_PROJECT_PATH is not set.', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
if project_id is None:
|
|
|
|
print('CI_PROJECT_ID is not set.', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2018-11-04 10:26:55 +01:00
|
|
|
if pipeline_id is None:
|
|
|
|
print('CI_PIPELINE_ID is not set.', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2018-11-04 02:35:33 +01:00
|
|
|
|
|
|
|
changelog: Optional[str] = parse_changelog(release_tag)
|
|
|
|
if changelog is None:
|
|
|
|
print('Changelog could not be parsed.', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2018-11-04 10:26:55 +01:00
|
|
|
job_ids: Dict[str, str] = fetch_job_ids(project_id, pipeline_id, api_token)
|
|
|
|
|
|
|
|
base_url: str = f'https://gitlab.com/{project_name}/-'
|
2018-11-04 02:35:33 +01:00
|
|
|
|
2018-11-04 10:26:55 +01:00
|
|
|
wheel_url, wheel_sha_url = fetch_wheel_url(base_url, job_ids)
|
|
|
|
debian_url, debian_sha_url = fetch_wheel_url(base_url, job_ids)
|
|
|
|
arch_url, arch_sha_url = fetch_wheel_url(base_url, job_ids)
|
2018-11-04 02:35:33 +01:00
|
|
|
|
|
|
|
augmented_changelog = f'''{changelog.strip()}
|
|
|
|
|
|
|
|
### Download
|
|
|
|
|
|
|
|
- [Python Wheel]({wheel_url}) ([sha256]({wheel_sha_url}))
|
|
|
|
- [Debian Package]({debian_url}) ([sha256]({debian_sha_url}))
|
2018-11-04 02:51:57 +01:00
|
|
|
- [Arch Linux Package]({arch_url}) ([sha256]({arch_sha_url}))
|
|
|
|
- Docker image: registry.gitlab.com/{project_name}:{release_tag}'''
|
2018-11-04 02:35:33 +01:00
|
|
|
|
2018-11-04 10:43:50 +01:00
|
|
|
post_body: str = json.dumps({'description': augmented_changelog})
|
2018-11-04 02:35:33 +01:00
|
|
|
|
|
|
|
gitlab_release_api_url: str = \
|
|
|
|
f'https://gitlab.com/api/v4/projects/{project_id}/repository/tags/{release_tag}/release'
|
|
|
|
headers: Dict[str, str] = {
|
2018-11-04 10:43:50 +01:00
|
|
|
'Private-Token': api_token,
|
|
|
|
'Content-Type': 'application/json; charset=utf-8'
|
2018-11-04 02:35:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
request = urllib.request.Request(
|
|
|
|
gitlab_release_api_url,
|
|
|
|
post_body.encode('utf-8'),
|
|
|
|
headers=headers,
|
|
|
|
method='POST'
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
response: http.client.HTTPResponse = urllib.request.urlopen(request)
|
|
|
|
except HTTPError as e:
|
|
|
|
print(e.read().decode())
|
|
|
|
sys.exit(1)
|
|
|
|
response_bytes: bytes = response.read()
|
|
|
|
response_str: str = response_bytes.decode()
|
2018-11-04 10:43:50 +01:00
|
|
|
response_data: Dict[str, Any] = json.loads(response_str)
|
2018-11-04 02:35:33 +01:00
|
|
|
|
|
|
|
if response_data['tag_name'] != release_tag:
|
|
|
|
print('Something went wrong...', file=sys.stderr)
|
|
|
|
print(response_str, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
print(response_data['description'])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|