Skip to content

Instantly share code, notes, and snippets.

@jehiah
Created January 15, 2013 03:21
Show Gist options
  • Save jehiah/4535777 to your computer and use it in GitHub Desktop.
Save jehiah/4535777 to your computer and use it in GitHub Desktop.
This is a script that updates hipchat hooks on a github repository to only have a small set of actions they trigger based on (ie: push, issues).
#!/usr/bin/env python
"""
This is a script that updates hipchat hooks on github repository to only have a small set of actions they trigger based on.
created by Jehiah Czebotar 2013
Usage:
update_github_hipchat_hook.py --repo=jehiah/json2csv --access_token={{...}}
"""
import tornado.options
import tornado.httpclient
import urllib
import urllib2
import simplejson as json
hook_events = {
"push": "Any git push to a Repository.",
"issues": "Any time an Issue is opened or closed.",
"issue_comment": "Any time an Issue is commented on.",
"commit_comment": "Any time a Commit is commented on.",
"pull_request": "Any time a Pull Request is opened, closed, or synchronized (updated due to a new push in the branch that the pull request is tracking).",
"gollum": "Any time a Wiki page is updated.",
"watch": "Any time a User watches the Repository.",
"download": "Any time a Download is added to the Repository.",
"fork": "Any time a Repository is forked.",
"fork_apply": "Any time a patch is applied to the Repository from the Fork Queue.",
"member": "Any time a User is added as a collaborator to a non-Organization Repository.",
"public": "Any time a Repository changes from private to public.",
"status": "Any time a Repository has a status update from the API",
}
def update_hook(hook_id, config, events, repo, access_token):
params = dict(access_token=access_token)
url = "https://api.github.com/repos/%s/hooks/%s?%s" % (repo, hook_id, urllib.urlencode(params))
body = dict(name="hipchat", config=config, events=events)
request = urllib2.Request(url, data=json.dumps(body))
request.get_method = lambda: 'PATCH'
resp = urllib2.urlopen(request)
body = resp.read()
print resp.info(), body
def get_hooks(repo, access_token):
http = tornado.httpclient.HTTPClient()
params = dict(access_token=access_token)
resp = http.fetch('https://api.github.com/repos/%s/hooks?%s' % (repo, urllib.urlencode(params)))
return json.loads(resp.body)
def run(repo, access_token, events):
for hook in get_hooks(repo, access_token):
if hook['name'] != "hipchat":
continue
print hook
update_hook(hook['id'], hook['config'], events, repo, access_token)
if __name__ == "__main__":
tornado.options.define("repo", type=str, help="the user/repo combination")
tornado.options.define('access_token', type=str, help="github OAuth access token")
tornado.options.define("event", type=str, multiple=True, default=["push", "issues"], help="the events to set")
tornado.options.parse_command_line()
o = tornado.options.options
for event in o.event:
assert event in hook_events, "%s not in %s" % (event, hook_events.keys())
assert o.repo, "missing --repo"
assert o.access_token, "missing --access-token"
run(o.repo, o.access_token, o.event)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment