Skip to content

Instantly share code, notes, and snippets.

@pcchin
Created May 1, 2021 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcchin/73f402fb5c250a0a585a2e5515cff873 to your computer and use it in GitHub Desktop.
Save pcchin/73f402fb5c250a0a585a2e5515cff873 to your computer and use it in GitHub Desktop.
A simple Python script to migrate your GitHub organisations to Gitea.
import requests
GITHUB_SRC_ORG = "ORGANISATION NAME ON GITHUB"
GITHUB_USERNAME = "YOUR GITHUB USERNAME"
GITHUB_TOKEN = "YOUR GITHUB PERSONAL ACCESS TOKEN"
GITEA_API_TOKEN = "YOUR GITEA PERSONAL ACCESS TOKEN"
GITEA_URL = "https://try.gitea.io"
GITEA_TARGET_ORG = "ORGANISATION NAME ON GITEA"
IS_PRIVATE = True
gitea_post_object = {
"auth_token": GITHUB_TOKEN,
"mirror": True,
"mirror_interval": "18h",
"private": IS_PRIVATE,
"repo_owner": GITEA_TARGET_ORG,
"service": "github",
"wiki": True,
}
gitea_header_object = {
"Authorization": f"token {GITEA_API_TOKEN}"
}
def main():
github_repo_list = populate_github()
create_gitea_repos(github_repo_list)
def populate_github():
repo_list = []
i = 1
while True:
current_request = requests.get(f"https://api.github.com/users/{GITHUB_SRC_ORG}/repos?page={i}", auth=(GITHUB_USERNAME, GITHUB_TOKEN))
if current_request.status_code == 200:
if len(current_request.json()) == 0:
break
else:
repo_list = repo_list + current_request.json()
print(f"Requesting page {i}")
i += 1
else:
print(current_request.status_code)
break
return repo_list
def create_gitea_repos(github_repo_list):
for repo in github_repo_list:
repo_post_object = gitea_post_object.copy()
repo_post_object["clone_addr"] = repo["clone_url"]
if repo["description"] is not None:
repo_post_object["description"] = repo["description"]
repo_post_object["repo_name"] = repo["name"]
current_request = requests.post(f"{GITEA_URL}/api/v1/repos/migrate", data=repo_post_object, headers=gitea_header_object)
print(f"Creating repo {repo['full_name']} with request code {current_request.status_code}")
if current_request.status_code != 201 and current_request.status_code != 504:
print(current_request.text)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment