--- /dev/null
+#!/usr/bin/
+#
+# Copyright (c) 2018 Samsung Electronics.Co.Ltd.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; version 2 of the License
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+# for more details.
+#
+"""Buildmonitor extention"""
+
+import os
+import sys
+
+from buildmonitor_db import connect_db, disconnect_db, do_query, do_many_query, \
+ get_multi_values_from_query_data, get_value_from_query_data
+
+class BuildMonitorExtention(object):
+ """ Interface to BuildMonitor(Database) """
+ def __init__(self):
+ if os.getenv("BUILDMONITOR_ENABLED", "0") != "0":
+ self.enabled = True
+ #connect
+ self.conn = connect_db()
+ self.connect = True
+ else:
+ self.enabled = False
+ self.conn = None
+ self.connect = False
+ print 'BuildMonitorExtention init'
+
+ def __enter__(self):
+ print 'BuildMonitorExtention enter'
+ return self
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ #disconnect
+ if self.conn:
+ disconnect_db()
+ self.connect = False
+ print 'BuildMonitorExtention exit'
+
+ def is_connect(self):
+ return self.connect
+
+ def get_auto_reject_projects(self):
+ # get project in database
+ # return : list
+
+ if not self.is_connect():
+ return []
+
+ prj_list = []
+
+ query = "SELECT name FROM main_project WHERE sr_auto_reject = %s"
+ query_data = "1"
+ projects = get_multi_values_from_query_data(query, query_data)
+ for prj in projects:
+ prj_list.append("%s" %(prj))
+
+ return prj_list
+
+ def get_build_project_id(self, project):
+
+ if not self.is_connect():
+ return
+
+ # get post_build_project_id
+ query = "SELECT bp.id \
+ FROM build_project bp \
+ WHERE bp.info_project_id = (SELECT id FROM info_project WHERE project_name = %s ) \
+ AND bp.id > (SELECT bp.id FROM build_project bp, info_project ip, build_snapshot bs \
+ WHERE ip.project_name = %s AND ip.id = bp.info_project_id AND bp.id = bs.build_project_id \
+ ORDER BY id desc LIMIT 1) \
+ ORDER BY id LIMIT 1"
+ query_data = (project, project)
+ build_project_id = get_value_from_query_data(query, query_data)
+ print "1)build_project_id(%s)" % (build_project_id)
+ if build_project_id == 0:
+ # get last_bpid_of_build_snapshot
+ query = "SELECT bp.id \
+ FROM build_project bp, info_project ip, build_snapshot bs \
+ WHERE ip.project_name = %s \
+ AND ip.id = bp.info_project_id \
+ AND bp.id = bs.build_project_id \
+ ORDER BY id desc LIMIT 1"
+ query_data = (project,)
+ build_project_id = get_value_from_query_data(query, query_data)
+ print "2)build_project_id(%s)" % (build_project_id)
+
+ return build_project_id
+
+ def get_repository_id(self, repo_name):
+
+ if not self.is_connect():
+ return
+
+ # Select id from git_repository
+ query = "SELECT id FROM git_repository WHERE name = %s"
+ query_data = (repo_name,)
+
+ return get_value_from_query_data(query, query_data)
+
+ def update_repository(self, repo_name):
+
+ if not self.is_connect():
+ return
+
+ query = "INSERT INTO git_repository (name) SELECT * FROM (SELECT %s) AS tmp \
+ WHERE NOT EXISTS (SELECT name FROM git_repository WHERE name = %s) LIMIT 1"
+ do_query(query, (repo_name, repo_name))
+
+ def get_commit_id(self, git_path_id, commit_id):
+
+ if not self.is_connect():
+ return
+
+ # Select id from git_commit
+ query = "SELECT id FROM git_commit WHERE git_repository_id = %s AND commit_id = %s"
+ query_data = (git_path_id, commit_id)
+
+ return get_value_from_query_data(query, query_data)
+
+ def update_commit(self, git_path_id, commit_id, commit_date, committer=None, commit_message=None):
+
+ if not self.is_connect():
+ return
+
+ if committer and commit_message:
+ query = "INSERT INTO git_commit (git_repository_id, commit_id, committer, commit_date, commit_message) \
+ SELECT * FROM (SELECT %s, %s, %s, %s, %s) AS tmp \
+ WHERE NOT EXISTS (SELECT git_repository_id, commit_id \
+ FROM git_commit WHERE git_repository_id = %s AND commit_id = %s) \
+ LIMIT 1"
+ do_query(query, ('%d' % git_path_id, commit_id, committer, commit_date, commit_message, \
+ '%d' % git_path_id, commit_id))
+ else:
+ query = "INSERT INTO git_commit (git_repository_id, commit_id, commit_date) \
+ SELECT * FROM (SELECT %s, %s, %s) AS tmp \
+ WHERE NOT EXISTS (SELECT git_repository_id, commit_id \
+ FROM git_commit WHERE git_repository_id = %s AND commit_id = %s) \
+ LIMIT 1"
+ do_query(query, ('%d' % git_path_id, commit_id, commit_date, \
+ '%d' % git_path_id, commit_id))
+
+ def get_tag_id(self, git_commit_id, tag_name, tag_revision):
+
+ if not self.is_connect():
+ return
+
+ query = 'SELECT id FROM git_tag WHERE git_commit_id=%s AND tag_name=%s AND tag_revision=%s'
+ return get_value_from_query_data(query, (git_commit_id, tag_name, tag_revision))
+
+ def update_tag(self, git_commit_id, tag_name, tag_revision, tagger, tag_date, tag_message):
+
+ if not self.is_connect():
+ return
+
+ query = "INSERT INTO git_tag (git_commit_id, tag_name, tag_revision, tagger, tag_date, tag_message) \
+ SELECT * FROM (SELECT %s, %s, %s, %s, %s, %s) AS tmp \
+ WHERE NOT EXISTS (SELECT git_commit_id, tag_revision \
+ FROM git_tag WHERE git_commit_id = %s AND tag_revision = %s) LIMIT 1"
+ do_query(query, ('%d' % git_commit_id, tag_name, tag_revision, tagger, tag_date, tag_message, \
+ '%d' % git_commit_id, tag_revision))
+
+ def get_obs_package_id(self, package_name):
+
+ if not self.is_connect():
+ return
+
+ # Select id from git_repository
+ query = "SELECT id FROM obs_package WHERE name = %s"
+ query_data = (package_name,)
+
+ return get_value_from_query_data(query, query_data)
+
+ def update_obs_package(self, package_name):
+
+ if not self.is_connect():
+ return
+
+ query = "INSERT INTO obs_package (name) SELECT * FROM (SELECT %s) AS tmp \
+ WHERE NOT EXISTS (SELECT name FROM obs_package WHERE name = %s) LIMIT 1"
+ do_query(query, (package_name, package_name))
+
+
+ def get_manifest(self, build_project_id):
+
+ if not self.is_connect():
+ return
+
+ manifest_list = []
+ # Select git_repository name
+ query = "SELECT gr.name FROM snapshot_manifest sm, git_repository gr, git_commit gc \
+ WHERE sm.build_project_id = %s \
+ AND gr.id = sm.git_repository_id \
+ AND gc.id = sm.git_commit_id \
+ GROUP BY gr.name"
+ query_data = (build_project_id,)
+ rows = get_multi_values_from_query_data(query, query_data)
+ if rows == 0:
+ return manifest_list
+ for x in rows:
+ manifest_list.append("%s" % x)
+
+ return manifest_list
+
+ def update_manifest_bulkdata(self, manifest_data_bulk_data):
+
+ if not self.is_connect():
+ return
+
+ query = "INSERT INTO snapshot_manifest (build_project_id, git_repository_id, git_commit_id, obs_package_id, date) VALUES \
+ (%s, %s, %s, %s, %s)"
+ do_many_query(query, manifest_data_bulk_data)
+
+ def update_sr_accept_log(self, profile, sr_tag, repa_user, comment, decision):
+
+ if not self.is_connect():
+ return
+
+ # reject
+ repa_decision_db = 'R'
+ if decision == 'accept':
+ repa_decision_db = 'A'
+
+ query = "INSERT INTO sr_accept_log (sr, profile, repa_user, comment, decision, status) " \
+ " VALUES(%s, %s, %s, %s, %s, 'R')"
+ query_data = ( sr_tag, profile, repa_user, comment, repa_decision_db)
+ do_query(query, query_data)
+
+def put_snapshot_manifest(bm_ext, project, manifest_items, start_datetime):
+ """
+ PUT Snapshot Manifest
+ """
+ # bulk data
+ # INSERT INTO TABLE_A (A, B, C, D) VALUES
+ # (1,1,1,1),
+ # (2,2,2,2),
+ # (3,3,3,3),
+ # (4,4,4,4);
+ if not bm_ext.enabled:
+ print "Skip Put snapshot manifest..."
+ return False
+
+ build_project_id = bm_ext.get_build_project_id(project)
+ manifest_data = []
+ # getid from git_repository
+ for git_repo, git_commit, obs_name in manifest_items:
+
+ bm_ext.update_repository(git_repo)
+ git_repository_id = bm_ext.get_repository_id(git_repo)
+
+ bm_ext.update_commit(git_repository_id, git_commit, start_datetime)
+ git_commit_id = bm_ext.get_commit_id(git_repository_id, git_commit)
+
+ bm_ext.update_obs_package(obs_name)
+ obs_pkg_name_id = bm_ext.get_obs_package_id(obs_name)
+ manifest_data.append((build_project_id, git_repository_id, git_commit_id, obs_pkg_name_id, start_datetime))
+
+ bm_ext.update_manifest_bulkdata(manifest_data)
+
+def get_snapshot_manifest(bm_ext, project):
+ """
+ Get snapshot manifest
+ """
+ manifest_list = []
+
+ if not bm_ext.enabled:
+ print "Skip Get snapshot manifest..."
+ return
+
+ build_project_id = bm_ext.get_build_project_id(project)
+
+ manifest_list = bm_ext.get_manifest(build_project_id)
+
+ return manifest_list
+
+
from common.buildservice import BuildService
from common.buildtrigger import trigger_info, trigger_next
from common import runner
-from common import buildmonitor_db
from datetime import datetime
+from common.buildmonitor_extention import BuildMonitorExtention
# set default char-set endcoding to utf-8
reload(sys)
return purge_sr_tag
-def get_project_config():
- # get project in database
- # return : list
- projectlist = []
-
- query = "SELECT name " \
- " FROM main_project WHERE sr_auto_reject = %s"
- query_data = "1"
- projects = buildmonitor_db.get_multi_values_from_query_data(query, query_data)
- for project in projects:
- print 'project = %s ' %(project)
- projectlist.append("%s" %(project))
-
- return projectlist
-
-def insert_repa_reject(profile, sr_tag, repa_user, comment, decision):
- print "[%s] insert_repa %s, %s, %s, %s, %s\n", (__file__, sr_tag, profile, repa_user, comment, \
- decision)
- # reject
- repa_decision_db = 'R'
- if decision == 'accept':
- repa_decision_db = 'A'
-
- query = "INSERT INTO sr_accept_log (sr, profile, repa_user, comment, decision, status) " \
- " VALUES(%s, %s, %s, %s, %s, 'R')"
- query_data = ( sr_tag, profile, repa_user, comment, repa_decision_db)
- buildmonitor_db.do_query(query, query_data)
-
def trigger_next_to_repa(project, tag, obs_user, decision, comment, count):
""" trigger next to repa """
print '---[JOB STARTED: repa auto reject]-------------------------'
build = BuildService(obs_api, obs_user, obs_passwd)
-
- buildmonitor_db.connect_db()
+ bm_ext = BuildMonitorExtention()
count = 0
repa_decision = 'declined'
- for project in get_project_config():
+ for project in bm_ext.get_auto_reject_projects():
for sr_tag in auto_reject_submission_list(build, project):
print 'sr_tag : %s' %(sr_tag)
repa_comment = "Auto Rejected Submission " + sr_tag
- insert_repa_reject( project, sr_tag, obs_user, repa_comment, repa_decision)
+ bm_ext.update_sr_accept_log( project, sr_tag, obs_user, repa_comment, repa_decision)
trigger_next_to_repa(project, sr_tag, obs_user, repa_decision, repa_comment, count)
count += count + 1
- buildmonitor_db.disconnect_db()
-
return 0
if __name__ == '__main__':