[Git-Sync] Update a body description for e-mail 88/146788/3
authorYonghee Han <onstudy@samsung.com>
Mon, 4 Sep 2017 09:12:13 +0000 (18:12 +0900)
committerYonghee Han <onstudy@samsung.com>
Mon, 4 Sep 2017 23:57:15 +0000 (08:57 +0900)
skip the deleted git repository

Change-Id: I167b2a603fb3502b7a5ca2f0cde7086e71b7a43e

job_submit_request_git_sync.py

index 3075c73..5d60dfa 100644 (file)
@@ -29,6 +29,8 @@ class GitUpdateError(Exception): pass
 class RevisionError(Exception): pass
 class UpstreamError(Exception): pass
 class OverwriteError(Exception): pass
+class MergeError(Exception): pass
+class SubmitError(Exception): pass
 
 class MailSender(object):
     email_title = ''
@@ -106,6 +108,18 @@ class MailSender(object):
                      os.getenv('NOREPLY_EMAIL_SENDER'), \
                      self.email_to)
 
+def submit_gerrit_change(gerritobj, gitprj, project, branch, commit):
+    result = True
+    gerritobj.review(commit=commit, project=project, message='Submitted by system administrator', verified=1, codereview=2, submit=True)
+    gerrit_query = 'project:%s branch:%s commit:%s status:merged' % (project, branch, commit)
+    ret = gerritobj.query(gerrit_query)
+    if not ret:
+        print 'Warning: Submit failed, try direct push...'
+        ret = gitprj.push('-q', 'origin', '%s:refs/heads/%s' % (commit, branch))
+        if ret == None:
+            result = False
+    return result
+
 def conv_rev2commit(gitprj, git_dir, revision):
     commit = ''
     try:
@@ -132,21 +146,26 @@ def update_sync_for_git_sync(tmpdir, private_gerrit, target, requests, request_s
     Update sync for git sync.
     """
 
+    for item in ('target', 'branch', 'notify', 'fork', 'source_branch'):
+        if item not in target:
+            print 'Error: target doesn\'t contain %s' % item
+            return -1
+
     target_project = target.get('target') or []
-    target_fork_prefix = target.get('fork') or []
     branch = target.get('branch') or [] # tizen_4.0_mcd_z4
-    source_branch = target.get('source_branch') or [] # public_tizen
     email_to_members = target.get('notify') or []
+    GIT_PREFIX = target.get('fork') or []
+    UPSTREAM_BRANCH = "public_" + target.get('source_branch') or []
 
-    GIT_PREFIX = target_fork_prefix
-    UPSTREAM_BRANCH = "public_" + source_branch
-    overwrite_branches = [branch]
     request_projects = []
     new_private_projects = []
     overwrite_alreadys = []
     overwrite_successs = []
     upstream_failures = []
     overwrite_failures = []
+    merge_failures = []
+    merge_successs = []
+    submit_failures = []
 
     private_projects = private_gerrit.ls_projects()
 
@@ -158,8 +177,9 @@ def update_sync_for_git_sync(tmpdir, private_gerrit, target, requests, request_s
         revision = item['cid']
         submitter = item['submitter']
         git_tag = item['git_tag']
+        deleted = item['deleted']
 
-        if project.startswith(HIGHLIGHT):
+        if project.startswith(HIGHLIGHT) or deleted:
             continue
         else:
            project = project[1:]
@@ -201,11 +221,6 @@ def update_sync_for_git_sync(tmpdir, private_gerrit, target, requests, request_s
             if ret == None:
                 raise UpstreamError('git push failed(%s)' % (private_project))
 
-            print '\n* Overwrite Branches:', overwrite_branches
-            if not overwrite_branches:
-                print 'not found overwrite branch information of %s, skip overwrite..' % (private_project)
-                continue
-
             print '\n* Update private git repo %s' % (private_project)
             if not git.clone_gitproject(private_project, os.path.join(tmpdir, private_project)):
                 raise OverwriteError('private git update failed(%s)' % (private_project))
@@ -219,7 +234,49 @@ def update_sync_for_git_sync(tmpdir, private_gerrit, target, requests, request_s
             if retcode != 0:
                 raise RevisionError('error')
 
-            for branch in overwrite_branches:
+            if True: ## Merge 
+                if branch in private_gitprj.branch_contains(commit):
+                    print 'already merge %s branch..' % (branch)
+                    continue
+
+                print '\n* Merge %s %s %s' % (commit, branch, private_project)
+                #retcode = private_gitprj.checkout('origin/%s' % branch, '--')[0]
+                private_gitprj.checkout('origin/%s' % branch)
+                #if retcode != 0:
+                #    raise MergeError('not found %s branch(%s)' % (branch, private_project))
+
+                retcode = private_gitprj._exec_git('merge', ['--no-ff', commit])[0]
+                if retcode != 0:
+                    raise MergeError('merge %s branch failed(%s, %s)' % (branch, private_project, commit))
+
+                retcode, merge_commit = private_gitprj._exec_git('log', ['-n1', '--pretty=%H', 'HEAD', '--'])
+                if retcode != 0:
+                    raise MergeError('not found merge commit(%s, %s)' % (branch, private_project))
+
+                #print '\n* Commit --amend --no-edit.. '
+                ret = private_gitprj._exec_git('commit', ['--amend', '--no-edit'])
+                if ret == None:
+                    raise OverwriteError('git commit --amend --no-edit failed(%s)' % (private_project))
+
+                ret, outs = private_gitprj._exec_git('log', ['-n1', '--pretty=%H'])
+                if ret == None:
+                    raise OverwriteError('git log -n1 --pretty=%H failed(%s)' % (private_project))
+                #change overwrite_commit
+                merge_commit = outs.strip()
+
+                print '\n* Push %s %s %s' % (merge_commit, branch, private_project)
+                ret = private_gitprj._exec_git('push',['-q', 'origin', '%s:refs/for/%s' % (merge_commit, branch)])
+                if ret == None:
+                    raise MergeError('git push failed(%s, %s)' % (private_project, merge_commit))
+
+                print '\n* Submit', merge_commit
+                ret = submit_gerrit_change(gerritobj=private_gerrit, gitprj=private_gitprj, \
+                                           project=private_project, \
+                                           branch=branch, commit=merge_commit)
+                if not ret:
+                    raise SubmitError('submit failed(%s, %s)' % (private_project, merge_commit))
+                merge_successs.append((private_project, '%s -> %s' %(commit, merge_commit)))
+            else: ## Overwrite branch
                 print '\n* Overwrite %s %s %s' % (commit, branch, private_project)
                 ret, overwrite_commit = private_gitprj.create_overwrite_commit(branch, commit)
                 if not ret:
@@ -249,9 +306,12 @@ def update_sync_for_git_sync(tmpdir, private_gerrit, target, requests, request_s
                 if ret == None:
                      raise OverwriteError('git push failed(%s)' % (private_project))
                 print '\n* Submit', overwrite_commit
-                private_gerrit.review(commit = overwrite_commit, project =private_project, \
-                                      message = 'Submitted by system administrator', verified = 1, \
-                                      codereview = 2, submit = True)
+                ret = submit_gerrit_change(gerritobj=private_gerrit, gitprj=private_gitprj, \
+                                           project=private_project, \
+                                           branch=branch, commit=overwrite_commit)
+                if not ret:
+                    raise SubmitError('submit failed(%s, %s)' % (private_project, overwrite_commit))
+
                 overwrite_successs.append((private_project, '%s -> %s' %(commit, overwrite_commit)))
         except UpstreamError, exc:
             upstream_failures.append((project, revision, exc))
@@ -259,6 +319,12 @@ def update_sync_for_git_sync(tmpdir, private_gerrit, target, requests, request_s
         except OverwriteError, exc:
             overwrite_failures.append((project, revision, exc))
             print 'Error:', exc
+        except MergeError, exc:
+            merge_failures.append((project, revision, exc))
+            print 'Error:', exc
+        except SubmitError, exc:
+            submit_failures.append((project, revision, exc))
+            print 'Error:', exc
         finally:
             if upstream_git_dir and os.path.exists(upstream_git_dir):
                 shutil.rmtree(upstream_git_dir)
@@ -289,14 +355,26 @@ def update_sync_for_git_sync(tmpdir, private_gerrit, target, requests, request_s
         print '\n-----[Overwrite into %s Branch Failure List]----------'%(branch)
         print '\n'.join(str(item) for item in overwrite_failures)
 
+    if merge_failures:
+        print '\n-----[Merge Failure List]---------------------------------'
+        print '\n'.join(str(item) for item in merge_failures)
+
+    if merge_successs:
+        print '\n-----[Merge Success List]---------------------------------'
+        print '\n'.join(str(item) for item in merge_successs)
+
+    if submit_failures:
+        print '\n-----[Submit Failure List]--------------------------------'
+        print '\n'.join(str(item) for item in submit_failures)
+
     #### Send mail to maintainers
     if True:
         ### Init MailSender ####
         my_mail = MailSender()
 
         my_mail.add_title('[Auto Git-Sync] Target Project:%s Branch:%s' %(target_project, branch))
-        my_mail.add_message('Hello\n A Build System infrom you about Auto Git-Sync results\n\n')
-        my_mail.add_message('Public Source Snapshot: %s Upstream Branch: %s\nTarget Project: %s\nTarget Branch: %s' \
+        my_mail.add_message('Hello\n A Build System inform you about Auto Git-Sync results\n\n')
+        my_mail.add_message('Public Source Snapshot: %s \nUpstream Branch: %s\nTarget Project: %s\nTarget Branch: %s' \
                             %(request_snapshot, UPSTREAM_BRANCH, target_project, branch))
 
         print 'members:',email_to_members
@@ -308,7 +386,7 @@ def update_sync_for_git_sync(tmpdir, private_gerrit, target, requests, request_s
             [my_mail.add_message(str(item)) for item in new_private_projects]
 
         if overwrite_alreadys:
-            my_mail.add_message('\n----[ Already Overwrite into %s Branch List]-----------'%(branch))
+            my_mail.add_message('\n----[Already Overwrite into %s Branch List]-----------'%(branch))
             [my_mail.add_message(str(item)) for item in overwrite_alreadys]
 
         if overwrite_successs:
@@ -323,9 +401,22 @@ def update_sync_for_git_sync(tmpdir, private_gerrit, target, requests, request_s
             my_mail.add_message('\n-----[Overwrite into %s Branch Failure List]----------'%(branch))
             [my_mail.add_message(str(item)) for item in overwrite_failures]
 
-        # Send mail to maintainers
-        my_mail.send_mail()
+        if merge_failures:
+            my_mail.add_message('\n-----[Merge Failure List]---------------------------------')
+            [my_mail.add_message(str(item)) for item in merge_failures]
+
+        if merge_successs:
+            my_mail.add_message('\n-----[Merge Success List]---------------------------------')
+            [my_mail.add_message(str(item)) for item in merge_successs]
+
+        if submit_failures:
+            my_mail.add_message('\n-----[Submit Failure List]--------------------------------')
+            [my_mail.add_message(str(item)) for item in submit_failures]
 
+        if new_private_projects or overwrite_alreadys or overwrite_successs or \
+            upstream_failures or overwrite_failures or merge_failures or submit_failures or merge_successs:
+            # Send mail to maintainers
+            my_mail.send_mail()
 
 def main():
     """
@@ -356,7 +447,7 @@ def main():
             tmpdir = tempfile.mkdtemp(prefix=os.getenv('WORKSPACE')+'/')
             update_sync_for_git_sync(tmpdir, private_gerrit, target, content['requests'][0], content['snapshot'])
         else:
-            print '%s project : git-sync option is not enabled..' %(target.get('target'))
+            print 'obs_dest_prj: %s project : git-sync option is not enabled..' %(target.get('target'))
 
 if __name__ == '__main__':
     sys.exit(main())