Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Jira JQL] Add: support custom JIRA fields and enhance value mapping #1508

Merged
merged 4 commits into from
Jan 24, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions redash/query_runner/jql.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,33 @@ def to_json(self):
return json.dumps({'rows': self.rows, 'columns': self.columns.values()})


def parse_issue(issue):
def parse_issue(issue, fieldMapping):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be field_mapping (we use snake_case in the Python code).

result = OrderedDict()
result['key'] = issue['key']

for k, v in issue['fields'].iteritems():
if k.startswith('customfield_'):
continue

if isinstance(v, dict):
# if field mapping is defined optionally change output key and parsing rules for value
if k in fieldMapping:
mapping = fieldMapping[k]
output_key = k
if 'name' in mapping:
output_key = mapping['name']
put_value(result, output_key, v, mapping)

else:
put_value(result, k, v, {})

return result


def put_value(result, k, v, mapping):
if isinstance(v, dict):
if 'member' in mapping:
result[k] = v[mapping['member']]

else:
# these special mapping rules are kept for backwards compatibility
if 'key' in v:
result['{}_key'.format(k)] = v['key']
if 'name' in v:
Expand All @@ -45,19 +63,27 @@ def parse_issue(issue):

if 'watchCount' in v:
result[k] = v['watchCount']
# elif isinstance(v, list):
# pass
else:
result[k] = v

elif isinstance(v, list):
listValues = []
for listItem in v:
if isinstance(listItem, dict):
if 'member' in mapping:
listValues.append(listItem[mapping['member']])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if mapping['member'] not in listItem? Better to use listItem.get or check for this case and raise an explicit error.

else:
listValues.append(listItem)
if len(listValues) > 0:
result[k] = ','.join(listValues)

return result
else:
result[k] = v


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

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

return results

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

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

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