I have a text file with a list of repos. I wanted to use pygithub to iterate through the list and tell me for each repo on whether the last build was successful or failure on dev or master branch depending on which one exists. This is what I have
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)}")
When I try printing the status it appears to be empty because in the log it will say
List of statuses:
but nothing prints afterwards meaning it doesn't even execute print(f"State: {status.state}, Description: {status.description}")
. I used the following documentation as reference https://pygithub.readthedocs.io/en/latest/github_objects/Commit.html#github.Commit.Commit.get_statuses
It's easy for me to see the status of a repo just by using the UI of github and just clicking on action of a repo but I was hoping to do this through automation. When I try printing out status from
# Check the status of the latest commit
statuses = latest_commit.get_statuses()
print(f"Statuses: {statuses}")
It will print out something like <github.PaginatedList.PaginatedList object at 0x000001CD1AF03310>
so I am certain that I am populating the variable statues correctly with .get_statuses() but afterwards I am not certain if what I am doing is the correct way. I forgot to mention print(f"Latest commit: {latest_commit}") also appears to print correctly so I don't think there's an issue with the commit I'm grabbing
UPDATE: I also tried the following code snippet as per someone's suggestion
# Get the last commit associated with the pull request
last_commit = pr.get_commits().reversed[0].commit
print(f"Last commit SHA: {last_commit.sha}")
# Get the check runs associated with the last commit
check_runs = last_commit.get_check_runs()
# Check if all check runs have a conclusion of "success"
passing = all(check.conclusion == "success" for check in check_runs)
print("Build passed? ", passing)
But it gave me the error 'GitCommit' object has no attribute 'get_check_runs'
Seems that commit status is something different to GH Action result.
From GitHub Documentation
There are two types of status checks on GitHub:
- Checks
- Statuses
Checks are different from statuses in that they provide line annotations, more detailed messaging, and are only available for use with GitHub Apps.
Based on this, I think statuses might be the correct one for CI-systems other than GH Actions, but for GH Actions you need to query for checks.
If you want to check whether all the checks for the latest commit were successful, this works:
check_runs = latest_commit.get_check_runs()
passing = all([check.conclusion == "success" for check in check_runs])
print("Build passed? ", passing)
get_check_runs()
lists results for all the checks executed for the commit, and you can iterate over them to for example note if conclusion
for all of them was "success"
.