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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Apr 15 14:11:09 CDT 2015


Mark Michelson has submitted this change and it was merged.

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 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, 26 insertions(+), 148 deletions(-)

Approvals:
  Mark Michelson: Looks good to me, approved; Verified



diff --git a/digium_jira.py b/digium_jira.py
index bd4734f..560b7c2 100644
--- a/digium_jira.py
+++ b/digium_jira.py
@@ -1,166 +1,44 @@
 #!/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
 
-
-JIRA_WSDL = "https://issues.asterisk.org/jira/rpc/soap/jirasoapservice-v2?wsdl"
+from jira.client import JIRA
 
 
-class DigiumJIRA:
-    '''This is a wrapper for the JIRA SOAP API.
+def _get_jira_auth():
+    """Get JIRA credentials"""
 
-    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.
+    try:
+        with open(os.path.expanduser('~') + "/.jira_login", "r") as jira_cache:
+            jira_user = jira_cache.readline().strip()
+            jira_pw = jira_cache.readline().strip()
+            return (jira_user, jira_pw)
+    except IOError:
+        pass
 
-    soap_access - Set to False to not set up JIRA SOAP access.
-    '''
-    def __init__(self, soap_access=True):
-        ''' Initialize the DigiumJIRA interface.
+    # Didn't get auth details from file, try interactive instead.
+    print "Please enter your username and pw for JIRA."
+    jira_user = raw_input("Username: ")
+    jira_pw = getpass.getpass("Password: ")
 
-        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
-
-    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
-
-        # 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()
+    return (jira_user, jira_pw)
 
 
-        match = re.search("img src=['\"](/jira/charts.*?)['\"]", page)
+def get_jira_client():
+    """Return a connected JIRA client"""
 
-        if not match:
-            print "Failed to get chart URL for chart: %s" % url
-            return
+    jira_user, jira_password = _get_jira_auth()
 
-        try:
-            f = open(dest, "w")
-        except IOError:
-            print "Failed to open %s for writing chart image" % dest
-            return
+    jira_options = {
+        'server': 'https://issues.asterisk.org/jira/'
+    }
 
-        f.write(urllib2.urlopen("https://issues.asterisk.org/%s" %
-                                match.group(1)).read())
+    jira = JIRA(options=jira_options, basic_auth=(jira_user, jira_password))
 
-        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 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())
+    return jira

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I69932dd472aef4290af97e809ce6b9ec9c25b39d
Gerrit-PatchSet: 4
Gerrit-Project: repotools
Gerrit-Branch: master
Gerrit-Owner: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Jared K. Smith <jaredsmith at jaredsmith.net>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Mark Michelson <mmichelson at digium.com>



More information about the asterisk-commits mailing list