[asterisk-commits] Change in repotools[master]: digium_jira_user: Add a wrapper for obtaining User informati...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Apr 16 14:55:03 CDT 2015


Matt Jordan has submitted this change and it was merged.

Change subject: digium_jira_user: Add a wrapper for obtaining User information from JIRA
......................................................................


digium_jira_user: Add a wrapper for obtaining User information from JIRA

This adds a module that provides a new class, AsteriskUser, that wraps
up all the user related information that we generally care about.
This includes their name, e-mail address, JIRA username, etc.

In addition, it provides a utility function that pulls all useful
information for a user, including their full name, e-mail address, and
CLA license, if available.

Change-Id: I3da7e729daedd2a4828d00bf8f8418d7e1b9604f
---
A digium_jira_user.py
1 file changed, 147 insertions(+), 0 deletions(-)

Approvals:
  Scott Griepentrog: Looks good to me, but someone else must approve
  Matt Jordan: Looks good to me, approved; Verified



diff --git a/digium_jira_user.py b/digium_jira_user.py
new file mode 100644
index 0000000..f17839d
--- /dev/null
+++ b/digium_jira_user.py
@@ -0,0 +1,147 @@
+#!/usr/bin/env python
+"""Asterisk project user management
+
+Copyright (C) 2013, Digium, Inc.
+Matt Jordan <mjordan at digium.com>
+"""
+
+import getpass
+
+from digium_jira import get_jira_client
+
+# A mapping of known organizations to usernames
+# of people that happen to work there
+organization_mappings = {
+    'Digium': ['mmichelson', 'mfredrickson', 'creslin', 'dbailey', 'markster',
+               'mspiceland', 'sruffell', 'espiceland', 'rmudgett',
+               'mjordan', 'jrose', 'kmoore', 'jcolp', 'jbigelow', 'dlee',
+               'mdavenport', 'rnewton', 'sgriepentrog', 'sgalarneau',
+               'rmeyerriecks', 'dbailey', 'jhardin', 'asanders',],
+    'Xorcom': ['tzafrir', 'leonidf', 'diegoias', 'dennis-xorcom'],
+    'Sangoma': ['jparker', 'moy', 'dyatsin', 'tm1000', 'jameswf',
+                'p_lindheimer', 'schmoozecom'],
+    'Edvina': ['oej'],
+    'Avoxi': ['dsessions', 'lmadsen'],
+    'Red Hat': ['russell', 'twilson', 'dvossell'], }
+
+name_mappings = {
+    'creslin': 'mfredrickson',
+    'datachomper': 'rmeyerriecks',
+    'mattf': 'mfredrickson',
+    'putnopvut': 'mmichelson',
+    'corydon76': 'tilghman',
+    'north': 'jparker',
+    'qwell': 'jparker',
+    'tempest1': 'bbryant',
+    'otherwiseguy': 'twilson',
+    'drumkilla': 'russell',
+    'russellb': 'russell',
+    'citats': 'jamesgolovich',
+    'blitzrage': 'lmadsen',
+    'opticron': 'kmoore',
+    'file': 'jcolp',
+    'andrewsnagy': 'tm1000',
+    'TheJames': 'jameswf', }
+
+
+class AsteriskUser(object):
+    """An Asterisk project participant/user
+
+    Attributes:
+    full_name     - The full name of the user
+    username      - The JIRA user name
+    email         - The user's e-mail address
+    license       - The user's CLA number
+    jira_user_obj - If we looked the user up, their JIRA user object
+    """
+
+    def __init__(self, username=None):
+        """Constructor
+
+        Keyword Arguments:
+        username - The unique ID for the user
+        """
+        self.full_name = None
+        self._username = None
+        self.email = None
+        self.license = None
+        self.jira_user_obj = None
+        self.organization = None
+
+        self.username = username
+
+    @property
+    def username(self):
+        """Get accessor for username"""
+        return self._username
+
+    @username.setter
+    def username(self, val):
+        """Set accessor for username"""
+        self._username = name_mappings.get(val) or val
+        self.update_organization()
+
+    def update_organization(self):
+        """Infers the organization of a user based on their username
+
+        This will set the organization attribute
+        """
+        for organization, members in organization_mappings.items():
+            if self._username in members:
+                self.organization = organization
+                break
+
+    def __repr__(self):
+        """Create a string representation of the object"""
+        name = ''
+        if self.full_name:
+            name = unicode(self.full_name).encode('utf-8')
+        elif self.username:
+            name = self.username
+        else:
+            name = "Unknown"
+        if self.email:
+            name = "{0} <{1}>".format(name, self.email)
+        if self.license:
+            name = "{0} (License {1})".format(name, self.license)
+        return name
+
+
+def get_user_by_username(username, issue=None, jira=None):
+    """Get a user by their username
+
+    Keyword Arguments:
+    username - The user name to lookup
+    issue    - The issue the user is associated with.
+    jira     - The JIRA client connection
+
+    If the issue is specified, we can attempt to see if this user has a
+    license associated with them. Without an issue with a patch on it,
+    we can't determine the license number.
+
+    Returns:
+    A User object populated with the user's information
+    """
+
+    user = AsteriskUser(username)
+
+    if not jira:
+        jira = get_jira_client()
+
+    jira_user = jira.user(user.username)
+    if not jira_user:
+        return user
+
+    user.jira_user_obj = jira_user.raw
+    user.email = jira_user.raw['emailAddress']
+    user.full_name = jira_user.raw['displayName']
+
+    if issue:
+        jira_issue = jira.issue(issue)
+        attachments = filter(lambda a: a.raw['author']['name'] == username,
+                jira_issue.fields.attachment)
+        if len(attachments) != 0:
+            attachment = jira.attachment(attachments[0].id)
+            user.license = attachment.raw['properties']['license']
+    return user
+

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I3da7e729daedd2a4828d00bf8f8418d7e1b9604f
Gerrit-PatchSet: 1
Gerrit-Project: repotools
Gerrit-Branch: master
Gerrit-Owner: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Scott Griepentrog <sgriepentrog at digium.com>



More information about the asterisk-commits mailing list