parse branch name from tag name, submitted by gbs submit
authorLin Yang <lin.a.yang@intel.com>
Thu, 2 Aug 2012 08:54:18 +0000 (16:54 +0800)
committerLin Yang <lin.a.yang@intel.com>
Thu, 2 Aug 2012 08:54:18 +0000 (16:54 +0800)
buildcheck.py
envparas.py
git.py
policycheck.py
send_mail.py
submitobs.py

index 8e6449d..458ce4a 100755 (executable)
@@ -23,7 +23,6 @@ import gbp
 import buildservice
 from send_mail import sendmail
 
-mappingprj = 'scm/git-obs-mapping'
 gerritcmd = 'ssh -p %s %s@%s gerrit' % (GERRIT_SSHPORT, GERRIT_USERNAME, GERRIT_HOSTNAME)
 giturl = 'ssh://%s@%s:%s' % (GERRIT_USERNAME, GERRIT_HOSTNAME, GERRIT_SSHPORT)
 
@@ -61,7 +60,7 @@ if __name__ == '__main__':
 
     packagingdir = utils.parse_link('%s/%s' % (prjdir, 'packaging'))
 
-    mapping = utils.parse_mapping('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, mappingprj), GERRIT_PROJECT, 'trunk')
+    mapping = utils.parse_mapping('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, mappingprj), GERRIT_PROJECT, GERRIT_BRANCH)
 
     print "Project Mapping info: ", mapping
 
index 0df6e28..24c1e86 100644 (file)
@@ -13,6 +13,7 @@ envargs = [# General parameters
            'OBS_API_URL',
            'OBS_USERNAME',
            'OBS_OSCRC_PATH',
+           'OBS_URL',
            'MYSQL_HOSTNAME',
            'MYSQL_PORT', 
            'MYSQL_USERNAME',
@@ -23,6 +24,10 @@ envargs = [# General parameters
            'BUILD_NUMBER',
            'WORKSPACE',
            'MAPPING_PROJECT',
+           'FROM_EMAIL',
+           'SMTP_SERVER',
+           'mappingprj',
+           'rpmlintprj',
            # Change merged event
            'GERRIT_BRANCH',
            'GERRIT_CHANGE_NUMBER',
diff --git a/git.py b/git.py
index cfc96f2..e97a786 100644 (file)
--- a/git.py
+++ b/git.py
@@ -76,13 +76,14 @@ class Git:
         """
         return filter(None, self._exec_git('ls-files')[1].splitlines())
 
-    def rev_parse(self, name):
+    def rev_parse(self, name, *args):
         """ Find the SHA1 of a given name commit id"""
-        options = [ "--quiet", "--verify", name ]
-        cmd = ['git', 'rev-parse']
-        ret, commit = runner.runtool(' '.join(cmd + options))
-        if ret == 0:
-            return commit.strip()
+        options = ["--quiet", "--verify", name]
+        options += list(args)
+        with Workdir(self.path):
+            ret, outs = self._exec_git('rev-parse', options)
+        if not ret:
+            return outs.strip()
         else:
             return None
 
@@ -322,6 +323,8 @@ class Git:
             ret, outs = self._exec_git('show', list(args))
             if not ret:
                 return outs.splitlines()
+            else:
+                return None
 
     def fetch(self, *args):
         """Download objects and refs from another repository
@@ -338,6 +341,8 @@ class Git:
             ret, outs = self._exec_git('describe', list(args))
             if not ret:
                 return outs.strip()
+            else:
+                return None
 
     def pull(self, *args):
         """Fetch from and merge with another repository or a local branch
@@ -364,6 +369,8 @@ class Git:
             ret, outs = self._exec_git('format-patch', list(args))
         if not ret:
             return outs.splitlines()
+        else:
+            return None
 
     def branch_contains(self, cmitid):
         with Workdir(self.path):
index 1246672..ea3b305 100755 (executable)
@@ -17,7 +17,6 @@ import obspkg
 from envparas import *
 import gbp.rpm
 
-mappingprj = 'scm/git-obs-mapping'
 gerritcmd = 'ssh -p %s %s@%s gerrit' % (GERRIT_SSHPORT, GERRIT_USERNAME, GERRIT_HOSTNAME)
 giturl = 'ssh://%s@%s:%s' % (GERRIT_USERNAME, GERRIT_HOSTNAME, GERRIT_SSHPORT)
 
@@ -72,13 +71,20 @@ if __name__ == '__main__':
     packagingdir = utils.parse_link('%s/%s' % (prjdir, 'packaging'))
     print('packaging dir is %s/%s' % (prjdir, packagingdir))
 
+    '''
     # Only when there is tag submit/*, it's for submit
     needsr = False
     print('\n------------------------\nCheck whether contain submit tag')
-    tag = mygit.describe('--exact-match --match "submit/*"', GERRIT_PATCHSET_REVISION)
+    if GERRIT_BRANCH == 'master':
+        branch = 'trunk'
+    else:
+        branch = GERRIT_BRANCH
+    tag = mygit.describe('--exact-match --match "submit/%s/*"' % branch, GERRIT_PATCHSET_REVISION)
     if tag:
+        print 'submit tag : %s, on branch %s'  % (tag, GERRIT_BRANCH)
         needsr = True
     print('submitted to obs: %s' % needsr)
+    '''
 
     mapping = utils.parse_mapping('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, mappingprj), GERRIT_PROJECT, GERRIT_BRANCH)
     print 'git-obs-mapping:', mapping
@@ -104,6 +110,7 @@ if __name__ == '__main__':
                 specfile = '%s/%s/%s.spec' % (prjdir, packagingdir, prj)
         print('specfile %s' % specfile)
 
+        '''
         if not obs_stg_prj or needsr:
             print('check obs target: %s' % obs_dest_prj)
             obstarget.append(obs_dest_prj)
@@ -116,6 +123,7 @@ if __name__ == '__main__':
                 obsprj = obspkg.ObsProject(obs_dest_prj, apiurl = OBS_API_URL, oscrc = OBS_OSCRC_PATH)
                 if check_obs_newpkg(obsprj, spec.name):
                     newpkg = True
+        '''
 
         if specfile not in speclist:
             speclist.append(specfile)
@@ -131,18 +139,22 @@ if __name__ == '__main__':
 
     if not mapping:
         checkobsmsg = '[IMPORTANT NOTICE]: The change for %s branch will not be submitted to OBS according configuration in gerrit scm/git-obs-mapping project. If needed, please modify scm/git-obs-mapping to enable submission to OBS.' % GERRIT_BRANCH
+    '''
     elif not obstarget:
-        checkobsmsg = '[IMPORTANT NOTICE]: This change will not be submitted to OBS. If want to trigger submission to OBS, please create an annotated tag submit/$(tizen_version)/$(date -u +%F.%H%M%S) on this commit and push to gerrit.'
+        checkobsmsg = '[IMPORTANT NOTICE]: This change will not be submitted to OBS. If want to trigger submission to OBS, please use gbs submit, which will create an annotated tag submit/$(tizen_version)/$(date -u +%F.%H%M%S) on this commit and push to gerrit.'
     else:
         checkobsmsg = '[IMPORTANT NOTICE]: '
         if newpkg:
             checkobsmsg += '[New Package] '
         checkobsmsg += 'This change will be submitted to OBS %s project!!!' % ' '.join(obstarget)
+    '''
     if missedspec:
         checkspecmsg += '\n\nError: Can not find %s under packaging directory!' % ' '.join(missedspec)
 
     print('\n-------------------------------\ncheck obs target result:\n%s' % checkobsmsg)
+    if checkobsmsg:
+        runner.show('%s %s %s,%s --message \'"%s"\'' % (gerritcmd, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, checkobsmsg))
     print('\n-------------------------------\ncheck specfile result:\n%s' % checkspecmsg)
-    runner.show('%s %s %s,%s --message \'"%s"\'' % (gerritcmd, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, checkobsmsg))
-    runner.show('%s %s %s,%s --message \'"%s"\'' % (gerritcmd, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, checkspecmsg))
+    if checkspecmsg:
+        runner.show('%s %s %s,%s --message \'"%s"\'' % (gerritcmd, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, checkspecmsg))
     end()
index 5a5852e..a5e1a2b 100644 (file)
@@ -8,7 +8,7 @@ from email.MIMEMultipart import MIMEMultipart
 from email.MIMEBase import MIMEBase
 from email import Encoders
 
-SMTP_SERVER = "sh-out.intel.com"
+from envparas import *
 
 def sendmail(subject = '', body = '', from_email = None, to = None,
              bcc = None, attachment = None):
index 40d220f..736518e 100755 (executable)
@@ -17,10 +17,10 @@ import obspkg
 from envparas import *
 import errors
 import mysql
+from send_mail import sendmail
+
 import gbp.rpm
 
-mappingprj = 'scm/git-obs-mapping'
-rpmlintprj = 'scm/rpmlint-config'
 giturl = 'ssh://%s@%s:%s' % (GERRIT_USERNAME, GERRIT_HOSTNAME, GERRIT_SSHPORT)
 gerritcmd = 'ssh -p %s %s@%s gerrit' % (GERRIT_SSHPORT, GERRIT_USERNAME, GERRIT_HOSTNAME)
 ret = { 'rc' : {'success' : 0, 'failure' : 0, 'retry' : 1},
@@ -81,6 +81,7 @@ if __name__ == '__main__':
         end('retry')
 
     # if scm/rpmlint-config changed, update HOME/.config/rpmlint
+    print '\n', GERRIT_PROJECT, rpmlintprj
     if GERRIT_PROJECT == rpmlintprj:
         if not os.path.exists('%s/.config' % JENKINS_HOME):
             os.makedirs('%s/.config' % JENKINS_HOME)
@@ -90,38 +91,60 @@ if __name__ == '__main__':
 
     mygit = git.Git(prjdir)
 
+    checktagmsg = ''
     if GERRIT_EVENT_TYPE == "REF_UPDATED":
-        branchs = mygit.branch_contains(GERRIT_REFNAME)
-        if branchs:
-            # the tagged commit has been merged on gerrit
-            mygit.checkout(GERRIT_REFNAME)
-            tag = GERRIT_REFNAME[len('refs/tags/'):]
-            print 'submit tag : %s, which belongs to branch %s'  % (tag, ''.join(branchs))
+        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:
+                    checktagmsg = 'Tag %s only can be used to submit commit on %s branch to OBS. Please check and re-submit another proper tag. Recommend to use "gbs submit" to do this.' % (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'
+                checktagmsg = 'The commit tag %s attached is still open in gerrit. After reviewer accpet this commit, it will be submitted to OBS corresponding project.' % tag
         else:
-            # The tagged commmit still open, abort submit this time
-            print '\nThis change is still open in gerrit, exit now'
-            end('success')
+            print 'tag %s don\'t match the name format, exit now' % tag
+            checktagmsg = 'Backend service cannot find branch info in tag %s. Please create tag "submit/<branchname>/<date.time>" and submit to gerrit. Recommend to use "gbs submit" to do this.' % tag
     else:
         # Only when there is tag submit/*, it's for submit
-        tag = mygit.describe('--exact-match --match "submit/*"', GERRIT_PATCHSET_REVISION)
-        if tag: 
-            mygit.checkout(GERRIT_PATCHSET_REVISION)
-            branchs = [GERRIT_BRANCH]
-            print 'submit tag : %s, which belongs to branch %s'  % (tag, ''.join(branchs))
+        if GERRIT_BRANCH == 'master':
+            branch = 'trunk'
         else:
-            print '\nThis change don\'t contain submit/* tag, exit now'
+            branch = GERRIT_BRANCH
+        tag = mygit.describe('--exact-match --match "submit/%s/*"' % branch, GERRIT_PATCHSET_REVISION)
+        if tag:
+            print 'submit tag : %s, on branch %s'  % (tag, GERRIT_BRANCH)
+        else:
+            print '\nThis change don\'t contain submit/*/* tag, exit now'
             end('success')
 
+    commitinfo = mygit.get_commit_info(tag)
+    tagger = mygit.get_tagger(tag)
+    if not tagger:
+        print '\ntag %s is not annotated tag, exit now' % tag
+        end('failure')
+    if checktagmsg:
+        print checktagmsg
+        checktagmsg = 'Hi, %s,\n\n' % tagger['author'] + checktagmsg + '\n\n----------------------------------------------------------------\nAutomatically generated by backend service.\nPlease DO NOT Reply!'
+        sendmail('[submit to OBS failed]: tag %s' % tag, checktagmsg, FROM_EMAIL, tagger['email'])
+        end('failure')
+
     packagingdir = utils.parse_link('%s/%s' % (prjdir, 'packaging'))
     print('packaging dir is %s/%s' % (prjdir, packagingdir))
 
-    commitinfo = mygit.get_commit_info(tag)
-    tagger = mygit.get_tagger(tag)
     msg = 'Tag: %s\nCommit: %s %s\nSubmitter: %s <%s>' % (tag, commitinfo['id'], commitinfo['subject'], tagger['author'], tagger['email'])
 
-    mapping = []
-    for br in branchs:
-        mapping += utils.parse_mapping('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, mappingprj), GERRIT_PROJECT, br)
+    mygit.checkout(tag)
+
+    mapping = utils.parse_mapping('%s/git/%s/git-obs-mapping.xml' % (JENKINS_HOME, mappingprj), GERRIT_PROJECT, GERRIT_BRANCH)
     print 'git-obs-mapping:', mapping
 
     reqlist = []
@@ -200,17 +223,26 @@ if __name__ == '__main__':
             end('retry')
 
     # post sr info back to gerrit
-    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']
-
-    if GERRIT_CHANGE_NUMBER and GERRIT_CHANGE_NUMBER:
-        comment = 'A request has been created to submit this commit to OBS %s project.\n- Submitter: %s <%s>\nTag: %s\nRequest ID: %s' % (' '.join(dstprjlist), tagger['author'], tagger['email'], tag, ' '.join(reqlist))
-        print '%s %s %s,%s --message \'"%s"\'' % (gerritcmd, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, comment)
-        runner.show('%s %s %s,%s --message \'"%s"\'' % (gerritcmd, '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)
+    if reqlist:
+        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']
+
+        requrl = ''
+        for req in reqlist:
+            requrl += ' %s/request/show/%s' % (OBS_URL, req)
+        comment = 'A request has been created to submit this commit to OBS %s project.\n- Commit: %s %s\n- Tag: %s\n- Submitter: %s <%s>\n- Request URL:%s' % (' '.join(dstprjlist), commitinfo['id'], commitinfo['subject'], tag, tagger['author'], tagger['email'], requrl)
+
+        if GERRIT_CHANGE_NUMBER and GERRIT_CHANGE_NUMBER:
+            print '%s %s %s,%s --message \'"%s"\'' % (gerritcmd, 'review', GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER, comment)
+            runner.show('%s %s %s,%s --message \'"%s"\'' % (gerritcmd, '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!' 
+        sendmail('[submit to OBS success]: tag %s' % tag, comment, FROM_EMAIL, tagger['email'])
+
     end('success')