<commit_before>from trac.core import *
from trac.config import Option, IntOption, ListOption, BoolOption
from trac.web.api import IRequestFilter, IRequestHandler, Href
from hook import CommitHook
import simplejson

class GithubPlugin(Component):
    implements(IRequestHandler)
    
    key = Option('github', 'apitoken', '', doc="""Your GitHub API Token found here: https://github.com/account, """)

    def __init__(self):
        self.hook = CommitHook(self.env)
        self.env.log.debug("API Token: %s" % self.key)

    
    # IRequestHandler methods
    def match_request(self, req):
        self.env.log.debug("Request Method: %s" % req.method)
        self.env.log.debug("Request #1: %s" % req.path_info)
        self.env.log.debug("Request #2: %s" % req.path_info.rstrip('/'))
        serve = req.path_info.rstrip('/') == ('/github/%s' % self.key) and req.method == 'POST'
        self.env.log.debug("Match Request: %s" % serve)
        self.env.log.debug("Match Token: %s" % req.form_token)
        self.env.log.debug("Match Token: %s" % req.args.get('__FORM_TOKEN'))
        if serve:
            #This is hacky but it's the only way I found to let Trac post to this request
            #   without a valid form_token
            req.form_token = None
        return serve
    
    def process_request(self, req):
        self.env.log.debug("Process Request")
        self.env.log.debug("Request Data: %s" % req.read())

        #Not sure why this part is breaking? Noting is coming in from the request..
        #May be related to the Hack??
            
        #try:
        #data = simplejson.loads(req.read())

        #for sha1, commit in data['commits'].items():
        #    self.env.log.debug("Commit %s:" % commit)
            #self.hook.process(commit)
        #req.send_response(200)
        #req.send_header('Content-Type', 'text/plain')
        #req.end_headers()
        #req.write('Hello world!')
        #except json.ReadException, e:
        #    req.send_response(400)
        #    req.send_header('Content-type', 'text/plain')
        #    req.end_headers()
        #    req.write(e.message)<commit_msg>Got the payload to parse now from the request object, next get data and hook into the commit hook<commit_after>from trac.core import *
from trac.config import Option, IntOption, ListOption, BoolOption
from trac.web.api import IRequestFilter, IRequestHandler, Href
from hook import CommitHook
import simplejson

class GithubPlugin(Component):
    implements(IRequestHandler)
    
    key = Option('github', 'apitoken', '', doc="""Your GitHub API Token found here: https://github.com/account, """)

    def __init__(self):
        self.hook = CommitHook(self.env)
        self.env.log.debug("API Token: %s" % self.key)

    
    # IRequestHandler methods
    def match_request(self, req):
        self.env.log.debug("Request Method: %s" % req.method)
        self.env.log.debug("Request #1: %s" % req.path_info)
        self.env.log.debug("Request #2: %s" % req.path_info.rstrip('/'))
        serve = req.path_info.rstrip('/') == ('/github/%s' % self.key) and req.method == 'POST'
        self.env.log.debug("Match Request: %s" % serve)
        self.env.log.debug("Match Token: %s" % req.form_token)
        self.env.log.debug("Match Token: %s" % req.args.get('__FORM_TOKEN'))
        if serve:
            #This is hacky but it's the only way I found to let Trac post to this request
            #   without a valid form_token
            req.form_token = None
        return serve
    
    def process_request(self, req):
        self.env.log.debug("Process Request")
        self.env.log.debug("Request Data TEXT: %s" % req.args.get('payload'))
        data = req.args.get('payload')
            
        #try:
        jsondata = simplejson.loads(data)

        for sha1, commit in jsondata['commits']:
            self.env.log.debug("Commit %s:" % commit)
            #self.hook.process(commit)





        #for sha1, commit in jsondata['commits']:
        #req.send_response(200)
        #req.send_header('Content-Type', 'text/plain')
        #req.end_headers()
        #req.write('Hello world!')
        #except json.ReadException, e:
        #    req.send_response(400)
        #    req.send_header('Content-type', 'text/plain')
        #    req.end_headers()
        #    req.write(e.message)
{'fraction_non_alphanumeric': 0.13751730502999537, 'fraction_numerical': 0.004383940932164283, 'mean_word_length': 2.5101214574898787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2921562', 'n_tokens_mistral': 1368, 'n_tokens_neox': 1344, 'n_words': 327}