From 28dd56dba30ff7f6659effb0323cab49e288d34d Mon Sep 17 00:00:00 2001 From: Mike Nestor Date: Thu, 24 Jul 2014 06:37:30 -0400 Subject: [PATCH 1/2] don't try to login to smtp server if we don't have a username watch for tag pushes - @tags/tagprefix-* --- config/config.ini | 19 +++++++++++++------ glhooks/config.py | 23 ++++++++++++++++++++--- glhooks/mailer/mailer.py | 9 ++++++--- glhooks/server.py | 10 +++++++--- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/config/config.ini b/config/config.ini index b2eb4bd..ba665a2 100644 --- a/config/config.ini +++ b/config/config.ini @@ -1,9 +1,11 @@ [server] host=gitlab.example.com log_file=/path/to/your/log/file.log ; default: /var/log/glhooks.access.log +; errors on deploying tagas will be emailed here email=admin@example.com ; deploy errors will be sended there [mailer] +; remove user/password if not required by smtp system user=noreply@example.com password=*** host=smtp.example.com @@ -12,14 +14,19 @@ sender=GitLab deployer at gitlab.example.com ; list of repositories -[http://gitlab.example.com/user/repository] +; [REPO_URL @ BRANCH] or [REPO_URL @ tags/TAG_NAME] +; path=path on system to do checkout in + +[http://gitlab.example.com/user/repository@develop] +path=/path/to/directory/with/your/dev/project/ + +[http://gitlab.example.com/user/repository@tags/dev] +; matches for any tag pushes where tag name starts dev- +; would match dev-v0.1, dev-v1.0, dev-testme, dev-* path=/path/to/directory/with/your/dev/project/ -branch=develop -[http://gitlab.example.com/user/repository] +[http://gitlab.example.com/user/repository@master] path=/path/to/directory/with/your/production/project/ -branch=master -[http://gitlab.example.com/another-user/repo] +[http://gitlab.example.com/another-user/repo@master] path=/path/to/directory/with/your/another-project/ -branch=master diff --git a/glhooks/config.py b/glhooks/config.py index 5cc568e..e076ae7 100644 --- a/glhooks/config.py +++ b/glhooks/config.py @@ -43,8 +43,12 @@ def _parse(self, file_paths): if self._REPO_SECTION_PATTERN.match(section): if section.endswith("/"): # remove ending slash section = section[:-1] + if '@' not in section: + section = '%s@%s' % (section, self.DEFAULT_BRANCH) data[section] = section_data - data[section]["branch"] = section_data.get("branch", self.DEFAULT_BRANCH) + data[section]["branch"] = section.split('@')[1] + if data[section]["branch"].startswith('tags/'): + data[section]["branch"] = data[section]["branch"].split('/')[1] else: data[section] = section_data @@ -74,9 +78,22 @@ def _build_logger(self, log_file_path): return logger - def find_repo(self, url): - repo = self._params.get(url) + def find_repo(self, url, ref): + path_info = ref.split('/') + path = path_info[2] + + if path_info[1] == 'tags' and '-' in path_info[2]: + path = 'tags/%s' % path_info[2].split('-')[0] + pathed_section = '%s@%s' % (url, path) + + if pathed_section in self._params: + repo = self._params.get(pathed_section) + else: + repo = self._params.get(url) + if repo is not None: repo = dict(repo) + repo["branch"] = path_info[2] + return repo diff --git a/glhooks/mailer/mailer.py b/glhooks/mailer/mailer.py index 3ff1593..28d290f 100644 --- a/glhooks/mailer/mailer.py +++ b/glhooks/mailer/mailer.py @@ -23,7 +23,7 @@ class SmtpMailer(object): "plain": smtplib.SMTP, } - def __init__(self, user, password, host="", port=0, security="tls"): + def __init__(self, user=None, password=None, host="", port=0, security="tls"): try: self._mailer = self._MAILERS[security] except KeyError: @@ -48,11 +48,14 @@ def __call__(self, message): def _connect(self): mailer = self._mailer(self._host, self._port) mailer.ehlo() - mailer.login(self._user, self._password) + + if self._user != None and self._password != None: + raise Exception('WHY!!!!!!!!!!!!!!!!!!!!!!!!! %s' % self._password) + mailer.login(self._user, self._password) return mailer class GmailMailer(SmtpMailer): - def __init__(self, user, password): + def __init__(self, user=None, password=None): super(GmailMailer, self).__init__(user, password, "smtp.gmail.com") diff --git a/glhooks/server.py b/glhooks/server.py index 998ed58..d9b715c 100644 --- a/glhooks/server.py +++ b/glhooks/server.py @@ -50,7 +50,8 @@ def do_POST(self): def handle_commits_data(self, commits_json): repo_url = commits_json["repository"]["homepage"] - repo_data = self.context.find_repo(repo_url) + repo_ref = commits_json["ref"] + repo_data = self.context.find_repo(repo_url, repo_ref) if repo_data is None: raise Exception("No configuration found for repository.", repo_url) @@ -76,8 +77,11 @@ def _send_email(self, commits_json, exception): def _gather_emails(self, commits_json): emails = set() - for commit in commits_json["commits"]: - emails.add(commit["author"]["email"]) + if 'commits' in commits_json: + for commit in commits_json["commits"]: + emails.add(commit["author"]["email"]) + else: + emails.add(self.context["server"]["email"]) emails.add(self.context["server"]["email"]) return list(emails) From 1eafb5d8a66e641d4a287b05747e6ececf7f0c1e Mon Sep 17 00:00:00 2001 From: Mike Nestor Date: Mon, 28 Jul 2014 07:00:16 -0400 Subject: [PATCH 2/2] better branch/tag handling and downgrade not-configured from an exception --- glhooks/config.py | 10 +++------- glhooks/server.py | 6 ++++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/glhooks/config.py b/glhooks/config.py index e076ae7..d7c3ae0 100644 --- a/glhooks/config.py +++ b/glhooks/config.py @@ -86,14 +86,10 @@ def find_repo(self, url, ref): path = 'tags/%s' % path_info[2].split('-')[0] pathed_section = '%s@%s' % (url, path) - if pathed_section in self._params: - repo = self._params.get(pathed_section) - else: - repo = self._params.get(url) + repo = self._params.get(pathed_section, self._params.get(url)) if repo is not None: repo = dict(repo) + repo["branch"] = path_info[2] - repo["branch"] = path_info[2] - - return repo + return (repo, path) diff --git a/glhooks/server.py b/glhooks/server.py index d9b715c..83a751a 100644 --- a/glhooks/server.py +++ b/glhooks/server.py @@ -51,9 +51,11 @@ def do_POST(self): def handle_commits_data(self, commits_json): repo_url = commits_json["repository"]["homepage"] repo_ref = commits_json["ref"] - repo_data = self.context.find_repo(repo_url, repo_ref) + repo_data, fixed_ref = self.context.find_repo(repo_url, repo_ref) if repo_data is None: - raise Exception("No configuration found for repository.", repo_url) + #it's not an error if we aren't configured + self.context.logger.info("No configuration found for repository. [%s@%s]" % (repo_url, fixed_ref)) + return repo = Repository(repo_data["path"]) repo.pull(branch=repo_data.get("branch"))