<commit_before>#!/usr/bin/env python
"""
github.py - Willie Github Module
Copyright 2012, <NAME> http://tyrope.nl/
Licensed under the Eiffel Forum License 2.

http://willie.dftba.net/
"""

from datetime import datetime
from urllib2 import HTTPError
import json, re, web

def checkConfig(willie):
    if not willie.config.has_option('github', 'oath_token') or not willie.config.has_option('github', 'repo'):
        return False
    else:
        return [willie.config.github.oath_token, willie.config.github.repo]

def configure(config):
    chunk = ''
    if config.option('Configuring github issue reporting and searching module', False):
        config.interactive_add('github', 'Oath_token', 'Github API Oauth2 token', '')
        config.interactive_add('github', 'repo', 'Github repository', 'embolalia/willie')
    return chunk
    
def issue(willie, trigger):
    """Create a GitHub issue, also known as a bug report. Syntax: .issue Title of the bug report"""
    #check input
    if not trigger.group(2):
        return willie.say('Please title the issue')

    #Is the Oauth token and repo available?
    gitAPI = checkConfig(willie)
    if gitAPI == False:
        return willie.say('Git module not configured, make sure git_Oath_token and git_repo are defined')

    #parse input
    now = ' '.join(str(datetime.utcnow()).split(' ')).split('.')[0]+' UTC'
    body = 'Submitted by: %s\nFrom channel: %s\nAt %s' % (trigger.nick, trigger.sender, now)
    data = {"title":trigger.group(2).encode('utf-8'), "body":body, "labels": ["IRC"]}
    #submit
    try:
        raw = web.post('https://api.github.com/repos/'+gitAPI[1]+'/issues?access_token='+gitAPI[0], json.dumps(data))
    except HTTPError:
        return willie.say('The GitHub API returned an error.')
    
    data = json.loads(raw)
    willie.say('Issue #%s posted. %s' % (data['number'], data['html_url']))
    willie.debug('','Issue #%s created in %s' % (data['number'],trigger.sender),'warning')
issue.commands = ['issue','bug']
issue.priority = 'medium'

def findIssue(willie, trigger):
    """Search for a GitHub issue by keyword. usage: .findissue search keywords (optional) You can specify the first keyword as "CLOSED" to search closed issues."""
    if not trigger.group(2):
        return willie.reply('What are you searching for?')

    #Is the Oauth token and repo available?
    gitAPI = checkConfig(willie)
    if gitAPI == False:
        return willie.say('Git module not configured, make sure git_Oath_token and git_repo are defined')
    if trigger.group(2).split(' ')[0] == 'CLOSED':
        if '%20'.join(trigger.group(2).split(' ')[1:]) not in ('','\x02','\x03'):
            URL = 'https://api.github.com/legacy/issues/search/'+gitAPI[1]+'/closed/'+'%20'.join(trigger.group(2).split(' ')[1:])
        else:
            return willie.reply('What are you searching for?')
    else:
        URL = 'https://api.github.com/legacy/issues/search/'+gitAPI[1]+'/open/'+trigger.group(2)

    try:
        raw = web.get(URL)
    except HTTPError:
        return willie.say('The GitHub API returned an error.')

    try:
        data = json.loads(raw)['issues'][-1]
    except (KeyError, IndexError):
        return willie.say('No search results.')
    if len(data['body'].split('\n')) > 1:<commit_msg>[GitHub]Added searching for issue IDs as per #132<commit_after>#!/usr/bin/env python
"""
github.py - Willie Github Module
Copyright 2012, <NAME> http://tyrope.nl/
Licensed under the Eiffel Forum License 2.

http://willie.dftba.net/
"""

from datetime import datetime
from urllib2 import HTTPError
import json, re, web

def checkConfig(willie):
    if not willie.config.has_option('github', 'oath_token') or not willie.config.has_option('github', 'repo'):
        return False
    else:
        return [willie.config.github.oath_token, willie.config.github.repo]

def configure(config):
    chunk = ''
    if config.option('Configuring github issue reporting and searching module', False):
        config.interactive_add('github', 'Oath_token', 'Github API Oauth2 token', '')
        config.interactive_add('github', 'repo', 'Github repository', 'embolalia/willie')
    return chunk

def issue(willie, trigger):
    """Create a GitHub issue, also known as a bug report. Syntax: .issue Title of the bug report"""
    #check input
    if not trigger.group(2):
        return willie.say('Please title the issue')

    #Is the Oauth token and repo available?
    gitAPI = checkConfig(willie)
    if gitAPI == False:
        return willie.say('Git module not configured, make sure git_Oath_token and git_repo are defined')

    #parse input
    now = ' '.join(str(datetime.utcnow()).split(' ')).split('.')[0]+' UTC'
    body = 'Submitted by: %s\nFrom channel: %s\nAt %s' % (trigger.nick, trigger.sender, now)
    data = {"title":trigger.group(2).encode('utf-8'), "body":body, "labels": ["IRC"]}
    #submit
    try:
        raw = web.post('https://api.github.com/repos/'+gitAPI[1]+'/issues?access_token='+gitAPI[0], json.dumps(data))
    except HTTPError:
        return willie.say('The GitHub API returned an error.')
    
    data = json.loads(raw)
    willie.say('Issue #%s posted. %s' % (data['number'], data['html_url']))
    willie.debug('','Issue #%s created in %s' % (data['number'],trigger.sender),'warning')
issue.commands = ['issue','bug']
issue.priority = 'medium'

def findIssue(willie, trigger):
    """Search for a GitHub issue by keyword. usage: .findissue search keywords (optional) You can specify the first keyword as "CLOSED" to search closed issues."""
    if not trigger.group(2):
        return willie.reply('What are you searching for?')

    #Is the Oauth token and repo available?
    gitAPI = checkConfig(willie)
    if gitAPI == False:
        return willie.say('Git module not configured, make sure git_Oath_token and git_repo are defined')
    firstParam = trigger.group(2).split(' ')[0]
    if firstParam.isdigit():
        URL = 'https://api.github.com/repos/%s/issues/%s' % (gitAPI[1], trigger.group(2))
    elif firstParam == 'CLOSED':
        if '%20'.join(trigger.group(2).split(' ')[1:]) not in ('','\x02','\x03'):
            URL = 'https://api.github.com/legacy/issues/search/'+gitAPI[1]+'/closed/'+'%20'.join(trigger.group(2).split(' ')[1:])
        else:
            return willie.reply('What are you searching for?')
    else:
        URL = 'https://api.github.com/legacy/issues/search/%s/open/%s' % (gitAPI[1], trigger.group(2))

    try:
        raw = web.get(URL)
    except HTTPError:
        return willie.say('The GitHub API returned an error.')

    try:
        if firstParam.isdigit():
            data = json.loads(raw)
        else:
            data = json.loads(raw)['issues'][-1]
    except (KeyError, IndexError):
        return willie.say('No search results.')
    if len(data['body'].split('\n')) > 1:
{'fraction_non_alphanumeric': 0.1584955363676277, 'fraction_numerical': 0.010390750768330162, 'mean_word_length': 3.3976833976833976, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 7, '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': '2594111', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2177, 'n_words': 593}