gogs.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/11/08 14:29:44
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc :
  8. """
  9. from .base_platform import BasePlatform
  10. import csv, re, subprocess
  11. import json, os
  12. class GogsIE(BasePlatform):
  13. """ """
  14. gityoqi_repo_list = 'gityoqi_repo_list.csv'
  15. def __init__(self, username:str, token:str, host:str =None ,params: dict = None) -> None:
  16. super().__init__(username=username,token=token)
  17. self._host = 'https://git.yoqi.me' if host is None else host
  18. self.repo_private = True if params.get('gogs_private', "true").lower() == 'true' else False
  19. def create_org_repo(self, org_name: str, repo_name: str):
  20. """create org repo"""
  21. url = f'{self._host}/api/v1/orgs/{org_name}/repos'
  22. payload = {
  23. 'name': repo_name,
  24. 'description': 'This is your first repository',
  25. 'private': self.repo_private,
  26. 'auto_init': True,
  27. 'gitignores': 'Go',
  28. 'license': 'Apache v2 License',
  29. 'readme': 'Default',
  30. }
  31. r = self.sess.post(url, data=json.dumps(payload))
  32. if r.status_code != 201:
  33. print(
  34. 'create org repo {} failed, status code {}'.format(
  35. repo_name, r.status_code
  36. )
  37. )
  38. return
  39. print('create org repo {} success'.format(repo_name))
  40. def create_repo(self, repo_name: str):
  41. """create a repo"""
  42. url = f'{self._host}/api/v1/user/repos'
  43. payload = {
  44. 'name': repo_name,
  45. 'description': 'This is your first repository',
  46. 'private': True,
  47. # "auto_init": True,
  48. # "gitignores": "Go",
  49. # "license": "Apache v2 License",
  50. # "readme": "Default",
  51. }
  52. r = self.sess.post(url, data=json.dumps(payload))
  53. if r.status_code != 201:
  54. print(
  55. 'create repo {} failed, status code {}'.format(repo_name, r.status_code)
  56. )
  57. return
  58. print('create repo {} success'.format(repo_name))
  59. def delete(self, repo_name: str):
  60. """delete a repo, maybe request a confirm by input"""
  61. url = f'{self._host}/api/v1/repos/{self.username}/{repo_name}'
  62. r = self.sess.delete(url)
  63. if r.status_code != 204:
  64. print(
  65. 'delete repo {} failed, status code {}'.format(repo_name, r.status_code)
  66. )
  67. return
  68. print('delete repo {} success'.format(repo_name))
  69. def get_repo_list(self, repo_name: str):
  70. """get repo list"""
  71. url = f'{self._host}/api/v1/users/{self.username}/repos'
  72. r = self.sess.get(url)
  73. if r.status_code != 200:
  74. print('get repo list failed, status code {}'.format(r.status_code))
  75. return
  76. self.repos = r.json()
  77. print('get repo list success')
  78. def clone(self):
  79. pass
  80. def pull(self, local_repo_path: str):
  81. if local_repo_path[-1] == os.path.sep:
  82. local_repo_path = local_repo_path[:-1]
  83. repo_name = local_repo_path.split(os.path.sep)[-1]
  84. print(f'pull repo:{self.username}/{repo_name} from {self._host}')
  85. pur_host = re.search(r'(?<=//)[^/]+', self._host).group()
  86. os.chdir(local_repo_path)
  87. os.system('git remote remove origin_gogs')
  88. os.system(
  89. f'git remote add origin_gogs https://{self.username}:{self.token}@{pur_host}/{self.username}/{repo_name}.git'
  90. )
  91. result = subprocess.run(['git', 'symbolic-ref', '--short', 'HEAD'], capture_output=True, text=True)
  92. current_branch = result.stdout.strip()
  93. os.system(f'git pull origin_gogs {current_branch}')
  94. os.system('git remote remove origin_gogs')
  95. os.chdir('..')
  96. def push(self, local_repo_path: str):
  97. if local_repo_path[-1] == os.path.sep:
  98. local_repo_path = local_repo_path[:-1]
  99. repo_name = local_repo_path.split(os.path.sep)[-1]
  100. print(f'push repo:{self.username}/{repo_name} to {self._host}')
  101. pur_host = re.search(r'(?<=//)[^/]+', self._host).group()
  102. os.chdir(local_repo_path)
  103. os.system('git remote remove origin_gogs')
  104. os.system(
  105. f'git remote add origin_gogs https://{self.username}:{self.token}@{pur_host}/{self.username}/{repo_name}.git'
  106. )
  107. result = subprocess.run(['git', 'symbolic-ref', '--short', 'HEAD'], capture_output=True, text=True)
  108. current_branch = result.stdout.strip()
  109. os.system(f'git pull origin_gogs {current_branch}')
  110. os.system(f'git push -u origin_gogs {current_branch}')
  111. os.system('git remote remove origin_gogs')
  112. os.chdir('..')
  113. @classmethod
  114. def suitable(cls, extractor: str) -> bool:
  115. """check if this extractor is suitable for this platform"""
  116. if extractor == 'gogs':
  117. return True
  118. else:
  119. return False