aliyun.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2024/07/31 01:47:38
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : aliyun devops
  8. read docs:
  9. https://help.aliyun.com/document_detail/460450.html?spm=a2c4g.460449.0.0.4cc62367VCclNI
  10. '''
  11. from repo_sync.platform.base_platform import BasePlatform
  12. import csv,subprocess
  13. import os
  14. from repo_sync.repo import Repo
  15. from repo_sync.utils.colors import bcolors
  16. class AliyunDevOps(BasePlatform):
  17. """aliyun devops"""
  18. _host = 'https://devops.cn-hangzhou.aliyuncs.com'
  19. def __init__(self, username:str, token:str,host:str =None, params: dict = None) -> None:
  20. super().__init__(username=username, token=token)
  21. self.sess.headers.update({'Content-Type': 'multipart/form-data'})
  22. self.repo_private = True if params.get('aliyun_private', "true").lower() == 'true' else False
  23. self.companyId = 1411978
  24. self.groupId = 1411978
  25. def create_project(self, project_name: str):
  26. """create a project
  27. 这里概念是:代码组
  28. """
  29. url = f'{self._api}/repository/groups/create'
  30. form_data = {
  31. "accessToken":"",
  32. 'name': project_name,
  33. 'path': project_name,
  34. 'visibilityLevel': self.repo_private==True ? 10 : 0,
  35. 'parentId': 1411978,
  36. 'private': self.repo_private,
  37. }
  38. r = self.sess.post(url, params=form_data)
  39. if r.status_code != 200:
  40. print(f'{bcolors.FAIL}create project {project_name} failed, status code {r.status_code}{bcolors.ENDC}')
  41. return
  42. else:
  43. print(r.json())
  44. print(f'{bcolors.OKGREEN}create project {project_name} success{bcolors.ENDC}')
  45. print(f'{bcolors.OKGREEN}{self._host}/{self.username}/{project_name}{bcolors.ENDC}')
  46. def get_project_info(self, project_name: str):
  47. """get project info"""
  48. url = f'{self._api}/api/4/groups/find_by_path'
  49. form_data = {
  50. "organizationId": self.companyId,
  51. "identity": "%s/%s" % (self.companyId, project_name)
  52. }
  53. r = self.sess.get(url, params=form_data)
  54. if r.status_code != 200:
  55. print(f"{bcolors.FAIL}get project info failed{bcolors.ENDC}")
  56. return
  57. else:
  58. print(r.json())
  59. return r.json()
  60. def delete_project(self, project_name: str):
  61. """delete a project"""
  62. # get group id
  63. url = f'{self._api}/api/4/groups/find_by_path'
  64. form_data = {
  65. "organizationId": self.companyId,
  66. "identity": "%s/%s" % (self.companyId, project_name)
  67. }
  68. r = self.sess.get(url, params=form_data)
  69. if r.status_code != 200:
  70. print(f"{bcolors.FAIL}get group id failed{bcolors.ENDC}")
  71. return
  72. else:
  73. # delete the project
  74. group = r.json()
  75. groupId = group['result']['id']
  76. url = f'{self._api}/repository/groups/{groupId}/remove'
  77. form_data = {
  78. "accessToken":"",
  79. "organizationId": self.companyId,
  80. "groupId": groupId,
  81. "reason":"not need"
  82. }
  83. response = self.sess.post(url)
  84. if response.status_code == 204:
  85. print(f'{bcolors.OKGREEN}Project: {project_name} deleted from aliyun successfully!{bcolors.ENDC}')
  86. else:
  87. print(f'{bcolors.FAIL}Failed to delete project: {project_name} from aliyun. Error {response.status_code}: {response.text}{bcolors.ENDC}')
  88. def get_project_list(self) -> list:
  89. pass
  90. def get_repo_list(self) -> list:
  91. """ get repo list"""
  92. url = f'{self._api}/repository/list'
  93. form_data = {
  94. "organizationId": self.companyId,
  95. "page": 1,
  96. "pageSize": 100,
  97. "orderBy":"last_activity_at",
  98. # "search":"",
  99. "archived":"true",
  100. }
  101. r = self.sess.get(url, params=form_data)
  102. if r.status_code != 200:
  103. print(f"{bcolors.FAIL}get repo list failed, status code {r.status_code}{bcolors.ENDC}")
  104. return
  105. repo_list = r.json()
  106. return repo_list
  107. def _get_repo_info(self, repo_name: str):
  108. """get repo info"""
  109. url = f'{self._api}/repository/get'
  110. form_data = {
  111. "accessToken":"",
  112. "organizationId": self.companyId,
  113. "name": repo_name
  114. }
  115. r = self.sess.get(url, params=form_data)
  116. if r.status_code != 200:
  117. print(f"{bcolors.FAIL}get repo info failed, status code {r.status_code}{bcolors.ENDC}")
  118. return
  119. return r.json()
  120. def create_repo(self, repo_name: str):
  121. """create a repo"""
  122. url = f'{self._api}/repository/create'
  123. form_data = {
  124. "accessToken":"",
  125. 'name': repo_name,
  126. 'private': self.repo_private,
  127. }
  128. r = self.sess.post(url, params=form_data)
  129. if r.status_code != 201:
  130. print(f"{bcolors.FAIL}create repo {repo_name} failed, status code {r.status_code}{bcolors.ENDC}")
  131. return
  132. def delete(self, repo_name: str):
  133. """delete a project"""
  134. url = f'{self._api}/project/{repo_name}'
  135. response = self.sess.delete(url)
  136. if response.status_code == 204:
  137. print(f"{bcolors.OKGREEN}Project: {repo_name} deleted from aliyun successfully!{bcolors.ENDC}")
  138. else:
  139. print(f'{bcolors.FAIL}Failed to delete project: {repo_name} from aliyun. Error {response.status_code}: {response.text}{bcolors.ENDC}')
  140. def pull(self, local_repo_path: str):
  141. if local_repo_path[-1] == os.path.sep:
  142. local_repo_path = local_repo_path[:-1]
  143. repo_name = local_repo_path.split(os.path.sep)[-1]
  144. print(f"{bcolors.OKGREEN}pull repo:{self.username}/{repo_name} from aliyun{bcolors.ENDC}")
  145. os.chdir(local_repo_path)
  146. result = subprocess.run(['git', 'pull', 'https://codeup.aliyun.com/api/v1/repository/get?accessToken=&organizationId=1411978&name='+repo_name])
  147. def push(self, local_repo_path: str):
  148. """ push local repo to aliyun"""
  149. if local_repo_path[-1] == os.path.sep:
  150. local_repo_path = local_repo_path[:-1]
  151. repo_name = local_repo_path.split(os.path.sep)[-1]
  152. print(f"{bcolors.OKGREEN}push repo:{self.username}/{repo_name} to aliyun{bcolors.ENDC}")
  153. os.chdir(local_repo_path)
  154. result = subprocess.run(['git', 'push', 'https://codeup.aliyun.com/api/v1/repository/create?accessToken=&organizationId=1411978&name='+repo_name])
  155. def clone(self):
  156. """ clone repo from aliyun"""
  157. pass
  158. @classmethod
  159. def suitable(cls, extractor: str) -> bool:
  160. """check if this extractor is suitable for this platform"""
  161. if extractor == 'aliyun_devops':
  162. return True
  163. else:
  164. return False