<commit_before># -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from abc import ABC, abstractmethod
import json
import requests

from backend_common.cache import cache


@cache.memoize()
def get_github_commit(mercurial_commit):
    r = requests.get('https://api.pub.build.mozilla.org/mapper/gecko-dev/rev/hg/%s' % mercurial_commit)
    return r.text.split(' ')[0]


class Coverage(ABC):
    @staticmethod
    @abstractmethod
    def get_coverage(changeset):
        pass

    @staticmethod
    @abstractmethod
    def get_file_coverage(changeset, filename):
        pass

    @staticmethod
    @abstractmethod
    def get_latest_build():
        pass

    @staticmethod
    @abstractmethod
    def get_directory_coverage(changeset, prev_changeset, directory):
        pass


class CoverallsCoverage(Coverage):
    URL = 'https://coveralls.io'

    @staticmethod
    @cache.memoize()
    def get_coverage(changeset):
        r = requests.get(CoverallsCoverage.URL + '/builds/%s.json' % get_github_commit(changeset))

        if r.status_code != requests.codes.ok:
            raise Exception('Error while loading coverage data.')

        result = r.json()

        return {
          'cur': result['covered_percent'],
          'prev': result['covered_percent'] + result['coverage_change'],
        }

    @staticmethod
    @cache.memoize()
    def get_file_coverage(changeset, filename):
        r = requests.get(CoverallsCoverage.URL + '/builds/%s/source.json' % get_github_commit(changeset), params={
            'filename': filename,
        })

        if r.status_code != requests.codes.ok:
            return None

        return r.json()

    @staticmethod
    def get_latest_build():
        r = requests.get(CoverallsCoverage.URL + '/github/marco-c/gecko-dev.json?page=1')
        builds = r.json()['builds']

        return builds[0]['commit_sha'], builds[1]['commit_sha']

    @staticmethod
    @cache.memoize()
    def get_directory_coverage(changeset, prev_changeset, directory):
        r = requests.get(CoverallsCoverage.URL + '/builds/' + get_github_commit(changeset) + '.json?paths=' + directory + '/*')

        if r.status_code != requests.codes.ok:
            raise Exception('Error while loading coverage data.')

        result = r.json()

        return {
          'files_num': result['selected_source_files_count'],
          'cur': result['paths_covered_percent'],
          'prev': result['paths_previous_covered_percent'],
        }


class CodecovCoverage(Coverage):
    URL = 'https://codecov.io/api/gh/marco-c/gecko-dev'

    @staticmethod
    @cache.memoize()
    def get_coverage(changeset):
        r = requests.get(CodecovCoverage.URL + '/commit/%s' % get_github_commit(changeset))

        if r.status_code != requests.codes.ok:
            raise Exception('Error while loading coverage data.')

        result = r.json()

        return {
          'cur': result['commit']['totals']['c'],
          'prev': result['commit']['parent_totals']['c'],
        }

    @staticmethod
    @cache.memoize()
    def get_file_coverage(changeset, filename):
        r = requests.get(CodecovCoverage.URL + '/src/%s/%s' % (get_github_commit(changeset), filename))
<commit_msg>shipit_uplift: Don't fail requesting coverage info for a commit if we don't have info about the parent (#621)<commit_after># -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from abc import ABC, abstractmethod
import json
import requests

from backend_common.cache import cache


@cache.memoize()
def get_github_commit(mercurial_commit):
    r = requests.get('https://api.pub.build.mozilla.org/mapper/gecko-dev/rev/hg/%s' % mercurial_commit)
    return r.text.split(' ')[0]


class Coverage(ABC):
    @staticmethod
    @abstractmethod
    def get_coverage(changeset):
        pass

    @staticmethod
    @abstractmethod
    def get_file_coverage(changeset, filename):
        pass

    @staticmethod
    @abstractmethod
    def get_latest_build():
        pass

    @staticmethod
    @abstractmethod
    def get_directory_coverage(changeset, prev_changeset, directory):
        pass


class CoverallsCoverage(Coverage):
    URL = 'https://coveralls.io'

    @staticmethod
    @cache.memoize()
    def get_coverage(changeset):
        r = requests.get(CoverallsCoverage.URL + '/builds/%s.json' % get_github_commit(changeset))

        if r.status_code != requests.codes.ok:
            raise Exception('Error while loading coverage data.')

        result = r.json()

        return {
          'cur': result['covered_percent'],
          'prev': result['covered_percent'] + result['coverage_change'],
        }

    @staticmethod
    @cache.memoize()
    def get_file_coverage(changeset, filename):
        r = requests.get(CoverallsCoverage.URL + '/builds/%s/source.json' % get_github_commit(changeset), params={
            'filename': filename,
        })

        if r.status_code != requests.codes.ok:
            return None

        return r.json()

    @staticmethod
    def get_latest_build():
        r = requests.get(CoverallsCoverage.URL + '/github/marco-c/gecko-dev.json?page=1')
        builds = r.json()['builds']

        return builds[0]['commit_sha'], builds[1]['commit_sha']

    @staticmethod
    @cache.memoize()
    def get_directory_coverage(changeset, prev_changeset, directory):
        r = requests.get(CoverallsCoverage.URL + '/builds/' + get_github_commit(changeset) + '.json?paths=' + directory + '/*')

        if r.status_code != requests.codes.ok:
            raise Exception('Error while loading coverage data.')

        result = r.json()

        return {
          'files_num': result['selected_source_files_count'],
          'cur': result['paths_covered_percent'],
          'prev': result['paths_previous_covered_percent'],
        }


class CodecovCoverage(Coverage):
    URL = 'https://codecov.io/api/gh/marco-c/gecko-dev'

    @staticmethod
    @cache.memoize()
    def get_coverage(changeset):
        r = requests.get(CodecovCoverage.URL + '/commit/%s' % get_github_commit(changeset))

        if r.status_code != requests.codes.ok:
            raise Exception('Error while loading coverage data.')

        result = r.json()

        return {
          'cur': result['commit']['totals']['c'],
          'prev': result['commit']['parent_totals']['c'] if result['commit']['parent_totals'] else '?',
        }

    @staticmethod
    @cache.memoize()
    def get_file_coverage(changeset, filename):
        r = requests.get(CodecovCoverage.URL + '/src/%s/%s' % (get_github_commit(changeset), filename))

{'fraction_non_alphanumeric': 0.129570516540917, 'fraction_numerical': 0.0030470110272780033, 'mean_word_length': 2.912031782065834, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7579050', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2077, 'n_words': 442}