2

I have an instance of GitHub Enterprise and I am trying to do an audit of my Organization within GHE of repositories which have no protected branches. I am able to find which repository branches are protected or not using PyGitHub. But I am trying to figure out Python logic which would only give me repositories which do not have any protected branches. GitHub's API only provides branches which are protected.

Here's my code so far:

from github import Github
g = Github(access_token, base_url='<github-enterprise-url>/api/v3')
for repo in g.get_organization('orgName').get_repos():
    for branch in repo.get_branches():
        b = repo.get_branch(branch.name)
        u = g.get_user()
        if b.protected:
            print("Repo is --> ",repo.name, "| Repo ID is ", repo.id ,"| Branch --> ", b.name, " (protected)")
        else:
            print("Repo is --> ",repo.name, "| Repo ID is ", repo.id ,"| Branch --> ", b.name, " (not protected)")

Output I get is:

Repo is -->  puppet | Repo ID is  2 | Branch -->  dev  (protected)
Repo is -->  puppet | Repo ID is  2 | Branch -->  master  (not protected)
Repo is -->  java | Repo ID is  3 | Branch -->  master  (not protected)
Repo is -->  foobar| Repo ID is  7 | Branch -->  master  (not protected)
Repo is -->  afu.github.io | Repo ID is  8 | Branch -->  master  (protected)

How would I use Python to only give me the repos with no unprotected branches in the logic?

redhatter
  • 63
  • 2
  • 4

0 Answers0