[asterisk-dev] Change in repotools[master]: digium_jira: Refactor module to wrap the Atlassian JIRA REST...

Matt Jordan (Code Review) asteriskteam at digium.com
Sun Apr 12 16:17:09 CDT 2015


Matt Jordan has uploaded a new change for review.

  https://gerrit.asterisk.org/69

Change subject: digium_jira: Refactor module to wrap the Atlassian JIRA REST client
......................................................................

digium_jira: Refactor module to wrap the Atlassian JIRA REST client

Many of the our scripts that use the JIRA REST Python client first have
to obtain credentials from the .jira_login file, or else prompt for
them. The act of getting credentials and setting up the basic client is
done in a number of scripts.

Rather than reproduce that logic across many modules, this patch takes
the defunct digium_jira module and repurposes it to contain those
functions. Since Atlassian no longer supports its SOAP API and we no
longer use it in any of our modules, its reasonable to locate this
functionality in this module.

Change-Id: I69932dd472aef4290af97e809ce6b9ec9c25b39d
---
M digium_jira.py
1 file changed, 30 insertions(+), 173 deletions(-)


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

diff --git a/digium_jira.py b/digium_jira.py
index abbbfaa..06767be 100644
--- a/digium_jira.py
+++ b/digium_jira.py
@@ -1,188 +1,45 @@
 #!/usr/bin/env python
-'''Utilities that assist in accessing the Digium JIRA server.
+"""Utility function that wraps the Atlassian JIRA REST client
 
-Copyright (C) 2009 - 2010, Digium, Inc.
+Copyright (C) 2009 - 2015, Digium, Inc.
 Russell Bryant <russell at digium.com>
+"""
 
-This module is deprecated in favor of jira-python
-'''
-
-import sys
 import os
-import re
-import urllib2
 import getpass
 
+from jira.client import JIRA
 
-JIRA_WSDL = "https://issues.asterisk.org/jira/rpc/soap/jirasoapservice-v2?wsdl"
+def _get_jira_auth():
+    """Get JIRA credentials"""
+
+    try:
+        jira_cache = open(os.path.expanduser('~') + "/.jira_login", "r")
+        jira_user = jira_cache.readline().strip()
+        jira_pw = jira_cache.readline().strip()
+        jira_cache.close()
+        return (jira_user, jira_pw)
+    except IOError:
+        pass
+
+    # Didn't get auth deatils from file, try interactive instead.
+    print "Please enter your username and pw for JIRA."
+    jira_user = raw_input("Username: ")
+    jira_pw = getpass.getpass("Password: ")
+
+    return (jira_user, jira_pw)
 
 
-class DigiumJIRA:
-    '''This is a wrapper for the JIRA SOAP API.
+def get_jira_client():
+    """Return a connected JIRA client"""
 
-    Each SOAP API call requires JIRA authentication credentials.  This will
-    pull the login information from the ~/.jira_login file.  It expects the
-    first line in this file to be the username, and the 2nd line to be the
-    password.
+    jira_user, jira_password = _get_jira_auth()
 
-    soap_access - Set to False to not set up JIRA SOAP access.
-    '''
-    def __init__(self, soap_access=True):
-        ''' Initialize the DigiumJIRA interface.
+    jira_options = {
+        'server': 'https://issues.asterisk.org/jira/'
+    }
 
-        An exception will be raised if authentication information can not be
-        read from the ~/.jira_login file.
-        '''
-        self.mantis_map = None
-        self.status_map = None
-        if not soap_access:
-            return
-        import SOAPpy
-        self.jira = SOAPpy.WSDL.Proxy(JIRA_WSDL)
-        self.__get_jira_auth()
-        try:
-            self.auth = self.jira.login(self.jira_user, self.jira_pw)
-        except:
-            print 'Failed to login to JIRA'
-            raise
+    jira = JIRA(options=jira_options, basic_auth=(jira_user, jira_password))
 
-    def __get_jira_auth(self):
-        '''Private method that looks up JIRA login credentials.
-        '''
-        try:
-            f = open(os.path.expanduser('~') + "/.jira_login", "r")
-            self.jira_user = f.readline().strip()
-            self.jira_pw = f.readline().strip()
-            f.close()
-            return
-        except:
-            pass
+    return jira
 
-        # Didn't get auth deatils from file, try interactive instead.
-        print "Please enter your username and pw for JIRA."
-        self.jira_user = raw_input("Username: ")
-        self.jira_pw = getpass.getpass("Password: ")
-
-    def get_chart(self, dest, url):
-        '''Scrape the chart from a JIRA report URL.
-
-        JIRA has nice reporting capabilities for projects.  This function
-        allows you to pass in a JIRA report URL and a filename for where to
-        save off the chart that is generated for the report.
-
-        dest - The filename for where to save the chart.
-        url - The JIRA report URL.
-        '''
-        page = urllib2.urlopen("%s&os_username=%s&os_password=%s" %
-                                   (url, self.jira_user, self.jira_pw)).read()
-
-
-        match = re.search("img src=['\"](/jira/charts.*?)['\"]", page)
-
-        if not match:
-            print "Failed to get chart URL for chart: %s" % url
-            return
-
-        try:
-            f = open(dest, "w")
-        except IOError:
-            print "Failed to open %s for writing chart image" % dest
-            return
-
-        f.write(urllib2.urlopen("https://issues.asterisk.org/%s" %
-                                match.group(1)).read())
-
-        f.close()
-
-    def get_issue(self, issue_id):
-        '''Get issue details via the JIRA SOAP API.
-
-        This method takes a single parameter, issue_id, which is an int that
-        indications which issue ID should be looked up.
-
-        The return value is a data structure describing the issue as defined
-        by the JIRA SOAP API.
-        '''
-        try:
-            return self.jira.getIssue(self.auth, issue_id)
-        except:
-            print "error getting JIRA issue %s: %s" % (issue_id, sys.exc_info()[0])
-            return None
-
-    def new_issue(self, issue):
-        return self.jira.createIssue(self.auth, issue)
-
-    def get_issue_types(self):
-        return self.jira.getIssueTypes(self.auth)
-
-    def get_priorities(self):
-        return self.jira.getPriorities(self.auth)
-
-    def get_components(self, project_key):
-        return self.jira.getComponents(self.auth, project_key)
-
-    def get_saved_filters(self):
-        return self.jira.getSavedFilters(self.auth)
-
-    def get_issues_from_filter(self, filter_id):
-        return self.jira.getIssuesFromFilter(self.auth, filter_id)
-
-    def get_custom_fields(self):
-        return self.jira.getCustomFields(self.auth)
-
-    def get_custom_field_id(self, field_name):
-        fields = self.get_custom_fields()
-        for f in fields:
-            if f["name"] == field_name:
-                return f["id"]
-        return None
-
-    def map_mantis_to_jira(self, mantis_id):
-        if self.mantis_map is None:
-            try:
-                f = open("mantis-to-jira-map.txt", "r")
-            except:
-                try:
-                    f = open(os.path.join(os.path.dirname(os.path.realpath(__file__)),
-                                                  "mantis-to-jira-map.txt"), "r")
-                except:
-                    print "Failed to find mantis-to-jira-map.txt"
-                    return 1
-            self.mantis_map = {}
-            for line in f:
-                fields = line.split("\t")
-                self.mantis_map[fields[0]] = fields[1].strip()
-            f.close()
-
-        if mantis_id in self.mantis_map:
-            return self.mantis_map[mantis_id]
-        else:
-            return None
-
-    def status_to_str(self, status):
-        if self.status_map is None:
-            statuses = self.jira.getStatuses(self.auth)
-            self.status_map = {}
-            for s in statuses:
-                self.status_map[s.id] = s.name
-        if status in self.status_map:
-            return self.status_map[status]
-        else:
-            return status
-
-
-def main(argv=None):
-    if argv is None:
-        argv = sys.argv
-
-    if len(argv) != 2:
-        print "Usage: python digium_jira.py <issue key>"
-        return 1
-
-    print DigiumJIRA().get_issue(argv[1])
-
-    return 0
-
-
-if __name__ == "__main__":
-    sys.exit(main())

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

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



More information about the asterisk-dev mailing list