This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate_config.py
73 lines (58 loc) · 2.39 KB
/
generate_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import yaml
import argparse
from github import Github
import datetime
from utils import replace_relative_paths, DATETIME_FORMAT
def get_yaml_content(repo, release):
content = {
'version': release.tag_name,
'name': repo.name,
'displayName': repo.name,
'createdAt': release.created_at.strftime(DATETIME_FORMAT),
'description': repo.description,
'digest': datetime.datetime.now().strftime(DATETIME_FORMAT),
'license': repo.get_license().license.spdx_id,
'homeURL': 'https://keptn.sh/docs/integrations/',
'keywords': ['keptn', repo.owner.login.split('-')[1]],
'links': [{
'name': 'Source',
'url': f'https://github.com/{repo.owner.login}/{repo.name}/tree/{release.target_commitish}'
}],
'annotations': {
'keptn/org': repo.owner.login.split('-')[1]
}
}
return content
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--repository', type=str,
required=True, help='Name of Github repository')
parser.add_argument('-v', '--version', type=str,
default='', help='Version (default=latest)')
args = parser.parse_args()
g = Github()
repo = g.get_repo(args.repository)
if args.version == '':
if repo.get_releases().totalCount != 0:
release = repo.get_latest_release()
else:
raise Exception('No release available')
else:
try:
release = repo.get_release(args.version)
except:
raise Exception(f'Release {args.version} not found.')
folder_path = os.path.join(repo.name, release.tag_name)
os.makedirs(repo.name, exist_ok=True)
os.makedirs(folder_path, exist_ok=True)
branch_name = release.target_commitish
# https://github.com/PyGithub/PyGithub/issues/1157#issuecomment-507498931
readme = repo.get_contents(
'README.md', ref=branch_name).decoded_content.decode('utf-8')
# Replace relative image paths with absolute paths pointing to the github repo
readme = replace_relative_paths(readme, args.repository, branch_name)
with open(os.path.join(folder_path, 'README.md'), 'w') as f:
f.write(readme)
with open(os.path.join(folder_path, 'artifacthub-pkg.yml'), 'w') as f:
yaml.dump(get_yaml_content(repo, release), f, sort_keys=False)