From 38987b4d975e74fc8bdce3c80fd81abd43afd6dd Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Mon, 3 Nov 2014 15:25:52 +0100 Subject: [PATCH] gitlab_jenkins_trigger.py: Make email recipients configurable. --- gitlab_jenkins_trigger.py | 71 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/gitlab_jenkins_trigger.py b/gitlab_jenkins_trigger.py index f558532..11da928 100755 --- a/gitlab_jenkins_trigger.py +++ b/gitlab_jenkins_trigger.py @@ -46,6 +46,9 @@ email_maintainer = True # Email Uploaders (specified in debian/control) when a build fails email_uploaders = True + +# Additional space separated list of email recipients for build failures +email_recipients = dcops@profitbricks.com benjamin.drung@profitbricks.com ================================================================================ """ @@ -69,6 +72,7 @@ except ImportError: from ConfigParser import SafeConfigParser as ConfigParser import debian.changelog +import debian.deb822 import gitlab import jenkins @@ -111,6 +115,7 @@ class BuildchainConfig(ConfigParser): self.set('debian', 'distros', 'auto') self.set('debian', 'email_maintainer', 'True') self.set('debian', 'email_uploaders', 'True') + self.set('debian', 'email_recipients', '') class JenkinsJob(object): @@ -313,6 +318,65 @@ def get_job_name(source_name, distribution, branch): return job_name +def get_recipients(config, gitlab_client, data, logger): + """Return a space separated list of email recipients. + + If email_maintainer and/or email_uploaders are enabled in the .buildchain config, + the Maintainer and/or Uploaders are extracted from debian/control. + """ + email_addresses = [] + if config.getboolean('debian', 'email_maintainer') or \ + config.getboolean('debian', 'email_uploaders'): + logger.info("Getting debian/control...") + control = gitlab_client.getrawblob(data['project_id'], data['after'], 'debian/control') + if control is False: + logger.error("No debian/control file found in the git repository.") + sys.exit(1) + control = debian.deb822.Deb822(control) + if config.getboolean('debian', 'email_maintainer'): + try: + maintainer = [m.strip() for m in control['Maintainer'].split(',')] + email_addresses += maintainer + logger.info("Extracted maintainer from debian/control: {maintainer}" + .format(maintainer=", ".join(maintainer))) + except KeyError: + logger.warn("email_maintainer is enabled, but the Maintainer field is missing in " + "debian/control.") + if config.getboolean('debian', 'email_uploaders'): + try: + uploaders = [m.strip() for m in control['Uploaders'].split(',')] + email_addresses += uploaders + logger.info("Extracted uploaders from debian/control: {uploaders}" + .format(uploaders=", ".join(uploaders))) + except KeyError: + logger.warn("email_uploaders is enabled, but the Uploaders field is missing in " + "debian/control.") + + additional_recipients = [e for e in config.get('debian', 'email_recipients').split(' ') if e] + if len(additional_recipients) > 0: + logger.info("Additional email recipients: {recipients}" + .format(recipients=" ".join(additional_recipients))) + email_addresses += additional_recipients + + # Strip names from email addresses and add valid ones + recipients = set() + for recipient in email_addresses: + match = re.match('(?:.*<)?([^<>@ ]+@[^<>@ ]+)(>.*)?$', recipient) + if match: + email = match.group(1) + match = re.match('([^@]+)@profitbricks.com$', email) + if match: + logger.info("Adding '{email}' to recipients list.".format(email=match.group(1))) + recipients.add(match.group(1)) + else: + logger.info("Not adding '{email}' to recipients list, because its not a " + "ProfitBricks email address.".format(email=email)) + else: + logger.warn("Failed to extract email address from '{email}'.".format(email=recipient)) + + return sorted(recipients) + + def main(): logging.basicConfig(format=_LOG_FORMAT, level=_LOG_LEVEL) logger = logging.getLogger(os.path.basename(__file__)) @@ -354,10 +418,10 @@ def main(): config = read_buildchain_config(data, gitlab_client, logger) changelog = get_debian_changelog(config, data, gitlab_client, logger) - source_name = changelog.package branch = re.sub('^refs/heads/', '', data['ref']) + recipients = get_recipients(config, gitlab_client, data, logger) for distro in get_distros(config, changelog, branch, logger): - job_name = get_job_name(source_name, distro, branch) + job_name = get_job_name(changelog.package, distro, branch) logger.info("Creating/updating Jenkins job '{name}'...".format(name=job_name)) description = ("Debian package build job for the source package {package} from " '{url}
Warning: This job is ' @@ -371,8 +435,7 @@ def main(): 'gitrepo': data['repository']['url'], 'branch': branch, 'homepage': data['repository']['homepage'], - # TODO: fill useful list of recipients - 'recipients': 'benjamin.drung@profitbricks.com', + 'recipients': " ".join(recipients), 'description': description, } jenkins_job = JenkinsJob.from_template(jenkins_client, job_name, substitutions, logger) -- 2.39.5