<commit_before>import logging

from sirbot.plugins.github.hookimpl import hookimpl
from sirbot.slack.message import SlackMessage, Attachment

logger = logging.getLogger('sirbot.pythondev')


async def issues(event, facades):
    att = None

    if event['action'] == 'opened':
        att = issue_format(event, 'good')
    elif event['action'] == 'closed':
        att = issue_format(event, 'danger')

    if att:
        slack = facades.get('slack')
        channel = await slack.channels.get(name='community_projects')
        msg = SlackMessage(to=channel)
        msg.attachments.append(att)
        await slack.send(msg)


def issue_format(event, color):

    att = Attachment(
        fallback='issue {}'.format(event['action']),
        pretext='Issue {action} in <{url}|{name}>'.format(
            url=event['repository']['html_url'],
            name=event['repository']['name'],
            action=event['action']
        ),
        color=color,
        text=event['issue']['body'],
        title='Issue {action} in <{url}|{name}>: {title}'.format(
                url=event['repository']['html_url'],
                name=event['repository']['name'],
                action=event['action'],
                title=event['issue']['title']
        ),
        title_link=event['issue']['html_url'],
        author_icon=event['sender']['avatar_url'],
        author_name=event['sender']['login'],
        author_link=event['sender']['html_url'],
        footer=', '.join(label['name'] for label in event['issue']['labels'])
    )

    return att


async def pull_request(event, facades):
    att = None

    if event['action'] == 'opened':
        data = {'color': 'good', 'action': 'opened'}
        att = pull_request_format(event, data)
    elif event['action'] == 'closed':
        if event['pull_request']['merged']:
            data = {'color': '#6f42c1', 'action': 'merged'}
        else:
            data = {'color': 'danger', 'action': 'closed'}

        att = pull_request_format(event, data)

    if att:
        slack = facades.get('slack')
        channel = await slack.channels.get(name='community_projects')
        msg = SlackMessage(to=channel)
        msg.attachments.append(att)
        await slack.send(msg)


def pull_request_format(event, data):

    footer = '+ {add} / - {del_}'.format(
        add=event['pull_request']['additions'],
        del_=event['pull_request']['deletions']
    )

    att = Attachment(
        fallback='pull request {}'.format(data['action']),
        title='Pull request {action} in <{url}|{name}>: {title}'.format(
            url=event['repository']['html_url'],
            name=event['repository']['name'],
            action=data['action'],
            title=event['pull_request']['title'],
        ),
        color=data['color'],
        text=event['pull_request']['body'],
        title_link=event['pull_request']['html_url'],
        author_icon=event['sender']['avatar_url'],
        author_name=event['sender']['login'],
        author_link=event['sender']['html_url'],
        footer=footer
    )

    return att


@hookimpl
def register_github_events():

    events = [
        {
            'event': 'issues',
            'func': issues
        },
        {
            'event': 'pull_request',<commit_msg>update github notification<commit_after>import logging

from sirbot.plugins.github.hookimpl import hookimpl
from sirbot.slack.message import SlackMessage, Attachment

logger = logging.getLogger('sirbot.pythondev')


async def issues(event, facades):
    att = None

    if event['action'] == 'opened':
        att = issue_format(event, 'good')
    elif event['action'] == 'closed':
        att = issue_format(event, 'danger')

    if att:
        slack = facades.get('slack')
        channel = await slack.channels.get(name='community_projects')
        msg = SlackMessage(to=channel)
        msg.attachments.append(att)
        await slack.send(msg)


def issue_format(event, color):
    att = Attachment(
        fallback='issue {}'.format(event['action']),
        color=color,
        text=event['issue']['body'],
        title='Issue {action} in <{repo_url}|{name}>: <{url}|{title}>'.format(
            repo_url=event['repository']['html_url'],
            url=event['issue']['html_url'],
            name=event['repository']['name'],
            action=event['action'],
            title=event['issue']['title']
        ),
        author_icon=event['sender']['avatar_url'],
        author_name=event['sender']['login'],
        author_link=event['sender']['html_url'],
        footer=', '.join(label['name'] for label in event['issue']['labels'])
    )

    return att


async def pull_request(event, facades):
    att = None

    if event['action'] == 'opened':
        data = {'color': 'good', 'action': 'opened'}
        att = pull_request_format(event, data)
    elif event['action'] == 'closed':
        if event['pull_request']['merged']:
            data = {'color': '#6f42c1', 'action': 'merged'}
        else:
            data = {'color': 'danger', 'action': 'closed'}

        att = pull_request_format(event, data)

    if att:
        slack = facades.get('slack')
        channel = await slack.channels.get(name='community_projects')
        msg = SlackMessage(to=channel)
        msg.attachments.append(att)
        await slack.send(msg)


def pull_request_format(event, data):
    footer = '+ {add} / - {del_}'.format(
        add=event['pull_request']['additions'],
        del_=event['pull_request']['deletions']
    )

    att = Attachment(
        fallback='pull request {}'.format(data['action']),
        title='Pull request {action} in <{repo_url}|{name}>:'
              ' <{url}|{title}>'.format(
            repo_url=event['repository']['html_url'],
            url=event['pull_request']['html_url'],
            name=event['repository']['name'],
            action=data['action'],
            title=event['pull_request']['title'],
        ),
        color=data['color'],
        text=event['pull_request']['body'],
        author_icon=event['sender']['avatar_url'],
        author_name=event['sender']['login'],
        author_link=event['sender']['html_url'],
        footer=footer
    )

    return att


@hookimpl
def register_github_events():
    events = [
        {
            'event': 'issues',
            'func': issues
        },
        {
            'event': 'pull_request',
{'fraction_non_alphanumeric': 0.17117398780678442, 'fraction_numerical': 0.0012505862122870095, 'mean_word_length': 2.7284382284382285, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, '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': '799453', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1838, 'n_words': 334}