Search code examples
pythongithubgithub-apipygithub

Is there a way in Python GitHub library to check if the branch for which a PR has been raised is out-of-date from it's base branch or not?


  • My python script is getting a list of PRs and then it needs to run a check if the branches being used in those PRs are out-of-date from their base branch or not.
  • I need to perform this action remotely i.e without cloning the repo to my local machine.

enter image description here

  • The image attached shows what we see in the GUI if we go to the PR manually.

  • My script searches for PRs (that have the string: DEVELOPERS-XXXX). Once I have the paginated list, it uses them one by one:

    from github import Github

    github_t = os.environ.get('GITHUB_TOKEN')
    github_c = Github(github_t)
    git_i = github_c.search_issues('DEVELOPERS-XXXX', state='open')
    for search in git_i:
  • I am unable to find an attribute that can help me check if the branch used in the PR is out-of-date from it's base branch.

Solution

    • Since I could not find the attribute to resolve my query, I used a gh bash command to get past my issue:
    result = subprocess.run(['gh', 'pr', 'view', fun_search_pr.html_url, '--json', 'mergeStateStatus'], capture_output=True, text=True)
    
    • If the result shows BEHIND, it means that the head ref is out of date:
    {
      "mergeStateStatus": "BEHIND"
    }