|
@@ -13,22 +13,23 @@ import os,subprocess,sys
|
|
|
from repo_sync.platform.base_platform import BasePlatform
|
|
|
from .project import Project
|
|
|
from .repo import Repo
|
|
|
+from repo_sync.utils.colors import bcolors
|
|
|
|
|
|
class CodingIE(BasePlatform):
|
|
|
"""coding util"""
|
|
|
client_id = ''
|
|
|
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)
|
|
|
+ super().__init__(username, token)
|
|
|
self.project_name = params.get('coding_project', '')
|
|
|
- self.repo_shared = False if params.get('coding_private', "true").lower() == 'true' else True
|
|
|
+ self.repo_shared = False if params.get('coding_private', "true").lower() == 'true' else True
|
|
|
self.url = f'{self._host}/open-api'
|
|
|
-
|
|
|
+
|
|
|
def create_project(self):
|
|
|
- ''' createt a project '''
|
|
|
+ ''' create a project '''
|
|
|
data = {
|
|
|
'Action': 'CreateCodingProject',
|
|
|
'Name': '',
|
|
@@ -43,10 +44,12 @@ class CodingIE(BasePlatform):
|
|
|
r = self.sess.post(self.url, json=data)
|
|
|
if r.status_code == 200:
|
|
|
res_data = r.json()
|
|
|
+ print(bcolors.OKGREEN + 'Create project success' + bcolors.ENDC)
|
|
|
return True
|
|
|
else:
|
|
|
+ print(bcolors.FAIL + 'Failed to create project' + bcolors.ENDC)
|
|
|
return False
|
|
|
-
|
|
|
+
|
|
|
def delete_project(self):
|
|
|
data = {
|
|
|
'Action': 'DeleteOneProject',
|
|
@@ -55,15 +58,17 @@ class CodingIE(BasePlatform):
|
|
|
r = self.sess.post(self.url, json=data)
|
|
|
if r.status_code == 200:
|
|
|
res_data = r.json()
|
|
|
+ print(bcolors.OKGREEN + 'Delete project success' + bcolors.ENDC)
|
|
|
return True
|
|
|
else:
|
|
|
+ print(bcolors.FAIL + 'Failed to delete project' + bcolors.ENDC)
|
|
|
return False
|
|
|
-
|
|
|
+
|
|
|
def get_project_list(self):
|
|
|
pass
|
|
|
-
|
|
|
+
|
|
|
def get_repo_list(self, username: str = None) -> list:
|
|
|
- """ get repo list from a project
|
|
|
+ """ get repo list from a project
|
|
|
Args: username: the target username may not self.username
|
|
|
return: repo list
|
|
|
"""
|
|
@@ -74,7 +79,6 @@ class CodingIE(BasePlatform):
|
|
|
'PageSize': 50
|
|
|
}
|
|
|
r = self.sess.post(self.url, json=data)
|
|
|
-
|
|
|
if r.status_code == 200:
|
|
|
res_data = r.json()
|
|
|
try:
|
|
@@ -84,20 +88,21 @@ class CodingIE(BasePlatform):
|
|
|
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)
|
|
|
+ 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
|
|
|
# the other pages
|
|
@@ -110,10 +115,9 @@ class CodingIE(BasePlatform):
|
|
|
}
|
|
|
r = self.sess.post(self.url, json=data)
|
|
|
res_data = r.json()
|
|
|
-
|
|
|
DepotList = res_data['Response']["DepotData"]["Depots"]
|
|
|
for repo in DepotList:
|
|
|
- repo_model= Repo(
|
|
|
+ repo_model = Repo(
|
|
|
Id=repo['Id'],
|
|
|
Name=repo['Name'],
|
|
|
HttpsUrl=repo['HttpsUrl'],
|
|
@@ -130,9 +134,9 @@ class CodingIE(BasePlatform):
|
|
|
currentPage += 1
|
|
|
return DepotList
|
|
|
else:
|
|
|
- print(f'can not find repo in project {self.project_name}')
|
|
|
+ print(bcolors.WARNING + f'Cannot find repo in project {self.project_name}' + bcolors.ENDC)
|
|
|
except Exception as e:
|
|
|
- raise Exception(e)
|
|
|
+ print(bcolors.FAIL + str(e) + bcolors.ENDC)
|
|
|
|
|
|
def _get_repo_info(self, repo_name: str):
|
|
|
"""get repo list"""
|
|
@@ -164,25 +168,25 @@ class CodingIE(BasePlatform):
|
|
|
)
|
|
|
return depot
|
|
|
else:
|
|
|
- print(f'can not find repo {repo_name} in project {self.project_name}')
|
|
|
+ print(bcolors.WARNING + f'Cannot find repo {repo_name} in project {self.project_name}' + bcolors.ENDC)
|
|
|
except Exception as e:
|
|
|
- raise Exception(f'can not find repo {repo_name} in project {self.project_name}')
|
|
|
-
|
|
|
- def get_project_info(self)->Project:
|
|
|
+ print(bcolors.FAIL + f'Cannot find repo {repo_name} in project {self.project_name}: {str(e)}' + bcolors.ENDC)
|
|
|
+
|
|
|
+ def get_project_info(self) -> Project:
|
|
|
data = {
|
|
|
"Action": "DescribeCodingProjects",
|
|
|
"ProjectName": self.project_name,
|
|
|
"DepotName": "",
|
|
|
"PageNumber": 1,
|
|
|
"PageSize": 50
|
|
|
- }
|
|
|
+ }
|
|
|
r = self.sess.post(self.url, json=data)
|
|
|
if r.status_code == 200:
|
|
|
res_data = r.json()
|
|
|
try:
|
|
|
if res_data['Response']["Data"]["TotalCount"] > 0:
|
|
|
ProjectList = res_data['Response']["Data"]["ProjectList"]
|
|
|
- projet = Project(
|
|
|
+ project = Project(
|
|
|
Id=ProjectList[0]['Id'],
|
|
|
Name=ProjectList[0]['Name'],
|
|
|
DisplayName=ProjectList[0]['DisplayName'],
|
|
@@ -190,11 +194,11 @@ class CodingIE(BasePlatform):
|
|
|
TeamOwnerId=ProjectList[0]['TeamOwnerId'],
|
|
|
TeamId=ProjectList[0]['TeamId']
|
|
|
)
|
|
|
- return projet
|
|
|
+ return project
|
|
|
except Exception as e:
|
|
|
- print(res_data)
|
|
|
- print(e)
|
|
|
-
|
|
|
+ print(bcolors.FAIL + 'Error retrieving project info: ' + str(e) + bcolors.ENDC)
|
|
|
+ print(bcolors.FAIL + str(res_data) + bcolors.ENDC)
|
|
|
+
|
|
|
def create_repo(self, repo_name: str):
|
|
|
"""create a repo"""
|
|
|
# get project id
|
|
@@ -203,42 +207,43 @@ class CodingIE(BasePlatform):
|
|
|
repo = self._get_repo_info(repo_name=repo_name)
|
|
|
if repo is None:
|
|
|
data = {
|
|
|
- "Action": "CreateGitDepot",
|
|
|
- "ProjectId": project.Id,
|
|
|
- "DepotName": repo_name,
|
|
|
- "Shared": self.repo_shared,
|
|
|
- "Description": "this is your first depot"
|
|
|
- }
|
|
|
+ "Action": "CreateGitDepot",
|
|
|
+ "ProjectId": project.Id,
|
|
|
+ "DepotName": repo_name,
|
|
|
+ "Shared": self.repo_shared,
|
|
|
+ "Description": "this is your first depot"
|
|
|
+ }
|
|
|
r = self.sess.post(self.url, json=data)
|
|
|
if r.status_code == 200:
|
|
|
- print(f'create repo {repo_name} success', data,r.json())
|
|
|
+ print(bcolors.OKGREEN + f'Create repo {repo_name} success' + bcolors.ENDC)
|
|
|
return True
|
|
|
else:
|
|
|
+ print(bcolors.FAIL + 'Failed to create repo' + bcolors.ENDC)
|
|
|
return False
|
|
|
else:
|
|
|
- print("repo: %s is exist" % repo_name)
|
|
|
+ print(bcolors.WARNING + f"Repo: {repo_name} already exists" + bcolors.ENDC)
|
|
|
else:
|
|
|
- print("project: %s is not exist, cannot create repo in it." % self.project_name)
|
|
|
-
|
|
|
+ print(bcolors.FAIL + f"Project: {self.project_name} does not exist, cannot create repo in it." + bcolors.ENDC)
|
|
|
+
|
|
|
def delete(self, repo_name: str):
|
|
|
- """delete a repo"""
|
|
|
- repo = self._get_repo_info(repo_name=repo_name)
|
|
|
- if repo is not None:
|
|
|
- data = {
|
|
|
- "Action": "DeleteGitDepot",
|
|
|
- "DepotId": repo.Id
|
|
|
- }
|
|
|
- r = self.sess.post(self.url, json=data)
|
|
|
- if r.status_code == 200:
|
|
|
- print(f'delete repo {repo_name} success', data,r.json())
|
|
|
- return True
|
|
|
- else:
|
|
|
- return False
|
|
|
+ """delete a repo"""
|
|
|
+ repo = self._get_repo_info(repo_name=repo_name)
|
|
|
+ if repo is not None:
|
|
|
+ data = {
|
|
|
+ "Action": "DeleteGitDepot",
|
|
|
+ "DepotId": repo.Id
|
|
|
+ }
|
|
|
+ r = self.sess.post(self.url, json=data)
|
|
|
+ if r.status_code == 200:
|
|
|
+ print(f'delete repo {repo_name} success', data,r.json())
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ return False
|
|
|
|
|
|
def pull(self, local_repo_path: str):
|
|
|
''' pull a repo from remote
|
|
|
Args: local_repo_path: local repo path
|
|
|
- '''
|
|
|
+ '''
|
|
|
if local_repo_path[-1] == os.path.sep:
|
|
|
local_repo_path = local_repo_path[:-1]
|
|
|
repo_name = local_repo_path.split(os.path.sep)[-1]
|
|
@@ -262,7 +267,7 @@ class CodingIE(BasePlatform):
|
|
|
def push(self, local_repo_path: str):
|
|
|
''' push a local repo to remote
|
|
|
Args: local_repo_path: local repo path
|
|
|
- '''
|
|
|
+ '''
|
|
|
# check if remote repo is exist
|
|
|
|
|
|
if local_repo_path[-1] == os.path.sep:
|
|
@@ -288,7 +293,7 @@ class CodingIE(BasePlatform):
|
|
|
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:
|
|
@@ -306,3 +311,4 @@ class CodingIE(BasePlatform):
|
|
|
return True
|
|
|
else:
|
|
|
return False
|
|
|
+
|