add support for github -> local sync
[git-mirror.git] / webhook.py
1 #!/usr/bin/python3
2 import urllib.request, urllib.parse, json, os, sys
3
4 def is_github(remote_addr):
5     '''Returns whether the address is a github hook address. This function requires Python 3.3.'''
6     from ipaddress import ip_address, ip_network
7     remote_addr = ip_address(ip_network)
8     github = urllib.request.urlopen('https://api.github.com/meta').read()
9     github = json.loads(github.decode('utf-8'))
10     for net in github['hooks']:
11         if remote_addr in ip_network(net):
12             return True
13
14 # get repository from query string
15 query = os.getenv("QUERY_STRING")
16 query = urllib.parse.parse_qs(query)
17 repository = query.get('repository', [])
18 repository = repository[0] if len(repository) else ''
19
20 # execute the actual script
21 git_mirror = "/home/ralf/git-mirror/update.py"
22 os.execlp("sudo", "sudo", "-n", "-u", "git", git_mirror, "--web-hook", "--repository", repository)