Browse Source

Merge branch 'release/1.4.0'

liuyuqi-dellpc 1 year ago
parent
commit
8b2bb57544
4 changed files with 88 additions and 11 deletions
  1. 4 1
      README.md
  2. 80 7
      repo_sync/platform/coding/coding.py
  3. 3 2
      repo_sync/repo_sync.py
  4. 1 1
      scripts/sync.xys

+ 4 - 1
README.md

@@ -1,5 +1,5 @@
 # repo_sync
-[![](https://img.shields.io/badge/version-1.0.1-brightgreen.svg)](https://git.yoqi.me/lyq/repo_sync)
+[![](https://img.shields.io/badge/version-1.4.0-brightgreen.svg)](https://git.yoqi.me/lyq/repo_sync)
 [![](https://img.shields.io/badge/Python-3.11.5-brightgreen.svg)](https://git.yoqi.me/lyq/repo_sync)
 
 
@@ -24,6 +24,9 @@ windows 下载 [release]()使用即可
 ```
 python main.py --help
 python main.py create --platform gitlab --repo_path F:\workspace\python\repo_sync
+
+# clone all repo to local path
+python main.py clone --platform coding --repo_path F:\workspace\python\repo_sync
 ```
 
 ## License

+ 80 - 7
repo_sync/platform/coding/coding.py

@@ -20,7 +20,7 @@ class CodingIE(BasePlatform):
     client_serect = ''
     _host = 'https://e.coding.net'  # 新版接口统一地址
 
-    def __init__(self, username:str, token:str,host:str =None ,params: dict = None) -> None:
+    def __init__(self, username:str, token:str, host:str =None , params: dict = None) -> None:
         """init"""
         super().__init__(username , token)
         self.project_name = params.get('coding_project', '')
@@ -63,18 +63,80 @@ class CodingIE(BasePlatform):
     def get_project_list(self):
         pass
 
-    def get_repo_list(self, username: str):
-        """get repo list"""
+    def get_repo_list(self, username: str = None) -> list:
+        """ get repo list  from a project
+            Args: username: the target username may not self.username
+            return: repo list
+        """
         url = f'{self._host}/open-api'
         data = {
             'Action': 'DescribeTeamDepotInfoList',
-            'ProjectName': '',
-            'DepotName': '',
+            'ProjectName': self.project_name,
             'PageNumber': 1,
             'PageSize': 50
         }
         r = self.sess.post(url, json=data)
     
+        if r.status_code == 200:
+            res_data = r.json()
+            try:
+                totalPage = res_data['Response']["DepotData"]["Page"]["TotalPage"]
+                if totalPage > 0:
+                    currentPage = 1
+                    DepotList = []
+                    # the first page
+                    DepotList = res_data['Response']["DepotData"]["Depots"]
+                    repo_model = Repo(
+                        Id=DepotList[0]['Id'],
+                        Name=DepotList[0]['Name'],
+                        HttpsUrl=DepotList[0]['HttpsUrl'],
+                        ProjectId=DepotList[0]['ProjectId'],
+                        SshUrl=DepotList[0]['SshUrl'],
+                        WebUrl=DepotList[0]['WebUrl'],
+                        ProjectName=DepotList[0]['ProjectName'],
+                        Description=DepotList[0]['Description'],
+                        CreatedAt=DepotList[0]['CreatedAt'],
+                        GroupId=DepotList[0]['GroupId'],
+                        GroupName=DepotList[0]['GroupName']
+                    )
+                    DepotList.append(repo_model)
+                    
+                    currentPage += 1
+                    # the other pages
+                    for i in range(2, totalPage + 1):
+                        data = {
+                            'Action': 'DescribeTeamDepotInfoList',
+                            'ProjectName': self.project_name,
+                            'PageNumber': currentPage,
+                            'PageSize': 50
+                        }
+                        r = self.sess.post(url, json=data)
+                        res_data = r.json()
+
+                        DepotList = res_data['Response']["DepotData"]["Depots"]
+                        for repo in DepotList:
+                            repo_model= Repo(
+                                Id=repo['Id'],
+                                Name=repo['Name'],
+                                HttpsUrl=repo['HttpsUrl'],
+                                ProjectId=repo['ProjectId'],
+                                SshUrl=repo['SshUrl'],
+                                WebUrl=repo['WebUrl'],
+                                ProjectName=repo['ProjectName'],
+                                Description=repo['Description'],
+                                CreatedAt=repo['CreatedAt'],
+                                GroupId=repo['GroupId'],
+                                GroupName=repo['GroupName']
+                            )
+                            DepotList.append(repo_model)
+                        currentPage += 1
+                    return DepotList
+                else:
+                    print(f'can not find repo in project {self.project_name}')
+                    exit(1)
+            except Exception as e:
+                raise Exception(e)
+        
     def get_repo_info(self, repo_name: str):
         """get repo list"""
         url = f'{self._host}/open-api'
@@ -223,8 +285,19 @@ class CodingIE(BasePlatform):
         os.chdir('..')
         print('push success')
 
-    def clone(self, repo_name: str):
-        pass
+    def clone(self, repo_path: str):
+        ''' clone all repo from remote
+            Args: repo_name: repo name
+         '''
+        repos = self.get_repo_list()
+        for repo in repos:
+            try:
+                cmd = f'git clone https://{self.username}:{self.token}@e.coding.net/{self.username}/{self.project_name}/{repo["Name"]}.git {repo_path}/{repo["Name"]}'
+                # print(cmd)
+                os.system(cmd)
+                print(f'clone repo:{repo["Name"]} success')
+            except Exception as e:
+                pass
 
     @classmethod
     def suitable(cls, extractor: str) -> bool:

+ 3 - 2
repo_sync/repo_sync.py

@@ -6,7 +6,7 @@
 @License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
 @Desc    :   sync utils
 '''
-import os,csv
+import os,csv,re
 import logging
 from .platform import gen_extractor_classes
 from .repo import Repo
@@ -29,6 +29,7 @@ class RepoSync(object):
         for p in gen_extractor_classes():
             self.platforms.append(p)
         if params.get('repo_path', None) is not None:
+            self.repo_path = params.get('repo_path', None)
             self.get_local_repo_list(params.get('repo_path', None))
     
     def get_local_repo_list(self, repo_path):
@@ -85,7 +86,7 @@ class RepoSync(object):
             host = self.params.get(f'{platform}_host', None)
 
             if command == 'clone':
-                # current_platform(username, token, host = host).clone(name)
+                current_platform(username, token, host, self.params).clone(self.repo_path)
                 return
             if os.path.exists(self.repo_list_path):
                 with open(self.repo_list_path, 'r', encoding='utf8') as f:

+ 1 - 1
scripts/sync.xys

@@ -1,5 +1,5 @@
 $InitialPath = "<curpath>";
-    $GETResult = urldecode(html('<html><style>body{text-align:center}.selection{text-align:left;margin-left:10%;margin-right:10%;padding-bottom:20px}.submtbtn{padding-left:20px;padding-right:20px;padding-top:10px;padding-bottom:10px}</style><body><div><h1>repo_sync tools v1.12</h1></div><form method="get"action="xys:"><div class="selection"><span>Repo:&nbsp;&nbsp;</span><span>' . $InitialPath . '</span></div><div class="selection">Operation:<input type=radio checked name="operation"value="create">Create&nbsp;&nbsp;<input type=radio name="operation"value="push">Push&nbsp;&nbsp;<input type=radio name="operation"value="pull">Pull&nbsp;&nbsp;<input type=radio name="operation"value="delete">Delete&nbsp;&nbsp;</div><div class="selection">Platform:<input type=radio checked name="platform"value="github">Github&nbsp;&nbsp;<input type=radio name="platform"value="gitlab">Gitlab&nbsp;&nbsp;<input type=radio name="platform"value="gitee">Gitee&nbsp;&nbsp;<input type=radio name="platform"value="gogs">git.yoq.me&nbsp;&nbsp;<input type=radio name="platform"value="coding">Coding&nbsp;&nbsp;</div><div><input class="submtbtn"type="submit"name="mysubmit"value="run it"></div></form></body></html>'));
+    $GETResult = urldecode(html('<html><style>body{text-align:center}.selection{text-align:left;margin-left:10%;margin-right:10%;padding-bottom:20px}.submtbtn{padding-left:20px;padding-right:20px;padding-top:10px;padding-bottom:10px}</style><body><div><h1>repo_sync tools v1.12</h1></div><form method="get"action="xys:"><div class="selection"><span>Local Path:&nbsp;&nbsp;</span><span>' . $InitialPath . '</span></div><div class="selection">Operation:&nbsp;&nbsp;<input id="operation_create"type=radio checked name="operation"value="create"><label for="operation_create">Create&nbsp;&nbsp;</label><input id="operation_push"type=radio name="operation"value="push"><label for="operation_push">Push&nbsp;&nbsp;</label><input id="operation_pull"type=radio name="operation"value="pull"><label for="operation_pull">Pull&nbsp;&nbsp;</label><input id="operation_clone"type=radio name="operation"value="clone"><label for="operation_clone">Clone&nbsp;&nbsp;</label><input id="operation_delete"type=radio name="operation"value="delete"><label for="operation_delete">Delete&nbsp;&nbsp;</label></div><div class="selection">Platform:&nbsp;&nbsp;<input id="platform_github"type=radio checked name="platform"value="github"><label for="platform_github">Github&nbsp;&nbsp;</label><input id="platform_gitlab"type=radio name="platform"value="gitlab"><label for="platform_gitlab">Gitlab&nbsp;&nbsp;</label><input id="platform_gitee"type=radio name="platform"value="gitee"><label for="platform_gitee">Gitee&nbsp;&nbsp;</label><input id="platform_gogs"type=radio name="platform"value="gogs"><label for="platform_gogs">git.yoq.me&nbsp;&nbsp;</label><input id="platform_coding"type=radio name="platform"value="coding"><label for="platform_coding">Coding&nbsp;&nbsp;</label></div><div><input class="submtbtn"type="submit"name="mysubmit"value="run it"></div></form></body><script></script></html>'));
         IF ("$GETResult"=="") {sub "_Cancel";}
         // substr $GETResult, $GETResult, 1;
         $operation1=gettoken($GETResult, "1", "&");