def _import_project_milestones(gitea_api: pygitea.API, milestones: T.List[gitlab.v4.objects.ProjectMilestone], owner: string, repo: string): for milestone in milestones: if not milestone_exists(gitea_api, owner, repo, milestone.title): due_date = None if milestone.due_date is not None and milestone.due_date != '': due_date = dateutil.parser.parse(milestone.due_date).strftime('%Y-%m-%dT%H:%M:%SZ') import_response: requests.Response = gitea_api.post("/repos/" + owner + "/" + repo + "/milestones", json={ "description": milestone.description, "due_on": due_date, "title": milestone.title, }) if import_response.ok: print_info("Milestone " + milestone.title + " imported!") existing_milestone = import_response.json() if existing_milestone: # update milestone state, this cannot be done in the initial import :( # TODO: gitea api ignores the closed state... update_response: requests.Response = gitea_api.patch("/repos/" + owner + "/" + repo + "/milestones/" + str(existing_milestone['id']), json={ "description": milestone.description, "due_on": due_date, "title": milestone.title, "state": milestone.state }) if update_response.ok: print_info("Milestone " + milestone.title + " updated!") else: print_error("Milestone " + milestone.title + " update failed: " + update_response.text) else: print_error("Milestone " + milestone.title + " import failed: " + import_response.text)
def get_user_or_group(gitea_api: pygitea.API, project: gitlab.v4.objects.Project) -> T.Dict: result = None response: requests.Response = gitea_api.get("/users/" + project.namespace['path']) if response.ok: result = response.json() else: response: requests.Response = gitea_api.get("/orgs/" + name_clean(project.namespace["name"])) if response.ok: result = response.json() else: print_error("Failed to load user or group " + project.namespace["name"] + "! " + response.text) return result
def _import_project_repo_collaborators(gitea_api: pygitea.API, collaborators: T.List[gitlab.v4.objects.ProjectMember], project: gitlab.v4.objects.Project): for collaborator in collaborators: if not collaborator_exists(gitea_api, project.namespace['name'], name_clean(project.name), collaborator.username): permission = "read" if collaborator.access_level == 10: # guest access permission = "read" elif collaborator.access_level == 20: # reporter access permission = "read" elif collaborator.access_level == 30: # developer access permission = "write" elif collaborator.access_level == 40: # maintainer access permission = "admin" elif collaborator.access_level == 50: # owner access (only for groups) print_error("Groupmembers are currently not supported!") continue # groups are not supported else: print_warning("Unsupported access level " + str(collaborator.access_level) + ", setting permissions to 'read'!") import_response: requests.Response = gitea_api.put("/repos/" + project.namespace['name'] +"/" + name_clean(project.name) + "/collaborators/" + collaborator.username, json={ "permission": permission }) if import_response.ok: print_info("Collaborator " + collaborator.username + " imported!") else: print_error("Collaborator " + collaborator.username + " import failed: " + import_response.text)
def _import_users(gitea_api: pygitea.API, users: T.List[gitlab.v4.objects.User], notify: bool = False): for user in users: keys: T.List[gitlab.v4.objects.UserKey] = user.keys.list(all=True) print("Importing user " + user.username + "...") print("Found " + str(len(keys)) + " public keys for user " + user.username) if not user_exists(gitea_api, user.username): tmp_password = '******' + ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)) tmp_email = user.username + '@noemail-git.local' # Some gitlab instances do not publish user emails try: tmp_email = user.email except AttributeError: pass import_response: requests.Response = gitea_api.post("/admin/users", json={ "email": tmp_email, "full_name": user.name, "login_name": user.username, "password": tmp_password, "send_notify": notify, "source_id": 0, # local user "username": user.username }) if import_response.ok: print_info("User " + user.username + " imported, temporary password: "******"User " + user.username + " import failed: " + import_response.text) # import public keys _import_user_keys(gitea_api, keys, user)
def _import_project_repo(gitea_api: pygitea.API, project: gitlab.v4.objects.Project): if not repo_exists(gitea_api, project.namespace['name'], name_clean(project.name)): clone_url = project.http_url_to_repo if GITLAB_ADMIN_PASS == '' and GITLAB_ADMIN_USER == '': clone_url = project.ssh_url_to_repo private = project.visibility == 'private' or project.visibility == 'internal' # Load the owner (users and groups can both be fetched using the /users/ endpoint) owner = get_user_or_group(gitea_api, project) if owner: import_response: requests.Response = gitea_api.post("/repos/migrate", json={ "auth_password": GITLAB_ADMIN_PASS, "auth_username": GITLAB_ADMIN_USER, "clone_addr": clone_url, "description": project.description, "mirror": False, "private": private, "repo_name": name_clean(project.name), "uid": owner['id'] }) if import_response.ok: print_info("Project " + name_clean(project.name) + " imported!") else: print_error("Project " + name_clean(project.name) + " import failed: " + import_response.text) else: print_error("Failed to load project owner for project " + name_clean(project.name))
def repo_exists(gitea_api: pygitea.API, owner: string, repo: string) -> bool: repo_response: requests.Response = gitea_api.get("/repos/" + owner + "/" + repo) if repo_response.ok: print_warning("Project " + repo + " does already exist in Gitea, skipping!") else: print("Project " + repo + " not found in Gitea, importing!") return repo_response.ok
def collaborator_exists(gitea_api: pygitea.API, owner: string, repo: string, username: string) -> bool: collaborator_response: requests.Response = gitea_api.get("/repos/" + owner + "/" + repo + "/collaborators/" + username) if collaborator_response.ok: print_warning("Collaborator " + username + " does already exist in Gitea, skipping!") else: print("Collaborator " + username + " not found in Gitea, importing!") return collaborator_response.ok
def organization_exists(gitea_api: pygitea.API, orgname: string) -> bool: group_response: requests.Response = gitea_api.get("/orgs/" + orgname) if group_response.ok: print_warning("Group " + orgname + " does already exist in Gitea, skipping!") else: print("Group " + orgname + " not found in Gitea, importing!") return group_response.ok
def user_exists(gitea_api: pygitea.API, username: string) -> bool: user_response: requests.Response = gitea_api.get("/users/" + username) if user_response.ok: print_warning("User " + username + " does already exist in Gitea, skipping!") else: print("User " + username + " not found in Gitea, importing!") return user_response.ok
def get_team_members(gitea_api: pygitea.API, teamid: int) -> T.List: existing_members = [] member_response: requests.Response = gitea_api.get("/teams/" + str(teamid) + "/members") if member_response.ok: existing_members = member_response.json() else: print_error("Failed to load existing members for team " + str(teamid) + "! " + member_response.text) return existing_members
def get_user_keys(gitea_api: pygitea.API, username: string) -> T.Dict: result = [] key_response: requests.Response = gitea_api.get("/users/" + username + "/keys") if key_response.ok: result = key_response.json() else: print_error("Failed to load user keys for user " + username + "! " + key_response.text) return result
def get_collaborators(gitea_api: pygitea.API, owner: string, repo: string) -> T.List: existing_collaborators = [] collaborator_response: requests.Response = gitea_api.get("/repos/" + owner+ "/" + repo + "/collaborators") if collaborator_response.ok: existing_collaborators = collaborator_response.json() else: print_error("Failed to load existing collaborators for project " + repo + "! " + collaborator_response.text) return existing_collaborators
def get_teams(gitea_api: pygitea.API, orgname: string) -> T.List: existing_teams = [] team_response: requests.Response = gitea_api.get("/orgs/" + orgname + "/teams") if team_response.ok: existing_teams = team_response.json() else: print_error("Failed to load existing teams for organization " + orgname + "! " + team_response.text) return existing_teams
def get_milestones(gitea_api: pygitea.API, owner: string, repo: string) -> T.List: existing_milestones = [] milestone_response: requests.Response = gitea_api.get("/repos/" + owner + "/" + repo + "/milestones") if milestone_response.ok: existing_milestones = milestone_response.json() else: print_error("Failed to load existing milestones for project " + repo + "! " + milestone_response.text) return existing_milestones
def get_issues(gitea_api: pygitea.API, owner: string, repo: string) -> T.List: existing_issues = [] issue_response: requests.Response = gitea_api.get("/repos/" + owner + "/" + repo + "/issues", params={ "state": "all", "page": -1 }) if issue_response.ok: existing_issues = issue_response.json() else: print_error("Failed to load existing issues for project " + repo + "! " + issue_response.text) return existing_issues
def _import_user_keys(gitea_api: pygitea.API, keys: T.List[gitlab.v4.objects.UserKey], user: gitlab.v4.objects.User): for key in keys: if not user_key_exists(gitea_api, user.username, key.title): import_response: requests.Response = gitea_api.post("/admin/users/" + user.username + "/keys", json={ "key": key.key, "read_only": True, "title": key.title, }) if import_response.ok: print_info("Public key " + key.title + " imported!") else: print_error("Public key " + key.title + " import failed: " + import_response.text)
def _import_project_labels(gitea_api: pygitea.API, labels: T.List[gitlab.v4.objects.ProjectLabel], owner: string, repo: string): for label in labels: if not label_exists(gitea_api, owner, repo, label.name): import_response: requests.Response = gitea_api.post("/repos/" + owner + "/" + repo + "/labels", json={ "name": label.name, "color": label.color, "description": label.description # currently not supported }) if import_response.ok: print_info("Label " + label.name + " imported!") else: print_error("Label " + label.name + " import failed: " + import_response.text)
def _import_group_members(gitea_api: pygitea.API, members: T.List[gitlab.v4.objects.GroupMember], group: gitlab.v4.objects.Group): # TODO: create teams based on gitlab permissions (access_level of group member) existing_teams = get_teams(gitea_api, name_clean(group.name)) if existing_teams: first_team = existing_teams[0] print("Organization teams fetched, importing users to first team: " + first_team['name']) # add members to teams for member in members: if not member_exists(gitea_api, member.username, first_team['id']): import_response: requests.Response = gitea_api.put("/teams/" + str(first_team['id']) + "/members/" + member.username) if import_response.ok: print_info("Member " + member.username + " added to group " + name_clean(group.name) + "!") else: print_error("Failed to add member " + member.username + " to group " + name_clean(group.name) + "!") else: print_error("Failed to import members to group " + name_clean(group.name) + ": no teams found!")
def _import_project_issues(gitea_api: pygitea.API, issues: T.List[gitlab.v4.objects.ProjectIssue], owner: string, repo: string): # reload all existing milestones and labels, needed for assignment in issues existing_milestones = get_milestones(gitea_api, owner, repo) existing_labels = get_labels(gitea_api, owner, repo) for issue in issues: if not issue_exists(gitea_api, owner, repo, issue.title): due_date = '' if issue.due_date is not None: due_date = dateutil.parser.parse(issue.due_date).strftime('%Y-%m-%dT%H:%M:%SZ') assignee = None if issue.assignee is not None: assignee = issue.assignee['username'] assignees = [] for tmp_assignee in issue.assignees: assignees.append(tmp_assignee['username']) milestone = None if issue.milestone is not None: existing_milestone = next((item for item in existing_milestones if item["title"] == issue.milestone['title']), None) if existing_milestone: milestone = existing_milestone['id'] labels = [] for label in issue.labels: existing_label = next((item for item in existing_labels if item["name"] == label), None) if existing_label: labels.append(existing_label['id']) import_response: requests.Response = gitea_api.post("/repos/" + owner + "/" + repo + "/issues", json={ "assignee": assignee, "assignees": assignees, "body": issue.description, "closed": issue.state == 'closed', "due_on": due_date, "labels": labels, "milestone": milestone, "title": issue.title, }) if import_response.ok: print_info("Issue " + issue.title + " imported!") else: print_error("Issue " + issue.title + " import failed: " + import_response.text)
def _import_groups(gitea_api: pygitea.API, groups: T.List[gitlab.v4.objects.Group]): for group in groups: members: T.List[gitlab.v4.objects.GroupMember] = group.members.list(all=True) print("Importing group " + name_clean(group.name) + "...") print("Found " + str(len(members)) + " gitlab members for group " + name_clean(group.name)) if not organization_exists(gitea_api, name_clean(group.name)): import_response: requests.Response = gitea_api.post("/orgs", json={ "description": group.description, "full_name": group.full_name, "location": "", "username": name_clean(group.name), "website": "" }) if import_response.ok: print_info("Group " + name_clean(group.name) + " imported!") else: print_error("Group " + name_clean(group.name) + " import failed: " + import_response.text) # import group members _import_group_members(gitea_api, members, group)