Automatically send messages to all forkers of your Github repo

Aslak Hellesoy, creator of Cucumber and co-founder of RSpec has recently posted on the cucumber groups that he is looking for someone to take ownership of the Cucumber Textmate bundle.

Someone suggested that he should message all the forkers and ask if anyone was willing to take the responsibility of merging all the forks into one and maintain it under the cucumber organization. It was a good idea and Aslak asked for someone to actually script that because Github doesn’t have the functionality to mass message all the users that have forked your repo.

He did post some guidelines(as seen in his second post in the thread) and seeing a challenge, I offered to try and play with the Github API.

To start off, he posted a url that returns a JSON string of the user info of all the forkers of his repo: https://github.com/api/v2/json/repos/show/cucumber/cucumber-tmbundle/network

The next step was to parse that JSON and get all their emails/usernames to either email them all or mass message them using the Github messaging system. To make the long story short, I did finish playing around with it, using the json, net/http, and mechanize gems.

Here’s the actual code with comments:

require 'rubygems' require 'mechanize' require 'net/http' require 'json'

#get the tmbundle forkers url = "http://github.com/api/v2/json/repos/show/cucumber/cucumber-tmbundle/network" cuke_tmbundle_forkers = JSON.parse(Net::HTTP.get_response(URI.parse(url)).body).first[1].map{ |x| x["owner"] }

agent = Mechanize.new

Get the github sign in page

page = agent.get('https://github.com/login')

Fill out the login form

form = page.form_with(:action => '/session') form.login = ARGV[0] form.password = ARGV[1] page = agent.submit(form)

cuke_tmbundle_forkers.each do |forker| page = agent.get("https://github.com/inbox/new/#{forker}")

go to inbox, set subject and body and send!

form = page.form_with(:action => "/inbox") form["message[subject]"] = "test for cucumber tmbundle network" form["message[body]"] = "lorem ipsum text for you know foo bar"

agent.submit(form) end

As you can see, it is pretty simple:

  1. Go to the URL using NET::HTTP
  2. Parse it using JSON
  3. get the first argument and the second array(the first[1] call)
  4. map them and get all the ["owner"] values

You’ll get an array with all the Github usernames of the forkers and it is easy from there:

Using Mechanize:

  1. Copied the flickr upload example in mechanize’s repo: https://github.com/tenderlove/mechanize/blob/master/examples/flickr_upload.rb
  2. Substitute Github instead of flickr
  3. Login using your credentials(for security, you put your credentials when running the script, instead of putting it in the source code(e.g. ruby github_auto_messager.rb myusername mypassword
  4. Go to the send message page for each user
  5. Input your subject and body
  6. ??? 7 PROFIT!

Well not really, but you get the point.

There are probably other more exciting and fun ways to use GIthub’s API so play around with it and see what you can do.

If you want to fork the code, you can do so by forking the gist here: https://gist.github.com/982479