# Path: f8a_worker/workers/githuber.py
# class GithubTask(BaseTask):
#     """Collects statistics using Github API."""
# 
#     # used for testing
#     _repo_name = None
#     _repo_url = None
# 
#     @classmethod
#     def create_test_instance(cls, repo_name, repo_url):
#         """Create instance of task for tests."""
#         assert cls
#         instance = super().create_test_instance()
#         # set for testing as we are not querying DB for mercator results
#         instance._repo_name = repo_name
#         instance._repo_url = repo_url
#         return instance
# 
#     def _get_last_years_commits(self, repo_url):
#         """Get weekly commit activity for last year."""
#         try:
#             activity = get_response(urljoin(repo_url + '/', "stats/commit_activity"))
#             if activity is None:
#                 return []
#             return [x.get('total', 0) for x in activity]
#         except NotABugTaskError as e:
#             self.log.debug(e)
#             return []
# 
#     def _get_repo_stats(self, repo):
#         """Collect various repository properties."""
#         try:
#             url = repo.get('contributors_url', '')
#             if url:
#                 contributors = get_gh_contributors(url)
#             else:
#                 contributors = -1
#         except NotABugTaskError as e:
#             self.log.debug(e)
#             contributors = -1
#         d = {'contributors_count': contributors}
#         for prop in REPO_PROPS:
#             d[prop] = repo.get(prop, -1)
#         return d
# 
#     def _get_repo_name(self, url):
#         """Retrieve GitHub repo from a preceding Mercator scan."""
#         parsed = parse_gh_repo(url)
#         if not parsed:
#             self.log.debug('Could not parse Github repo URL %s', url)
#         else:
#             self._repo_url = 'https://github.com/' + parsed
#         return parsed
# 
#     def execute(self, arguments):
#         """Task code.
# 
#         :param arguments: dictionary with task arguments
#         :return: {}, results
#         """
#         result_data = {'status': 'unknown',
#                        'summary': [],
#                        'details': {}}
#         # For testing purposes, a repo may be specified at task creation time
#         if self._repo_name is None:
#             # Otherwise, get the repo name from earlier Mercator scan results
#             self._repo_name = self._get_repo_name(arguments['url'])
#             if self._repo_name is None:
#                 # Not a GitHub hosted project
#                 return result_data
# 
#         repo_url = urljoin(self.configuration.GITHUB_API + "repos/", self._repo_name)
#         try:
#             repo = get_response(repo_url)
#             if not repo:
#                 raise NotABugFatalTaskError('Page not found on {}'.format(repo_url))
#         except NotABugTaskError as e:
#             self.log.error(e)
#             raise NotABugFatalTaskError from e
# 
#         result_data['status'] = 'success'
# 
#         issues = {}
#         # Get Repo Statistics
#         notoriety = self._get_repo_stats(repo)
# 
#         if notoriety:
#             issues.update(notoriety)
#         issues['topics'] = repo.get('topics', [])
#         issues['license'] = repo.get('license') or {}
# 
#         # Get Commit Statistics
#         last_year_commits = self._get_last_years_commits(repo['url'])
#         commits = {'last_year_commits': {'sum': sum(last_year_commits),
#                                          'weekly': last_year_commits}}
#         t_stamp = datetime.datetime.utcnow()
#         refreshed_on = {'updated_on': t_stamp.strftime("%Y-%m-%d %H:%M:%S")}
#         issues.update(refreshed_on)
#         issues.update(commits)
# 
#         # Get PR/Issue details for previous Month and Year
#         gh_pr_issue_details = get_gh_pr_issue_counts(repo['full_name'])
#         issues.update(gh_pr_issue_details)
# 
#         result_data['details'] = issues
#         return result_data


import pytest
from f8a_worker.workers import GithubTask
# -*- coding: utf-8 -*-

"""Tests for the GithubTask worker task."""




@pytest.mark.usefixtures("dispatcher_setup")
class TestGithuber(object):
    """Tests for the GithubTask worker task."""

    @pytest.mark.parametrize(('repo_name', 'repo_url'), [
        ('projectatomic/atomic-reactor', 'https://github.com/projectatomic/atomic-reactor'),
    ])
    def test_execute(self, repo_name, repo_url):
        """Start the GithubTask worker task and test its results."""

# Path: f8a_worker/workers/githuber.py
# class GithubTask(BaseTask):
#     """Collects statistics using Github API."""
# 
#     # used for testing
#     _repo_name = None
#     _repo_url = None
# 
#     @classmethod
#     def create_test_instance(cls, repo_name, repo_url):
#         """Create instance of task for tests."""
#         assert cls
#         instance = super().create_test_instance()
#         # set for testing as we are not querying DB for mercator results
#         instance._repo_name = repo_name
#         instance._repo_url = repo_url
#         return instance
# 
#     def _get_last_years_commits(self, repo_url):
#         """Get weekly commit activity for last year."""
#         try:
#             activity = get_response(urljoin(repo_url + '/', "stats/commit_activity"))
#             if activity is None:
#                 return []
#             return [x.get('total', 0) for x in activity]
#         except NotABugTaskError as e:
#             self.log.debug(e)
#             return []
# 
#     def _get_repo_stats(self, repo):
#         """Collect various repository properties."""
#         try:
#             url = repo.get('contributors_url', '')
#             if url:
#                 contributors = get_gh_contributors(url)
#             else:
#                 contributors = -1
#         except NotABugTaskError as e:
#             self.log.debug(e)
#             contributors = -1
#         d = {'contributors_count': contributors}
#         for prop in REPO_PROPS:
#             d[prop] = repo.get(prop, -1)
#         return d
# 
#     def _get_repo_name(self, url):
#         """Retrieve GitHub repo from a preceding Mercator scan."""
#         parsed = parse_gh_repo(url)
#         if not parsed:
#             self.log.debug('Could not parse Github repo URL %s', url)
#         else:
#             self._repo_url = 'https://github.com/' + parsed
#         return parsed
# 
#     def execute(self, arguments):
#         """Task code.
# 
#         :param arguments: dictionary with task arguments
#         :return: {}, results
#         """
#         result_data = {'status': 'unknown',
#                        'summary': [],
#                        'details': {}}
#         # For testing purposes, a repo may be specified at task creation time
#         if self._repo_name is None:
#             # Otherwise, get the repo name from earlier Mercator scan results
#             self._repo_name = self._get_repo_name(arguments['url'])
#             if self._repo_name is None:
#                 # Not a GitHub hosted project
#                 return result_data
# 
#         repo_url = urljoin(self.configuration.GITHUB_API + "repos/", self._repo_name)
#         try:
#             repo = get_response(repo_url)
#             if not repo:
#                 raise NotABugFatalTaskError('Page not found on {}'.format(repo_url))
#         except NotABugTaskError as e:
#             self.log.error(e)
#             raise NotABugFatalTaskError from e
# 
#         result_data['status'] = 'success'
# 
#         issues = {}
#         # Get Repo Statistics
#         notoriety = self._get_repo_stats(repo)
# 
#         if notoriety:
#             issues.update(notoriety)
#         issues['topics'] = repo.get('topics', [])
#         issues['license'] = repo.get('license') or {}
# 
#         # Get Commit Statistics
#         last_year_commits = self._get_last_years_commits(repo['url'])
#         commits = {'last_year_commits': {'sum': sum(last_year_commits),
#                                          'weekly': last_year_commits}}
#         t_stamp = datetime.datetime.utcnow()
#         refreshed_on = {'updated_on': t_stamp.strftime("%Y-%m-%d %H:%M:%S")}
#         issues.update(refreshed_on)
#         issues.update(commits)
# 
#         # Get PR/Issue details for previous Month and Year
#         gh_pr_issue_details = get_gh_pr_issue_counts(repo['full_name'])
#         issues.update(gh_pr_issue_details)
# 
#         result_data['details'] = issues
#         return result_data


# Path: tests/workers/test_githuber.py
import pytest
from f8a_worker.workers import GithubTask

# -*- coding: utf-8 -*-

"""Tests for the GithubTask worker task."""




@pytest.mark.usefixtures("dispatcher_setup")
class TestGithuber(object):
    """Tests for the GithubTask worker task."""

    @pytest.mark.parametrize(('repo_name', 'repo_url'), [
        ('projectatomic/atomic-reactor', 'https://github.com/projectatomic/atomic-reactor'),
    ])
    def test_execute(self, repo_name, repo_url):
        """Start the GithubTask worker task and test its results."""

        task = GithubTask.create_test_instance(repo_name, repo_url)