aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Sokolovsky <paul.sokolovsky@linaro.org>2016-04-21 00:13:27 +0300
committerPaul Sokolovsky <paul.sokolovsky@linaro.org>2016-05-10 22:01:53 +0300
commit909611172fd7aca4ddb1729f886cc3bd83f012b5 (patch)
treea5d0c49ab7830b37e69595e11718fec5d6c8e1a4
parentce9f894cd88bdfa0a62686c1aef0dc83a7b0a4b0 (diff)
update-gerrit-parents.py: Cronjob to check/set project parents.
This allows to define mapping between project path prefixes and Gerrit parent project which should be set for all matching projects. It also allows to define more than one possible parent, in which case the script just issues a warning in case of non-match, leaving choosing the right parent to operator. Change-Id: Ia53fb9359dd717fe003200c81643619f4d2ab837
-rwxr-xr-xlinaro_gerrit.py30
-rwxr-xr-xupdate-gerrit-parents.py68
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