Skip to content

Commit 5658a54

Browse files
committed
fix name and email parsing
See pr metajack#27 mostly addressed this, but this version is more paranoid. When this lands, metajack#27 can be closed out. Signed-off-by: Brian Harring <[email protected]>
1 parent d45f795 commit 5658a54

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

notify-webhook.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
from collections import OrderedDict
1616
from datetime import datetime
1717

18-
EMAIL_RE = re.compile(r"^(\"?)(?P<name>.*)\1\s+<(?P<email>.*)>$")
19-
2018
# see git-diff-tree 'RAW OUTPUT FORMAT'
2119
# https://git-scm.com/docs/git-diff-tree#_raw_output_format
2220
DIFF_TREE_RE = re.compile(
@@ -96,14 +94,32 @@ def get_repo_description():
9694
return ""
9795

9896

99-
def extract_name_email(s):
100-
p = EMAIL_RE
101-
_ = p.search(s.strip())
102-
if not _:
103-
return (None, None)
104-
name = (_.group("name") or "").strip()
105-
email = (_.group("email") or "").strip()
106-
return (name, email)
97+
_STRIP_QUOTED_NAME_RE = re.compile(r"^\s*([\"'])\s*(?P<value>.*?)\s*\1\s*$")
98+
99+
100+
def _strip_quoted_name(val):
101+
# I can't think of why it would come through with \' beyond presumably git CLI escaping, but
102+
# whatever, cover that base too.
103+
if m := _STRIP_QUOTED_NAME_RE.match(val):
104+
return m.groupdict()["value"]
105+
return val
106+
107+
108+
_EMAIL_RE = re.compile(r"\s*(?P<name>[^<]+?)\s*<\s*(?P<email>[^>]+?)\s*>\s*$")
109+
110+
111+
def extract_name_email(s, default_missing=""):
112+
s = s.strip()
113+
114+
if m := _EMAIL_RE.match(s):
115+
g = m.groupdict()
116+
# compatability: strip out quotation, since the original code tried to do this.
117+
118+
return (_strip_quoted_name(g["name"]), g["email"])
119+
# guess a bit
120+
if "@" in s:
121+
return default_missing, s
122+
return (_strip_quoted_name(s) if s else default_missing, default_missing)
107123

108124

109125
def get_repo_owner():
@@ -237,12 +253,8 @@ def get_revisions(
237253
tzstr = props["date"][-5:]
238254
props["date"] = basetime.strftime("%Y-%m-%dT%H:%M:%S") + tzstr
239255

240-
m = EMAIL_RE.match(props["author"])
241-
if m:
242-
props["author"] = AuthorData(name=m.group(1), email=m.group(2))
243-
else:
244-
props["author"] = AuthorData(name="unknown", email="unkown")
245-
256+
(name, email) = extract_name_email(props["author"], "unknown")
257+
props["author"] = AuthorData(name=name, email=email)
246258
yield CommitData(**props)
247259
s += 2
248260

0 commit comments

Comments
 (0)