[Asterisk-code-review] digium commits: Improve reliability of extracting user from ... (repotools[master])

Matt Jordan asteriskteam at digium.com
Tue Jun 9 06:41:19 CDT 2015


Matt Jordan has uploaded a new change for review.

  https://gerrit.asterisk.org/608

Change subject: digium_commits: Improve reliability of extracting user from messages
......................................................................

digium_commits: Improve reliability of extracting user from messages

This patch makes extracting user information from a variety of fields in
commit messages slightly more reliable. It:
* Checks and parses usernames using the same method where all user names
  can appear. Previously, a full username extraction was only performed
  on coders; now, authors, testers, and other fields are fully parsed.
* The regex for "Tested by" and "Reported by" previously allowed for any
  number of spaces between the first and second word. In practice, this
  caused matching problems. We now looking for only a single space or a
  hyphen.

Change-Id: Id85d719aa2cc59015f1b49539563ddbb7b2b1928
---
M digium_commits.py
1 file changed, 47 insertions(+), 33 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/repotools refs/changes/08/608/1

diff --git a/digium_commits.py b/digium_commits.py
index 522731f..7739e66 100644
--- a/digium_commits.py
+++ b/digium_commits.py
@@ -9,7 +9,8 @@
 import re
 from datetime import datetime
 
-from digium_jira_user import AsteriskUser
+from digium_jira_user import get_user_by_username
+
 
 class DigiumCommitMessageParser:
     '''
@@ -37,12 +38,49 @@
         string "Blocked revisions " is found in the commit message.
         '''
         self.message = unicode(message).encode('utf-8')
-        self.author = AsteriskUser(author.strip())
+        self.author = get_user_by_username(author.strip())
         self.raw = raw
         self.block = False
 
         if self.message.find("Blocked revisions ") >= 0:
             self.block = True
+
+    def extract_user(self, raw_user):
+        """Create an AsteriskUser object from a raw user string
+
+        Keyword Arguments:
+        raw_user The raw string containing the user information
+
+        Returns:
+        An AsteriskUser object
+        """
+        realname = None
+        email = None
+        license = None
+
+        name = raw_user.strip()
+        if ':' in name:
+            name.replace(':', '')
+        if '<' in name:
+            realname = name[:name.index('<')]
+            email = name[(name.index('<') + 1):name.index('>')]
+        if '(' in name:
+            if not realname:
+                realname = name[:name.index('(')]
+                license = name[(name.index('(') + 1):name.index(')')]
+        if not realname:
+            realname = name
+        if len(realname) == 0:
+            return None
+
+        realname = realname.strip()
+        user = get_user_by_username(realname)
+        if email:
+            user.email = email.strip()
+        if license:
+            user.license = license.strip()
+
+        return user
 
     def get_issues(self, closed=False):
         '''
@@ -92,11 +130,11 @@
         The return value is a list of strings.
         '''
         all_testers = []
-        for match in re.finditer("[Tt]ested(\s*|-)[Bb]y:?(.*)", self.message):
+        for match in re.finditer("[Tt]ested[- ][Bb]y:?(.*)", self.message):
             testers = match.group(1).split(",")
             for t in testers:
-                user = AsteriskUser(t)
-                if user not in all_testers:
+                user = self.extract_user(t)
+                if user and user not in all_testers:
                     all_testers.append(user)
         return all_testers
 
@@ -116,9 +154,6 @@
         coders = []
         tokens = self.message.split('\n')
         patches = False
-        realname = None
-        email = None
-        license = None
         for token in tokens:
             if 'patches:' in token.lower():
                 patches = True
@@ -130,28 +165,8 @@
             if 'submitted by' in token.lower():
                 name = token[token.lower().index('submitted by') + 12:]
             if name:
-                name = name.strip()
-                if ':' in name:
-                    name.replace(':', '')
-                if '<' in name:
-                    realname = name[:name.index('<')]
-                    email = name[(name.index('<') + 1):name.index('>')]
-                if '(' in name:
-                    if not realname:
-                        realname = name[:name.index('(')]
-                    license = name[(name.index('(') + 1):name.index(')')]
-                if not realname:
-                    realname = name
-                if len(realname) == 0:
-                    continue
-                realname = realname.strip()
-                user = AsteriskUser(realname)
-                if email:
-                    user.email = email.strip()
-                if license:
-                    user.license = license.strip()
-
-                if user not in coders:
+                user = self.extract_user(name)
+                if user and user not in coders:
                     coders.append(user)
         if len(coders) == 0:
             coders.append(self.author)
@@ -166,9 +181,9 @@
         "Reported by: ", then returns the name found after that.
         '''
         reporter = None
-        match = re.search("[Rr]eported(\s*|-)[Bb]y:?(.*)", self.message)
+        match = re.search("[Rr]eported[ -][Bb]y:?(.*)", self.message)
         if match is not None:
-            reporter = AsteriskUser(match.group(1))
+            reporter = self.extract_user(match.group(1))
         return reporter
 
     def get_commit_summary(self):
@@ -223,7 +238,6 @@
                 text += '\n'
 
         return text
-
 
     def __repr__(self):
         '''Return a string represenation of the object'''

-- 
To view, visit https://gerrit.asterisk.org/608
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id85d719aa2cc59015f1b49539563ddbb7b2b1928
Gerrit-PatchSet: 1
Gerrit-Project: repotools
Gerrit-Branch: master
Gerrit-Owner: Matt Jordan <mjordan at digium.com>



More information about the asterisk-code-review mailing list