- data = get_github_payload()
- ref = data["ref"]
- oldsha = data["before"]
- newsha = data["after"]
- # validate the ref name
- if re.match('refs/[a-z/]+', ref) is None:
- raise Exception("Invalid ref name {0}".format(ref))
- # collect URLs of this repository, to find the mirror name
- urls = []
- for key in ("git_url", "ssh_url", "clone_url"):
- urls.append(data["repository"][key])
- mirror = repo.find_mirror_by_url(urls)
- if mirror is None:
- raise Exception("Could not find the mirror.")
- repo.update_ref_from_mirror(ref, oldsha, newsha, mirror, suppress_stderr = True)
- # print an answer
- print("Content-Type: text/plain")
- print()
- print("Updated {0}:{1} from mirror {2} from {3} to {4}".format(reponame, ref, mirror, oldsha, newsha))
+ data = get_github_payload(repo, githubSignature)
+ if githubEvent == 'ping':
+ # github sends this initially
+ print("Content-Type: text/plain")
+ print()
+ print("Pong!")
+ sys.exit(0)
+ elif githubEvent == 'push':
+ ref = data["ref"]
+ oldsha = data["before"]
+ newsha = data["after"]
+ # validate the ref name
+ if re.match('refs/[a-z/]+', ref) is None:
+ raise Exception("Invalid ref name {}".format(ref))
+ # collect URLs of this repository, to find the mirror name
+ urls = []
+ for key in ("git_url", "ssh_url", "clone_url"):
+ urls.append(data["repository"][key])
+ mirror = repo.find_mirror_by_url(urls)
+ if mirror is None:
+ raise Exception("Could not find the mirror.")
+ stdout = repo.update_ref_from_mirror(ref, oldsha, newsha, mirror, suppress_stderr = True)
+ # print an answer
+ print("Content-Type: text/plain")
+ print()
+ print("Updated {}:{} from mirror {} from {} to {}".format(reponame, ref, mirror, oldsha, newsha))
+ print(stdout)
+ else:
+ raise Exception("Unexpected github event {}.".format(githubEvent))