use gerrit class to handle comments posting, fix attachment bug
authorHasan Wan <hasan.wan@intel.com>
Mon, 13 Aug 2012 08:19:09 +0000 (16:19 +0800)
committerLin Yang <lin.a.yang@intel.com>
Tue, 28 Aug 2012 03:23:07 +0000 (11:23 +0800)
Change-Id: Ic00b720d3dabdf60e7a8bd07ed5e00858358f5e6
Signed-off-by: Hasan Wan <hasan.wan@intel.com>
buildcheck.py

index 676fd95..61531fd 100755 (executable)
@@ -16,6 +16,7 @@ import gzip
 from common import obspkg
 from common import utils
 from common import git
+from common import gerrit
 from common import runner
 from common.envparas import *
 from common import buildservice
@@ -29,10 +30,10 @@ envparas = ['JENKINS_HOME',
             'GERRIT_USERNAME',
             'GERRIT_HOSTNAME',
             'GERRIT_PROJECT',
-            'GERRIT_CHANGE_NUMBER',
             'GERRIT_REFSPEC',
-            'GERRIT_PATCHSET_NUMBER',
             'GERRIT_BRANCH',
+            'GERRIT_CHANGE_OWNER',
+            'GERRIT_PATCHSET_REVISION',
             'BUILD_NUMBER',
             'BUILD_TAG',
             'OBS_USERNAME',
@@ -42,7 +43,10 @@ export(envparas, locals())
 GERRIT_CMD = 'ssh -p %s %s@%s gerrit' % (GERRIT_SSHPORT, GERRIT_USERNAME, GERRIT_HOSTNAME)
 GIT_URL = 'ssh://%s@%s:%s' % (GERRIT_USERNAME, GERRIT_HOSTNAME, GERRIT_SSHPORT)
 
-def sendmail(subject, body, mail_from, mail_to):
+
+g=gerrit.Gerrit(GERRIT_HOSTNAME, GERRIT_USERNAME, GERRIT_SSHPORT)
+
+def sendmail(subject, body, mail_from, mail_to, attachment = None):
     """ Prepare the envorinment parameter for trigger mail_sender job"""
     mail_env = {}
     mail_env['subject'] = subject
@@ -50,6 +54,9 @@ def sendmail(subject, body, mail_from, mail_to):
     mail_env['From'] = mail_from
     mail_env['To'] = mail_to
 
+    if attachment:
+        mail_env['attachment'] = attachment
+
     with open('%s.env' %(BUILD_TAG),'w') as f:
         for key in mail_env.keys():
             f.write('%s=%s\n' %(key, mail_env[key]))
@@ -135,24 +142,29 @@ if __name__ == '__main__':
 
         msg='[BUILD CHECK RESULT] This change is built against OBS project %s: \n\n %-15s%-15s%-15s' %(obs_dest_prj, "repository", "arch", "result")
 
+        return_value = 0
+
         for repo in status.keys():
             for arch in status[repo]:
                 msg = msg + '\n\n %-15s%-15s%-15s' %(repo, arch, status[repo][arch])
                 if status[repo][arch] != 'succeeded':
-                    buildlog_gz = "%s/buildlog-%s-%s-%s.log.gz" %(WORKSPACE, spec.name, arch, status[repo][arch])
+                    return_value = -1
+                    tempdir = tempfile.mkdtemp(prefix=WORKSPACE+'/')
+                    buildlog_gz = "%s/buildlog-%s-%s-%s.log.gz" %(tempdir,spec.name, arch, status[repo][arch])
                     log=bs.getBuildLog(buildcheck_project, '%s/%s'%(repo,arch), spec.name)
                     f = gzip.open(buildlog_gz, "wb")
                     f.write(log)
                     f.close()
-                    sendmail('[BUILD CHECK RESULT]: Your change for %s is failed to build against %s' %(GERRIT_PROJECT, obs_dest_prj), msg, 'no-replay@tz-testing-jenkins.bj.intel.com', '%s' %GERRIT_CHANGE_OWNER_EMAIL, attachment = buildlog_gz)
-                    print 'The log file %s is deleted' %(buildlog_gz)
-                    os.remove(buildlog_gz)
+                    sendmail('[BUILD CHECK RESULT]: Your change for %s is failed to build against %s' %(GERRIT_PROJECT, obs_dest_prj), msg, 'no-replay@tz-testing-jenkins.bj.intel.com', '%s' %GERRIT_CHANGE_OWNER, attachment = buildlog_gz)
+
         if not status:
-            runner.show('%s %s %s,%s --message \'"%s"\'' % (GERRIT_CMD, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, "[BUILD CHECK RESULT] The project %s is not ready for build, please contact the build systerm administrator"))
+            msg = "[BUILD CHECK RESULT] The project %s is not ready for build, please contact the build systerm administrator" %(buildcheck_project)
+            g.review(commit = GERRIT_PATCHSET_REVISION, message = msg)
             end(1)
-            
+
         print 'The project %s is deleted on OBS' %(buildcheck_project)
         bs.deleteProject(buildcheck_project)
         print msg
-        runner.show('%s %s %s,%s --message \'"%s"\'' % (GERRIT_CMD, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, msg))
-    end()
+        g.review(commit = GERRIT_PATCHSET_REVISION, message = msg)
+
+    end(return_value)