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,8 +111,9 @@ class TestsManager: ...@@ -111,8 +111,9 @@ class TestsManager:
self.branches[name].destroy() self.branches[name].destroy()
del self.branches[name] del self.branches[name]
del self.details["branches"][name] if name in self.details["branches"]:
self.save_details() del self.details["branches"][name]
self.save_details()
def save_details(self): def save_details(self):
"""Save details on disk""" """Save details on disk"""
...@@ -124,31 +125,44 @@ class TestsManager: ...@@ -124,31 +125,44 @@ class TestsManager:
self.instances.run() self.instances.run()
def process_hook(self, data): def process_hook(self, data):
"""Process a webhook received by GitLab""" """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
mr = self.branches[branch].mr
# Rebuild the branch from scratch
self.remove_branch(branch)
self.load_branch(branch, mr)
# 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.")
def _process_merge_request_hook(self, data):
"""Process a merge request hook received from GitLab"""
obj = data["object_attributes"] obj = data["object_attributes"]
branch = obj["source_branch"] branch = obj["source_branch"]
# The branch was loaded # The branch was loaded but the merge request is now closed
if branch in self.branches: if branch in self.branches and obj["state"] != "opened":
# Delete old branches self.remove_branch(branch)
if obj["state"] != "opened": self.gitlab.post_comment(obj["id"], "L'istanza è stata rimossa.")
self.remove_branch(branch)
self.gitlab.post_comment(obj["id"], "L'istanza è stata rimossa.")
return
# Get the local commit
with open("%s/%s/commit" % (self.envs_dir, branch)) as f:
commit = f.read()
# Rebuild if there is a new commit
if commit != obj["last_commit"]["id"]:
self.remove_branch(branch)
self.load_branch(branch, obj["id"])
self.gitlab.post_comment(obj["id"], "L'istanza è stata "
"aggiornata con il nuovo codice.")
# The instance should be created # 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.load_branch(branch, obj["id"])
self.gitlab.post_comment(obj["id"], "Istanza live disponibile su " self.gitlab.post_comment(obj["id"], "Istanza live disponibile su "
"http://wwwtest.ubuntu-it.org/%s." % branch) "http://wwwtest.ubuntu-it.org/%s." % branch)
...@@ -93,8 +93,6 @@ class Branch: ...@@ -93,8 +93,6 @@ class Branch:
(self.manager.git_dir, self.manager.build_dir, self.name), (self.manager.git_dir, self.manager.build_dir, self.name),
"cd %s && invoke build" % self.manager.build_dir, "cd %s && invoke build" % self.manager.build_dir,
"virtualenv %s/%s" % (self.manager.envs_dir, self.name), "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" % "%s/%s/bin/pip install %s/build/packages/*.whl" %
(self.manager.envs_dir, self.name, self.manager.build_dir), (self.manager.envs_dir, self.name, self.manager.build_dir),
("ln -s %s/%s/lib/python3.%s/site-packages/uitwww/static " ("ln -s %s/%s/lib/python3.%s/site-packages/uitwww/static "
......
...@@ -64,7 +64,7 @@ def create_app(manager, processor): ...@@ -64,7 +64,7 @@ def create_app(manager, processor):
@app.route("/+hook", methods=["POST"]) @app.route("/+hook", methods=["POST"])
def hook(): def hook():
processor.append(flask.request.data) processor.append(flask.request.json)
return "OK" return "OK"
return app return app
...@@ -42,7 +42,7 @@ class InstancesManager: ...@@ -42,7 +42,7 @@ class InstancesManager:
self._branches[branch.name] = branch self._branches[branch.name] = branch
if self.running: if self.running:
self._run_branch(branch.name) self._run_branch(branch)
def remove_branch(self, name): def remove_branch(self, name):
"""Remove a branch from the instances manager""" """Remove a branch from the instances manager"""
...@@ -50,7 +50,7 @@ class InstancesManager: ...@@ -50,7 +50,7 @@ class InstancesManager:
return return
del self._branches[name] del self._branches[name]
if self.running: if self.running and name in self._processes:
self._processes[name].terminate() self._processes[name].terminate()
del self._processes[name] 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