Skip to content

Instantly share code, notes, and snippets.

@nyluntu
Created January 11, 2021 07:04
Show Gist options
  • Select an option

  • Save nyluntu/ab3d1bc3f6f09c47eee650cadd6d876d to your computer and use it in GitHub Desktop.

Select an option

Save nyluntu/ab3d1bc3f6f09c47eee650cadd6d876d to your computer and use it in GitHub Desktop.
Github kutsuminen organisaatioon
# Require https://github.com/PyGithub/PyGithub
from github import Github
from github.NamedUser import NamedUser
# Global variables to change when script is executed.
# List of github users to invite. Separated with commas.
github_usernames_to_invite = "username1,username2,username3"
# organization slug from url that is used in invitation.
my_organization_slug = "hamk-ohjelmointi-intip20x6"
# Your personal access token to run this script.
# https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
github_access_token = ""
github_api = login_to_github(github_access_token)
authenticated_github_user = github_api.get_user()
# Will authenticate user with personal access token.
def login_to_github(personal_access_token=None):
g = Github(personal_access_token)
return g
def invite_users_to_organization(github_api, github_organization, usernames_to_invite):
default_role="direct_member"
separator=","
for username in usernames_to_invite.split(sep=separator):
print("Next user to invite: ", username)
user_to_invite = github_api.get_user(username)
github_organization.invite_user(user=user_to_invite, role=default_role)
# Main program starts
print(authenticated_github_user)
for organization in authenticated_github_user.get_orgs():
if my_organization_slug in organization.url: # dont do anything if url not match to wanted organization
print("Organization: ", organization.url)
invite_users_to_organization(github_api, organization, github_usernames_to_invite)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment