diff options
-rwxr-xr-x | linaro_gerrit.py | 30 | ||||
-rwxr-xr-x | update-gerrit-parents.py | 68 |
2 files changed, 98 insertions, 0 deletions
diff --git a/linaro_gerrit.py b/linaro_gerrit.py index 544e253..2b29fd0 100755 --- a/linaro_gerrit.py +++ b/linaro_gerrit.py @@ -164,6 +164,36 @@ class LinaroGerrit: log.debug("Not actually doing it because --dryrun") return True + def list_projects(self, parents=False): + log.debug("Listing projects") + url = "%s/a/projects/" % self.base + params = {} + if parents: + params["t"] = "" + r = requests.get(url, params=params, **self.reqargs) + if r.status_code == 200: + return json.loads(self.strip_gerrit_junk(r.content)) + else: + log.error("Failed to list projects") + return {} + + def set_project_parent(self, project, parent, commit_msg=""): + log.debug("Setting project %s parent as %s", project, parent) + project = requests.utils.quote(project, safe='') + url = "%s/a/projects/%s/parent" % (self.base, project) + body = {"parent": parent, "commit_message": commit_msg} + headers = {"Content-Type": "application/json"} + if not self.dryrun: + r = requests.put(url, data=json.dumps(body), headers=headers, **self.reqargs) + if r.status_code == 201 or r.status_code == 200: + return True + else: + log.error("Failed to set project %s parent as %s", project, + parent) + return False + log.debug("Not actually doing it because --dryrun") + return True + def add_gerrit_args(parser): parser.add_argument('--username', help="Gerrit HTTP API Username") diff --git a/update-gerrit-parents.py b/update-gerrit-parents.py new file mode 100755 index 0000000..74e3a31 --- /dev/null +++ b/update-gerrit-parents.py @@ -0,0 +1,68 @@ +#!/usr/bin/python +from __future__ import print_function +import sys +import json +import logging +import argparse +import linaro_gerrit + +from update_gerrit_parents_conf import PARENT_MAP +# update_gerrit_parents_conf.py should contain per-server parent map in +# the following format: +# (prefix, parent project) +# or +# (prefix, (parent, more parents...)) +# e.g.: +# +#PARENT_MAP = { +# "https://review.linaro.org": [ +# ("toolchain", +# ("Toolchain-Projects", "Toolchain-Upstream-Mirrors")), +# ("infrastructure", "infrastructure"), +# ], +#} + +parser = argparse.ArgumentParser( + description='Update Gerrit project parents based on tree prefix') +parser.add_argument('--server', + help="Server to use config for (if differs from --base)") +parser.add_argument('--limit', type=int, default=-1, + help="Process at most LIMIT projects") +linaro_gerrit.add_gerrit_args(parser) +args = parser.parse_args() +linaro_gerrit.apply_gerrit_conf(args) +logging.basicConfig() +log = logging.getLogger("update-gerrit-parents") +log.setLevel(getattr(logging, args.loglevel.upper())) + +gerrit = linaro_gerrit.LinaroGerrit(args.base, args.username, args.password, + args.noverify, args.loglevel, args.dryrun) +parent_map = PARENT_MAP[args.server or args.base] + +proj_map = gerrit.list_projects(parents=True) + +limit = args.limit +for name, info in proj_map.items(): + if limit == 0: + break + for prefix, parents in parent_map: + if name != prefix and name.startswith(prefix): + if not isinstance(parents, tuple): + parents = (parents,) + if info["parent"] == "All-Projects": + if len(parents) == 1: + log.debug("Current parent of %s is %s, setting to %s", + name, info["parent"], parents[0]) + gerrit.set_project_parent(name, parents[0]) + limit -= 1 + else: + log.warn("Current parent of %s is %s, should be one of %s", + name, info["parent"], parents) + elif info["parent"] not in parents: + log.info("Parent of '%s' is '%s', but not among expected %s", + name, info["parent"], parents) + else: + log.debug("Parent of '%s' is already '%s'", + name, info["parent"]) + # If matched one rule, skip processing next + break |