Read about our upcoming Code of Conduct on this issue

Commit e663b7cc authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

fetch-watch-tags: moved down in a main function

will make some parts testable for this script that has to run
unattended with nothing to do most of the times.
parent de7ea8d2dcf7
......@@ -20,14 +20,23 @@
return int(split[0]), int(split[1])
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--simulate", action='store_true')
parser.add_argument("--gitlab-repo", default='gitlab-ce',
help="Path to GitLab CE Git repo, relative "
"to this script location")
parser.add_argument("--create-issue", action='store_true')
parser.add_argument("--heptapod-token",
default='~/.gitlab-release-watcher.token')
parser.add_argument("--heptapod-server", default='https://foss.heptapod.net')
parser.add_argument("--heptapod-project", default='heptapod/heptapod')
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--simulate", action='store_true')
parser.add_argument("--gitlab-repo", default='gitlab-ce',
help="Path to GitLab CE Git repo, relative "
"to this script location")
parser.add_argument("--create-issue", action='store_true')
parser.add_argument("--heptapod-token",
default='~/.gitlab-release-watcher.token')
parser.add_argument("--heptapod-server",
default='https://foss.heptapod.net')
parser.add_argument("--heptapod-project", default='heptapod/heptapod')
parser.add_argument("--version-series", type=level2_version, nargs='*',
help="Level two version series to watch, ex: 13.7. "
"If specified, only the maximal version in the series "
"is considered, e.g, 13.7.6 if 13.7.5 "
"and 13.7.6 are new."
)
......@@ -33,7 +42,29 @@
parser.add_argument("--version-series", type=level2_version, nargs='*',
help="Level two version series to watch, ex: 13.7. "
"If specified, only the maximal version in the series "
"is considered, e.g, 13.7.6 if 13.7.5 and 13.7.6 are new."
)
cl_args = parser.parse_args()
simulate = cl_args.simulate
parent_dir = os.path.dirname(__file__)
os.chdir(parent_dir)
os.chdir(cl_args.gitlab_repo)
cmd = ['git', 'fetch', '--tags']
if simulate:
cmd.append('--dry-run')
git = Popen(cmd, stderr=PIPE, stdout=PIPE)
out, err = git.communicate()
if git.returncode != 0:
raise RuntimeError("Git returned code %d. stdout: %r, stderr: %r" % (
git.returncode, out, err))
new_versions = []
for line in err.splitlines(): # all Git useful output is on stderr
line = line.strip().decode('ascii', 'ignore')
m = NEW_TAG_RX.search(line)
if m is not None:
new_versions.append((int(m.group(1)),
int(m.group(2)),
int(m.group(3)),
))
......@@ -39,7 +70,17 @@
cl_args = parser.parse_args()
simulate = cl_args.simulate
parent_dir = os.path.dirname(__file__)
os.chdir(parent_dir)
os.chdir(cl_args.gitlab_repo)
if new_versions:
print("Releases to consider in new tags: %r" % new_versions)
else:
print("No release found in new tags")
return 0
series = cl_args.version_series
if series:
print("Filtering according to specified version series: "
+ repr(series))
try:
new_versions = [max(v for v in new_versions if v[:2] in series)]
except ValueError: # max() on empty sequence
print("No release matches the given series")
return 0
......@@ -45,5 +86,12 @@
cmd = ['git', 'fetch', '--tags']
if simulate:
cmd.append('--dry-run')
if cl_args.create_issue:
for version in new_versions:
version_str = '.'.join(str(i) for i in version)
print("Creating issue for GitLab " + repr(version))
url = '/'.join((cl_args.heptapod_server, 'api', 'v4',
'projects',
cl_args.heptapod_project.replace('/', '%2F'),
'issues'))
with open(os.path.expanduser(cl_args.heptapod_token)) as tokenf:
token = tokenf.read().strip()
......@@ -49,3 +97,10 @@
git = Popen(cmd, stderr=PIPE, stdout=PIPE)
post_data = dict(
description="A new release tag v%s appeared in "
"upstream GitLab."
"\n\n"
"It should be considered for a stable Heptapod "
"release.\n" % version_str,
title="GitLab " + version_str
)
......@@ -51,7 +106,18 @@
out, err = git.communicate()
if git.returncode != 0:
raise RuntimeError("Git returned code %d. stdout: %r, stderr: %r" % (
git.returncode, out, err))
if simulate:
print("Simulate mode: would POST %r, data=%r" % (url,
post_data))
else:
resp = requests.post(url,
headers={'Private-Token': token},
data=post_data)
as_json = resp.json()
if resp.status_code < 400:
print("Issue %d created. Details: %r" % (as_json['iid'],
as_json))
else:
print("Error %d creating issue. Details: %r" % (
resp.status_code, as_json))
return 2
......@@ -56,61 +122,4 @@
new_versions = []
for line in err.splitlines(): # all Git useful output is on stderr
line = line.strip().decode('ascii', 'ignore')
m = NEW_TAG_RX.search(line)
if m is not None:
new_versions.append((int(m.group(1)),
int(m.group(2)),
int(m.group(3)),
))
if new_versions:
print("Releases to consider in new tags: %r" % new_versions)
else:
print("No release found in new tags")
sys.exit(0)
series = cl_args.version_series
if series:
print("Filtering according to specified version series: " + repr(series))
try:
new_versions = [max(v for v in new_versions if v[:2] in series)]
except ValueError: # max() on empty sequence
print("No release matches the given series")
sys.exit(0)
if cl_args.create_issue:
for version in new_versions:
version_str = '.'.join(str(i) for i in version)
print("Creating issue for GitLab " + repr(version))
url = '/'.join((cl_args.heptapod_server, 'api', 'v4',
'projects',
cl_args.heptapod_project.replace('/', '%2F'),
'issues'))
with open(os.path.expanduser(cl_args.heptapod_token)) as tokenf:
token = tokenf.read().strip()
post_data = dict(
description="A new release tag v%s appeared in upstream GitLab."
"\n\n"
"It should be considered for a stable Heptapod "
"release.\n" % version_str,
title="GitLab " + version_str
)
if simulate:
print("Simulate mode: would POST %r, data=%r" % (url, post_data))
else:
resp = requests.post(url,
headers={'Private-Token': token},
data=post_data)
as_json = resp.json()
if resp.status_code < 400:
print("Issue %d created. Details: %r" % (as_json['iid'],
as_json))
else:
print("Error %d creating issue. Details: %r" % (
resp.status_code, as_json))
sys.exit(2)
if __name__ == '__main__':
sys.exit(main())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment