forked from Lesin/reposync
Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
|
e2835f7c59 | |
|
9da27ce977 | |
|
d906ee6c55 | |
![]() |
2976a1acf7 |
2
main.py
2
main.py
|
@ -25,7 +25,7 @@ app.include_router(LOG)
|
|||
app.include_router(AUTH)
|
||||
app.include_router(SYNC_CONFIG)
|
||||
|
||||
app.mount("/", StaticFiles(directory="web/dist"), name="static")
|
||||
# app.mount("/", StaticFiles(directory="web/dist"), name="static")
|
||||
|
||||
if __name__ == '__main__':
|
||||
# workers 参数仅在命令行使用uvicorn启动时有效 或使用环境变量 WEB_CONCURRENCY
|
||||
|
|
|
@ -16,7 +16,7 @@ from src.router import SYNC_CONFIG as router
|
|||
from src.do.sync_config import SyncDirect
|
||||
from src.dto.sync_config import SyncRepoDTO, SyncBranchDTO, LogDTO, ModifyRepoDTO
|
||||
from src.service.sync_config import SyncService, LogService
|
||||
from src.service.cronjob import sync_repo_task, sync_branch_task, modify_repos
|
||||
from src.service.cronjob import sync_repo_task, sync_branch_task, modify_repos, delete_repo_dir
|
||||
from src.base.status_code import Status, SYNCResponse, SYNCException
|
||||
from src.service.cronjob import GITMSGException
|
||||
|
||||
|
@ -38,11 +38,11 @@ class SyncDirection(Controller):
|
|||
dto: SyncRepoDTO = Body(..., description="绑定同步仓库信息")
|
||||
):
|
||||
api_log(LogType.INFO, f"用户 {user} 使用 POST 方法访问接口 {request.url.path} ", user)
|
||||
if not base.check_addr(dto.external_repo_address) or not base.check_addr(dto.internal_repo_address):
|
||||
return SYNCResponse(
|
||||
code_status=Status.REPO_ADDR_ILLEGAL.code,
|
||||
msg=Status.REPO_ADDR_ILLEGAL.msg
|
||||
)
|
||||
# if not base.check_addr(dto.external_repo_address) or not base.check_addr(dto.internal_repo_address):
|
||||
# return SYNCResponse(
|
||||
# code_status=Status.REPO_ADDR_ILLEGAL.code,
|
||||
# msg=Status.REPO_ADDR_ILLEGAL.msg
|
||||
# )
|
||||
|
||||
if dto.sync_granularity not in [1, 2]:
|
||||
return SYNCResponse(code_status=Status.SYNC_GRAN_ILLEGAL.code, msg=Status.SYNC_GRAN_ILLEGAL.msg)
|
||||
|
@ -197,6 +197,15 @@ class SyncDirection(Controller):
|
|||
):
|
||||
api_log(LogType.INFO, f"用户 {user} 使用 DELETE 方法访问接口 {request.url.path} ", user)
|
||||
data = await self.service.delete_repo(repo_name=repo_name)
|
||||
try:
|
||||
if data.code_status == 0:
|
||||
delete_repo_dir(repo_name, user)
|
||||
await self.log_service.delete_logs(repo_name=repo_name)
|
||||
except GITMSGException as GITError:
|
||||
return SYNCResponse(
|
||||
code_status=GITError.status,
|
||||
msg=GITError.msg
|
||||
)
|
||||
return SYNCResponse(
|
||||
code_status=data.code_status,
|
||||
msg=data.status_msg
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import os
|
||||
import shutil
|
||||
import re
|
||||
import shlex
|
||||
import subprocess
|
||||
|
@ -34,14 +35,19 @@ def get_repo_address_with_token(address: str, token: str) -> str:
|
|||
|
||||
if address.startswith('https'):
|
||||
owner_name = address[8:].split("/")[1]
|
||||
return address[:8] + owner_name + ":" + token + '@' + address[8:]
|
||||
return address[:8] + "oauth2:" + token + '@' + address[8:]
|
||||
elif address.startswith('http'):
|
||||
owner_name = address[7:].split("/")[1]
|
||||
return address[:7] + owner_name + ":" + token + '@' + address[7:]
|
||||
return address[:7] + "oauth2:" + token + '@' + address[7:]
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
|
||||
def delete_repo_dir(repo_name, user: str):
|
||||
repo_dir = os.path.join(SYNC_DIR, repo_name)
|
||||
os.path.exists(repo_dir) and shutil.rmtree(repo_dir)
|
||||
|
||||
|
||||
def shell(cmd, dire: str, log_name: str, user: str):
|
||||
log = f'Execute cmd: ' + cmd
|
||||
if 'git clone' in log:
|
||||
|
@ -133,7 +139,7 @@ def outer_to_inter(repo, branch, log_name: str, user: str, force_flag):
|
|||
async def sync_repo_task(repo, user, force_flag):
|
||||
if repo.sync_granularity == SyncType.one:
|
||||
branches = await sync_branch_dao.sync_branch(repo_id=repo.id)
|
||||
await sync_branch_task(repo, branches, repo.sync_direction, user)
|
||||
await sync_branch_task(repo, branches, repo.sync_direction, user, force_flag)
|
||||
else:
|
||||
log_name = f'sync_{repo.repo_name}.log'
|
||||
try:
|
||||
|
|
|
@ -112,14 +112,14 @@ class SyncService(Service):
|
|||
if repo is None:
|
||||
return SYNCException(Status.REPO_NOTFOUND)
|
||||
update_fields = {}
|
||||
if dto.internal_repo_address is not None:
|
||||
if not base.check_addr(dto.internal_repo_address):
|
||||
return SYNCException(Status.REPO_ADDR_ILLEGAL)
|
||||
update_fields['internal_repo_address'] = dto.internal_repo_address
|
||||
if dto.external_repo_address is not None:
|
||||
if not base.check_addr(dto.external_repo_address):
|
||||
return SYNCException(Status.REPO_ADDR_ILLEGAL)
|
||||
update_fields['external_repo_address'] = dto.external_repo_address
|
||||
# if dto.internal_repo_address is not None:
|
||||
# if not base.check_addr(dto.internal_repo_address):
|
||||
# return SYNCException(Status.REPO_ADDR_ILLEGAL)
|
||||
update_fields['internal_repo_address'] = dto.internal_repo_address
|
||||
# if dto.external_repo_address is not None:
|
||||
# if not base.check_addr(dto.external_repo_address):
|
||||
# return SYNCException(Status.REPO_ADDR_ILLEGAL)
|
||||
update_fields['external_repo_address'] = dto.external_repo_address
|
||||
if dto.inter_token is not None:
|
||||
update_fields['inter_token'] = dto.inter_token
|
||||
if dto.exter_token is not None:
|
||||
|
@ -215,3 +215,11 @@ class LogService(Service):
|
|||
logs = await self.sync_log_dao.get_log(repo_name_list=repo_name_list, branch_id_list=branch_id_list,
|
||||
page_number=page_num, page_size=page_size, create_sort=create_sort)
|
||||
return logs
|
||||
|
||||
async def delete_logs(self, repo_name: str) -> SYNCException:
|
||||
logs = await self.sync_log_dao.filter(repo_name=repo_name)
|
||||
if logs:
|
||||
for log in logs:
|
||||
await self.sync_log_dao.delete(log)
|
||||
|
||||
return SYNCException(Status.SUCCESS)
|
||||
|
|
Loading…
Reference in New Issue