-
Notifications
You must be signed in to change notification settings - Fork 107
/
update_templating_versions.py
310 lines (268 loc) · 9.78 KB
/
update_templating_versions.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python3
"""
This script checks for new GitHub releases and updates YAML template with them.
"""
import argparse
import io
import logging
import os
import re
import sys
from urllib.parse import urlparse
try:
from github import Github
except ImportError:
sys.exit("pygithub python module is required for this script.")
try:
import ruamel.yaml
yaml = ruamel.yaml.YAML()
except ImportError:
sys.exit("yuamel.yaml python module is required for this script.")
try:
from packaging import version
except ImportError:
sys.exit("packaging python module is required for this script.")
def getLatestGHReleaseVersion(github_token, exporter_name, url):
"""
Gets latest GitHub release for specified repository
"""
github_repo_path = urlparse(url).path.strip("/")
g = Github(github_token)
repo = g.get_repo(github_repo_path)
releases = repo.get_releases()
for release in releases:
if not release.prerelease and not release.draft:
github_latest_release_tag = release.tag_name
github_latest_release_body = release.body
github_latest_release_url = release.html_url
break
logging.debug(
"Latest %s release tag: %s" % (exporter_name, github_latest_release_tag)
)
github_latest_release_version = re.search(
r"\d+\.\d+(\.\d+)?", github_latest_release_tag
)
if not github_latest_release_version.group():
logging.error(
"Could not get release version for %s from latest release tag: %s"
% (exporter_name, github_latest_release_tag)
)
sys.exit(1)
else:
logging.info(
"Latest %s release version: %s"
% (exporter_name, github_latest_release_version.group())
)
return (
github_latest_release_version.group(),
github_latest_release_body,
github_latest_release_url,
)
def getGHBranches(github_token):
"""
Gets existing branches to check if PR is already present
"""
github_repo_path = "lest/prometheus-rpm"
logging.debug("Getting list of existing GitHub branches")
g = Github(github_token)
branches = []
for branch in g.get_repo(github_repo_path).get_branches():
branches.append(branch.name)
return branches
def updateGHTemplate(
github_token, filename, branch, message, template, release_notes, url, pr_labels
):
"""
Creates PR with updated version
"""
github_repo_path = "lest/prometheus-rpm"
g = Github(github_token)
repo = g.get_repo(github_repo_path)
# create new branch:
sb = repo.get_branch("master")
logging.debug("Creating new GitHub branch: %s" % branch)
repo.create_git_ref(ref="refs/heads/" + branch, sha=sb.commit.sha)
# format PR body:
pr_body = "%s\nRelease notes:\n```\n%s\n```" % (url, release_notes)
logging.debug(pr_body)
# format YAML file:
formatted_template = io.BytesIO()
yaml.explicit_start = True
yaml.indent(sequence=4, offset=2)
yaml.width = 500
yaml.dump(template, formatted_template)
# get existing file checksum:
file = repo.get_contents(filename)
# update template in new branch:
logging.debug("Updating %s with new values" % filename)
repo.update_file(
filename, message, formatted_template.getvalue(), file.sha, branch=branch
)
# create new pull request:
logging.debug("Creating new pull request")
pr = repo.create_pull(title=message, body=pr_body, head=branch, base="master")
logging.info("Pull request #%u created" % pr.number)
# add PR labels:
if len(pr_labels) > 0:
logging.debug("Adding PR labels: %s" % ", ".join(pr_labels))
pr.set_labels(*pr_labels)
if __name__ == "__main__":
env_github_token = os.environ.get("GH_TOKEN")
env_template_config = os.environ.get("TEMPLATE_CONFIG_FILE", "./templating.yaml")
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument(
"-l",
"--loglevel",
default="info",
type=str.lower,
choices=["error", "warning", "info", "debug"],
help="Set log level to {error,warning,info|normal,debug}",
)
parser.add_argument(
"--templates",
metavar="N",
type=str,
nargs="+",
default="all",
help="A list of templates to generate",
)
parser.add_argument(
"--template-config",
metavar="file",
type=str,
default=env_template_config,
help="The configuration file to generate templates with",
)
parser.add_argument(
"--github-token", type=str, default=env_github_token, help="GitHub API token"
)
parser.add_argument(
"--check",
action="store_true",
default=False,
help="Check mode, does not push changes to GitHub",
)
args = parser.parse_args()
templates = args.templates
template_config = args.template_config
github_token = args.github_token
check_mode = args.check
# logging settings:
if args.loglevel == "info":
logging.basicConfig(level=logging.INFO)
elif args.loglevel == "warning":
logging.basicConfig(level=logging.WARNING)
elif args.loglevel == "debug":
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.ERROR)
# check if github token is specified
if not github_token:
if check_mode:
logging.warning(
"GH_TOKEN environment variable is not set, you might hit GitHub API rate limits soon"
)
else:
logging.error(
"GH_TOKEN environment variable is not set and --github-token argument is not specified, quitting"
)
sys.exit(1)
# load existing template yaml file:
try:
with open(template_config, "r") as tc:
config = yaml.load(tc)
except IOError:
logging.error("Couldn't open config file: %s" % template_config)
sys.exit(1)
else:
logging.debug("Configuration file loaded successfully: %s" % template_config)
# work out which templates we are calculating:
if templates == "all":
work = config["packages"]
else:
work = {}
for t in templates:
work[t] = config["packages"][t]
# get existing GitHub branches:
existing_github_branches = getGHBranches(github_token)
for exporter_name, exporter_config in work.items():
# first we need to work out the context for this exporter:
context = exporter_config["context"]["static"]
exporter_url = context["URL"]
exporter_current_version = str(context["version"])
logging.info(
"Checking updates for %s" % (exporter_name.upper().replace("_", " "))
)
# get latest exporter release:
(
exporter_latest_version,
exporter_release_notes,
exporter_url,
) = getLatestGHReleaseVersion(github_token, exporter_name, exporter_url)
logging.info(
"%s: current version: %s, latest version %s"
% (exporter_name, exporter_current_version, exporter_latest_version)
)
# check if we are already on latest version:
if version.parse(exporter_latest_version) > version.parse(
exporter_current_version
):
# add labels depending on version change:
pr_labels = ["enhancement"]
if (
version.parse(exporter_latest_version).major
> version.parse(exporter_current_version).major
):
pr_labels.append("major update")
elif (
version.parse(exporter_latest_version).minor
> version.parse(exporter_current_version).minor
):
pr_labels.append("minor update")
elif (
version.parse(exporter_latest_version).micro
> version.parse(exporter_current_version).micro
):
pr_labels.append("patch update")
github_branch = "%s_%s" % (
exporter_name,
exporter_latest_version.replace(".", "_"),
)
# check if there is already existing PR for this update:
if github_branch not in existing_github_branches:
logging.info(
"Updating %s: %s -> %s"
% (exporter_name, exporter_current_version, exporter_latest_version)
)
# update package version:
config["packages"][exporter_name]["context"]["static"][
"version"
] = exporter_latest_version
# remove package release:
if config["packages"][exporter_name]["context"]["static"]["release"]:
del config["packages"][exporter_name]["context"]["static"][
"release"
]
github_commit_message = "Update %s from %s to %s" % (
exporter_name,
exporter_current_version,
exporter_latest_version,
)
if not check_mode:
updateGHTemplate(
github_token,
template_config,
github_branch,
github_commit_message,
config,
exporter_release_notes,
exporter_url,
pr_labels,
)
# exit so that there is exactly one update present in PR:
sys.exit(0)
else:
logging.info(
"Branch '%s' is already present, not pushing updates"
% github_branch
)