-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.py
275 lines (234 loc) · 10.4 KB
/
script.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# script to collect commit message
#!/usr/bin/python
# from simple_term_menu import TerminalMenu
import requests
# import sys
import yaml
# import configparser
import datetime
import os
import re
from pathlib import Path
class Dumper(yaml.Dumper):
def increase_indent(self, flow=False, *args, **kwargs):
return super().increase_indent(flow=flow, indentless=False)
def parse_commit_msg(commit_str):
# return a list [commit_type, commit_msg]
commit_line=re.sub(r'''['\n]+''', ' ', commit_str)
commit_match = re.match(r'''^['\s]*\[?([\w\s-]+)\s*\]?:?\s*(.+)['\s]*$''', commit_line)
if commit_match:
commit_groups = commit_match.groups()
commit_type = commit_groups[0]
commit_msg = commit_groups[1]
else:
commit_type = "trivial"
commit_msg = commit_line
return commit_type, commit_msg
def parse_commit_type(commit_type):
if re.search("bug", commit_type.lower()):
res_type = "bugfixes"
elif re.search("major", commit_type.lower()):
res_type = "major_changes"
elif re.search("minor", commit_type.lower()):
res_type = "minor_changes"
elif re.search("ignore", commit_type.lower()):
res_type = "ignore_changes"
else:
res_type = "trivial"
return res_type
def parse_commit_content(commit_msg):
res_msg = commit_msg
if re.match(r'''^[a-z]''', commit_msg):
res_msg = commit_msg[0].upper() + commit_msg[1:]
return res_msg
def annotation_unsupported_gen(data):
for k, v in data.get('classes', {}).items():
if v.get('isConfigurable', False):
if v.get('properties', {}).get('annotation', None) is None:
yield k
# config = configparser.ConfigParser()
# # add condition
# config.read(sys.argv[1])
# Use var get from github action env var
# repo_url replace by env var
# repo_url = config['settings']['repo_url']
repo = os.getenv('GITHUB_REPOSITORY')
repo_url = "https://api.github.com/repos/" + repo
github_token = os.getenv('GITHUB_TOKEN')
pr_name = "release_pr"
# parse var repo to get the collection name
# collection = config['settings']['collection_name']
# collection_name = 'ansible-' + collection
collection_name = repo.split("/")[1]
product_name = collection_name.split("-")[1]
# target version can be proposed while generating changlog file
# target_version = config['settings']['target_version']
latest_version = None
for i in range(10):
latest = requests.get('{0}/releases/latest'.format(repo_url))
latest_version = latest.json().get('tag_name')
if latest_version is not None:
break
print("The latest release: " + latest_version)
url = '{0}/compare/{1}...master'.format(repo_url, latest_version)
r = requests.get(url)
result = r.json()
commits = result.get('commits')
print("The repo is " + repo)
print("The product name is " + product_name)
print("The repo_url is " + repo_url)
print("The compare url is " + url)
bug = []
minor = []
major = []
trivial = []
# cd to directory of the collection
# directory is an absolute path
# collection directory in github action ubuntu
# directory = config['settings']['directory']
directory = "./collection"
script_dir = "./release_script"
# remote branch is defined in github action as origin
# remote_branch = config['settings']['remote_branch']
remote_branch = "origin"
change_log_path = '{0}/changelogs/changelog.yaml'.format(directory)
# options = ["Update changelog and create a releasing PR", "Build collection and release"]
# terminal_menu = TerminalMenu(options)
# menu_entry_index = terminal_menu.show()
# if menu_entry_index == 0:
# 1. Create a new branch release_{target_version} and get latest version
# os.system("cd {0} && git checkout master && git branch -D release_{1} && git checkout -b release_{2}".format(directory, target_version, target_version))
os.system("chmod +x {0}/create_branch.sh && {1}/create_branch.sh {2} {3} {4}".format(script_dir, script_dir, directory, remote_branch, pr_name))
# 2. Update changelog.yml
# put all commit msg without prefix into trivial list
for commit in commits:
# detect whether the commit msg is multiple line, if so, split into different commit msg
# rm single quote
# use regex to match
commit_message = commit.get('commit').get('message')
print("Commit Message: " + commit_message)
commit_type, commit_msg = parse_commit_msg(commit_message)
parsed_commit_type = parse_commit_type(commit_type)
parsed_commit_content = parse_commit_content(commit_msg)
if parsed_commit_type == 'bugfixes':
bug.append(parsed_commit_content)
elif parsed_commit_type == 'minor_changes':
minor.append(parsed_commit_content)
elif parsed_commit_type == 'major_changes':
major.append(parsed_commit_content)
elif parsed_commit_type == 'ignore_changes':
pass
else:
trivial.append(parsed_commit_content)
print("The trivial commits are: " + str(trivial))
print("The bug commits are: " + str(bug))
print("The minor commits are: " + str(minor))
print("The major commits are: " + str(major))
release_date = datetime.date.today()
# propose the version number target version
latest_version_split = latest_version[1:].split(".")
if major:
latest_version_split[0] = str(int(latest_version_split[0])+1)
latest_version_split[1] = "0"
latest_version_split[2] = "0"
elif minor:
latest_version_split[1] = str(int(latest_version_split[1])+1)
latest_version_split[2] = "0"
else:
latest_version_split[2] = str(int(latest_version_split[2])+1)
target_version = ".".join(latest_version_split)
change_log = dict(
changes = dict(
release_summary = 'Release v{0} of the ``{1}`` collection on {2}.\nThis changelog describes all changes made to the modules and plugins included in this collection since {3}.\n'.format(target_version, collection_name, release_date, latest_version)
),
release_date = str(release_date)
)
if len(bug) > 0:
change_log['changes']['bugfixes'] = bug
if len(minor) > 0:
change_log['changes']['minor_changes'] = minor
if len(major) > 0:
change_log['changes']['major_changes'] = major
if len(trivial):
change_log['changes']['trivial'] = trivial
print("Changelog: " + str(change_log))
with open(change_log_path) as f:
dataMap = yaml.safe_load(f)
dataMap['releases'][target_version] = change_log
with open(change_log_path, 'w') as f:
yaml.dump(dataMap, f)
# edit changelog.yaml as we want
# os.system("vim {0}".format(change_log_path))
# 3. Update galaxy.yml
galaxy_path = '{0}/galaxy.yml'.format(directory)
with open(galaxy_path, 'r') as f:
data = f.read().splitlines(True)
for i, line in enumerate(data):
if line.startswith("version"):
data[i] = "version: " + target_version + "\n"
with open(galaxy_path, 'w') as f:
f.writelines(data)
# 4. Update meta/runtime.yml
meta_runtime_path = '{0}/meta/runtime.yml'.format(directory)
modules_path = '{0}/plugins/modules/'.format(directory)
action_group = [f[:-3] for f in os.listdir(modules_path) if os.path.isfile(os.path.join(modules_path, f)) and f != '__init__.py']
action_group.sort()
print("The action_group modules are: " + str(action_group))
groups = {}
for module in action_group:
module_group = module.split('_')[0]
if module_group in groups:
groups[module_group].append(module)
else:
groups[module_group] = [module]
with open(meta_runtime_path) as f:
dataRuntimeMap = yaml.safe_load(f)
dataRuntimeMap['action_groups'] = {}
if len(groups) > 1:
groups['all'] = action_group
dataRuntimeMap['action_groups'] = groups
else:
dataRuntimeMap['action_groups']['all'] = action_group
with open(meta_runtime_path, 'w') as f:
yaml.dump(dataRuntimeMap, f, sort_keys=False, default_flow_style=False, explicit_start=True, Dumper=Dumper)
# 5. Update annotation unsupported classes
annotation_unsupported_file = Path('{0}/plugins/module_utils/annotation_unsupported.py'.format(directory))
if annotation_unsupported_file.exists():
url = "https://pubhub.devnetcloud.com/media/model-doc-latest/docs/doc/jsonmeta/aci-meta.json"
apic = os.getenv("APIC")
if apic:
url = "https://{0}/acimeta/aci-meta.json".format(apic)
data = requests.get(url, verify=False, allow_redirects=True).json()
with annotation_unsupported_file.open(mode="wt") as f:
f.write('# Code generated by release_script GitHub action; DO NOT EDIT MANUALLY.\n')
f.write("ANNOTATION_UNSUPPORTED = [\n")
for c in annotation_unsupported_gen(data):
f.write(f' "{c}",\n')
f.write("]\n")
# 6. Update CHANGELOG.rst & galaxy.yml and push a releasing PR
os.system("chmod +x {0}/update_changelog.sh && {1}/update_changelog.sh {2} {3} {4}".format(script_dir, script_dir, directory, pr_name, target_version))
# else:
# # get latest code after PR merged
# os.system("chmod +x get_code.sh && ./get_code.sh {0} {1} {2} {3}".format(directory, config['settings']['collection_name'], target_version, remote_branch))
# # 5. Write release body content accoding to changelog.yml
# with open(change_log_path) as f:
# dataMap = yaml.safe_load(f)
# change_content = dataMap['releases'][target_version]['changes']
# with open("{0}/release_content.txt".format(directory), 'w') as f:
# f.write("1. New release v{0}\n2. Changes\n".format(target_version))
# yaml.dump(change_content, f)
# f.write("3. Detailed changelog: https://github.com/xinyuezhao/ansible-mso/compare/{0}...v{1}".format(latest_version, target_version))
# # 6. Public release in github release
# print("releasing in github release")
# os.system("chmod +x release_galaxy.sh && ./release_galaxy.sh {0} {1} {2}".format(directory, config['settings']['collection_name'], target_version))
# # 7. Publish release in Galaxy
# # ansible-galaxy collection publish xinyuezhao18-mso-1.4.0.tar.gz --api-key=6807bdc1da2e0b2a3f6a132a6daf41e43004a0ec
# # by default distribuiting server will be ansible galaxy
# # print("releasing in ansible galaxy")
# # galaxy_key = config['settings']['galaxy_key']
# # galaxy_ns = config['settings']['galaxy_namespace']
# # galaxy_path = "{0}-{1}-{2}.tar.gz".format(galaxy_ns, collection, target_version)
# # os.system("chmod +x galaxy.sh && ./galaxy.sh {0} {1}".format(galaxy_path, galaxy_key))
# # TODO: add an ansible.cfg file defining server&api-key
# # 8. Publish release in RedHat automation hub
# # ansible-galaxy collection publish xinyuezhao18-mso-1.4.0.tar.gz --server --api-key=xxx