From f3c60aa816c5f044f4bc9876b524139d398eac77 Mon Sep 17 00:00:00 2001
From: Alessio Igor Bogani <alessio.bogani@elettra.eu>
Date: Mon, 3 May 2021 08:32:59 +0200
Subject: [PATCH] Fix a couple of minor issues

---
 doc/gitlab_event.txt | 72 +-------------------------------------------
 inau-dispatcher.py   |  8 ++---
 inau.py              | 71 +++++++++++++++++++++++++------------------
 3 files changed, 46 insertions(+), 105 deletions(-)

diff --git a/doc/gitlab_event.txt b/doc/gitlab_event.txt
index 00ad68d..6194b17 100644
--- a/doc/gitlab_event.txt
+++ b/doc/gitlab_event.txt
@@ -1,71 +1 @@
-{'object_kind': 'push',
-'event_name': 'push',
-'before': '67475d770a571dec7da457bd86de73447bd92ccb',
-'after': '2900aab369da0f3e48012ef3fc30eb042752f118', 
-'ref': 'refs/heads/master', 
-'checkout_sha': '2900aab369da0f3e48012ef3fc30eb042752f118', 
-'message': None, 
-'user_id': 13, 
-'user_name': 'Alessio Igor Bogani', 
-'user_username': 'alessio.bogani', 
-'user_email': '', 
-'user_avatar': 'https://secure.gravatar.com/avatar/f13312e20198c46ed5356c9c7fa1bc2a?s=80&d=identicon', 
-'project_id': 297, 
-'project': {'id': 297,
-	 'name': 'fake',
-	 'description': 'Fake device, just for testing', 
-	 'web_url': 'https://gitlab.elettra.eu/cs/ds/fake', 
-	 'avatar_url': None, 
-	 'git_ssh_url': 'git@gitlab.elettra.eu:cs/ds/fake.git', 
-	 'git_http_url': 'https://gitlab.elettra.eu/cs/ds/fake.git', 
-	 'namespace': 'ds', 
-	 'visibility_level': 20, 
-	 'path_with_namespace': 'cs/ds/fake', 
-	 'default_branch': 'master', 
-	 'ci_config_path': None, 
-	 'homepage': 'https://gitlab.elettra.eu/cs/ds/fake', 
-	 'url': 'git@gitlab.elettra.eu:cs/ds/fake.git', 
-	 'ssh_url': 'git@gitlab.elettra.eu:cs/ds/fake.git', 
-	 'http_url': 'https://gitlab.elettra.eu/cs/ds/fake.git'
-	 },
-'commits': [
-	{'id': '2900aab369da0f3e48012ef3fc30eb042752f118', 
-	'message': 'Update Makefile\n', 
-	'title': 'Update Makefile', 
-	'timestamp': '2020-11-16T13:54:23+01:00', 
-	'url': 'https://gitlab.elettra.eu/cs/ds/fake/-/commit/2900aab369da0f3e48012ef3fc30eb042752f118',
-	'author': {'name': 'Alessio Igor Bogani', 
-		'email': 'alessio.bogani@elettra.eu'},
-	'added': [],
-	'modified': ['Makefile'],
-	'removed': []},
-	 {'id': '804ccdf62dfcc8605b550e7ea9c456432798c678', 
-	 'message': 'test 25\n',
-	 'title': 'test 25', 
-	 'timestamp': '2020-09-23T14:06:57+02:00', 
-	 'url': 'https://gitlab.elettra.eu/cs/ds/fake/-/commit/804ccdf62dfcc8605b550e7ea9c456432798c678', 
-	 'author': {'name': 'Alessio Igor Bogani',
-	 	'email': 'alessio.bogani@elettra.eu'},
-	 'added': [],
-	 'modified': ['src/Fake.cpp'], 
-	 'removed': []}, 
-	 {'id': '67475d770a571dec7da457bd86de73447bd92ccb', 
-	 'message': 'test 24\n', 
-	 'title': 'test 24', 
-	 'timestamp': '2020-09-23T09:59:28+02:00', 
-	 'url': 'https://gitlab.elettra.eu/cs/ds/fake/-/commit/67475d770a571dec7da457bd86de73447bd92ccb', 
-	 'author': {'name': 'Alessio Igor Bogani', 
-	 	'email': 'alessio.bogani@elettra.eu'}, 
-	 'added': [], 
-	 'modified': ['src/Fake.cpp'], 
-	 'removed': []}
-	 ],
-'total_commits_count': 3, 
-'push_options': {}, 
-'repository': {'name': 'fake', 
-	'url': 'git@gitlab.elettra.eu:cs/ds/fake.git', 
-	'description': 'Fake device, just for testing', 
-	'homepage': 'https://gitlab.elettra.eu/cs/ds/fake', 
-	'git_http_url': 'https://gitlab.elettra.eu/cs/ds/fake.git', 
-	'git_ssh_url': 'git@gitlab.elettra.eu:cs/ds/fake.git', 
-	'visibility_level': 20}}
+{"object_kind":"tag_push","event_name":"tag_push","before":"0000000000000000000000000000000000000000","after":"d56ffed6a0e25a046f4debd0451ebd606286a578","ref":"refs/tags/1.0.0[USED]","checkout_sha":"ff00ed95be0e2c6737e3d96b19051b2dc339c0db","message":"v1.0.0","user_id":13,"user_name":"Alessio Igor Bogani","user_username":"alessio.bogani","user_email":"","user_avatar":"https://secure.gravatar.com/avatar/f13312e20198c46ed5356c9c7fa1bc2a?s=80\u0026d=identicon","project_id":297,"project":{"id":297,"name":"fake","description":"Fake device, just for testing","web_url":"https://gitlab.elettra.eu/cs/ds/fake","avatar_url":null,"git_ssh_url":"git@gitlab.elettra.eu:cs/ds/fake.git","git_http_url":"https://gitlab.elettra.eu/cs/ds/fake.git","namespace":"ds","visibility_level":20,"path_with_namespace":"cs/ds/fake[USED]","default_branch":"master","ci_config_path":null,"homepage":"https://gitlab.elettra.eu/cs/ds/fake","url":"git@gitlab.elettra.eu:cs/ds/fake.git","ssh_url":"git@gitlab.elettra.eu:cs/ds/fake.git[USED]","http_url":"https://gitlab.elettra.eu/cs/ds/fake.git"},"commits":[{"id":"ff00ed95be0e2c6737e3d96b19051b2dc339c0db","message":"CHange 2\n","title":"CHange 2","timestamp":"2021-03-17T14:30:24+01:00","url":"https://gitlab.elettra.eu/cs/ds/fake/-/commit/ff00ed95be0e2c6737e3d96b19051b2dc339c0db","author":{"name":"Alessio Igor Bogani","email":"alessio.bogani@elettra.eu[USED]"},"added":[],"modified":["src/Fake.cpp"],"removed":[]}],"total_commits_count":1,"push_options":{},"repository":{"name":"fake","url":"git@gitlab.elettra.eu:cs/ds/fake.git","description":"Fake device, just for testing","homepage":"https://gitlab.elettra.eu/cs/ds/fake","git_http_url":"https://gitlab.elettra.eu/cs/ds/fake.git","git_ssh_url":"git@gitlab.elettra.eu:cs/ds/fake.git","visibility_level":20}}
diff --git a/inau-dispatcher.py b/inau-dispatcher.py
index 7e705d2..73f63d3 100755
--- a/inau-dispatcher.py
+++ b/inau-dispatcher.py
@@ -180,11 +180,11 @@ class Builder:
             except subprocess.CalledProcessError as c:
                 sendEmailAdmins("Subprocess failed", str(c))
                 logger.error("Subprocess failed: ", str(c))
-                session.rollback()
+                self.session.rollback()
             except Exception as e:
                 sendEmailAdmins("Generic error", str(e))
                 logger.error("Generic error: ", str(e))
-                session.rollback()
+                self.session.rollback()
             except KeyboardInterrupt as k:
                 self.session.rollback()
                 break
@@ -313,9 +313,7 @@ class Server(BaseHTTPRequestHandler):
                 return
 
             for r in session.query(db.Repositories).filter(db.Repositories.name==post_json['project']['path_with_namespace']).all():
-                if r.name == "cs/ds/makefiles" and self.headers['X-Gitlab-Event'] == 'Push Hook' and post_json['event_name'] == 'push':
-                    job = Update(repository_name = r.name, repository_url = post_json['project']['ssh_url'], build_tag=post_json['ref'])
-                elif self.headers['X-Gitlab-Event'] == 'Tag Push Hook' and post_json['event_name'] == 'tag_push':
+                if self.headers['X-Gitlab-Event'] == 'Tag Push Hook' and post_json['event_name'] == 'tag_push':
                     job = Store(repository_name = r.name, repository_url = post_json['project']['ssh_url'], build_tag=post_json['ref'],
                             repository_id = r.id, repository_type = r.type, emails=[post_json['commits'][0]['author']['email'], post_json['user_email']])
                 else:
diff --git a/inau.py b/inau.py
index ecdb6a2..5d957bd 100644
--- a/inau.py
+++ b/inau.py
@@ -11,7 +11,7 @@ import hashlib
 import os
 import git
 from enum import Enum, IntEnum
-from werkzeug.exceptions import HTTPException, Unauthorized, Forbidden, InternalServerError, MethodNotAllowed, BadRequest, UnprocessableEntity
+from werkzeug.exceptions import HTTPException, Unauthorized, Forbidden, InternalServerError, MethodNotAllowed, BadRequest, UnprocessableEntity, NotFound
 from smtplib import SMTP
 from email.mime.text import MIMEText
 from flask import Flask, request, make_response, got_request_exception, render_template
@@ -267,6 +267,8 @@ def install(username, reponame, tag, destinations, itype):
     retval = []
     user = Users.query.filter(Users.name == username) \
             .first_or_404(description='User not found')
+    if not destinations.items():
+        raise NotFound
     for server, hosts in destinations.items():
         repository = Repositories.query.with_parent(server.platform) \
                 .filter(Repositories.name == reponame) \
@@ -927,6 +929,7 @@ class HostHandler(Resource):
         db.session.commit()
         return {}, 204
 
+
 files_fields = { 'filename': fields.String() }
 class FilesHandler(Resource):
     @marshal_with(files_fields)
@@ -935,11 +938,13 @@ class FilesHandler(Resource):
                 filter(Facilities.name == facilityname,
                         Hosts.name == hostname).\
                 first_or_404()
-        LatestInstallations = Builds.query.join('repository').join('installations').\
-                with_entities(Repositories.id, Installations.host_id,
-                        func.max(Installations.id).label('installation_id')).\
-                group_by(Repositories.id, Installations.host_id).\
-                subquery()
+        LatestInstallations = db.session.query(Installations)\
+                .with_entities(Repositories.id, Installations.host_id,\
+                    func.max(Installations.id).label('installation_id'))\
+                .select_from(Installations)\
+                .join(Builds).join(Repositories)\
+                .group_by(Repositories.id, Installations.host_id)\
+                .subquery()
         retval = []
         for artifact in Builds.query.join('installations').join('artifacts').\
                 join(LatestInstallations, Installations.id == LatestInstallations.c.installation_id).\
@@ -955,11 +960,13 @@ class FileHandler(Resource):
                 filter(Facilities.name == facilityname,
                         Hosts.name == hostname).\
                 first_or_404()
-        LatestInstallations = Builds.query.join('repository').join('installations').\
-                with_entities(Repositories.id, Installations.host_id,
-                        func.max(Installations.id).label('installation_id')).\
-                group_by(Repositories.id, Installations.host_id).\
-                subquery()
+        LatestInstallations = db.session.query(Installations)\
+                .with_entities(Repositories.id, Installations.host_id,\
+                    func.max(Installations.id).label('installation_id'))\
+                .select_from(Installations)\
+                .join(Builds).join(Repositories)\
+                .group_by(Repositories.id, Installations.host_id)\
+                .subquery()
         artifact = Builds.query.\
                 join('installations').\
                 join('artifacts').\
@@ -981,11 +988,13 @@ class CSInstallationsHandler(Resource):
     @marshal_with(cs_installations_fields)
     def get(self):
         args = mode_parser.parse_args(strict=True)
-        LatestInstallations = Builds.query.join('repository').join('installations').\
-                with_entities(Repositories.id, Installations.host_id,
-                        func.max(Installations.id).label('installation_id')).\
-                group_by(Repositories.id, Installations.host_id).\
-                subquery()
+        LatestInstallations = db.session.query(Installations)\
+                .with_entities(Repositories.id, Installations.host_id,\
+                    func.max(Installations.id).label('installation_id'))\
+                .select_from(Installations)\
+                .join(Builds).join(Repositories)\
+                .group_by(Repositories.id, Installations.host_id)\
+                .subquery()
         if args['mode'] == 'status':
             installations = Installations.query.options(
                     joinedload('user', innerjoin=True),\
@@ -1053,11 +1062,13 @@ class FacilityInstallationsHandler(Resource):
                 filter(Facilities.name == facilityname).\
                 first_or_404()
         args = mode_parser.parse_args(strict=True)
-        LatestInstallations = Builds.query.join('repository').join('installations').\
-                with_entities(Repositories.id, Installations.host_id,
-                        func.max(Installations.id).label('installation_id')).\
-                group_by(Repositories.id, Installations.host_id).\
-                subquery()
+        LatestInstallations = db.session.query(Installations)\
+                .with_entities(Repositories.id, Installations.host_id,\
+                    func.max(Installations.id).label('installation_id'))\
+                .select_from(Installations)\
+                .join(Builds).join(Repositories)\
+                .group_by(Repositories.id, Installations.host_id)\
+                .subquery()
         if args['mode'] == 'status':
             installations = Installations.query.options(
                     joinedload('user', innerjoin=True),\
@@ -1132,11 +1143,13 @@ class HostInstallationsHandler(Resource):
                         Hosts.name == hostname).\
                 first_or_404()
         args = mode_parser.parse_args(strict=True)
-        LatestInstallations = Builds.query.join('repository').join('installations').\
-                with_entities(Repositories.id, Installations.host_id,
-                        func.max(Installations.id).label('installation_id')).\
-                group_by(Repositories.id, Installations.host_id).\
-                subquery()
+        LatestInstallations = db.session.query(Installations)\
+                .with_entities(Repositories.id, Installations.host_id,\
+                    func.max(Installations.id).label('installation_id'))\
+                .select_from(Installations)\
+                .join(Builds).join(Repositories)\
+                .group_by(Repositories.id, Installations.host_id)\
+                .subquery()
         if args['mode'] == 'status':
             installations = Installations.query.options(
                     joinedload('user', innerjoin=True),\
@@ -1319,9 +1332,9 @@ if __name__ == '__main__':
     db.init_app(app)
 
     # Create and configure APScheduler
-    sched = APScheduler()
-    sched.init_app(app)
-    sched.start()
+#    sched = APScheduler()
+#    sched.init_app(app)
+#    sched.start()
 
     # Create all DB tables if necessary
     db.create_all()
-- 
GitLab