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 _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 _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_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)