Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Tag pushes and unauthenticated email #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions config/config.ini
Original file line number Diff line number Diff line change
@@ -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 protected] ; deploy errors will be sended there

[mailer]
; remove user/password if not required by smtp system
[email protected]
password=***
host=smtp.example.com
Expand All @@ -12,14 +14,19 @@ sender=GitLab deployer at gitlab.example.com <[email protected]>


; 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
21 changes: 17 additions & 4 deletions glhooks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -74,9 +78,18 @@ 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)

repo = self._params.get(pathed_section, self._params.get(url))

if repo is not None:
repo = dict(repo)
repo["branch"] = path_info[2]

return repo
return (repo, path)
9 changes: 6 additions & 3 deletions glhooks/mailer/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
14 changes: 10 additions & 4 deletions glhooks/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ 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, 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"))
Expand All @@ -76,8 +79,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)
Expand Down