Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/GitHub/GitHub_Add_new_member_to_team.ipynb
2973 views
Kernel: Python 3

GitHub.png

GitHub - Add new member to team

Give Feedback | Bug report

Tags: #github #teams #snippet #operations #invitations

Last update: 2023-04-12 (Created: 2022-05-07)

Description: This notebook provides instructions on how to add a new member to a GitHub team.

Input

Import libraries

import requests from naas_drivers import github import pandas as pd import naas

Setup GitHub

How to find your personal access token on GitHub?

# GitHub token GITHUB_TOKEN = "ENTER_YOUR_GITHUB_TOKEN_HERE" # EXAMPLE : "ghp_fUYP0Z5i29AG4ggX8owctGnHU**********" # GitHub teams url GITHUB_TEAMS_URL = "ENTER_YOUR_GITHUB_TEAMS_URL_HERE" # EXAMPLE : "https://github.com/orgs/jupyter-naas/teams/opensource-contributors" # New members to add : str or list of members GITHUB_NEW_MEMBERS = ( "ENTER_YOUR_NEW_USERS_HERE" # EXAMPLE : "FlorentLvr" or ["FlorentLvr", "Dr0p42"] )

Model

Add members to team

def add_members(team_url, new_members): # Init output df = pd.DataFrame() # Get org id and team id team_id = team_url.split("/teams/")[-1].split("/")[0] team_org = team_url.split("https://github.com/orgs/")[-1].split("/")[0] # If a particular member already is present in the team, # then it does not create a copy of that member. No need to worry :) if isinstance(new_members, str): new_members = [new_members] # Add new members to team data = [] for member in new_members: member = member.split("https://github.com/")[-1].split("/")[0] req_url = f"https://api.github.com/orgs/{team_org}/teams/{team_id}/memberships/{member}" headers = { "Authorization": f"token {GITHUB_TOKEN}", "Accept": "application/vnd.github.v3+json", } res = requests.put(req_url, headers=headers) res.raise_for_status() if res.status_code == 200: print(f"✅ Member {member} successfully invited to your team {team_id}") res_json = res.json() data.append(res_json) # Send result as dataframe df = pd.DataFrame(data) return df.reset_index(drop=True) df_new = add_members(GITHUB_TEAMS_URL, GITHUB_NEW_MEMBERS)

Output

New members added

df_new