Skip to content

Commit

Permalink
Merge pull request #490 from /issues/479/1
Browse files Browse the repository at this point in the history
Fixes #479 - Better filter and sort integration for issues page.
  • Loading branch information
Mike Taylor committed Dec 16, 2014
2 parents 5e8fcc0 + eaf3911 commit dfd0ed6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
18 changes: 10 additions & 8 deletions webcompat/api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
back to GitHub'''

import json
import re

from flask import abort
from flask import Blueprint
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -188,13 +189,14 @@ def get_category_from_search(issue_category):
issues from /issues/category/<issue_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/<int:number>/comments', methods=['GET', 'POST'])
Expand Down
31 changes: 30 additions & 1 deletion webcompat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import datetime
import math
import urlparse
import re

from babel.dates import format_timedelta
from flask import session
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit dfd0ed6

Please sign in to comment.