Updating git commits into dashboard db 55/168455/1
authorhyokeun <hyokeun.jeon@samsung.com>
Sat, 27 Jan 2018 08:54:16 +0000 (17:54 +0900)
committerhyokeun <hyokeun.jeon@samsung.com>
Sat, 27 Jan 2018 08:54:16 +0000 (17:54 +0900)
Change-Id: Idb66e55e3998f41c4fb95862031f34bc9466bb18

common/buildmonitor_extention.py
common/git.py
job_update_local_git.py

index 8f2d8df..a9395c9 100644 (file)
@@ -47,6 +47,25 @@ class BuildMonitorExtention(object):
     def is_connect(self):
         return self.connect
 
+    def get_user_email_id(self, email):
+
+        if not self.is_connect():
+            return
+
+        query = "SELECT id FROM user_email WHERE email = %s"
+        query_data = (email,)
+
+        return get_value_from_query_data(query, query_data)
+
+    def update_user_email(self, email):
+
+        if not self.is_connect():
+            return
+
+        query = "INSERT INTO user_email (email) SELECT * FROM (SELECT %s) AS tmp \
+                 WHERE NOT EXISTS (SELECT email FROM user_email WHERE email = %s) LIMIT 1"
+        do_query(query, (email, email))
+
     def get_auto_reject_projects(self):
         # get project in database
         # return : list
@@ -94,6 +113,25 @@ class BuildMonitorExtention(object):
 
         return build_project_id
 
+    def get_git_branch_id(self, branch):
+
+        if not self.is_connect():
+            return
+
+        query = "SELECT id FROM git_branch WHERE name = %s"
+        query_data = (branch,)
+
+        return get_value_from_query_data(query, query_data)
+
+    def update_git_branch(self, branch):
+
+        if not self.is_connect():
+            return
+
+        query = "INSERT INTO git_branch (name) SELECT * FROM (SELECT %s) AS tmp \
+                 WHERE NOT EXISTS (SELECT name FROM git_branch WHERE name = %s) LIMIT 1"
+        do_query(query, (branch, branch))
+
     def get_repository_id(self, repo_name):
 
         if not self.is_connect():
@@ -147,6 +185,61 @@ class BuildMonitorExtention(object):
             do_query(query, ('%d' % git_path_id, commit_id, commit_date, \
                              '%d' % git_path_id, commit_id))
 
+    def update_git_commit_strict(self, \
+                             git_path=None, \
+                             commit_id=None, \
+                             committer=None, \
+                             commit_date=None, \
+                             commit_message=None, \
+                             branch=None):
+
+        if not self.is_connect():
+            return
+
+        if git_path is None or commit_id is None or committer is None \
+            or commit_date is None or commit_message is None:
+            return
+
+        self.update_repository(git_path)
+        db_git_path_id = self.get_repository_id(git_path)
+
+        self.update_user_email(committer)
+        db_user_email_id = self.get_user_email_id(committer)
+
+        if branch is None or branch is '':
+            branch = ''
+        self.update_git_branch(branch)
+        db_git_branch_id = self.get_git_branch_id(branch)
+
+        if db_git_path_id == 0 or db_user_email_id == 0 or db_git_branch_id == 0:
+            return
+
+        db_git_commit_id = self.get_commit_id(db_git_path_id, commit_id)
+
+        if db_git_commit_id == 0:
+            query = "INSERT INTO git_commit \
+                     (git_repository_id, commit_id, commit_date, commit_message, \
+                     committer_id, branch_id) \
+                     VALUES(%s, %s, %s, %s, %s, %s)"
+            query_data = (db_git_path_id, commit_id, commit_date, commit_message, \
+                          db_user_email_id, db_git_branch_id)
+            do_query(query, query_data)
+        else:
+            query = "UPDATE git_commit SET \
+                     git_repository_id=%s, commit_id=%s, commit_date=%s, commit_message=%s, \
+                     committer_id=%s, branch_id=%s \
+                     WHERE (id=%s) \
+                     AND (commit_date <> %s OR commit_message <> %s \
+                     OR committer_id <> %s OR branch_id <> %s)\
+                     LIMIT 1"
+            query_data = (db_git_path_id, commit_id, commit_date, commit_message, \
+                          db_user_email_id, db_git_branch_id, db_git_commit_id,
+                          commit_date, commit_message, db_user_email_id, db_git_branch_id)
+            do_query(query, query_data)
+            db_git_commit_id = self.get_commit_id(db_git_path_id, commit_id)
+
+        return db_git_commit_id
+
     def get_tag_id(self, git_commit_id, tag_name, tag_revision):
 
         if not self.is_connect():
@@ -167,6 +260,70 @@ class BuildMonitorExtention(object):
         do_query(query, ('%d' % git_commit_id, tag_name, tag_revision, tagger, tag_date, tag_message, \
                          '%d' % git_commit_id, tag_revision))
 
+    def delete_git_tag(self, git_path, tag_name, tag_rev, deleted_by, deleted_at):
+
+        self.update_user_email(deleted_by)
+        db_user_email_id = self.get_user_email_id(deleted_by)
+
+        query = "SELECT gt.id FROM git_tag gt, git_commit gc, git_repository gr \
+                 WHERE gr.name=%s AND gr.id=gc.git_repository_id AND gc.id=gt.git_commit_id \
+                 AND gt.tag_name=%s AND gt.tag_revision=%s \
+                 LIMIT 1"
+        query_data = (git_path, tag_name, tag_rev)
+        db_git_tag_id = get_value_from_query_data(query, query_data)
+        if db_git_tag_id == 0:
+            return
+
+        query = "UPDATE git_tag SET deleted_by=%s, deleted_at=%s \
+                 WHERE id=%s AND tag_name=%s AND tag_revision=%s \
+                 LIMIT 1"
+        query_data = (db_user_email_id, deleted_at, \
+                      db_git_tag_id, tag_name, tag_rev)
+        do_query(query, query_data)
+
+    def update_git_tag_strict(self, \
+                              db_git_commit_id=None, \
+                              tag_name=None, \
+                              tag_rev=None, \
+                              tagger=None, \
+                              tag_date=None, \
+                              tag_message=None):
+
+        if not self.is_connect():
+            return
+
+        if db_git_commit_id is None or tag_name is None or tag_rev is None \
+            or tagger is None or tag_date is None or tag_message is None:
+            return
+
+        self.update_user_email(tagger)
+        db_user_email_id = self.get_user_email_id(tagger)
+
+        if db_user_email_id == 0:
+            return
+
+        db_git_tag_id = self.get_tag_id(db_git_commit_id, tag_name, tag_rev)
+
+        if db_git_tag_id == 0:
+            query = "INSERT INTO git_tag \
+                     (git_commit_id, tag_name, tag_revision, \
+                     tag_date, tag_message, tagger_id) \
+                     VALUES(%s, %s, %s, %s, %s, %s)"
+            query_data = (db_git_commit_id, tag_name, tag_rev, \
+                          tag_date, tag_message, db_user_email_id)
+            do_query(query, query_data)
+        else:
+            query = "UPDATE git_tag SET \
+                     git_commit_id=%s, tag_name=%s, tag_revision=%s, \
+                     tag_date=%s, tag_message=%s, tagger_id=%s \
+                     WHERE (id=%s) \
+                     AND (tag_date <> %s OR tag_message <> %s OR tagger_id <> %s)\
+                     LIMIT 1"
+            query_data = (db_git_commit_id, tag_name, tag_rev, \
+                          tag_date, tag_message, db_user_email_id, db_git_tag_id, \
+                          tag_date, tag_message, db_user_email_id)
+            do_query(query, query_data)
+
     def get_obs_package_id(self, package_name):
 
         if not self.is_connect():
@@ -295,4 +452,3 @@ def get_snapshot_manifest(bm_ext, project):
 
     return manifest_list
 
-
index b4df238..3f780f5 100644 (file)
@@ -187,6 +187,45 @@ class Git(GitRepository):
         args = GitArgs(remote, ':' + tag)
         self._git_command("push", args.args)
 
+    def get_commit_info(self, commitid):
+        data =  {"commit_id": None, \
+                "committer": None, \
+                "commit_date": None, \
+                "commit_message": None}
+        try:
+            t_outs, t_err, t_code = self._git_inout('log', \
+                ['-1', '--date=iso', \
+                 '--format=%H\n%aE\n%ad\n%s\n%b', \
+                 commitid])
+            log_list = t_outs.rstrip().split('\n')
+            data["commit_id"]      = log_list[0]
+            data["committer"]      = log_list[1]
+            data["commit_date"]    = log_list[2]
+            data["commit_message"] = log_list[3]
+        except Exception as err:
+            print repr(err)
+        return data
+
+    def get_tag_info(self, tag):
+        data = {"tag_name": None,
+                "tag_rev": None,
+                "tagger": None,
+                "tag_date": None,
+                "tag_message": None}
+        try:
+            t_outs, t_err, t_code = self._git_inout('for-each-ref', ['--sort=-taggerdate', '--count=1', \
+                '--format=%(refname)%0a%(objectname)%0a%(taggeremail)%0a%(taggerdate:iso)%0a%(subject)', \
+                tag])
+            log_list = t_outs.rstrip().split('\n')
+            data["tag_name"]    = log_list[0]
+            data["tag_rev"]     = log_list[1]
+            data["tagger"]      = log_list[2].replace('<','').replace('>','')
+            data["tag_date"]    = log_list[3]
+            data["tag_message"] = log_list[4]
+        except Exception as err:
+            print repr(err)
+        return data
+
 def _update_gitproject(localdir, gitpath=None):
     """Fetch latest code to local dir"""
 
index cc77485..c2293c5 100755 (executable)
@@ -24,7 +24,64 @@ import os
 import sys
 
 from common.gerrit import get_gerrit_event
-from common.git import clone_gitproject
+from common.git import Git, clone_gitproject
+
+from common.buildmonitor_extention import BuildMonitorExtention
+from datetime import datetime
+
+def update_dashboard(full_git_path):
+
+    #Only for ref-updated
+    if not os.getenv('GERRIT_EVENT_TYPE', None) \
+        or not os.getenv('GERRIT_REFNAME', None) \
+        or not os.getenv('GERRIT_NEWREV', None) \
+        or not os.getenv('GERRIT_PROJECT', None):
+        return
+
+    bm_ext = BuildMonitorExtention()
+
+    git_dir = '%s.git' % os.path.join(os.getenv('GIT_CACHE_DIR'), full_git_path)
+    mygit = Git(git_dir)
+    refname = os.getenv('GERRIT_REFNAME')
+    project = os.getenv('GERRIT_PROJECT')
+    newrev = os.getenv('GERRIT_NEWREV')
+    account_email = os.getenv('GERRIT_EVENT_ACCOUNT_EMAIL', None)
+
+    is_ref_deleted = False
+    if newrev == '0000000000000000000000000000000000000000':
+        is_ref_deleted = True
+
+    if refname.startswith('refs/tags/'):
+        branch = None
+        reference = refname
+    else:
+        branch = refname
+        reference = newrev
+
+    ## Tag and Commit have the same commit info
+    if is_ref_deleted != True:
+        ret_data = mygit.get_commit_info(reference)
+        commit_id      = ret_data["commit_id"]
+        committer      = ret_data["committer"]
+        commit_date    = ret_data["commit_date"]
+        commit_message = ret_data["commit_message"]
+        db_git_commit_id = bm_ext.update_git_commit_strict( \
+            full_git_path, newrev, committer, commit_date, commit_message, branch)
+
+    ## Tag
+    if refname.startswith('refs/tags/'):
+        if is_ref_deleted == True:
+            bm_ext.delete_git_tag(full_git_path, reference, os.getenv('GERRIT_OLDREV'), \
+                account_email, datetime.now())
+        else:
+            ret_data = mygit.get_tag_info(refname)
+            tag_name    = ret_data["tag_name"]
+            tag_rev     = ret_data["tag_rev"]
+            tagger      = ret_data["tagger"]
+            tag_date    = ret_data["tag_date"]
+            tag_message = ret_data["tag_message"]
+            bm_ext.update_git_tag_strict(db_git_commit_id, tag_name, tag_rev, \
+                tagger, tag_date, tag_message)
 
 def main():
     """The main body"""
@@ -44,5 +101,8 @@ def main():
             os.path.join(os.getenv('GIT_CACHE_DIR'), events['project']),
             bare=True)
 
+    update_dashboard(events['project'])
+
 if __name__ == '__main__':
     sys.exit(main())
+