Browse Source

Merge commit '6eeb79b8c7e3e3459feb51fcbdd5aeb11b46c460' into develop

liuyuqi-dellpc 1 week ago
parent
commit
bd7cdf5545

+ 8 - 0
.env.example

@@ -35,3 +35,11 @@ coding_private=true
 bitbucket_username=""
 bitbucket_token=""
 
+# aliyun
+aliyun_compoanyid="xx"
+aliyun_group_id=""  # 可空
+aliyun_username="sift"
+aliyun_token="xx"
+aliyun_private=true
+
+

+ 0 - 28
repo_sync/base_repo.py

@@ -1,28 +0,0 @@
-#!/usr/bin/env python
-# -*- encoding: utf-8 -*-
-'''
-@Contact :   liuyuqi.gov@msn.cn
-@Time    :   2023/04/27 02:55:45
-@License :   Copyright © 2017-2022 liuyuqi. All Rights Reserved.
-@Desc    :   
-'''
-import requests
-
-class BaseRepo(object):
-    '''
-    Repo class
-    '''
-    def __init__(self, user_name, repo_name, logger, debug=False):
-        self.sess= requests.Session()
-        self.user_name= user_name
-        self.repo_name = repo_name
-        self.repo_url = "https://github.com"
-        self.logger = logger
-
-    def sync(self):
-        '''
-        sync repo
-        '''
-        raise NotImplementedError
-    
- 

+ 0 - 0
repo_sync/repo.py → repo_sync/models.py


+ 1 - 1
repo_sync/options.py

@@ -30,7 +30,7 @@ def parser_args(overrideArguments=None):
         '-p',
         '--platform',
         help='set a platform',
-        choices=['github', 'gitee', 'gitlab', 'gogs', 'gitea', 'bitbucket', 'coding', 'gitcode'],
+        choices=['github', 'gitee', 'gitlab', 'gogs', 'gitea', 'bitbucket', 'coding', 'aliyun','gitcode'],
         default='github',
     )
     argparser.add_argument('-token', '--token', help='set a token')

+ 1 - 0
repo_sync/platform/__init__.py

@@ -6,6 +6,7 @@ from .gitlab import GitlabIE
 from .gogs import GogsIE
 from .coding import CodingIE
 from .gitcode import GitcodeIE
+from .aliyun import AliyunDevOpsIE
 
 _ALL_CLASSES = [klass for name, klass in globals().items()
                 if name.endswith('IE')]

+ 72 - 126
repo_sync/platform/aliyun.py

@@ -9,165 +9,111 @@ read docs:
 https://help.aliyun.com/document_detail/460450.html?spm=a2c4g.460449.0.0.4cc62367VCclNI
 '''
 
+from traceback import print_tb
 from repo_sync.platform.base_platform import BasePlatform
 import csv,subprocess
 import os
-from repo_sync.repo import Repo
+from repo_sync.models import Repo
 from repo_sync.utils.colors import bcolors
+import json
 
-
-class AliyunDevOps(BasePlatform):
+class AliyunDevOpsIE(BasePlatform):
     """aliyun devops"""
 
-    _host = 'https://devops.cn-hangzhou.aliyuncs.com'
+    # _host = 'https://devops.cn-hangzhou.aliyuncs.com'
+    # _host = r"https://codeup.aliyun.com"
+    _host = r"https://openapi-rdc.aliyuncs.com"
 
-    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:
         super().__init__(username=username, token=token)
-        self.sess.headers.update({'Content-Type': 'multipart/form-data'})
-        self.repo_private = True if params.get('aliyun_private', "true").lower()  == 'true' else False
-        self.companyId = 1411978
-        self.groupId = 1411978
+        self.sess.headers.update({'Content-Type': 'application/json',
+                                  'X-Yunxiao-Token': self.token})
+        self.repo_private = "private" if params.get('aliyun_private', "true").lower()  == 'true' else "public"
+        self.companyId = params.get('aliyun_compoanyid')
+        self._api = self._host + '/oapi/v1'
 
-    def create_project(self, project_name: str):
-        """create a project
-        这里概念是:代码组
-        """        
-        url = f'{self._api}/repository/groups/create'
-        form_data = {
-            "accessToken":"",
-            'name': project_name, 
-            'path': project_name,
-            'visibilityLevel': self.repo_private==True ? 10 : 0, 
-            'parentId': 1411978,
-            'private': self.repo_private,
-        }
-        r = self.sess.post(url, params=form_data)
-        if r.status_code != 200:
-            print(f'{bcolors.FAIL}create project {project_name} failed, status code {r.status_code}{bcolors.ENDC}')
-            return
-        else:
-            print(r.json())
-            print(f'{bcolors.OKGREEN}create project {project_name} success{bcolors.ENDC}')
-            print(f'{bcolors.OKGREEN}{self._host}/{self.username}/{project_name}{bcolors.ENDC}')
-            
-    def get_project_info(self, project_name: str):
-        """get project info"""
-        url = f'{self._api}/api/4/groups/find_by_path'
-        form_data = {
-            "organizationId": self.companyId,
-            "identity": "%s/%s" % (self.companyId, project_name)
-        }
-        r = self.sess.get(url, params=form_data)
-        if r.status_code != 200:
-            print(f"{bcolors.FAIL}get project info failed{bcolors.ENDC}")
-            return
-        else:
-            print(r.json())
-            return r.json()
-
-    def delete_project(self, project_name: str):
-        """delete a project"""
-        # get group id
-        url = f'{self._api}/api/4/groups/find_by_path'
-        form_data = {
-            "organizationId": self.companyId,
-            "identity": "%s/%s" % (self.companyId, project_name)
-        }
-        r = self.sess.get(url, params=form_data)
-        if r.status_code != 200:
-            print(f"{bcolors.FAIL}get group id failed{bcolors.ENDC}")
-            return
-        else:
-            # delete the project
-            group = r.json()
-            groupId = group['result']['id']
-            url = f'{self._api}/repository/groups/{groupId}/remove'
-            form_data = {
-                "accessToken":"",
-                "organizationId": self.companyId,
-                "groupId": groupId,
-                "reason":"not need"
-                }
-            response = self.sess.post(url)
-            if response.status_code == 204:
-                print(f'{bcolors.OKGREEN}Project: {project_name} deleted from aliyun successfully!{bcolors.ENDC}')
-            else:
-                print(f'{bcolors.FAIL}Failed to delete project: {project_name} from aliyun. Error {response.status_code}: {response.text}{bcolors.ENDC}')
-    def get_project_list(self) -> list:
-        pass
-    
-    def get_repo_list(self) -> list:
-        """ get repo list"""
-        url = f'{self._api}/repository/list'
-        form_data = {
-            "organizationId": self.companyId,
-            "page": 1,
-            "pageSize": 100,
-            "orderBy":"last_activity_at",
-            # "search":"",
-            "archived":"true",
-        }
-        r = self.sess.get(url, params=form_data)
-        if r.status_code != 200:
-            print(f"{bcolors.FAIL}get repo list failed, status code {r.status_code}{bcolors.ENDC}")
-            return
-        repo_list = r.json()
-        return repo_list
-    
-    def _get_repo_info(self, repo_name: str):
-        """get repo info"""
-        url = f'{self._api}/repository/get'
-        form_data = {
-            "accessToken":"",
-            "organizationId": self.companyId,
-            "name": repo_name
-        }
-        r = self.sess.get(url, params=form_data)
+    def _repo_exists(self, repo_name: str):
+        """repo if exists"""
+        url = f'{self._api}/codeup/organizations/{self.companyId}/repositories/{repo_name}'
+        r = self.sess.get(url)
         if r.status_code != 200:
             print(f"{bcolors.FAIL}get repo info failed, status code {r.status_code}{bcolors.ENDC}")
-            return
-        return r.json()
-
+            return False
+        return True
 
     def create_repo(self, repo_name: str):
-        """create a repo"""        
-        url = f'{self._api}/repository/create'
-        form_data = {
-        "accessToken":"",
-        'name': repo_name, 
-        'private': self.repo_private,
-        }
-        r = self.sess.post(url, params=form_data)
-        if r.status_code != 201:
-            print(f"{bcolors.FAIL}create repo {repo_name} failed, status code {r.status_code}{bcolors.ENDC}")
-            return
+        """create a repo"""
+        if not self._repo_exists(repo_name=repo_name):
+            url = f'{self._api}/codeup/organizations/{self.companyId}/repositories'
+            form_data = {
+                'name': repo_name, 
+                'path': repo_name,
+                'visibility': self.repo_private,
+            }
+            r = self.sess.post(url, data=json.dumps(form_data))
+            # r = self.sess.post(url, json=json.dumps(form_data))
+            if r.status_code != 200:
+                print(f"{bcolors.FAIL}create repo {repo_name} failed, status code {r.status_code}{bcolors.ENDC}")
+                print(f"{bcolors.FAIL}response: {r.text}{bcolors.ENDC}")
+                return False
+            print(f"{bcolors.OKGREEN}create repo {repo_name} success{bcolors.ENDC}")
+            print(f"{bcolors.OKGREEN}https://codeup.aliyun.com/{self.companyId}/{repo_name}{bcolors.ENDC}")
+            return True
+        else:
+            print(f"{bcolors.OKGREEN}repo {repo_name} already exists{bcolors.ENDC}")
+            print(f"{bcolors.OKGREEN}https://codeup.aliyun.com/{self.companyId}/{repo_name}{bcolors.ENDC}")
+            return True 
 
     def delete(self, repo_name: str):
             """delete a project"""
-            url = f'{self._api}/project/{repo_name}'
+            repositoryId=f"{self.companyId}%2F{repo_name}"
+            url = f'{self._api}/codeup/organizations/{self.companyId}/repositories/{repositoryId}'
 
             response = self.sess.delete(url)
-            if response.status_code == 204:
+            if response.status_code == 200:
                 print(f"{bcolors.OKGREEN}Project: {repo_name} deleted from aliyun successfully!{bcolors.ENDC}")
             else:
                 print(f'{bcolors.FAIL}Failed to delete project: {repo_name} from aliyun. Error {response.status_code}: {response.text}{bcolors.ENDC}')
+    
     def pull(self, local_repo_path: str):
         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]
         print(f"{bcolors.OKGREEN}pull repo:{self.username}/{repo_name} from aliyun{bcolors.ENDC}")
         os.chdir(local_repo_path)
-        result = subprocess.run(['git', 'pull', 'https://codeup.aliyun.com/api/v1/repository/get?accessToken=&organizationId=1411978&name='+repo_name])
-    
+
+        os.system('git remote remove origin_aliyun')
+        os.system(f'git remote add origin_aliyun https://{self.username}:{self.token}@codeup.aliyun.com/{self.companyId}/{repo_name}.git')
+        result = subprocess.run(['git', 'symbolic-ref', '--short', 'HEAD'], capture_output=True, text=True, encoding='utf-8')
+        current_branch = result.stdout.strip()
+        os.system(f'git pull origin_aliyun {current_branch}')
+        os.system('git remote remove origin_aliyun')
+        os.chdir('..')
+        
+        print(bcolors.OKGREEN + 'pull from aliyun success' + bcolors.ENDC)
+
     def push(self, local_repo_path: str):
         """ push local repo to aliyun"""
         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]
-        print(f"{bcolors.OKGREEN}push repo:{self.username}/{repo_name} to aliyun{bcolors.ENDC}")
+        print(bcolors.WARNING + f'push repo:{self.username}/{repo_name} to aliyun' + bcolors.ENDC)
+        self.create_repo(repo_name)
         os.chdir(local_repo_path)
-        result = subprocess.run(['git', 'push', 'https://codeup.aliyun.com/api/v1/repository/create?accessToken=&organizationId=1411978&name='+repo_name])
-            
+        os.system('git remote remove origin_aliyun')
+        os.system(f'git remote add origin_aliyun https://{self.username}:{self.token}@codeup.aliyun.com/{self.companyId}/{repo_name}.git')
+        
+        result = subprocess.run(['git', 'symbolic-ref', '--short', 'HEAD'], capture_output=True, text=True, encoding='utf-8')
+        current_branch = result.stdout.strip()
+        
+        os.system(f'git pull origin_aliyun {current_branch}')
+        os.system(f'git push -u origin_aliyun {current_branch}')
+        os.system('git remote remove origin_aliyun')
+        os.chdir('..')
+        
+        print(bcolors.OKGREEN + 'push to aliyun success' + bcolors.ENDC)
+
+
     def clone(self):
         """ clone repo from aliyun"""
         pass
@@ -175,7 +121,7 @@ class AliyunDevOps(BasePlatform):
     @classmethod
     def suitable(cls, extractor: str) -> bool:
         """check if this extractor is suitable for this platform"""
-        if extractor == 'aliyun_devops':
+        if extractor == 'aliyun':
             return True
         else:
             return False

+ 1 - 1
repo_sync/platform/base_platform.py

@@ -1,5 +1,5 @@
 import requests,csv,os
-from repo_sync.repo import Repo
+from repo_sync.models import Repo
 from repo_sync.utils.colors import bcolors
 
 class BasePlatform(object):

+ 1 - 1
repo_sync/platform/gitee.py

@@ -10,7 +10,7 @@
 from .base_platform import BasePlatform
 import csv, subprocess
 import os
-from repo_sync.repo import Repo
+from repo_sync.models import Repo
 from repo_sync.utils.colors import bcolors
 
 class GiteeIE(BasePlatform):

+ 1 - 1
repo_sync/platform/github.py

@@ -9,7 +9,7 @@
 import os
 import json
 import csv, subprocess
-from repo_sync.repo import Repo
+from repo_sync.models import Repo
 from .base_platform import BasePlatform
 from repo_sync.utils.colors import bcolors
 

+ 1 - 1
repo_sync/platform/gitlab.py

@@ -12,7 +12,7 @@ import re
 import csv
 import subprocess
 from .base_platform import BasePlatform
-from repo_sync.repo import Repo
+from repo_sync.models import Repo
 from repo_sync.utils.colors import bcolors
 
 class GitlabIE(BasePlatform):

+ 1 - 1
repo_sync/repo_sync.py

@@ -9,7 +9,7 @@
 import os,csv,re
 import logging
 from .platform import gen_extractor_classes
-from .repo import Repo
+from .models import Repo
 from repo_sync.utils.colors import bcolors
 
 class RepoSync(object):