From babb5aae6736e4909a51e4249d8384c86ffb17c3 Mon Sep 17 00:00:00 2001 From: Mike Taylor Date: Mon, 31 Aug 2015 14:08:49 -0500 Subject: [PATCH] Issue #687. Abstract out comment creation into a get_comment_data method. --- webcompat/api/endpoints.py | 13 +++++-------- webcompat/helpers.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/webcompat/api/endpoints.py b/webcompat/api/endpoints.py index b22123d57..599eba1e2 100644 --- a/webcompat/api/endpoints.py +++ b/webcompat/api/endpoints.py @@ -21,6 +21,7 @@ from webcompat import cache from webcompat import github from webcompat import limiter +from webcompat.helpers import get_comment_data from webcompat.helpers import get_headers from webcompat.helpers import get_request_headers from webcompat.helpers import get_user_info @@ -219,10 +220,9 @@ def proxy_comments(number): ''' if request.method == 'POST': try: - comment_data = json.loads(request.data) - body = json.dumps({"body": comment_data['rawBody']}) path = 'repos/{0}/{1}/comments'.format(ISSUES_PATH, number) - comment = github.raw_request('POST', path, data=body) + comment = github.raw_request('POST', path, + data=get_comment_data(request.data)) return (json.dumps(comment.json()), comment.status_code, {'content-type': JSON_MIME}) except GitHubError as e: @@ -233,11 +233,8 @@ def proxy_comments(number): if g.user: comments = github.raw_request( - 'GET', - 'repos/{0}/{1}/comments'.format( - ISSUES_PATH, number), - headers=request_headers - ) + 'GET', 'repos/{0}/{1}/comments'.format(ISSUES_PATH, number), + headers=request_headers) else: comments = proxy_request('get', '/{0}/comments'.format(number), headers=request_headers) diff --git a/webcompat/helpers.py b/webcompat/helpers.py index 3e35b0f2d..667074f46 100644 --- a/webcompat/helpers.py +++ b/webcompat/helpers.py @@ -5,6 +5,7 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. import datetime +import json import hashlib import math import os @@ -298,3 +299,12 @@ def format_link_header(link_header_data): links = ['<{0}>; rel="{1}"'.format(data['link'], data['rel']) for data in link_header_data] return ', '.join(links) + + +def get_comment_data(request_data): + '''Returns a comment ready to send to GitHub. + + We do this by JSON-encoding the rawBody property + of a request's data object.''' + comment_data = json.loads(request_data) + return json.dumps({"body": comment_data['rawBody']})