diff --git a/webcompat/api/endpoints.py b/webcompat/api/endpoints.py index f0d620c3d..eeeb461bb 100644 --- a/webcompat/api/endpoints.py +++ b/webcompat/api/endpoints.py @@ -8,7 +8,6 @@ back to GitHub''' import json -import re from flask import abort from flask import Blueprint @@ -25,6 +24,7 @@ from webcompat.helpers import get_headers from webcompat.helpers import get_request_headers from webcompat.helpers import get_user_info +from webcompat.helpers import normalize_api_params from webcompat.issues import filter_untriaged from webcompat.issues import proxy_request @@ -141,7 +141,7 @@ def get_issue_category(issue_category): @api.route('/issues/search') @limiter.limit('30/minute', key_func=lambda: get_username()) -def get_search_results(query_string=None): +def get_search_results(query_string=None, params=None): '''XHR endpoint to get results from GitHub's Search API. We're specifically searching "issues" here, which seems to make the most @@ -156,16 +156,17 @@ def get_search_results(query_string=None): Not cached. ''' + params = params or request.args.copy() + query_string = query_string or params.get('q') search_uri = 'https://api.github.com/search/issues' - # TODO: handle sort and order parameters. - params = request.args.copy() - if query_string is None: - query_string = params.get('q') - # restrict results to our repo. + # restrict results to our repo. query_string += " repo:{0}".format(REPO_PATH) params['q'] = query_string + # convert issues api to search api params here. + params = normalize_api_params(params) + if g.user: request_headers = get_request_headers(g.request_headers) results = github.raw_request('GET', 'search/issues', params=params, @@ -188,13 +189,14 @@ def get_category_from_search(issue_category): issues from /issues/category/. ''' category_list = ['contactready', 'needsdiagnosis', 'sitewait'] + params = request.args.copy() if issue_category in category_list: query_string = 'label:{0}'.format(issue_category) elif issue_category == 'untriaged': query_string = ('state:open -label:contactready ' '-label:sitewait -label:needsdiagnosis') - return get_search_results(query_string) + return get_search_results(query_string, params) @api.route('/issues//comments', methods=['GET', 'POST']) diff --git a/webcompat/helpers.py b/webcompat/helpers.py index 5be16d67a..673613285 100644 --- a/webcompat/helpers.py +++ b/webcompat/helpers.py @@ -7,7 +7,6 @@ import datetime import math import urlparse -import re from babel.dates import format_timedelta from flask import session @@ -139,6 +138,36 @@ def get_referer(request): return None +def normalize_api_params(params): + '''Normalize GitHub Issues API params to Search API conventions: + + Issues API params | Search API converted values + -------------------------|--------------------------------------- + state | into q as "state:open", "state:closed" + creator | into q as "author:username" + mentioned | into q as "mentions:username" + direction | order + ''' + if 'direction' in params: + params['order'] = params['direction'] + del params['direction'] + + # these params need to be added to the "q" param as substrings + if 'state' in params: + state_param = ' state:' + params['state'] + params['q'] += state_param + del params['state'] + if 'creator' in params: + creator_param = ' author:' + params['creator'] + params['q'] += creator_param + del params['creator'] + if 'mentioned' in params: + mentioned_param = ' mentions:' + params['mentioned'] + params['q'] += mentioned_param + del params['mentioned'] + return params + + def rewrite_links(link_header): '''Rewrites Link header Github API endpoints to our own.