Automated GitHub Organization Invites
with Ruby, Sinatra, Slim, Octokit and ngrok
I’m working as a TA for a university course (Software Specification and Design), and I’m creating a GitHub organization for students to submit assignments.
The problem is getting all students into this organization.
At first, I thought of using Google Spreadsheet to create a form and letting students fill in their GitHub username, but…
- I’d have to take care of inviting each username into the organization.
- I’d have to tell them to check their email.
- I’d have to wait for them to check their email and accept the invitation.
That’s too much work for me, so I started thinking of a solution:
a website where the student fills in their GitHub username,
clicks a button, and an invite is sent.
We are going to write it in Ruby, as tools (gems) to accomplish this are readily available:
- Sinatra is a very simple DSL for quickly creating web applications;
- Octokit is a GitHub API client library;
- GitHub has an API for adding members to team by username;
- Although not required, Slim makes writing HTML much easier, without those curly braces and closing tags; and finally,
- ngrok lets you publish your local web server online.
…and all we need to do is to put them together.
So, first, I ran bundle init and add some gems to the Gemfile:
Then I wrote a Ruby script to display a home page and a POST request handler to add the user to GitHub repository:
Here’s views/index.slim that displays a form that POSTs to /add:
After the server’s done, let’s run it:
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
…and also run ngrok to create a publicly-accessible tunnel:
Tunnel Status online
Forwarding http://2fc3c02e.ngrok.com -> 127.0.0.1:4567
Forwarding https://2fc3c02e.ngrok.com -> 127.0.0.1:4567
Web Interface 127.0.0.1:4040
# Conn 0
Avg Conn Time 0.00ms
Then I instructed the students to go to that URL…
…enter their GitHub username, and click the button…
…and they get an email instantly!
…and within minutes, most students are inside the organization.