惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
美团技术团队
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
L
LangChain Blog
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
Engineering at Meta
Engineering at Meta
腾讯CDC
Recent Announcements
Recent Announcements
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky
博客园 - 司徒正美
The Cloudflare Blog
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
C
Check Point Blog
小众软件
小众软件
V
Visual Studio Blog
V
V2EX
F
Full Disclosure
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
罗磊的独立博客
人人都是产品经理
人人都是产品经理
量子位
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
博客园_首页
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
Recorded Future
Recorded Future
G
Google Developers Blog
Vercel News
Vercel News
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
U
Unit 42
爱范儿
爱范儿
Jina AI
Jina AI

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
2019-06-12 · via 轶哥博客

迁移 github 私有/公有仓库到 gitea 是一件容易的事情,不过由于网络原因,部分较大的项目可能会失败。此脚本会自动过滤已经迁移完成的仓库,因此重复执行脚本直至 gitea 中的仓库和 github 中的仓库数量一致,即表示迁移完成。gitea 迁移失败的仓库会被系统自动删除,因此可能出现仓库数量浮动。为了确保代码资产安全,您在迁移完成后应该间隔十分钟再次执行,直到 gitea 中的仓库和 github 中的仓库数量仍然一致。

需要注意,使用该脚本前,需创建一个Github Token,并且需要临时关闭Two-factor authentication

Python3:      

import requests
from requests.adapters import HTTPAdapter
import time
import json

GITEA_URL = 'https://domain'
GITEA_USERNAME = 'username'
GITEA_PASSWORD = '***'
GITEA_ORGNAME_OR_USERNAME = 'orgName'
GITHUB_ORGNAME = 'orgName'
GITHUB_TOKEN = '***************************************'
GITHUB_USERNAME = 'yi-ge'
GITHUB_PASSWORD = '***'

finishedRepo = json.load(open('finishedRepo.json', 'r'))

# Get user Gitea
response_user = requests.get(
    f'{GITEA_URL}/api/v1/users/{GITEA_ORGNAME_OR_USERNAME}')

verify = False

if response_user.status_code == 200:
    uid = response_user.json()['id']
    for n in range(1000):
        page = n + 1
        n = page
        response_github = requests.get(
            f'https://api.github.com/orgs/{GITHUB_ORGNAME}/repos?' +
            f'per_page=100&page={page}',
            headers={'Authorization': 'token ' + GITHUB_TOKEN})

        # List Github repos
        if response_github.status_code == 200:
            if len(response_github.json()):
                for repo in response_github.json():
                    repo_clone_url = repo['clone_url']
                    repo_name = repo['name']
                    description = repo['description']

                    print('Creating repository: ' + repo_name)
                    print(repo_clone_url)

                    s = requests.Session()
                    s.mount('http://', HTTPAdapter(max_retries=3))
                    s.mount('https://', HTTPAdapter(max_retries=3))

                    response_migrate = s.post(
                        f'{GITEA_URL}/api/v1/repos/migrate',
                        json={
                            'clone_addr': repo_clone_url.replace(
                                'https://github.com',
                                f'https://{GITHUB_USERNAME}:' +
                                GITEA_PASSWORD +
                                '@github.com'),
                            'mirror': False,
                            'private': True,
                            "issues": True,
                            "labels": True,
                            "milestones": True,
                            "pull_requests": True,
                            "releases": True,
                            "wiki": True,
                            'repo_name': repo_name,
                            'uid': uid,
                            'description': description
                        },
                        auth=(GITEA_USERNAME, GITEA_PASSWORD),
                        timeout=120
                    )
                    if (response_migrate.status_code == 409
                            or response_migrate.json()['id'] > 0):
                        finishedRepo.append({
                            'name': repo_clone_url,
                            'time': time.strftime(
                                '%Y-%m-%d %H:%M:%S',
                                time.localtime()
                            ),
                            'created':
                                response_migrate.status_code == 409
                        })
                        open('finishedRepo.json', 'w').write(
                            json.dumps(finishedRepo))
                        if (response_migrate.status_code == 409):
                            print(
                                repo_name +
                                ': The repository already exists.'
                            )
                        else:
                            print(
                                repo_name +
                                ': Clone repository created!'
                            )
                            # time.sleep(8)
                    else:
                        print(repo_name + ': Clone repository error!')
                        print(response_migrate.json())
            else:
                if verify:
                    print('All clone finished!')
                else:
                    page = 1
                    verify = True
        else:
            print(response_github.status_code)
            print('Error list Github repos.')
else:
    print('Error user Gitea.')