I needed to add the user to the github organization repository but couldn't find anything.
Asked
Active
Viewed 910 times
0
-
Isn't this https://stackoverflow.com/questions/49372057/how-to-create-a-new-repository-in-an-organization-with-pygithub answer your question? – Peacepieceonepiece Oct 09 '21 at 08:10
2 Answers
0
Here's the code:
result = []
organization = g.get_organization("org_name")
team_name = 'backend'
# TYPES OF ROLES= ["admin", "direct_member", "billing_manager"], TEAMS is ARRAY
try:
teams = organization.get_teams()
for t in teams:
if t.name == team_name:
team = t
result = organization.invite_user(email="user@domain.com",
role="direct_member",
teams=[team])
except Exception as error:
raise error
if result is None:
print(f"Successfully sent invite")
This is the function definition as given in Organization class for PyGithub:
def invite_user(
self,
user=github.GithubObject.NotSet,
email=github.GithubObject.NotSet,
role=github.GithubObject.NotSet,
teams=github.GithubObject.NotSet,
):
"""
:calls: `POST /orgs/{org}/invitations <http://docs.github.com/en/rest/reference/orgs#members>`_
:param user: :class:`github.NamedUser.NamedUser`
:param email: string
:param role: string
:param teams: array of :class:`github.Team.Team`
:rtype: None
"""
assert user is github.GithubObject.NotSet or isinstance(
user, github.NamedUser.NamedUser
), user
assert email is github.GithubObject.NotSet or isinstance(email, str), email
assert (email is github.GithubObject.NotSet) ^ (
user is github.GithubObject.NotSet
), "specify only one of email or user"
parameters = {}
if user is not github.GithubObject.NotSet:
parameters["invitee_id"] = user.id
elif email is not github.GithubObject.NotSet:
parameters["email"] = email
if role is not github.GithubObject.NotSet:
assert isinstance(role, str), role
assert role in ["admin", "direct_member", "billing_manager"]
parameters["role"] = role
if teams is not github.GithubObject.NotSet:
assert all(isinstance(team, github.Team.Team) for team in teams)
parameters["team_ids"] = [t.id for t in teams]
headers, data = self._requester.requestJsonAndCheck(
"POST",
f"{self.url}/invitations",
headers={"Accept": Consts.mediaTypeOrganizationInvitationPreview},
input=parameters,
)
Some things to consider:
- You need to have admin access to invite a user
- You can add a user with/without the team parameter in the above code.
To get more info: PyGithub examples

Shloka Bhalgat
- 132
- 11
-1
This code should do the trick.
from github import Github
from github import Team
from github import NamedUser
access_token = "Your Personal Access Token"
g = Github(access_token)
org = g.get_organization("Your Organisation name")
org.login
# Invite user using email and role
org.invite_user(
email='email of the invitee',
role='direct_member')
# Other possible roles are: ["admin", "direct_member", "billing_manager"]

Abhijeet Anand
- 1
- 1
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the [help center](https://stackoverflow.com/help/how-to-answer). – Ethan Sep 24 '22 at 21:32