redesign to use force pushes only if necessary, to avoid race conditions, and to...
[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     return False
14
15 # get repository from query string
16 query = os.getenv("QUERY_STRING")
17 query = urllib.parse.parse_qs(query)
18 repository = query.get('repository', [])
19 repository = repository[0] if len(repository) else ''
20
21 # execute the actual script
22 webhook_core = "/home/ralf/git-mirror/webhook-core.py"
23 os.execlp("sudo", "sudo", "-n", "-u", "git", webhook_core, repository)