Browse Source

add coding clone

liuyuqi-dellpc 1 year ago
parent
commit
e85828e415
3 changed files with 86 additions and 9 deletions
  1. 3 0
      README.md
  2. 80 7
      repo_sync/platform/coding/coding.py
  3. 3 2
      repo_sync/repo_sync.py

+ 3 - 0
README.md

@@ -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: