Add to GitHub Team from Data File

I’ve been using GitHub pretty heavily in my classroom for the past few years. This year I’m branching out a bit and decided to put everybody in a course into a GitHub team. Biggest reason is that we have an enterprise account and can have a private GitHub pages site that they can access, but isn’t visible on the public web.

But when I went to add in the 100+ students I hit a pretty significant snag. There doesn’t appear to be a built in way to upload a file of usernames to add to the team.

image-20220826183330031

To get where I wanted I would have to go through and search for each student’s GitHub username, click invite, and then confirm the invitation. That’s a lot of typing and clicking.

Enter the GitHub API.

Turns out with the right permissions you can add users to teams.

Access Token

You’re going to need a personal access token for this to work, so go ahead and login to your GitHub account and go to your settings. On the bottom of the menu is Developer Settings. Click that, and then click on Personal access tokens on the next page.

Click on the Generate new token button.

image-20220826184222928

The permission you’re looking for is admin:org. Probably only need write:org under that option, but I went ahead and added the whole set, but I did revoke the token after I imported everyone so it doesn’t accidentally get used for something else.

image-20220826184434413

Data File

The data file I used was just a simple text file with one username per line like this:

user1
user2
user3
user4
user5

I saved mine as users.txt, so that’s what the example below will show. But you can save it as whatever make sense to you.

The Terminal

I used the bash terminal that comes with Git so that I had a Linux-ish command line to work from. WSL would probably be fine, but I can’t run that on my school computer so I didn’t get to test it.

In the command below you’ll need to replace YOUR_TOKEN with the access token you generated earlier, ORG_NAME with the organization name, and TEAM_NAME with the name of your team.

$ for i in `cat`; do curl -X PUT -H "Accept: application/vnd.github+json" -H "Authorization: token YOUR_TOKEN" https://api.github.com/orgs/ORG_NAME/teams/TEAM_NAME/memberships/$i -d ""; done < users.txt

Hit enter and it should take off and add your users. It will take a few minutes since each user is a separate API call.

Snags

Two things caught me while I was trying to do this.

The first is an easy issue. If the user isn’t already in the organization with the team they’ll be added as pending until they join the organization.

And if your organization requires SSO logins you have to explicitly set your access token to work through SSO. Not a big deal, just an extra click. But it was something I missed the first time I tried this.

This site contains affiliate links. If you click an affiliate link and make a purchase we may get a small commission. It doesn't affect the price you pay, but it is something we must disclose.