<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

'''Does the main work to prepare and create a new issue.'''

import argparse
import json
import jsonschema
import os
import re
import requests

from config import REPO_PATH, OAUTH_TOKEN
from schema import schema
from termcolor import cprint
from textwrap import fill

LABEL_VALIDATION_ERROR = '''
You attempted to create an issue with an unknown label. GitHub ignores unknown
labels when creating issues. Please email <EMAIL> if you feel like
this label should be added to the web-bugs issues repo. To ignore this type
of validation and proceed, try again using the --force option.
'''

REPO_URI = 'https://api.github.com/repos/{0}'.format(REPO_PATH)


def get_issue_body(json_data):
    '''Return the issue body in the proper format.'''
    body = u'''
**URL**: {0}
**Browser / Version**: {1}
**Operating System**: {2}

{3}'''
    return body.format(json_data['url'],
                       json_data['browser'],
                       json_data['os'],
                       json_data['body'])


def format_post_body(json_data):
    '''Create the POST "body" object.'''
    body = {'body': get_issue_body(json_data),
            'title': json_data['title'],
            'labels': json_data['labels']}
    return body


def api_post(uri, body, hooks=None):
    '''Generic method to create a resource at GitHub. `uri` will determine
    what gets created (currently either an issue or a comment).'''
    headers = {
        'Authorization': 'token {0}'.format(OAUTH_TOKEN),
        'User-Agent': 'Webcompat-Issue-Importer'
    }
    return requests.post(uri, data=json.dumps(body), headers=headers, hooks=hooks)


def create_issue(json_data):
    '''Create a new GitHub issue by POSTing data to the issues API endpoint.
    If successful, the URL to the new issue is printed. Otherwise, the error
    code is printed.'''
    body = format_post_body(json_data)
    uri = '{0}/issues'.format(REPO_URI)
    r = api_post(uri, body)
    if r.status_code != 201:
        cprint('Something went wrong. Response: {0}. See '
               'developer.github.com/v3/ for troubleshooting.'.format(
                   r.status_code), 'red')
        return False
    else:
        cprint(r.json()['html_url'] + ' successfully imported', 'green')
        if len(json_data['comments']) > 0:
            cprint('Importing comments...', 'yellow')
            import_comments(r.json()['number'], json_data['comments'])
        return True


def import_comments(number, comments):
    '''Handles importing all comments from the comments array,
    ideally in order.'''
    def next_comment(response, *args, **kwargs):
        '''Callback to wait for a 201 before importing the next comment.'''
        if response.status_code == 201:
            cprint("{0} created".format(response.json()['html_url']), 'green')
            try:
                add_comment(number, comments.next())
            except StopIteration:
                cprint(u'All done. 🍪', 'green')

    def add_comment(issue_number, comment):
        '''Add a single comment.'''
        uri = '{0}/issues/{1}/comments'.format(REPO_URI, issue_number)<commit_msg>GitHub sometimes lies about the order of comment creation.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

'''Does the main work to prepare and create a new issue.'''

import argparse
import json
import jsonschema
import os
import re
import requests

from config import REPO_PATH, OAUTH_TOKEN
from schema import schema
from termcolor import cprint
from textwrap import fill
from time import sleep

LABEL_VALIDATION_ERROR = '''
You attempted to create an issue with an unknown label. GitHub ignores unknown
labels when creating issues. Please email <EMAIL> if you feel like
this label should be added to the web-bugs issues repo. To ignore this type
of validation and proceed, try again using the --force option.
'''

REPO_URI = 'https://api.github.com/repos/{0}'.format(REPO_PATH)


def get_issue_body(json_data):
    '''Return the issue body in the proper format.'''
    body = u'''
**URL**: {0}
**Browser / Version**: {1}
**Operating System**: {2}

{3}'''
    return body.format(json_data['url'],
                       json_data['browser'],
                       json_data['os'],
                       json_data['body'])


def format_post_body(json_data):
    '''Create the POST "body" object.'''
    body = {'body': get_issue_body(json_data),
            'title': json_data['title'],
            'labels': json_data['labels']}
    return body


def api_post(uri, body, hooks=None):
    '''Generic method to create a resource at GitHub. `uri` will determine
    what gets created (currently either an issue or a comment).'''
    headers = {
        'Authorization': 'token {0}'.format(OAUTH_TOKEN),
        'User-Agent': 'Webcompat-Issue-Importer'
    }
    return requests.post(uri, data=json.dumps(body), headers=headers, hooks=hooks)


def create_issue(json_data):
    '''Create a new GitHub issue by POSTing data to the issues API endpoint.
    If successful, the URL to the new issue is printed. Otherwise, the error
    code is printed.'''
    body = format_post_body(json_data)
    uri = '{0}/issues'.format(REPO_URI)
    r = api_post(uri, body)
    if r.status_code != 201:
        cprint('Something went wrong. Response: {0}. See '
               'developer.github.com/v3/ for troubleshooting.'.format(
                   r.status_code), 'red')
        return False
    else:
        cprint(r.json()['html_url'] + ' successfully imported', 'green')
        if len(json_data['comments']) > 0:
            cprint('Importing comments...', 'yellow')
            import_comments(r.json()['number'], json_data['comments'])
        return True


def import_comments(number, comments):
    '''Handles importing all comments from the comments array,
    ideally in order.'''
    def next_comment(response, *args, **kwargs):
        '''Callback to wait for a 201 before importing the next comment.'''
        if response.status_code == 201:
            cprint("{0} created".format(response.json()['html_url']), 'green')
            try:
                add_comment(number, comments.next())
            except StopIteration:
                cprint(u'All done. 🍪', 'green')

    def add_comment(issue_number, comment):
        '''Add a single comment.'''
        sleep(1)
        uri = '{0}/issues/{1}/comments'.format(REPO_URI, issue_number)
{'fraction_non_alphanumeric': 0.12410256410256411, 'fraction_numerical': 0.00805860805860806, 'mean_word_length': 2.9662986635676933, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1416866', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1953, 'n_words': 672}