Add support for reporting build status to GitLab

Fixes: #2
parent 68f3d069
......@@ -83,6 +83,8 @@ def init_command(ctx, git_url):
with (root / "config.json").open("w") as f:
content = {
"base-url": "http://wwwtest.ubuntu-it.org",
"update-commit-status": False,
"git-url": git_url,
"gitlab-url": "http://code.ubuntu-it.org",
"gitlab-project": "ubuntu-it-web/www",
......
......@@ -84,14 +84,19 @@ class TestsManager:
def sync_branches_jobs(self):
"""Get the jobs needed to sync branches"""
jobs = []
for branch in list(self.branches.values()):
if branch.active:
if not branch.present():
# Set the branch status to "pending"
branch.update_status("pending")
# Deploy the branch and load it
def deploy(branch=branch):
branch.deploy()
self.instances.load_branch(branch)
yield deploy
jobs.append(deploy)
else:
if branch.present():
# Unload the branch, destroy and forget about it
......@@ -102,7 +107,9 @@ class TestsManager:
if branch.name in self.details["branches"]:
del self.details["branches"]
self.save_details()
yield destroy
jobs.append(destroy)
return jobs
def save_details(self):
"""Save details on disk"""
......
......@@ -135,10 +135,17 @@ class Branch:
file = self.manager.root / "branches" / self.name / "commit"
if file.exists():
if hasattr(self, "_gitlab_commit_cache"):
delattr(self, "_gitlab_commit_cache")
with file.open() as f:
return f.read().strip()
else:
return ""
# Ask GitLab about the commit
if not hasattr(self, "_gitlab_commit_cache"):
self._gitlab_commit_cache = self.manager.gitlab \
.branch_commit(self.name)
return self._gitlab_commit_cache
def commit_url(self):
"""Get the URL of a commit"""
......@@ -216,7 +223,7 @@ class Branch:
def log(msg):
"""Utility function to log a message to the build log"""
with log_file.open("a") as f:
f.write(msg)
f.write(msg+"\n")
# Create subdirectories
build_dir.mkdir()
......@@ -238,6 +245,8 @@ class Branch:
self.name
], stdout=commit_file.open("w"), stderr=log_file.open("a"))
self.update_status("running")
# Copy and load the configuration file
if not (build_dir / "managetests.json").exists():
log("[!] No managetests.json found in the branch!")
......@@ -272,6 +281,15 @@ class Branch:
if total == 0:
log("[!] Warning: no artifact was installed!")
# Check if the branch was successifully built
popen_args = self.command_popen_args(self.config["run"])
if os.path.exists(popen_args[0][0][0]):
log("[!] Info: build seems to be successful")
self.update_status("success")
else:
log("[!] Warning: build seems to have failed")
self.update_status("failed")
# Delete the build directory
shutil.rmtree(str(build_dir))
......@@ -291,8 +309,30 @@ class Branch:
ignore_errors=True
)
# Delete the commit cache
if hasattr(self, "_gitlab_commit_cache"):
delattr(self, "_gitlab_commit_cache")
try:
del self.manager.details["branches"][self.name]
except KeyError:
pass
self.manager.save_details()
def update_status(self, state, log=True):
"""Update the commit status"""
# Don't update status hooks on development instances
if not self.manager.config["update-commit-status"]:
return
if log:
url = "%s/+logs/%s/build.log" % (
self.manager.config["base-url"], self.name
)
else:
url = None
# Update the commit status in GitLab
self.manager.gitlab.commit_status(
self.commit(), state, self.name, url
)
......@@ -66,3 +66,23 @@ class GitLabAPI:
"""Post a comment on a merge request"""
return self.call("post", "projects/%s/merge_request/%s/comments" % (
self.project, id), data={"note": text})
def commit_status(self, commit, state, ref, url=None):
"""Update the status of a commit"""
data = {
"state": state,
"ref": ref,
"name": "managetests",
}
if url is not None:
data["target_url"] = url
return self.call("post", "projects/%s/statuses/%s" % (
self.project, commit
), data=data)
def branch_commit(self, branch):
"""Get the commit ID of a branch"""
return self.call("get", "projects/%s/repository/branches/%s" % (
self.project, branch
))["commit"]["id"]
......@@ -17,7 +17,7 @@
import pathlib
ROOT_DIR_VERSION = "2"
ROOT_DIR_VERSION = "3"
def is_root_valid(path):
......
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