Add a request funtion for the github
authorYonghee Han <onstudy@samsung.com>
Thu, 13 Dec 2018 04:45:54 +0000 (13:45 +0900)
committerYonghee Han <onstudy@samsung.com>
Thu, 31 Jan 2019 01:21:23 +0000 (10:21 +0900)
Make a accepted tag and accepted branch
Change the list to dict of a github data.

Change-Id: I519fb122e9db5128f06746481a56c6af952b4573

common/buildservice.py
common/workflow.py
job_request.py

index 3f41f53..868d18a 100755 (executable)
@@ -739,9 +739,9 @@ class BuildService(OSC):
                 submissions = filter(lambda a: a != s, submissions)
             saved_info['submissions'] = submissions + info['submissions']
         if 'github' in info:
-            for s in info['github']:
-                github = filter(lambda a: a != s, github)
-            saved_info['github'] = github + info['github']
+            for key, value in github.items():
+                if key not in saved_info['github']:
+                    saved_info['github'].update({key:value})
 
         # Retry three times if failed
         for retry_cnt in [1, 2, 3]:
index 2c48343..cad6070 100644 (file)
@@ -586,8 +586,8 @@ def create_project(build, obs_project, submit_list, mode=MODE_NORMAL):
             github_item[u.get('gerrit_project')] = u.get('github_fetch_url')
 
     if github_item:
-        info['github'] = []
-        info['github'].append(github_item)
+        info['github'] = {}
+        info['github'].update(github_item)
 
     if mode == MODE_SRSYNC:
         submissions = []
index ce8aa1e..591e0d0 100755 (executable)
@@ -61,16 +61,15 @@ def tag_info(prj, tag, event_fields=None):
     git_cache = os.path.join(os.getenv('GIT_CACHE_DIR'))
     if event_fields.get('github'):
         github = event_fields.get('github')
-        for item in github:
-            for p in item:
-                if prj == p:
-                    giturl = item[p]
-                    gitorg = giturl.split(':')[1]
-                    git_full_name = os.path.join(gitorg, prj)
-                    prjdir = os.path.join(git_cache, git_full_name)
-                    git_cache_dir = os.path.join(git_cache, gitorg)
-                    print 'org=%s git_full_name = %s' %(gitorg, git_full_name)
-                    break
+        if github.get(prj):
+            giturl = github.get(prj)
+            gitorg = giturl.split(':')[1]
+            git_full_name = os.path.join(gitorg, prj)
+            prjdir = os.path.join(git_cache, git_full_name)
+            git_cache_dir = os.path.join(git_cache, gitorg)
+            print 'org=%s git_full_name = %s' %(gitorg, git_full_name)
+        else:
+            print 'project: %s is not present in github'%(prj)
     else: # GERRIT
         prjdir = os.path.join(git_cache, prj)
         giturl = None
@@ -478,7 +477,7 @@ def sub_request_gerrit(event_type, event_fields, reqinfo):
 
     return data
 
-def request_github_accepted(data, targetpackagelist=[]):
+def request_github_accepted(data, gitprj, targetpackagelist=[]):
     """Do lots of things when request accepted"""
 
     print '====request accepted===================================='
@@ -497,6 +496,34 @@ def request_github_accepted(data, targetpackagelist=[]):
                           data["GIT_TAG"],
                           request_url(data['OBS_REQ_ID']))
 
+    obs_target = data['OBS_REQ_PRJ'].lower().replace(':', '/')
+    timestamp = datetime.datetime.utcnow().strftime("%Y%m%d.%H%M%S")
+
+    # use os.path.join() to avoid '//', which is invalid as tag name
+    accepted_tag = os.path.join('accepted', obs_target, timestamp)
+
+    gitprj.create_tag(accepted_tag, message, data['commitid'])
+    sleep(1)
+
+    remote = '%s/%s' % (data['GITHUB_URL'], data['GIT_PROJECT'])
+    try:
+        gitprj.push_tag(remote, accepted_tag)
+    except GitRepositoryError, gre:
+        print gre
+        return 1
+
+    # push accepted commit to accepted branch refs/heads/accepted/*
+    # e.g. changes, accepted for Tizen:Mobile should be pushed to
+    # branch refs/heads/accepted/tizen_mobile
+    dst = 'refs/heads/accepted/%s' % \
+            data['OBS_REQ_PRJ'].lower().replace(':', '_')
+    try:
+        gitprj.push(remote, data['commitid'], dst, force=True)
+    except GitRepositoryError, gre:
+        print gre
+        return 1
+
+
     additional_comment = REPAIR_COMMENT \
                      % (data['OBS_REQ_ID'], os.getenv('OBS_URL_EXTERNAL'), data['OBS_REQ_ID'], \
                         data['OBS_REQ_DESP'])
@@ -543,13 +570,43 @@ def sub_request_github(event_type, event_fields, reqinfo):
     for project_data in projects_data:
         data.update(project_data)
 
+        git_cache = os.path.join(os.getenv('GIT_CACHE_DIR'))
+        if event_fields.get('github'):
+            github = event_fields.get('github')
+            if github.get(data['GIT_PROJECT']):
+                giturl = event_fields.get('github').get(data['GIT_PROJECT'])
+                gitorg = giturl.split(':')[1]
+                git_full_name = os.path.join(gitorg, data['GIT_PROJECT'])
+                data['GITHUB_URL'] = giturl
+                prjdir = os.path.join(git_cache, git_full_name)
+                git_cache_dir = os.path.join(git_cache, gitorg)
+                print 'org=%s git_full_name = %s' %(gitorg, git_full_name)
+            else:
+                print 'project: %s is not present in github'%(prj)
+        else:
+            prjdir = os.path.join(git_cache, data['GIT_PROJECT'])
+            giturl = None
+            git_cache_dir = None
+
+        try:
+            gitprj = Git(prjdir)
+        except GitRepositoryError, err:
+            # clone project to local workspace if it doesn't exist in git cache
+            print "clone"
+            if not clone_gitproject(data['GIT_PROJECT'], \
+                       os.path.join(os.getenv('WORKSPACE'), data['GIT_PROJECT']), \
+                       giturl=giturl, git_cache_dir=git_cache_dir):
+                return -1
+            gitprj = Git('%s/%s' % (os.getenv('WORKSPACE'),
+                                   data['GIT_PROJECT']))
+
         if event_type == 'OBS_SRCSRV_REQUEST_STATECHANGE':
             if event_fields['state'] == 'declined':
                 request_github_rejected(data)
             elif event_fields['state'] == 'revoked':
                 request_github_revoked(data)
             elif event_fields['state'] == 'accepted':
-                request_github_accepted(data, reqinfo.get('targetpackagelist', None))
+                request_github_accepted(data, gitprj, reqinfo.get('targetpackagelist', None))
 
     return data
 
@@ -594,6 +651,7 @@ def main():
         saved_info = build.get_info(event_fields['sourceproject'])
         event_fields['packages'] = \
                     unicode_to_str(saved_info['packages'])
+
         # SR-SYNC: SR listing and its pure git repos.
         if 'submissions' in saved_info:
             submissions = unicode_to_str(saved_info['submissions'])