Julython

We are really big on open source at Eldarion and were excited to see Julythonannounced the other day on Twitter. However, we struggle with the problem of too many repositories to manage webhooks by hand.

In response to a tweet to this affect, David Glick @davisagligave me a link to how he added a webhook to a bunch of repos at once.

I decided to modify it a bit to use it for the same:

import getpass
import json
import sys

import requests
GH_URL = 'https://api.github.com'
USERNAME = raw_input("Username: ")
PASSWORD = getpass.getpass()
ORG = raw_input("Organization: ")
HOOK_URL = raw_input("Web Hook URL: ")
with requests.session(auth=(USERNAME, PASSWORD)) as s:
    response = s.get(GH_URL+'/orgs/%s/repos' % ORG)
    if response.status_code == 200:
        repos = [
            r['name']
            for r in json.loads(response.content)
            if not r['private']
        ]

        for r in repos:
            response = s.get(GH_URL+'/repos/%s/%s/hooks' % (ORG, r))
            hooks = json.loads(response.content)
            if response.status_code == 200:
                if not any([h['name'] == 'web' and h['config']['url'] == HOOK_URL for h in hooks]):
                    req = {
                        'name': 'web',
                        'active': True,
                        'config': {
                            'url': HOOK_URL,
                        }
                    }
                    print '%s/%s' % (ORG, r), '==>', HOOK_URL
                    s.post(GH_URL+'/repos/%s/%s/hooks' % (ORG, r), data=json.dumps(req))
            else:
                print "Hook Status Code", response.status_code, r
                print response.content
    else:
        print "Status Code: ", response.status_code
        print response.content

I ran this tonight adding the Julython webhook to all of Eldarion's and Pinax's open source repositories. Feel free to use it for your own organizations.

Now, let the coding begin!