Skip to content

Commit

Permalink
jira plugin fixed; use requests[-kerberos]
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Ward committed Aug 18, 2015
1 parent 17a3aac commit ff9356d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
5 changes: 2 additions & 3 deletions setup.py
Expand Up @@ -26,9 +26,8 @@
__scripts__ = ['source/status-report']
__irequires__ = [
'python_dateutil==2.4.2',
'sqlalchemy==1.0.0',
'kerberos==1.2.2', # not python 3 compatible!
'urllib2_kerberos==0.1.6', # not python 3 compatible!
'requests==2.7.0',
'requests-kerberos==0.7.0',
]
pip_src = 'https://pypi.python.org/packages/source'
__deplinks__ = []
Expand Down
10 changes: 5 additions & 5 deletions source/status-report
Expand Up @@ -34,7 +34,6 @@ range. By default all available stats for this week are reported.
from __future__ import unicode_literals, absolute_import

import sys
import kerberos
import optparse
import ConfigParser
from dateutil.relativedelta import relativedelta as delta
Expand Down Expand Up @@ -214,10 +213,6 @@ def main():
utils.log.error(error)
sys.exit(1)

except kerberos.GSSError as error:
utils.log.error("Kerberos authentication failed. Try kinit.")
sys.exit(2)

except ConfigParser.NoSectionError as e:
utils.log.error(e)
utils.log.error(
Expand All @@ -229,6 +224,11 @@ def main():
'[general]\nemail = "My Name" <{0}@domain.com>'.format(getuser()))
sys.exit(3)

except Exception as error:
utils.log.error("Perhaps authentication failed. Try kinit?")
sys.exit(2)



if __name__ == "__main__":
main()
60 changes: 39 additions & 21 deletions source/status_report/plugins/jira.py
Expand Up @@ -2,28 +2,33 @@
""" Comfortably generate reports - Jira """

import re
import json
import urllib
import urllib2
import urllib2_kerberos
from urllib import urlencode
import requests as rq
from requests_kerberos import HTTPKerberosAuth, DISABLED
import dateutil.parser
import cookielib
import warnings

from status_report.base import Stats, StatsGroup
from status_report.utils import Config, ReportError, log, pretty, listed

# Output warnings ONE time, then supress
warnings.simplefilter('default')

# Default identifier width
DEFAULT_WIDTH = 4

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Issue Investigator
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class Issue(object):

class Issue(Stats):
""" Jira issue investigator """

def __init__(self, issue=None, prefix=None):
""" Initialize issue """
# FIXME: return in __init__ doesn't seem to be very pythonic
# http://stackoverflow.com/questions/11981368/ raise an error instead?
if issue is None:
return
self.issue = issue
Expand All @@ -46,11 +51,11 @@ def __unicode__(self):
def search(query, stats):
""" Perform issue search for given stats instance """
log.debug("Search query: {0}".format(query))
result = stats.parent.session.open(
result = stats.parent.session.get(
"{0}/rest/api/latest/search?{1}".format(
stats.parent.url, urllib.urlencode(
stats.parent.url, urlencode(
{"jql": query, "fields": "summary,comment"})))
issues = json.loads(result.read())
issues = result.json()
log.debug(
"Search result: {0} found".format(
listed(issues["total"], "issue")))
Expand All @@ -70,6 +75,7 @@ def updated(self, user, options):
return True
return False


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Stats
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -115,6 +121,7 @@ def fetch(self):
self.options.since, self.options.until))
self.stats = Issue.search(query, stats=self)


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Stats Group
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -125,18 +132,21 @@ class JiraStats(StatsGroup):
# Default order
order = 600

def __init__(self, option, name=None, parent=None):
StatsGroup.__init__(self, option, name, parent)
def __init__(self, option, name=None, parent=None, user=None,
options=None):
super(JiraStats, self).__init__(
option=option, name=name, parent=parent, user=user,
options=options)
self._session = None
# Make sure there is an url provided
config = dict(Config().section(option))
if "url" not in config:
raise ReportsError(
raise ReportError(
"No Jira url set in the [{0}] section".format(option))
self.url = config["url"].rstrip("/")
# Make sure we have project set
if "project" not in config:
raise ReportsError(
raise ReportError(
"No project set in the [{0}] section".format(option))
self.project = config["project"]
# Check for custom prefix
Expand All @@ -152,19 +162,27 @@ def __init__(self, option, name=None, parent=None):
JiraResolved(
option=option + "-resolved", parent=self,
name="Issues resolved in {0}".format(option)),
]
]

@property
def session(self):
""" Initialize the session """
if self._session is None:
# OLD
# http://stackoverflow.com/questions/8811269/
# http://www.techchorus.net/using-cookie-jar-urllib2
cookie = cookielib.CookieJar()
self._session = urllib2.build_opener(
urllib2.HTTPSHandler(debuglevel=0),
urllib2.HTTPRedirectHandler,
urllib2.HTTPCookieProcessor(cookie),
urllib2_kerberos.HTTPKerberosAuthHandler)
self._session.open(self.url + "/step-auth-gss")
#self._session = urllib2.build_opener(
# urllib2.HTTPSHandler(debuglevel=0),
# urllib2.HTTPRedirectHandler,
# urllib2.HTTPCookieProcessor(cookie),
# urllib2_kerberos.HTTPKerberosAuthHandler)
#self._session.open(self.url + "/step-auth-gss")

# http://stackoverflow.com/questions/21578699/
auth = HTTPKerberosAuth(mutual_authentication=DISABLED)
self._session = rq.Session()
url = self.url + "/step-auth-gss"
# FIXME: not verifying SSL is a hack...
self._session.get(url, auth=auth, verify=False,
allow_redirects=True)
return self._session

0 comments on commit ff9356d

Please sign in to comment.