Ejemplo n.º 1
0
    def testEquivalentPatchsetsIsCached(self, mock_http_client):
        mock_http_client.return_value = (200,
                                         json.dumps({
                                             'status': 'NEW',
                                             'revisions': {
                                                 'aaaaaaaaaaa': {
                                                     '_number': 8,
                                                     'kind': 'REWORK',
                                                 },
                                             },
                                         }), None)

        self.assertEqual(0, mock_http_client.call_count)
        code_coverage_util.GetEquivalentPatchsets(
            'chromium-review.googlesource.com', 'chromium/src', 12345, 8)
        self.assertEqual(1, mock_http_client.call_count)
        code_coverage_util.GetEquivalentPatchsets(
            'chromium-review.googlesource.com', 'chromium/src', 12345, 8)
        self.assertEqual(1, mock_http_client.call_count)
Ejemplo n.º 2
0
 def testLatestPatchsetOfSubmittedCLsIsIgnored(self, mock_http_client):
     mock_http_client.return_value = (200,
                                      json.dumps({
                                          'status': 'MERGED',
                                          'revisions': {
                                              'aaaaaaaaaaa': {
                                                  '_number': 8,
                                                  'kind': 'REWORK',
                                              },
                                              'bbbbbbbbbbb': {
                                                  '_number': 7,
                                                  'kind': 'NO_CODE_CHANGE',
                                              },
                                          },
                                      }), None)
     result = code_coverage_util.GetEquivalentPatchsets(
         'chromium-review.googlesource.com', 'chromium/src', 12345, 8)
     self.assertListEqual([7], result)
Ejemplo n.º 3
0
    def testGetEquivalentPatchsets(self, mock_http_client):
        mock_http_client.return_value = (
            200,
            json.dumps({
                'status': 'NEW',
                'revisions': {
                    'aaaaaaaaaaa': {
                        '_number': 8,
                        'kind': 'TRIVIAL_REBASE',
                    },
                    'bbbbbbbbbbb': {
                        '_number': 7,
                        'kind': 'TRIVIAL_REBASE',
                    },
                    'ccccccccccc': {
                        '_number': 6,
                        'kind': 'MERGE_FIRST_PARENT_UPDATE',
                    },
                    'ddddddddddd': {
                        '_number': 5,
                        'kind': 'NO_CODE_CHANGE',
                    },
                    'eeeeeeeeeee': {
                        '_number': 4,
                        'kind': 'NO_CHANGE',
                    },
                    'fffffffffff': {
                        '_number': 3,
                        'kind': 'REWORK',
                    },
                    'ggggggggggg': {
                        '_number': 2,
                        'kind': 'TRIVIAL_REBASE',
                    },
                },
            }), None)

        result = code_coverage_util.GetEquivalentPatchsets(
            'chromium-review.googlesource.com', 'chromium/src', 12345, 7)
        self.assertListEqual([7, 6, 5, 4, 3], result)
Ejemplo n.º 4
0
  def _ServePerCLCoverageData(self):
    """Serves per-cl coverage data.

    The consumer is assumed to be the code coverage Gerrit plugin, so the format
    of the returned data conforms to:
    https://chromium.googlesource.com/infra/gerrit-plugins/code-coverage/+/213d226a5f1b78c45c91d49dbe32b09c5609e9bd/src/main/resources/static/coverage.js#93
    """
    host = self.request.get('host')
    project = self.request.get('project')
    change = int(self.request.get('change'))
    patchset = int(self.request.get('patchset'))

    logging.info('Serving coverage data for CL:')
    logging.info('host=%s', host)
    logging.info('change=%d', change)
    logging.info('patchset=%d', patchset)

    if host and host.replace('-review', '') not in _ALLOWED_GITILES_HOST:
      return BaseHandler.CreateError(
          error_message='Host "%s" is not whitelisted.' % host,
          return_code=400,
          allowed_origin='*')

    if project and project not in _PROJECTS_WHITELIST:
      kwargs = {'is_project_supported': False}
      return BaseHandler.CreateError(
          error_message='Project "%s" is not supported.' % project,
          return_code=400,
          allowed_origin='*',
          **kwargs)

    if not _IsServePresubmitCoverageDataEnabled():
      # TODO(crbug.com/908609): Switch to 'is_service_enabled'.
      kwargs = {'is_project_supported': False}
      return BaseHandler.CreateError(
          error_message=('The functionality has been temporarity disabled.'),
          return_code=400,
          allowed_origin='*',
          **kwargs)

    entity = PresubmitCoverageData.Get(
        server_host=host, change=change, patchset=patchset)
    if not entity:
      equivalent_patchsets = code_coverage_util.GetEquivalentPatchsets(
          host, project, change, patchset)
      if not equivalent_patchsets:
        return BaseHandler.CreateError(
            'Requested coverage data is not found.', 404, allowed_origin='*')

      latest_patchset = None
      latest_entity = None
      for ps in sorted(equivalent_patchsets, reverse=True):
        latest_entity = PresubmitCoverageData.Get(
            server_host=host, change=change, patchset=ps)
        if latest_entity:
          latest_patchset = ps
          break

      if latest_patchset is None:
        return BaseHandler.CreateError(
            'Requested coverage data is not found.', 404, allowed_origin='*')

      rebased_coverage_data = (
          code_coverage_util.RebasePresubmitCoverageDataBetweenPatchsets(
              host=host,
              project=project,
              change=change,
              patchset_src=latest_patchset,
              patchset_dest=patchset,
              coverage_data_src=latest_entity.data))
      entity = PresubmitCoverageData.Create(
          server_host=host,
          change=change,
          patchset=patchset,
          build_id=latest_entity.build_id,
          data=rebased_coverage_data)
      entity.absolute_percentages = latest_entity.absolute_percentages
      entity.incremental_percentages = latest_entity.incremental_percentages
      entity.put()

    data = entity.data
    formatted_data = {'files': []}
    for file_data in data:
      path = file_data['path']
      if path.startswith('//'):  # Check for safe. Old data don't have '//'.
        path = path[2:]

      formatted_data['files'].append({
          'path': path,
          'lines': code_coverage_util.DecompressLineRanges(file_data['lines']),
      })

    return {
        'data': {
            'host': host,
            'project': project,
            'change': change,
            'patchset': patchset,
            'data': formatted_data,
        },
        'allowed_origin': '*'
    }