]> Frank Brehm's Git Trees - profitbricks/jenkins-build-scripts.git/commitdiff
gitlab_jenkins_trigger.py: Make email recipients configurable.
authorBenjamin Drung <benjamin.drung@profitbricks.com>
Mon, 3 Nov 2014 14:25:52 +0000 (15:25 +0100)
committerBenjamin Drung <benjamin.drung@profitbricks.com>
Mon, 3 Nov 2014 14:25:52 +0000 (15:25 +0100)
gitlab_jenkins_trigger.py

index f558532338a4a3d9dd97ca03013567cb40b9358f..11da928f03dcc6e97cfd1d821055e13c68fd3607 100755 (executable)
@@ -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 <b>{package}</b> from "
                        '<a href="{url}">{url}</a><br/>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)