diff --git a/gitlab2gitea/gitlab2gitea.py b/gitlab2gitea/gitlab2gitea.py index 67d89f1..8106206 100644 --- a/gitlab2gitea/gitlab2gitea.py +++ b/gitlab2gitea/gitlab2gitea.py @@ -282,7 +282,7 @@ def _error(message): # Endpoint: GET /api/{GITLAB_API_VERSION}/version def check_gitlab(): - _debug(f'REQUEST: {GITLAB_URL}/api/{GITLAB_API_VERSION}/version') + _debug(f'REQUEST: GET {GITLAB_URL}/api/{GITLAB_API_VERSION}/version') response = requests.get(f'{GITLAB_URL}/api/{GITLAB_API_VERSION}/version', headers={ 'Content-Type': 'application/json', @@ -300,7 +300,7 @@ def check_gitlab(): # Endpoint: GET /api/{GITEA_API_VERSION}/version def check_gitea(): - _debug(f'REQUEST: {GITEA_URL}/api/{GITEA_API_VERSION}/version') + _debug(f'REQUEST: GET {GITEA_URL}/api/{GITEA_API_VERSION}/version') response = requests.get(f'{GITEA_URL}/api/{GITEA_API_VERSION}/version', headers={ 'Content-Type': 'application/json', @@ -320,7 +320,7 @@ def check_gitea(): def get_gitlab_groups() -> list: groups = [] - _debug(f'REQUEST: {GITLAB_URL}/api/{GITLAB_API_VERSION}/groups') + _debug(f'REQUEST: GET {GITLAB_URL}/api/{GITLAB_API_VERSION}/groups') response = requests.get(f'{GITLAB_URL}/api/{GITLAB_API_VERSION}/groups', params={ @@ -343,6 +343,25 @@ def get_gitlab_groups() -> list: return groups +# Endpoint: GET /api/{GITLAB_API_VERSION}/groups/{group_id} +def get_gitlab_group(group_id: int) -> dict: + _debug(f'REQUEST: GET {GITLAB_URL}/api/{GITLAB_API_VERSION}/groups/{group_id}') + + response = requests.get(f'{GITLAB_URL}/api/{GITLAB_API_VERSION}/groups/{group_id}', + headers={ + 'Content-Type': 'application/json', + 'Accept': 'application/json', + 'Authorization': f'Bearer {GITLAB_TOKEN}' + }) + + if response.status_code != 200: + response_message = response.json()['message'] if 'message' in response.json() else 'Unknown error' + raise Exception(f'Failed to get GitLab group: {response_message}') + else: + group = response.json() + + return group + # Endpoint: GET /api/{GITLAB_API_VERSION}/users def get_gitlab_users() -> list: pass @@ -355,7 +374,7 @@ def get_gitlab_projects() -> list: def get_gitea_groups() -> list: groups = [] - _debug(f'REQUEST: {GITEA_URL}/api/{GITEA_API_VERSION}/orgs?all_available=1&per_page=100') + _debug(f'REQUEST: GET {GITEA_URL}/api/{GITEA_API_VERSION}/orgs?all_available=1&per_page=100') response = requests.get(f'{GITEA_URL}/api/{GITEA_API_VERSION}/orgs', headers={ @@ -380,6 +399,46 @@ def get_gitea_users() -> list: def get_gitea_projects() -> list: pass +# Endpoint: POST /api/{GITEA_API_VERSION}/orgs +def create_gitea_org(name: str, description: str = '', website: str = '', visibility: str = 'private') -> dict: + + gitlab_group = get_gitlab_group(name) + + if not gitlab_group: + raise Exception(f'GitLab group "{name}" does not exist!') + + # Update detail fields + if description: + gitlab_group['description'] = description + + if website: + gitlab_group['web_url'] = website + + # Create Gitea group + + _debug(f'REQUEST: POST {GITEA_URL}/api/{GITEA_API_VERSION}/orgs') + + response = requests.post(f'{GITEA_URL}/api/{GITEA_API_VERSION}/orgs', json={ + 'username': name, + 'description': gitlab_group['description'], + 'website': gitlab_group['web_url'], + 'visibility': visibility + }, headers={ + 'Content-Type': 'application/json', + 'Accept': 'application/json', + 'Authorization': f'token {GITEA_TOKEN}' + }) + + _trace(f'RESPONSE: {response.json()}') + + if response.status_code != 201: + response_message = response.json()['message'] if 'message' in response.json() else 'Unknown error' + raise Exception(f'Failed to create Gitea group: {response_message}') + else: + group = response.json() + + return group + def cmp_gitlab_gitea_groups(gitlab_groups: list, gitea_groups: list) -> dict: # 0 = exists on both @@ -396,9 +455,9 @@ def cmp_gitlab_gitea_groups(gitlab_groups: list, gitea_groups: list) -> dict: # [ # { # "id": 15, - # "web_url": "https://git.madaboutpandas.com/groups/administration", - # "name": "Administration", - # "path": "administration", + # "web_url": "https://gitlab.example.org/groups/developers", + # "name": "Developers", + # "path": "developers", # "description": "", # "visibility": "private", # "share_with_group_lock": false, @@ -422,8 +481,8 @@ def cmp_gitlab_gitea_groups(gitlab_groups: list, gitea_groups: list) -> dict: # }, # "avatar_url": null, # "request_access_enabled": true, - # "full_name": "Administration", - # "full_path": "administration", + # "full_name": "Developers", + # "full_path": "developers", # "created_at": "2020-08-10T20:27:23.487Z", # "parent_id": null, # "organization_id": 1, @@ -438,16 +497,16 @@ def cmp_gitlab_gitea_groups(gitlab_groups: list, gitea_groups: list) -> dict: # [ # { # "id": 3, - # "name": "YerbaBuena", + # "name": "Developers", # "full_name": "", # "email": "", - # "avatar_url": "http://10.17.1.21/avatars/9f8ea65601abbf666adcec2b128180e4", + # "avatar_url": "http://gitea.example.org/avatars/9f8ea65601abbf666adcec2b128180e4", # "description": "", # "website": "", # "location": "", # "visibility": "public", # "repo_admin_change_team_access": True, - # "username": "YerbaBuena", + # "username": "Developers", # } # ] @@ -493,7 +552,23 @@ def cmp_gitlab_gitea_projects(gitlab_projects: list, gitea_projects: list) -> di pass def create_missing_groups(gitlab_groups: list, gitea_groups: list): - pass + + for gitlab_group in gitlab_groups: + name = gitlab_group['path'] + exists = False + + for gitea_group in gitea_groups: + if name == gitea_group['name']: + exists = True + break + + if not exists: + _info(f'Creating missing group "{name}" on Gitea...') + + try: + create_gitea_org(name) + except Exception as e: + _error(f'Failed to create Gitea group "{name}": {e}') def run_migration():