Dependência: PyGithub
from github import Github
from github.GithubException import GithubException
def add_users_to_team(usernames, org_name, team_slug, token, role="member"):
"""
Invite each user in `usernames` to the GitHub team `team_slug`
within organization `org_name`, using the given personal access `token`.
Role can be "member" or "maintainer".
"""
# Authenticate to GitHub
g = Github(token) # PyGithub client
# Get the organization and the team by slug
org = g.get_organization(org_name)
team = org.get_team_by_slug(team_slug)
for username in usernames:
try:
user = g.get_user(username) # Lookup the user object
# Add (or update) team membership; default role is "member"
team.add_membership(user, role)
print(f"✅ {username} invited to '{team_slug}' as {role}.")
except GithubException as e:
print(f"❌ Failed to invite {username}: {e.data.get('message', e)}")
if __name__ == "__main__":
USERS = ["my-user"]
ORGANIZATION = "my-org"
TEAM_SLUG = "my-team-slug"
TOKEN = "my-token"
add_users_to_team(USERS, ORGANIZATION, TEAM_SLUG, TOKEN, role="member")