Ejemplo n.º 1
0
    def get_context_data(self, **kwargs):
        """Get day statistic"""
        success = Tasks.find({
            'created': {
                '$gte': datetime.now() - timedelta(days=1)
            },
            'status': STATUS_SUCCESS
        }).count()

        failed = Tasks.find({
            'created': {
                '$gte': datetime.now() - timedelta(days=1)
            },
            'status': STATUS_FAILED
        }).count()

        if failed:
            failed_percent = failed * 100 / (success + failed)
        else:
            failed_percent = 0

        return {
            'failed': failed_percent,
            'success': 100 - failed_percent,
            'debug': settings.DEBUG,
            'site': Site.objects.get_current(),
        }
 def test_fail_on_travis_api_error(self):
     """Test fail on travis api error"""
     travis_ci.requests.get.side_effect = Exception
     data = self._create_task()
     with LogCapture() as log_capture:
         travis_ci.travis_ci_service(data).should.be.none
         list(log_capture.actual())[0].should.contain('ERROR')
     Tasks.find({}).count().should.be.equal(0)
Ejemplo n.º 3
0
 def test_fail_on_travis_api_error(self):
     """Test fail on travis api error"""
     travis_ci.requests.get.side_effect = Exception
     data = self._create_task()
     with LogCapture() as log_capture:
         travis_ci.travis_ci_service(data).should.be.none
         list(log_capture.actual())[0].should.contain('ERROR')
     Tasks.find({}).count().should.be.equal(0)
 def test_fail_if_already_exists(self):
     """Test fail if job already exists"""
     self._create_task()
     data = self._create_task()
     with LogCapture() as log_capture:
         travis_ci.travis_ci_service(data).should.be.none
         list(log_capture.actual())[0].should.contain('ERROR')
     Tasks.find({}).count().should.be.equal(1)
Ejemplo n.º 5
0
 def test_fail_if_already_exists(self):
     """Test fail if job already exists"""
     self._create_task()
     data = self._create_task()
     with LogCapture() as log_capture:
         travis_ci.travis_ci_service(data).should.be.none
         list(log_capture.actual())[0].should.contain('ERROR')
     Tasks.find({}).count().should.be.equal(1)
Ejemplo n.º 6
0
 def test_fail_on_wrong_project(self):
     """Test fail on wrong project"""
     travis_ci.requests.get.return_value = MagicMock(json=MagicMock(
         return_value={
             'repository_id': 2,
             'slug': 'wrong',
         }))
     data = self._create_task()
     with LogCapture() as log_capture:
         travis_ci.travis_ci_service(data).should.be.none
         list(log_capture.actual())[0].should.contain('ERROR')
     Tasks.find({}).count().should.be.equal(0)
 def test_fail_on_wrong_project(self):
     """Test fail on wrong project"""
     travis_ci.requests.get.return_value = MagicMock(
         json=MagicMock(return_value={
             'repository_id': 2,
             'slug': 'wrong',
         })
     )
     data = self._create_task()
     with LogCapture() as log_capture:
         travis_ci.travis_ci_service(data).should.be.none
         list(log_capture.actual())[0].should.contain('ERROR')
     Tasks.find({}).count().should.be.equal(0)
Ejemplo n.º 8
0
 def test_fail_with_wrong_project(self):
     """Test fail with wrong project"""
     task_id = Tasks.save({
         'project': 'test',
         'service': {
             'name': 'token',
             'token': ProjectFactory().token,
         }
     })
     data = Tasks.find_one(task_id)
     with LogCapture() as log_capture:
         token_service(data).should.be.none
         list(log_capture.actual())[0].should.contain('ERROR')
     Tasks.find({}).count().should.be.equal(0)
Ejemplo n.º 9
0
def travis_ci_service(data):
    """Create tasks from data received from travis-c

    :param data: Data received from service
    :type data: dict
    :returns: bson.ObjectId or None -- pk of created task
    """
    try:
        assert Tasks.find({
            'service.name': 'travis_ci',
            'service.job_id': data['service']['job_id'],
        }).count() <= 1

        job = requests.get(
            'https://api.travis-ci.org/jobs/{}'.format(
                data['service']['job_id'], ), ).json()

        repo = requests.get(
            'https://api.travis-ci.org/repos/{}'.format(
                job['repository_id'], ), ).json()

        # TODO: add pull request support
        assert data['project'] == repo['slug']

        if data['service'].get('pull_request_id'):
            pull_request = data['service']['pull_request_id']
            if pull_request != 'false':
                data['pull_request_id'] = int(
                    data['service']['pull_request_id'], )

        return Tasks.save(data)
    except Exception as e:
        # remove task on error
        Tasks.remove(data['_id'])
        logger.exception('Travis-ci service fail: {}'.format(e))
Ejemplo n.º 10
0
    def get_context_data(self, **kwargs):
        """Get day statistic"""
        success = Tasks.find({'created': {
            '$gte': datetime.now() - timedelta(days=1)
        }, 'status': STATUS_SUCCESS}).count()

        failed = Tasks.find({'created': {
            '$gte': datetime.now() - timedelta(days=1)
        }, 'status': STATUS_FAILED}).count()

        if failed:
            failed_percent = failed * 100 / (success + failed)
        else:
            failed_percent = 0

        return {
            'failed': failed_percent,
            'success': 100 - failed_percent,
            'debug': settings.DEBUG,
            'site': Site.objects.get_current(),
        }
Ejemplo n.º 11
0
 def _fill_statistic_parts(self, parts, grouper):
     """Fill statistic parts"""
     for task in Tasks.find({
         'project': self.name,
         'created': {'$exists': True},
         'success_percent': {'$exists': True},
     }):
         if type(task['created']) is datetime:
             group = grouper(task)
             parts[group]['count'] += 1
             parts[group]['sum_percent'] +=\
                 task['success_percent']
             parts[group][
                 'success' if task['status'] == STATUS_SUCCESS else 'failed'
             ] += 1
Ejemplo n.º 12
0
 def get_success_percents(self, count, branch=None):
     """Get project last success percents"""
     if branch is None:
         branch = self.dashboard_branch
     spec = {
         'project': self.name,
     }
     if branch:
         spec['commit.branch'] = branch
     return [
         task.get('success_percent', 0) for task in Tasks.find(
             spec, sort=[('created', DESCENDING)], fields={
                 'success_percent': True,
             }, limit=count)
     ]
Ejemplo n.º 13
0
def travis_ci_service(data):
    """Create tasks from data received from travis-c

    :param data: Data received from service
    :type data: dict
    :returns: bson.ObjectId or None -- pk of created task
    """
    try:
        assert Tasks.find({
            'service.name': 'travis_ci',
            'service.job_id': data['service']['job_id'],
        }).count() <= 1

        job = requests.get(
            'https://api.travis-ci.org/jobs/{}'.format(
                data['service']['job_id'],
            ),
        ).json()

        repo = requests.get(
            'https://api.travis-ci.org/repos/{}'.format(
                job['repository_id'],
            ),
        ).json()

        # TODO: add pull request support
        assert data['project'] == repo['slug']

        if data['service'].get('pull_request_id'):
            pull_request = data['service']['pull_request_id']
            if pull_request != 'false':
                data['pull_request_id'] = int(
                    data['service']['pull_request_id'],
                )

        return Tasks.save(data)
    except Exception as e:
        # remove task on error
        Tasks.remove(data['_id'])
        logger.exception('Travis-ci service fail: {}'.format(e))
Ejemplo n.º 14
0
 def branches(self):
     """Get project branches"""
     return Tasks.find({'project': self.name}).distinct('commit.branch')
 def handle(self, *args, **kwargs):
     for task in Tasks.find({}, sort=[('created', ASCENDING)]):
         try:
             prepare_violations(task['_id'])
         except Exception as e:
             print e