pythonpython-3.xgithub-apipygithub

python script to print the last commit message from specific git branch


from github import Github
import sys
import requests
import subprocess
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# github connection
gc = Github(login_or_token = '3d09afd6df8d92bd', password = None, b 
base_url = "https://eos2git.com/api/v3", timeout = 60, verify=False, retry=5)
# enter github repo
repo_gh = gc.get_repo('Devops/emporium')
# get the branches of the given repo
branches = repo_gh.get_branches()
for branch in branches:

    if branch.name == "dev/anjali-raghu/DSE-401":
        # print the last commit message of a branch 
        print(branch.commit)

But the print(branch.commit) print the commit SHA i.e, Commit(sha="026493bb65bc12c99f66c159eda050471d6757ff") instead of commit message.
Could you please help me to print the last commit message of a specific branch.


Solution

  • As you can read in the documentation for PyGitHub, GitCommit objects contain .message attribute with the data you need. Commit objects contain corresponding GitCommit object in .commit attribute. Thus you can access the data you need with branch.commit.commit.message