From 22df3ebd2204c0cb508470321a70de6eaa40af7e Mon Sep 17 00:00:00 2001
From: "Robin H. Johnson" <robbat2@gentoo.org>
Date: Sun, 8 Mar 2015 16:33:30 -0700
Subject: [PATCH 1/5] prepare to add more hooks.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
---
 notify-webhook.py | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/notify-webhook.py b/notify-webhook.py
index f5c0fb0..6e35c78 100755
--- a/notify-webhook.py
+++ b/notify-webhook.py
@@ -163,6 +163,8 @@ def get_base_ref(commit, ref):
         return base_ref
 
 def make_json(old, new, ref):
+    # Lots more fields could be added
+    # https://developer.github.com/v3/activity/events/types/#pushevent
     data = {
         'before': old,
         'after': new,
@@ -195,6 +197,7 @@ def make_json(old, new, ref):
                         'modified': r['modified']
                         })
     data['commits'] = commits
+    data['size'] = len(commits)
     data['head_commit'] = get_revisions(old, new, True)
 
     base_ref = get_base_ref(new, ref)

From e11af56ff981d808e2c42fc72530da1444ce9e1a Mon Sep 17 00:00:00 2001
From: "Robin H. Johnson" <robbat2@gentoo.org>
Date: Sun, 8 Mar 2015 16:42:59 -0700
Subject: [PATCH 2/5] Implement GitHub-compatible HMAC validation of content.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
---
 notify-webhook.py | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/notify-webhook.py b/notify-webhook.py
index 6e35c78..641a412 100755
--- a/notify-webhook.py
+++ b/notify-webhook.py
@@ -35,6 +35,7 @@ def get_repo_name():
 POST_USER = get_config('hooks.authuser')
 POST_PASS = get_config('hooks.authpass')
 POST_REALM = get_config('hooks.authrealm')
+POST_SECRET_TOKEN = get_config('hooks.secrettoken')
 POST_CONTENTTYPE = get_config('hooks.webhook-contenttype', 'application/x-www-form-urlencoded')
 REPO_URL = get_config('meta.url')
 COMMIT_URL = get_config('meta.commiturl')
@@ -208,10 +209,22 @@ def make_json(old, new, ref):
 
 def post(url, data):
     opener = urllib2.HTTPHandler
+    headers = {
+        'Content-Type': POST_CONTENTTYPE,
+    }
     if POST_CONTENTTYPE == 'application/json':
-        request = urllib2.Request(url, data, {'Content-Type': 'application/json'})
+        postdata = data
     elif POST_CONTENTTYPE == 'application/x-www-form-urlencoded':
-        request = urllib2.Request(url, urllib.urlencode({'payload': data}))
+        postdata = urllib.urlencode({'payload': data})
+    if POST_SECRET_TOKEN is not None:
+        import hmac
+        import hashlib
+        hmacobj = hmac.new(POST_SECRET_TOKEN, postdata, hashlib.sha1)
+        signature = 'sha1=' + hmacobj.hexdigest()
+        headers['X-Hub-Signature'] = signature
+
+    request = urllib2.Request(url, postdata, headers)
+
     if POST_USER is not None or POST_PASS is not None:
         password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
         password_mgr.add_password(POST_REALM, url, POST_USER, POST_PASS)

From 568295a22c4301958cad5ccc4785d277c68ee915 Mon Sep 17 00:00:00 2001
From: "Robin H. Johnson" <robbat2@gentoo.org>
Date: Sun, 8 Mar 2015 16:47:39 -0700
Subject: [PATCH 3/5] Include X-GitHub-Event header.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
---
 notify-webhook.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/notify-webhook.py b/notify-webhook.py
index 641a412..37f1aeb 100755
--- a/notify-webhook.py
+++ b/notify-webhook.py
@@ -211,6 +211,7 @@ def post(url, data):
     opener = urllib2.HTTPHandler
     headers = {
         'Content-Type': POST_CONTENTTYPE,
+        'X-GitHub-Event': 'push',
     }
     if POST_CONTENTTYPE == 'application/json':
         postdata = data

From 4b4e0d9cdbe076a33d68445908d9c10f4f1e0f52 Mon Sep 17 00:00:00 2001
From: "Robin H. Johnson" <robbat2@gentoo.org>
Date: Sun, 8 Mar 2015 17:06:31 -0700
Subject: [PATCH 4/5] Allow usage of gitweb configuration keys for
 description/name; with faster fallback as well.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
---
 notify-webhook.py | 43 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 8 deletions(-)

diff --git a/notify-webhook.py b/notify-webhook.py
index 37f1aeb..167b7b3 100755
--- a/notify-webhook.py
+++ b/notify-webhook.py
@@ -8,6 +8,9 @@
 from datetime import datetime
 import simplejson as json
 
+EMAIL_RE = re.compile("^(.*) <(.*)>$")
+DIFF_TREE_RE = re.compile("^:(?P<src_mode>[0-9]{6}) (?P<dst_mode>[0-9]{6}) (?P<src_hash>[0-9a-f]{7,40}) (?P<dst_hash>[0-9a-f]{7,40}) (?P<status>[ADMTUX]|[CR][0-9]{1,3})\s+(?P<file1>\S+)(?:\s+(?P<file2>\S+))?$", re.MULTILINE)
+
 def git(args):
     args = ['git'] + args
     git = subprocess.Popen(args, stdout = subprocess.PIPE)
@@ -31,6 +34,21 @@ def get_repo_name():
     else:
         return os.path.basename(os.path.dirname(os.getcwd()))
 
+def extract_name_email(s):
+    p = re.compile(EMAIL_RE)
+    p.search(s.strip())
+    name = _.group(1)
+    if name is not None:
+        name = name.strip()
+        if len(name) <= 0:
+            name = None
+    email = _.group(2)
+    if email is not None:
+        email = email.strip()
+        if len(email) <= 0:
+            email = None
+    return (name, email)
+
 POST_URL = get_config('hooks.webhookurl')
 POST_USER = get_config('hooks.authuser')
 POST_PASS = get_config('hooks.authpass')
@@ -47,18 +65,27 @@ def get_repo_name():
 REPO_NAME = get_repo_name()
 REPO_DESC = ""
 try:
-    REPO_DESC = get_config('meta.description') or open('description', 'r').read()
+    REPO_DESC = get_config('meta.description') or get_config('gitweb.description') or open('description', 'r').read()
 except Exception:
     pass
+
+# Explicit keys
 REPO_OWNER_NAME = get_config('meta.ownername')
 REPO_OWNER_EMAIL = get_config('meta.owneremail')
-if REPO_OWNER_NAME is None:
-    REPO_OWNER_NAME = git(['log','--reverse','--format=%an']).split("\n")[0]
-if REPO_OWNER_EMAIL is None:
-    REPO_OWNER_EMAIL = git(['log','--reverse','--format=%ae']).split("\n")[0]
-
-EMAIL_RE = re.compile("^(.*) <(.*)>$")
-DIFF_TREE_RE = re.compile("^:(?P<src_mode>[0-9]{6}) (?P<dst_mode>[0-9]{6}) (?P<src_hash>[0-9a-f]{7,40}) (?P<dst_hash>[0-9a-f]{7,40}) (?P<status>[ADMTUX]|[CR][0-9]{1,3})\s+(?P<file1>\S+)(?:\s+(?P<file2>\S+))?$", re.MULTILINE)
+# Fallback to gitweb
+gitweb_owner = get_config('gitweb.owner')
+if gitweb_owner is not None and REPO_OWNER_NAME is None and REPO_OWNER_EMAIL is None:
+    (name, email) = extract_name_email(gitweb_owner)
+    REPO_OWNER_NAME = name if name is not None
+    REPO_OWNER_EMAIL = email if email is not None
+# Fallback to the repo
+if REPO_OWNER_NAME is None or REPO_OWNER_EMAIL is None:
+    # You cannot include -n1 because it is processed before --reverse
+    logmsg = git(['log','--reverse','--format="%an%x09%ae"']).split("\n")[0]
+    # These will never be null
+    (name, email) = logmsg.split("\t")
+    REPO_OWNER_NAME = name if REPO_OWNER_NAME is None
+    REPO_OWNER_EMAIL = email if REPO_OWNER_EMAIL is None
 
 def get_revisions(old, new, head_commit=False):
     if re.match("^0+$", old):

From a2dd42d063376e77159185b122e32c8a519f9f53 Mon Sep 17 00:00:00 2001
From: "Robin H. Johnson" <robbat2@gentoo.org>
Date: Sun, 8 Mar 2015 17:18:05 -0700
Subject: [PATCH 5/5] Fixup.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
---
 notify-webhook.py | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/notify-webhook.py b/notify-webhook.py
index 167b7b3..6737d21 100755
--- a/notify-webhook.py
+++ b/notify-webhook.py
@@ -76,16 +76,20 @@ def extract_name_email(s):
 gitweb_owner = get_config('gitweb.owner')
 if gitweb_owner is not None and REPO_OWNER_NAME is None and REPO_OWNER_EMAIL is None:
     (name, email) = extract_name_email(gitweb_owner)
-    REPO_OWNER_NAME = name if name is not None
-    REPO_OWNER_EMAIL = email if email is not None
+    if name is not None:
+        REPO_OWNER_NAME = name
+    if email is not None:
+        REPO_OWNER_EMAIL = email
 # Fallback to the repo
 if REPO_OWNER_NAME is None or REPO_OWNER_EMAIL is None:
     # You cannot include -n1 because it is processed before --reverse
     logmsg = git(['log','--reverse','--format="%an%x09%ae"']).split("\n")[0]
     # These will never be null
     (name, email) = logmsg.split("\t")
-    REPO_OWNER_NAME = name if REPO_OWNER_NAME is None
-    REPO_OWNER_EMAIL = email if REPO_OWNER_EMAIL is None
+    if REPO_OWNER_NAME is None:
+        REPO_OWNER_NAME = name
+    if REPO_OWNER_EMAIL is None:
+        REPO_OWNER_EMAIL = email
 
 def get_revisions(old, new, head_commit=False):
     if re.match("^0+$", old):
