<commit_before># -*- coding: utf-8 -*-
from urllib.error import HTTPError
import requests

from cli_common.log import get_logger
from cli_common.taskcluster import get_service
from cli_common.utils import retry

from shipit_code_coverage.secrets import secrets


logger = get_logger(__name__)


class Notifier(object):

    def __init__(self, revision, client_id, access_token):
        self.revision = revision
        self.notify_service = get_service('notify', client_id, access_token)

    def get_coverage_summary(self, changeset):
        r = requests.get('https://uplift.shipit.staging.mozilla-releng.net/coverage/changeset_summary/%s' % changeset)
        r.raise_for_status()

        if r.status_code == 202:
            return None

        return r.json()

    def notify(self):
        content = ''

        # Get pushlog and ask the backend to generate the coverage by changeset
        # data, which will be cached.
        r = requests.get('https://hg.mozilla.org/mozilla-central/json-pushes?changeset=%s&version=2&full' % self.revision)
        r.raise_for_status()
        push_data = r.json()
        changesets = sum((data['changesets'] for data in push_data['pushes'].values()), [])

        for changeset in changesets:
            desc = changeset['desc'].split('\n')[0]

            if any(text in desc for text in ['r=merge', 'a=merge']):
                continue

            try:
                rev = changeset['node']
                coverage = retry(lambda: self.get_coverage_summary(rev), retries=10)
                if coverage is None:
                    continue

                if coverage['commit_covered'] < 0.2 * coverage['commit_added']:
                    content += '* [%s](https://firefox-code-coverage.herokuapp.com/#/changeset/%s): %d covered out of %d added.\n' % (desc, rev, coverage['commit_covered'], coverage['commit_added'])  # noqa
            except HTTPError as e:
                continue

        if content == '':
            return
        elif len(content) > 102400:
            # Content is 102400 chars max
            content = content[:102000] + '\n\n... Content max limit reached!'

        for email in secrets[secrets.EMAIL_ADDRESSES]:
            self.notify_service.email({
                'address': email,
                'subject': 'Coverage patches for %s' % self.revision,
                'content': content,
                'template': 'fullscreen',
            })<commit_msg>shipit_code_coverage: Misc improvement for low-coverage notifier (#1021)<commit_after># -*- coding: utf-8 -*-
import requests

from cli_common.log import get_logger
from cli_common.taskcluster import get_service
from cli_common.utils import retry

from shipit_code_coverage.secrets import secrets


logger = get_logger(__name__)


class Notifier(object):

    def __init__(self, revision, client_id, access_token):
        self.revision = revision
        self.notify_service = get_service('notify', client_id, access_token)

    def get_coverage_summary(self, changeset):
        r = requests.get('https://uplift.shipit.staging.mozilla-releng.net/coverage/changeset_summary/{}'.format(changeset))
        r.raise_for_status()

        if r.status_code == 202:
            return None

        return r.json()

    def notify(self):
        content = ''

        # Get pushlog and ask the backend to generate the coverage by changeset
        # data, which will be cached.
        r = requests.get('https://hg.mozilla.org/mozilla-central/json-pushes?changeset={}&version=2&full'.format(self.revision))
        r.raise_for_status()
        push_data = r.json()
        changesets = sum((data['changesets'] for data in push_data['pushes'].values()), [])

        for changeset in changesets:
            desc = changeset['desc'].split('\n')[0]

            if any(text in desc for text in ['r=merge', 'a=merge']):
                continue

            rev = changeset['node']

            try:
                coverage = retry(lambda: self.get_coverage_summary(rev))
            except requests.exceptions.HTTPError:
                logger.warn('Failure to retrieve coverage summary', rev=rev)
                continue

            if coverage is None:
                continue

            if coverage['commit_covered'] < 0.2 * coverage['commit_added']:
                content += '* [{}](https://firefox-code-coverage.herokuapp.com/#/changeset/{}): {} covered out of {} added.\n'.format(desc, rev, coverage['commit_covered'], coverage['commit_added'])  # noqa

        if content == '':
            return
        elif len(content) > 102400:
            # Content is 102400 chars max
            content = content[:102000] + '\n\n... Content max limit reached!'

        for email in secrets[secrets.EMAIL_ADDRESSES]:
            self.notify_service.email({
                'address': email,
                'subject': 'Coverage patches for {}'.format(self.revision),
                'content': content,
                'template': 'fullscreen',
            })
{'fraction_non_alphanumeric': 0.11811652035115722, 'fraction_numerical': 0.01157222665602554, 'mean_word_length': 2.313284864507601, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, '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': '6034177', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1385, 'n_words': 341}