Enhance the message of tag format check
authorLin Yang <lin.a.yang@intel.com>
Thu, 6 Sep 2012 09:30:53 +0000 (17:30 +0800)
committerLin Yang <lin.a.yang@intel.com>
Thu, 6 Sep 2012 09:30:53 +0000 (17:30 +0800)
submitobs.py

index fb6f397..85bbef9 100755 (executable)
@@ -20,6 +20,7 @@ from common import obspkg
 from common.envparas import *
 from common import errors
 from common import mysql
+from common import gerrit 
 from common.send_mail import prepare_mail
 
 import gbp.rpm
@@ -51,7 +52,6 @@ envparas = ['GERRIT_EVENT_TYPE',
             'RPMLINT_PRJ',
             'NOREPLY_EMAIL_SENDER']
 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)
 
 ret = { 'rc' : {'success' : 0, 'failure' : 0, 'retry' : 1},
@@ -66,6 +66,15 @@ ret = { 'rc' : {'success' : 0, 'failure' : 0, 'retry' : 1},
                                    'newRev' : GERRIT_NEWREV}},
       }
 
+WRONG_VERSION_MSG = 'The tag %s pushed, which means you want to submit commit %s in %s branch to OBS, but this commit does NOT exist in %s branch. please use correct branch name as version in tag name submit/{version}/{date.time}. Suggest to use "gbs submit" to trigger submission, or use following command to create tag "git tag submit/${version}/$(date --utc +%%Y%%m%%d.%%H%%M%%S) -m "****" ".'
+WRONG_DATE_MSG = 'The tag %s pushed, but time %s does NOT follow correct format. You can use shell command "date --utc +%%Y%%m%%d.%%H%%M%%S" to generate it, like 20120801.083113. Suggest to use "gbs submit" to trigger submission, or use following command to create tag "git tag submit/${version}/$(date --utc +%%Y%%m%%d.%%H%%M%%S) -m "****" ".'
+UNDER_REVIEW_MSG = 'The tag %s pushed, but the commit %s is still under review in gerrit. After reviewer accpet this commit, it will be submitted to OBS corresponding project.'
+WRONG_COMMIT_MSG = 'The tag %s pushed, but the commit %s does NOT exist in git tree or gerrit open change. Please make sure the commit has been pushed to gerrit and correct magical ref refs/for/branch.'
+UNKNOWN_FORMAT_MSG = 'The tag %s pushed, but unknown tag format, please follow the format submit/{version}/{date.time}. Suggest to use "gbs submit" to trigger submission, or use following command to create tag "git tag submit/${version}/$(date --utc +%%Y%%m%%d.%%H%%M%%S) -m "****" ".'
+NOT_ANNOTATED_MSG = 'Tag %s should be annotated tag. Suggest to use "gbs submit" to trigger submission, or use following command to create tag "git tag submit/${version}/$(date --utc +%%Y%%m%%d.%%H%%M%%S) -m "****" ".'
+TITLE_FAILED = '[Submit Request Failed]: tag: %s in %s'
+TITLE_PENDING = '[Submit Request Pending]: tag: %s in %s'
+
 def generate_logentry(gittag, tagname):
     """ Generate rpm format log entry """
     ret_list = []
@@ -148,13 +157,71 @@ def end(result = 'success'):
     db.update(ret['dbtable'][GERRIT_EVENT_TYPE], {'state' : ret['dbstate'][result]}, ret['dbkey'][GERRIT_EVENT_TYPE])
     exit(ret['rc'][result])
 
-if __name__ == '__main__':
+def check_tag_format(tag, git, gerrit):
+    """check whether tag follow proper format"""
+    global GERRIT_BRANCH
+
+    commitid = git.rev_parse(tag)
+    result = {}
+    if tag.find('/',len('submit/')) != -1:
+        branch = tag[len('submit/'):tag.rfind('/')]
+        date = tag[tag.rfind('/')+1:]
+        print 'check tag', branch, date
+        p = re.compile('^[0-9]{8}\.[0-9]{6}$')
+        if p.match(date):
+            if branch == 'trunk':
+                branch = 'master'
+            branches = git.branch_contains(tag)
+            print 'branches', branches
+            if branches:
+                # the tagged commit has been merged on gerrit
+                if branch not in branches:
+                    # The tag is on wrong branch
+                    result['title'] =  TITLE_FAILED % (tag, GERRIT_PROJECT)
+                    result['message'] = WRONG_VERSION_MSG % (tag, commitid, branch, branch)
+                else:
+                    GERRIT_BRANCH = branch
+                    print 'submit tag : %s, on branch %s'  % (tag, branch)
+            else:
+                # The tagged commit doesn't exist in git tree
+                gerritinfo = gerrit.query('--current-patch-set status: open project: %s commit: %s' % (GERRIT_PROJECT, commitid))
+                if len(gerritinfo) == 1 and gerritinfo[0].has_key('number') and gerritinfo[0].has_key('currentPatchSet'):
+                    # The tagged commit still open, abort submit this time
+                    print '\nThis change is still open in gerrit, exit now'
+                    result['title'] = TITLE_PENDING % (tag, GERRIT_PROJECT)
+                    result['message'] = UNDER_REVIEW_MSG % (tag, commitid)
+                else:
+                    # Cannot find the tagged commit in git tree or gerrit open change 
+                    print '\nCan not find this commit in gerrit open change, exit now'
+                    result['title'] = TITLE_FAILED % (tag, GERRIT_PROJECT)
+                    result['message'] = WRONG_COMMIT_MSG % (tag, commitid)
+        else:
+            # The time format in tag name is incorrect 
+            print '\nThe time format in tag name is incorrect, exit now'
+            result['title'] = TITLE_FAILED % (tag, GERRIT_PROJECT)
+            result['message'] = WRONG_DATE_MSG % (tag, date) 
+    else:
+        print 'tag %s don\'t match the name format, exit now' % tag
+        result['title'] = TITLE_FAILED % (tag, GERRIT_PROJECT)
+        result['message'] = UNKNOWN_FORMAT_MSG % tag
 
-    print '---[JOB STARTED]----------------------------------------'
-    tmpdir = tempfile.mkdtemp(prefix=WORKSPACE+'/')
-    prjdir = os.path.join(tmpdir, GERRIT_PROJECT)
-    prjpath, prj = os.path.split(GERRIT_PROJECT)
+    tagger = git.get_tag(tag)
+    if not tagger.has_key('author') or not tagger.has_key('email'):
+        if not result.has_key('title'):
+            result['title'] = TITLE_FAILED % (tag, GERRIT_PROJECT)
+        if result.has_key('message'):
+            result['message'] += '\n\nTag %s should be annotated tag.' % tag
+        else:
+            result['message'] = NOT_ANNOTATED_MSG % tag
 
+    return result
+
+def main():
+    # global variable
+    global GERRIT_PATCHSET_REVISION
+    print '---[JOB STARTED]----------------------------------------'
     # check whether tag name is start with 'submit/'
     if GERRIT_EVENT_TYPE == "REF_UPDATED":
         if not GERRIT_REFNAME.startswith('refs/tags/submit/'):
@@ -194,32 +261,11 @@ if __name__ == '__main__':
         end('success')
 
     mygit = git.Git(prjdir)
+    mygerrit = gerrit.Gerrit(GERRIT_HOSTNAME, GERRIT_USERNAME, GERRIT_SSHPORT)
 
     checktagmsg = ''
     if GERRIT_EVENT_TYPE == "REF_UPDATED":
         tag = GERRIT_REFNAME[len('refs/tags/'):]
-        if tag.find('/',len('submit/')) != -1:
-            branch = tag[len('submit/'):tag.find('/',len('submit/'))]
-            if branch == 'trunk':
-                branch = 'master'
-            branchs = mygit.branch_contains(GERRIT_REFNAME)
-            if branchs:
-                # the tagged commit has been merged on gerrit
-                if branch not in branchs:
-                    title = '[Submit Request Failed]: improper tag: %s' % tag
-                    checktagmsg = 'The tag %s pushed, but the related commit does NOT exist in %s branch. please follow the below format submit/{version}/{date.time}. Suggest to use gbs submit to trigger submission.' % (tag, branch)
-                else:
-                    GERRIT_BRANCH = branch
-                    print 'submit tag : %s, on branch %s'  % (tag, branch)
-            else:
-                # The tagged commmit still open, abort submit this time
-                print '\nThis change is still open in gerrit, exit now'
-                title = '[Submit Request Pending]: tag: %s' % tag
-                checktagmsg = 'The commit tag %s attached is still under review in gerrit. After reviewer accpet this commit, it will be submitted to OBS corresponding project.' % tag
-        else:
-            print 'tag %s don\'t match the name format, exit now' % tag
-            title = '[Submit Request Failed]: unknown tag: %s' % tag
-            checktagmsg = 'The tag %s pushed, but unknown tag format, please follow the below format submit/{version}/{date.time}. Suggest to use gbs submit to trigger submission.' % tag
     else:
         # Only when there is tag submit/*, it's for submit
         if GERRIT_BRANCH == 'master':
@@ -233,29 +279,23 @@ if __name__ == '__main__':
             print '\nThis change don\'t contain submit/*/* tag, exit now'
             end('success')
 
+    tagcheck = check_tag_format(tag, mygit, mygerrit)
     tagger = mygit.get_tag(tag)
-    if not tagger.has_key('author') or not tagger.has_key('email'):
-        print '\ntag %s is not annotated tag, exit now' % tag
-        end('failure')
-    if checktagmsg and title:
-        print checktagmsg
-        checktagmsg = 'Hi, %s,\n\n' % tagger['author'] + checktagmsg + '\n\n----------------------------------------------------------------\nAutomatically generated by backend service.\nPlease DO NOT Reply!'
-        prepare_mail("%s.env" %(BUILD_TAG), title, checktagmsg, NOREPLY_EMAIL_SENDER, tagger['email'])
+    if not GERRIT_PATCHSET_REVISION:
+        GERRIT_PATCHSET_REVISION = mygit.rev_parse(tag)
+    commitinfo = mygit.get_commit_info(GERRIT_PATCHSET_REVISION)
+    if tagcheck:
+        print tagcheck
+        mygerrit.review(commit = GERRIT_PATCHSET_REVISION, message = tagcheck['message'])
+        if tagger.has_key('author') and tagger.has_key('email'):
+            tagcheckmsg = 'Hi, %s,\n\n' % tagger['author'] + tagcheck['message'] + '\n\n----------------------------------------------------------------\nAutomatically generated by backend service.\nPlease DO NOT Reply!'
+            prepare_mail("%s.env" %(BUILD_TAG), tagcheck['title'], tagcheckmsg, NOREPLY_EMAIL_SENDER, tagger['email'])
         end('failure')
 
     packagingdir = utils.parse_link('%s/%s' % (prjdir, 'packaging'))
     print('packaging dir is %s/%s' % (prjdir, packagingdir))
 
-    if not GERRIT_PATCHSET_REVISION:
-        GERRIT_PATCHSET_REVISION = mygit.rev_parse(tag)
-    commitinfo = mygit.get_commit_info(GERRIT_PATCHSET_REVISION)
-    if GERRIT_EVENT_TYPE == "REF_UPDATED": 
-        gerritinfo = utils.get_gerrit_info(GERRIT_PROJECT, commitinfo['id'])
-        print 'gerritinfo', gerritinfo
-        if gerritinfo:
-            GERRIT_CHANGE_NUMBER = gerritinfo['changenum']
-            GERRIT_PATCHSET_NUMBER = gerritinfo['patchsetnum']
-    msg = 'Submitter: %s <%s>\nComments: %s\nGit project: %s\nTag:%s\nCommit: %s %s' % (tagger['author'], tagger['email'], tagger['message'], GERRIT_PROJECT, tag, commitinfo['id'], commitinfo['subject'])
+    msg = 'Submitter: %s <%s>\nComments: %s\nGit project: %s\nTag: %s\nCommit: %s %s' % (tagger['author'], tagger['email'], tagger['message'], GERRIT_PROJECT, tag, commitinfo['id'], commitinfo['subject'])
 
     mygit.checkout(tag)
 
@@ -309,10 +349,10 @@ if __name__ == '__main__':
             shutil.copy2(os.path.join(tarballdir, myfile), os.path.join(oscworkdir, myfile)) 
 
         # update changelog with git tag info
-        update_changelog(localpkg, mygit, tag)
+        #update_changelog(localpkg, mygit, tag)
 
         # update gerritinfo with git info
-        update_gerritinfo(localpkg, GERRIT_PROJECT, GERRIT_PATCHSET_REVISION)
+        #update_gerritinfo(localpkg, GERRIT_PROJECT, GERRIT_PATCHSET_REVISION)
 
         localpkg.update_local()
 
@@ -341,15 +381,23 @@ if __name__ == '__main__':
 
         # post sr info back to gerrit
         requrl = ' %s/request/show/%s' % (OBS_URL, newreq)
-        comment = 'A SR (Submit Request) has been trigger to submit the commit to OBS %s project.\n- Submitter: %s <%s>\n- Comments: %s\n- Git project: %s\n- Tag:%s\n- Commit: %s %s\n- Request URL:%s' % (obs_dst_prj, tagger['author'], tagger['email'], tagger['message'], GERRIT_PROJECT, tag, commitinfo['id'], commitinfo['subject'], requrl)
+        comment = 'A SR (Submit Request) has been trigger to submit the commit to OBS %s project.\n- Submitter: %s <%s>\n- Comments: %s\n- Git project: %s\n- Tag: %s\n- Commit: %s %s\n- Request URL:%s' % (obs_dst_prj, tagger['author'], tagger['email'], tagger['message'], GERRIT_PROJECT, tag, commitinfo['id'], commitinfo['subject'], requrl)
+        mygerrit.review(commit = GERRIT_PATCHSET_REVISION, message = comment)
 
-        if GERRIT_CHANGE_NUMBER and GERRIT_CHANGE_NUMBER:
-            print '%s %s %s,%s --message \'"%s"\'' % (GERRIT_CMD, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, comment)
-            runner.show('%s %s %s,%s --message \'"%s"\'' % (GERRIT_CMD, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, comment))
-        else:
-            print 'GERRIT_CHANGE_NUMBER %s and GERRIT_CHANGE_NUMBER %s is empty.' % (GERRIT_CHANGE_NUMBER, GERRIT_CHANGE_NUMBER)
+        '''
+        comment = 'Hi, %s,\n\n' % tagger['author'] + comment + '\n\n----------------------------------------------------------------\nAutomatically generated by backend service.\nPlease DO NOT Reply!' 
+        prepare_mail("%s.env" %(BUILD_TAG), '[Submit Request]: changes to %s/%s' % (obs_dst_prj, spec.name), comment, FROM_EMAIL, tagger['email'])
+        '''
+    end('success')
 
-        #comment = 'Hi, %s,\n\n' % tagger['author'] + comment + '\n\n----------------------------------------------------------------\nAutomatically generated by backend service.\nPlease DO NOT Reply!' 
-        #prepare_mail("%s.env" %(BUILD_TAG), '[Submit Request]: changes to %s/%s' % (obs_dst_prj, spec.name), comment, FROM_EMAIL, tagger['email'])
+if __name__ == '__main__':
+    # prepare separate temp directory for each build
+    tmpdir = tempfile.mkdtemp(prefix=WORKSPACE+'/')
+    prjdir = os.path.join(tmpdir, GERRIT_PROJECT)
+    prjpath, prj = os.path.split(GERRIT_PROJECT)
 
-    end('success')
+    try:
+        main()
+    except Exception, exc:
+        print exc
+        end('failure')