Search code examples
pythongithub-apipygithub

Python PyGithub usage to get/remove a member in an organization


I'm new to Python and Github API, currently trying to use Pygithub to access the Github enterprise API. The target is to find if a specific member is in the organization. If so, remove it from the Org. I have my current Code below:

access_token = "my_ghe_org_token"
g = Github(base_url="https://{my_ghe_hostname}/api/v3", login_or_token=access_token)
org = g.get_organization('my_org_name')
mem_exist = org.has_in_members('myu')
if mem_exist:
    org.remove_from_members('myu')

It gives below error:

    Traceback (most recent call last):
  File "disable_ghe.py", line 21, in <module>
    disable_ghe()
  File "disable_ghe.py", line 9, in disable_ghe
    mem_exist = org.has_in_members('myu')
  File "/Users/myu/Desktop/github/venv/lib/python3.6/site-packages/github/Organization.py", line 769, in has_in_members
    assert isinstance(member, github.NamedUser.NamedUser), member

Anyone know where is my error? Or explain a bit if I used the function incorrectly?

I'm trying to make this call: http://pygithub.readthedocs.io/en/stable/github_objects/Organization.html#github.Organization.Organization.has_in_members


Solution

  • You need to get the user first, and then pass the user object inside has_in_members and remove_from_members.

    user = g.get_user('myu')
    mem_exist = org.has_in_members(user)
    
    if mem_exist:
        org.remove_from_members(user)