from __future__ import absolute_import

from changes.api.base import APIView
from changes.config import db
from changes.constants import Result
from changes.models import Build, Job, TestCase


class BuildFlakyTestsAPIView(APIView):
    def get(self, build_id):
        build = Build.query.get(build_id)
        if build is None:
            return '', 404

        jobs = list(Job.query.filter(
            Job.build_id == build.id,
        ))

        flaky_tests_query = db.session.query(
            TestCase.name
        ).filter(
            TestCase.job_id.in_([j.id for j in jobs]),
            TestCase.result == Result.passed,
            TestCase.reruns > 1
        ).order_by(TestCase.name.asc())

        flaky_tests = map(lambda test: {'name': test.name}, flaky_tests_query)

        context = {
            'repositoryUrl': build.project.repository.url,
            'flakyTests': {
                'count': len(flaky_tests),
                'items': flaky_tests
            }
        }

        return self.respond(context)

from __future__ import absolute_import

from changes.api.base import APIView
from changes.config import db
from changes.constants import Result
from changes.models import Build, Job, TestCase


class BuildFlakyTestsAPIView(APIView):
    def get(self, build_id):
        build = Build.query.get(build_id)
        if build is None:
            return '', 404

        jobs = list(Job.query.filter(
            Job.build_id == build.id,
        ))

        flaky_tests_query = db.session.query(
            TestCase.name
        ).filter(
            TestCase.job_id.in_([j.id for j in jobs]),
            TestCase.result == Result.passed,
            TestCase.reruns > 1
        ).order_by(TestCase.name.asc())

        flaky_tests = map(lambda test: {'name': test.name}, flaky_tests_query)

        context = {
            'projectSlug': build.project.slug,
            'repositoryUrl': build.project.repository.url,
            'flakyTests': {
                'count': len(flaky_tests),
                'items': flaky_tests
            }
        }

        return self.respond(context)

Add projectSlug to build flaky tests API response
Add projectSlug to build flaky tests API response

Summary: We will use it in the test quarantine service to whitelist projects which support quarantine.

Test Plan:
Tested locally.

{
  "projectSlug": "changesjenkins",
  "repositoryUrl": "https://github.com/dropbox/changes.git",
  "flakyTests": {
    "count": 1,
    "items": [
      {
        "name": "tests.account.test_account.AccountTest.test_account_change_language"
      }
    ]
  }
}

Reviewers: haoyi

Reviewed By: haoyi

Subscribers: changesbot, mkedia

Differential Revision: https://tails.corp.dropbox.com/D123809

  from __future__ import absolute_import
  
  from changes.api.base import APIView
  from changes.config import db
  from changes.constants import Result
  from changes.models import Build, Job, TestCase
  
  
  class BuildFlakyTestsAPIView(APIView):
      def get(self, build_id):
          build = Build.query.get(build_id)
          if build is None:
              return '', 404
  
          jobs = list(Job.query.filter(
              Job.build_id == build.id,
          ))
  
          flaky_tests_query = db.session.query(
              TestCase.name
          ).filter(
              TestCase.job_id.in_([j.id for j in jobs]),
              TestCase.result == Result.passed,
              TestCase.reruns > 1
          ).order_by(TestCase.name.asc())
  
          flaky_tests = map(lambda test: {'name': test.name}, flaky_tests_query)
  
          context = {
+             'projectSlug': build.project.slug,
              'repositoryUrl': build.project.repository.url,
              'flakyTests': {
                  'count': len(flaky_tests),
                  'items': flaky_tests
              }
          }
  
          return self.respond(context)
  
Add projectSlug to build flaky tests API response
## Code Before:
from __future__ import absolute_import

from changes.api.base import APIView
from changes.config import db
from changes.constants import Result
from changes.models import Build, Job, TestCase


class BuildFlakyTestsAPIView(APIView):
    def get(self, build_id):
        build = Build.query.get(build_id)
        if build is None:
            return '', 404

        jobs = list(Job.query.filter(
            Job.build_id == build.id,
        ))

        flaky_tests_query = db.session.query(
            TestCase.name
        ).filter(
            TestCase.job_id.in_([j.id for j in jobs]),
            TestCase.result == Result.passed,
            TestCase.reruns > 1
        ).order_by(TestCase.name.asc())

        flaky_tests = map(lambda test: {'name': test.name}, flaky_tests_query)

        context = {
            'repositoryUrl': build.project.repository.url,
            'flakyTests': {
                'count': len(flaky_tests),
                'items': flaky_tests
            }
        }

        return self.respond(context)

## Instruction:
Add projectSlug to build flaky tests API response
## Code After:
from __future__ import absolute_import

from changes.api.base import APIView
from changes.config import db
from changes.constants import Result
from changes.models import Build, Job, TestCase


class BuildFlakyTestsAPIView(APIView):
    def get(self, build_id):
        build = Build.query.get(build_id)
        if build is None:
            return '', 404

        jobs = list(Job.query.filter(
            Job.build_id == build.id,
        ))

        flaky_tests_query = db.session.query(
            TestCase.name
        ).filter(
            TestCase.job_id.in_([j.id for j in jobs]),
            TestCase.result == Result.passed,
            TestCase.reruns > 1
        ).order_by(TestCase.name.asc())

        flaky_tests = map(lambda test: {'name': test.name}, flaky_tests_query)

        context = {
            'projectSlug': build.project.slug,
            'repositoryUrl': build.project.repository.url,
            'flakyTests': {
                'count': len(flaky_tests),
                'items': flaky_tests
            }
        }

        return self.respond(context)
