在Github中克隆或替换Labels(标签)

作者Github:https://github.com/NianBroken

在使用代码前,你需要先PyGithub库

pip install PyGithub

 程序代码

from github import Github, GithubException  # 导入Github库

# 输入你的GitHub用户名和令牌
USERNAME = "your_username"  # 你的GitHub用户名
TOKEN = "your_token"  # 你的GitHub令牌

# 选项:
# 1. 删除目标存储库的所有标签,然后进行克隆。
# 2. 遇到重复标签时替换。
# 3. 遇到重复标签时询问用户是否替换。
OPTION = 1

# 输入源存储库和目标存储库的信息
# 源存储库URL
SOURCE_REPO_URL = "https://github.com/NianBroken/ZFCheckScores"
# 目标存储库URL
TARGET_REPO_URLS = [
    "https://github.com/NianBroken/Great-Firewall",
    "https://github.com/NianBroken/Payment-Code",
    "https://github.com/NianBroken/Personal_Sakura_Guide_Page",
    "https://github.com/NianBroken/Firework_Simulator",
    "https://github.com/NianBroken/NianBroken",
]


def authenticate(username, token):
    # 认证GitHub用户名和令牌是否有效。
    try:
        test_g = Github(username, token)  # 创建Github实例
        test_g.get_user().login  # 获取用户登录
        return True  # 认证成功返回True
    except GithubException as e:
        if e.status == 401:
            print("认证失败。请检查你的用户名和令牌是否正确。")  # 认证失败提示
        else:
            print(f"创建 Github 实例时出错:{e}")  # 其他错误提示
        return False  # 认证失败返回False


def delete_all_labels(repo):
    # 删除目标存储库的所有标签。
    existing_labels = repo.get_labels()  # 获取目标存储库的所有标签
    for label in existing_labels:
        try:
            label.delete()  # 删除标签
            print(
                f"已成功删除标签 '{label.name}' 在 {repo.full_name} 中。"
            )  # 删除标签成功提示
        except GithubException as e:
            if e.status == 404:
                print(
                    f"标签 '{label.name}' 在 {repo.full_name} 中未找到。"
                )  # 标签未找到提示
            else:
                print(
                    f"删除 {repo.full_name} 中的标签 '{label.name}' 时出错:{e}"
                )  # 其他错误提示


def replace_label(existing_label, source_label):
    # 替换目标存储库中的标签。
    try:
        existing_label.edit(  # 编辑标签
            name=source_label.name,
            color=source_label.color,
            description=source_label.description,
        )
        print(
            f"标签 '{source_label.name}' 在 {existing_label.repository.full_name} 中已成功替换。"
        )  # 替换标签成功提示
    except GithubException as e:
        print(
            f"在 {existing_label.repository.full_name} 中替换标签 '{source_label.name}' 时出错:{e}"
        )  # 替换标签失败提示


def main():
    if not authenticate(USERNAME, TOKEN):
        exit()  # 认证失败,退出程序

    g = Github(USERNAME, TOKEN)  # 创建Github实例

    try:
        source_repo = g.get_repo(
            SOURCE_REPO_URL.replace("https://github.com/", "").strip("/")
        )  # 获取源存储库
    except GithubException as e:
        if e.status == 404:
            print(f"源存储库 {SOURCE_REPO_URL} 不存在。")  # 源存储库不存在提示
        else:
            print(f"获取源存储库 {SOURCE_REPO_URL} 时出错:{e}")  # 其他错误提示
        exit()  # 源存储库不存在,退出程序

    for target_repo_url in TARGET_REPO_URLS:
        try:
            target_repo = g.get_repo(
                target_repo_url.replace("https://github.com/", "").strip("/")
            )  # 获取目标存储库
        except GithubException as e:
            if e.status == 404:
                print(f"目标存储库 {target_repo_url} 不存在。")  # 目标存储库不存在提示
            else:
                print(f"获取目标存储库 {target_repo_url} 时出错:{e}")  # 其他错误提示
            continue

        existing_labels = target_repo.get_labels()  # 获取目标存储库的标签

        if OPTION == 1:
            delete_all_labels(target_repo)  # 删除目标存储库的所有标签

        source_labels = source_repo.get_labels()  # 获取源存储库的标签

        for source_label in source_labels:
            existing_label = next(
                (label for label in existing_labels if label.name == source_label.name),
                None,
            )

            if existing_label:
                if OPTION == 2:
                    replace_label(
                        existing_label, source_label
                    )  # 替换目标存储库中的标签
                elif OPTION == 3:
                    user_input = input(
                        f"标签 '{source_label.name}' 在 {target_repo.full_name} 中已存在。替换 (R) 或跳过 (S)? "
                    ).lower()
                    if user_input == "r":
                        replace_label(
                            existing_label, source_label
                        )  # 替换目标存储库中的标签
                    else:
                        print(
                            f"标签 '{source_label.name}' 在 {target_repo.full_name} 中已跳过。"
                        )
            else:
                try:
                    target_repo.create_label(  # 在目标存储库中创建标签
                        name=source_label.name,
                        color=source_label.color,
                        description=source_label.description,
                    )
                    print(
                        f"标签 '{source_label.name}' 成功克隆到 {target_repo.full_name}。"
                    )
                except GithubException as e:
                    print(
                        f"在 {target_repo.full_name} 中克隆标签 '{source_label.name}' 时出错:{e}"
                    )


if __name__ == "__main__":
    main()  # 执行主函数

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值