我有一个包含存储库列表的文本文件。我想使用 pygithub 迭代列表,并告诉我每个存储库最后一次构建在 dev 或 master 分支上是成功还是失败,具体取决于哪个分支存在。这就是我拥有的
from github import Github
g = Github(base_url="https://code.secret.url/api/v3", login_or_token="SECRET_TOKEN")
user = g.get_user()
login = user.login
org = g.get_organization("SECRET_ORG")
# Open the file containing the list of repositories
with open("repo_list.txt") as f:
for repo_name in f:
repo = org.get_repo(repo_name.strip())
try:
print(f"Processing repo: {repo_name.strip()}")
# Get branches as a PaginatedList object
branches = repo.get_branches()
# Check if the 'dev' branch exists
dev_branch_exists = False
for branch in branches:
if branch.name == 'dev':
dev_branch_exists = True
break
# Set base_branch to either dev or master
if dev_branch_exists:
base_branch = repo.get_branch("dev")
else:
base_branch = repo.get_branch("master")
# Get the latest commit
latest_commit = base_branch.commit
print(f"Latest commit: {latest_commit}")
# Check the status of the latest commit
statuses = latest_commit.get_statuses()
print(f"Statuses: {statuses}")
# Print the statuses if available
if statuses:
print("List of statuses:")
for status in statuses:
print(f"State: {status.state}, Description: {status.description}")
else:
print("No statuses found for the latest commit.")
except Exception as e:
print(f"An error occurred while processing {repo_name.strip()}: {str(e)}")
当我尝试打印状态时,它似乎是空的,因为在日志中它会说
List of statuses:
但之后没有任何打印,这意味着它甚至不执行
print(f"State: {status.state}, Description: {status.description}")
。我使用以下文档作为参考https://pygithub.readthedocs.io/en/latest/github_objects/Commit.html#github.Commit.Commit.get_statuses
对我来说,只需使用 github 的 UI 并单击存储库的操作即可轻松查看存储库的状态,但我希望通过自动化来完成此操作。当我尝试从
打印状态时 # Check the status of the latest commit
statuses = latest_commit.get_statuses()
print(f"Statuses: {statuses}")
它会打印出类似
<github.PaginatedList.PaginatedList object at 0x000001CD1AF03310>
的内容
所以我确信我正在使用 .get_statuses() 正确填充变量雕像,但之后我不确定我所做的是否是正确的方法。我忘了提及 print(f"Latest commit: {latest_commit}") 似乎也可以正确打印,所以我认为我正在抓取的提交没有问题
似乎提交状态与 GH Action 结果不同,但如果你想检查最新提交的所有检查是否都成功,这是有效的:
check_runs = latest_commit.get_check_runs()
passing = all([check.conclusion == "success" for check in check_runs])
print("Build passed? ", passing)
get_check_runs()
列出了为提交执行的所有检查的结果,您可以迭代它们,例如注意所有这些检查的 conclusion
是否为 "success"
。