<commit_before>import github
import github_helpers
import hmac
import textwrap
from django.conf import settings

assert settings.WEBHOOKS_SECRET_KEY is not None, (
    'You must set WEBHOOKS_SECRET_KEY in local_settings.py to use Jucybot')
_hmac = hmac.new(settings.WEBHOOKS_SECRET_KEY)


def get_secret_for_repo(repo):
    h = _hmac.copy()
    h.update(repo.lower())
    return h.hexdigest()


class JucyBot(object):
    '''
    Module allowing to control the Jucybot account
    '''
    def __init__(self, gh, login):
        self.gh = gh
        self.login = login
        self.label_objects = {}

    def get_webhooks_callback_url_for_repo(self, repo):
        return settings.WEBHOOKS_CALLBACK_URL % {
            'owner': repo.owner.login,
            'repository': repo.name,
            'hooktype': 'all_issues'
        }

    def setup_hooks_on_repo(self, repo):
        config = {
            'url': self.get_webhooks_callback_url_for_repo(repo),
            'secret': get_secret_for_repo(repo.full_name),
            'content_type': 'json',
            'secure_ssl': '1' if settings.DEBUG else '0',
        }
        try:
            hook = repo.create_hook('web', config,
                                    events=['issues', 'issue_comment'])
            return True
        except github.GithubException, e:
            if github_helpers.isGithubExceptionMessage(
                    e, github_helpers.E_HOOK_ALREADY_EXISTS):
                return True
            raise e

    def is_collaborator_on_repo(self, repo):
        return repo.has_in_collaborators(self.login)

    def add_as_collaborator_on_repo(self, repo):
        repo.add_to_collaborators(self.login)

    def get_label_object(self, repo, label_name):
        key = (repo.full_name, label_name)
        if key in self.label_objects:
            return self.label_objects[key]
        label = repo.get_label(label_name)
        self.label_objects[key] = label
        return label

    def create_issue(self, repo_fullname, title, contents, label_name):
        body = self.format_issue(contents, label_name)
        repo = self.gh.get_repo(repo_fullname)
        return repo.create_issue(
            title,
            body=body,
            labels=[self.get_label_object(repo, label_name)])

    def change_issue_label(self, issue, repository, label_name):
        labels = repository.get_labels()
        issue_labels = issue.get_labels()
        label = next((label for label in labels if label.name == label_name), None)
        if not label:
            return
        for issue_label in issue_labels:
            if issue_label.name != label_name:
                issue.remove_from_labels(label)
        issue.set_labels(label)

    def format_issue(self, contents, label_name):
        # TODO(db0): Specify the boilerplate content for Jucy issues.
        boilerplate = """*This issue was filed by Jucy*
Category: %(label_name)s

%(contents_as_quote)s
"""
        contents_as_quote = textwrap.fill(contents,
                                          initial_indent='> ',<commit_msg>Fix change_issue_label<commit_after>import github
import github_helpers
import hmac
import textwrap
from django.conf import settings

assert settings.WEBHOOKS_SECRET_KEY is not None, (
    'You must set WEBHOOKS_SECRET_KEY in local_settings.py to use Jucybot')
_hmac = hmac.new(settings.WEBHOOKS_SECRET_KEY)


def get_secret_for_repo(repo):
    h = _hmac.copy()
    h.update(repo.lower())
    return h.hexdigest()


class JucyBot(object):
    '''
    Module allowing to control the Jucybot account
    '''
    def __init__(self, gh, login):
        self.gh = gh
        self.login = login
        self.label_objects = {}

    def get_webhooks_callback_url_for_repo(self, repo):
        return settings.WEBHOOKS_CALLBACK_URL % {
            'owner': repo.owner.login,
            'repository': repo.name,
            'hooktype': 'all_issues'
        }

    def setup_hooks_on_repo(self, repo):
        config = {
            'url': self.get_webhooks_callback_url_for_repo(repo),
            'secret': get_secret_for_repo(repo.full_name),
            'content_type': 'json',
            'secure_ssl': '1' if settings.DEBUG else '0',
        }
        try:
            hook = repo.create_hook('web', config,
                                    events=['issues', 'issue_comment'])
            return True
        except github.GithubException, e:
            if github_helpers.isGithubExceptionMessage(
                    e, github_helpers.E_HOOK_ALREADY_EXISTS):
                return True
            raise e

    def is_collaborator_on_repo(self, repo):
        return repo.has_in_collaborators(self.login)

    def add_as_collaborator_on_repo(self, repo):
        repo.add_to_collaborators(self.login)

    def get_label_object(self, repo, label_name):
        key = (repo.full_name, label_name)
        if key in self.label_objects:
            return self.label_objects[key]
        label = repo.get_label(label_name)
        self.label_objects[key] = label
        return label

    def create_issue(self, repo_fullname, title, contents, label_name):
        body = self.format_issue(contents, label_name)
        repo = self.gh.get_repo(repo_fullname)
        return repo.create_issue(
            title,
            body=body,
            labels=[self.get_label_object(repo, label_name)])

    def change_issue_label(self, issue, repository, label_name):
        labels = repository.get_labels()
        issue_labels = issue.get_labels()
        label = next((label for label in labels if label.name == label_name), None)
        if not label:
            return
        for issue_label in issue_labels:
            if issue_label.name != label_name:
                issue.remove_from_labels(issue_label)
        issue.set_labels(label)

    def format_issue(self, contents, label_name):
        # TODO(db0): Specify the boilerplate content for Jucy issues.
        boilerplate = """*This issue was filed by Jucy*
Category: %(label_name)s

%(contents_as_quote)s
"""
        contents_as_quote = textwrap.fill(contents,
                                          initial_indent='> ',
{'fraction_non_alphanumeric': 0.09059912323429128, 'fraction_numerical': 0.000974184120798831, 'mean_word_length': 2.3734939759036147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1148028', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1845, 'n_words': 403}