Skip to content

Commit

Permalink
Merge pull request getredash#1508 from stefanseifert/feature/jql-cust…
Browse files Browse the repository at this point in the history
…om-fields

[Jira JQL] Add: support custom JIRA fields and enhance value mapping
  • Loading branch information
arikfr authored Jan 24, 2017
2 parents d4a6f59 + c37308c commit a9d5c7e
Showing 1 changed file with 90 additions and 20 deletions.
110 changes: 90 additions & 20 deletions query_runner/jql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import requests
import re

from collections import OrderedDict

Expand All @@ -26,38 +27,66 @@ def to_json(self):
return json.dumps({'rows': self.rows, 'columns': self.columns.values()})


def parse_issue(issue):
def parse_issue(issue, field_mapping):
result = OrderedDict()
result['key'] = issue['key']

for k, v in issue['fields'].iteritems():
if k.startswith('customfield_'):
continue
for k, v in issue['fields'].iteritems():#
output_name = field_mapping.get_output_field_name(k)
member_names = field_mapping.get_dict_members(k)

if isinstance(v, dict):
if 'key' in v:
result['{}_key'.format(k)] = v['key']
if 'name' in v:
result['{}_name'.format(k)] = v['name']

if k in v:
result[k] = v[k]

if 'watchCount' in v:
result[k] = v['watchCount']
# elif isinstance(v, list):
# pass
if len(member_names) > 0:
# if field mapping with dict member mappings defined get value of each member
for member_name in member_names:
if member_name in v:
result[field_mapping.get_dict_output_field_name(k,member_name)] = v[member_name]

else:
# these special mapping rules are kept for backwards compatibility
if 'key' in v:
result['{}_key'.format(output_name)] = v['key']
if 'name' in v:
result['{}_name'.format(output_name)] = v['name']

if k in v:
result[output_name] = v[k]

if 'watchCount' in v:
result[output_name] = v['watchCount']

elif isinstance(v, list):
if len(member_names) > 0:
# if field mapping with dict member mappings defined get value of each member
for member_name in member_names:
listValues = []
for listItem in v:
if isinstance(listItem, dict):
if member_name in listItem:
listValues.append(listItem[member_name])
if len(listValues) > 0:
result[field_mapping.get_dict_output_field_name(k,member_name)] = ','.join(listValues)

else:
# otherwise support list values only for non-dict items
listValues = []
for listItem in v:
if not isinstance(listItem, dict):
listValues.append(listItem)
if len(listValues) > 0:
result[output_name] = ','.join(listValues)

else:
result[k] = v
result[output_name] = v

return result


def parse_issues(data):
def parse_issues(data, field_mapping):
results = ResultSet()

for issue in data['issues']:
results.add_row(parse_issue(issue))
results.add_row(parse_issue(issue, field_mapping))

return results

Expand All @@ -68,6 +97,46 @@ def parse_count(data):
return results


class FieldMapping:

def __init__(cls, query_field_mapping):
cls.mapping = []
for k, v in query_field_mapping.iteritems():
field_name = k
member_name = None

# check for member name contained in field name
member_parser = re.search('(\w+)\.(\w+)', k)
if (member_parser):
field_name = member_parser.group(1)
member_name = member_parser.group(2)

cls.mapping.append({
'field_name': field_name,
'member_name': member_name,
'output_field_name': v
})

def get_output_field_name(cls,field_name):
for item in cls.mapping:
if item['field_name'] == field_name and not item['member_name']:
return item['output_field_name']
return field_name

def get_dict_members(cls,field_name):
member_names = []
for item in cls.mapping:
if item['field_name'] == field_name and item['member_name']:
member_names.append(item['member_name'])
return member_names

def get_dict_output_field_name(cls,field_name, member_name):
for item in cls.mapping:
if item['field_name'] == field_name and item['member_name'] == member_name:
return item['output_field_name']
return None


class JiraJQL(BaseQueryRunner):
noop_query = '{"queryType": "count"}'

Expand Down Expand Up @@ -109,6 +178,7 @@ def run_query(self, query, user):
try:
query = json.loads(query)
query_type = query.pop('queryType', 'select')
field_mapping = FieldMapping(query.pop('fieldMapping', {}))

if query_type == 'count':
query['maxResults'] = 1
Expand All @@ -127,7 +197,7 @@ def run_query(self, query, user):
if query_type == 'count':
results = parse_count(data)
else:
results = parse_issues(data)
results = parse_issues(data, field_mapping)

return results.to_json(), None
except KeyboardInterrupt:
Expand Down

0 comments on commit a9d5c7e

Please sign in to comment.