作者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() # 执行主函数