Commit c400d83f authored by Pietro Albini's avatar Pietro Albini

Fix and improve GitLab hooks

This commit allows to fetch updates also for pinned branches, separating new
commit hooks from merge request hooks. This also fixes some old weird
behaviors.
parent 86dbef23
......@@ -111,6 +111,7 @@ class TestsManager:
self.branches[name].destroy()
del self.branches[name]
if name in self.details["branches"]:
del self.details["branches"][name]
self.save_details()
......@@ -124,31 +125,44 @@ class TestsManager:
self.instances.run()
def process_hook(self, data):
"""Process a webhook received by GitLab"""
obj = data["object_attributes"]
branch = obj["source_branch"]
"""Process a webhook received from GitLab"""
if data["object_kind"] == "push":
self._process_push_hook(data)
elif data["object_kind"] == "merge_request":
self._process_merge_request_hook(data)
def _process_push_hook(self, data):
"""Process a push hook received from GitLab"""
branch = data["ref"]
if branch.startswith("refs/heads/"):
branch = branch[len("refs/heads/"):]
# Consider only the branches managed by managetests
if branch not in self.branches:
return
# The branch was loaded
if branch in self.branches:
# Delete old branches
if obj["state"] != "opened":
mr = self.branches[branch].mr
# Rebuild the branch from scratch
self.remove_branch(branch)
self.gitlab.post_comment(obj["id"], "L'istanza è stata rimossa.")
return
self.load_branch(branch, mr)
# Get the local commit
with open("%s/%s/commit" % (self.envs_dir, branch)) as f:
commit = f.read()
# Send an alert on the merge request, if this branch has one
if mr is not None:
self.gitlab.post_command(mr, "Istanza live aggiornata.")
# Rebuild if there is a new commit
if commit != obj["last_commit"]["id"]:
def _process_merge_request_hook(self, data):
"""Process a merge request hook received from GitLab"""
obj = data["object_attributes"]
branch = obj["source_branch"]
# The branch was loaded but the merge request is now closed
if branch in self.branches and obj["state"] != "opened":
self.remove_branch(branch)
self.load_branch(branch, obj["id"])
self.gitlab.post_comment(obj["id"], "L'istanza è stata "
"aggiornata con il nuovo codice.")
self.gitlab.post_comment(obj["id"], "L'istanza è stata rimossa.")
# The instance should be created
elif obj["state"] == "opened":
elif branch not in self.branches and obj["state"] == "opened":
self.load_branch(branch, obj["id"])
self.gitlab.post_comment(obj["id"], "Istanza live disponibile su "
"http://wwwtest.ubuntu-it.org/%s." % branch)
......@@ -93,8 +93,6 @@ class Branch:
(self.manager.git_dir, self.manager.build_dir, self.name),
"cd %s && invoke build" % self.manager.build_dir,
"virtualenv %s/%s" % (self.manager.envs_dir, self.name),
"cd %s && git rev-parse HEAD > %s/%s/commit" %
(self.manager.build_dir, self.manager.envs_dir, self.name),
"%s/%s/bin/pip install %s/build/packages/*.whl" %
(self.manager.envs_dir, self.name, self.manager.build_dir),
("ln -s %s/%s/lib/python3.%s/site-packages/uitwww/static "
......
......@@ -64,7 +64,7 @@ def create_app(manager, processor):
@app.route("/+hook", methods=["POST"])
def hook():
processor.append(flask.request.data)
processor.append(flask.request.json)
return "OK"
return app
......@@ -42,7 +42,7 @@ class InstancesManager:
self._branches[branch.name] = branch
if self.running:
self._run_branch(branch.name)
self._run_branch(branch)
def remove_branch(self, name):
"""Remove a branch from the instances manager"""
......@@ -50,7 +50,7 @@ class InstancesManager:
return
del self._branches[name]
if self.running:
if self.running and name in self._processes:
self._processes[name].terminate()
del self._processes[name]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment