コード例 #1
0
    def save(self, repository: Repository) -> Tuple[Revision, bool]:
        author = self._get_author(repository, self.author)
        if self.author == self.committer:
            committer = author
        else:
            committer = self._get_author(repository, self.committer)

        revision, created = create_or_update(
            Revision,
            where={
                'repository': repository,
                'sha': self.sha,
            },
            values={
                'author': author,
                'committer': committer,
                'message': self.message,
                'parents': self.parents,
                'branches': self.branches,
                'date_created': self.author_date,
                'date_committed': self.committer_date,
            }
        )

        # we also want to create a source for this item as it's the canonical
        # representation in the UI
        try_create(Source, {
            'revision_sha': self.sha,
            'repository': repository,
            'author': author,
        })

        return (revision, created)
コード例 #2
0
def mock_single_repository(user_ids=()):
    repo = factories.RepositoryFactory.build(
        status=models.RepositoryStatus.active,
        github=True,
    )
    try:
        with db.session.begin_nested():
            db.session.add(repo)
    except IntegrityError:
        repo = models.Repository.query.unrestricted_unsafe().filter(
            models.Repository.provider == repo.provider,
            models.Repository.owner_name == repo.owner_name,
            models.Repository.name == repo.name
        ).first()
        click.echo('Using {!r}'.format(repo))
    else:
        click.echo('Created {!r}'.format(repo))

    for user_id in user_ids:
        try_create(models.RepositoryAccess, {
            'repository_id': repo.id,
            'user_id': user_id,
        })

    db.session.commit()
    return repo
コード例 #3
0
async def mock_single_repository(user_ids: Iterable[UUID] = ()):
    repo = factories.RepositoryFactory.build(
        status=models.RepositoryStatus.active,
        github=True,
        owner_name="getsentry",
        name=choice(repo_names),
    )
    try:
        with db.session.begin_nested():
            db.session.add(repo)
    except IntegrityError:
        repo = (models.Repository.query.unrestricted_unsafe().filter(
            models.Repository.provider == repo.provider,
            models.Repository.owner_name == repo.owner_name,
            models.Repository.name == repo.name,
        ).first())
        click.echo("Using {!r}".format(repo))
    else:
        click.echo("Created {!r}".format(repo))

    for user_id in user_ids:
        try_create(models.RepositoryAccess, {
            "repository_id": repo.id,
            "user_id": user_id
        })

    db.session.commit()
    return repo
コード例 #4
0
def test_try_create_with_defaults_new_instance():
    instance = try_create(
        User,
        {"id": UUID("5edb7312-316e-11ea-b6cd-8c85906d3733")},
        {"email": "*****@*****.**"},
    )
    assert instance
    assert instance.id == UUID("5edb7312-316e-11ea-b6cd-8c85906d3733")
    assert instance.email == "*****@*****.**"
コード例 #5
0
    def save(self, repository: Repository) -> Tuple[Revision, bool]:
        author = self._get_author(repository, self.author)
        if self.author == self.committer:
            committer = author
        else:
            committer = self._get_author(repository, self.committer)

        revision, created = create_or_update(
            Revision,
            where={
                "repository": repository,
                "sha": self.sha
            },
            values={
                "author": author,
                "committer": committer,
                "message": self.message,
                "parents": self.parents,
                "branches": self.branches,
                "date_created": self.author_date,
                "date_committed": self.committer_date,
            },
        )

        # we also want to create a source for this item as it's the canonical
        # representation in the UI
        try_create(
            Source,
            {
                "revision_sha": self.sha,
                "repository": repository,
                "author": author
            },
        )

        return RevisionSaveResult(revision, created)
コード例 #6
0
def mock_build(
        repo: models.Repository,
        revision: models.Revision = None,
        parent_revision: models.Revision = None,
        user_ids=(),
        file_list=(),
        with_change_request=True,
) -> models.Build:
    if user_ids and randint(0, 1) == 0:
        chosen_user_id = choice(user_ids)
        author = mock_author(repo, chosen_user_id)
    else:
        author = None

    if not revision:
        revision, source = mock_revision(repo, parent_revision, author)
    else:
        for n in range(2):
            source = (models.Source.query.unrestricted_unsafe().filter(
                models.Source.repository_id == repo.id,
                models.Source.revision_sha == revision.sha,
            ).first())
            if source:
                break
            try_create(
                models.Source,
                {
                    "revision_sha": revision.sha,
                    "repository": repo,
                    "author_id": revision.author_id,
                },
            )
        else:
            raise NotImplementedError

    if with_change_request and parent_revision is None:
        parent_revision = factories.RevisionFactory.create(repository=repo)

    if with_change_request:
        factories.ChangeRequestFactory.create(
            repository=repo,
            head_revision=revision,
            head_revision_sha=revision.sha,
            parent_revision=parent_revision,
            github=True,
            **{"author": author} if author else {})

    parent_revision = revision

    build = factories.BuildFactory.create(source=source, travis=True)

    result = build_schema.dump(build)
    publish("builds", "build.create", result.data)
    click.echo("Created {!r}".format(build))

    # we need to find some filenames for the repo
    if file_list is None:
        file_list = find_files_in_repo(repo)

    for n in range(randint(0, 50)):
        try:
            with db.session.begin_nested():
                factories.FileCoverageFactory.create(
                    filename=choice(file_list),
                    build=build,
                    in_diff=randint(0, 5) == 0)
        except IntegrityError:
            continue

    for n in range(1, 4):
        has_failure = randint(0, 2) == 0
        job = factories.JobFactory.create(
            build=build,
            failed=has_failure,
            passed=not has_failure,
            travis=True,
            allow_failure=n == 3,
        )

        for n in range(randint(0, 50)):
            test_failed = has_failure and randint(0, 5) == 0
            factories.TestCaseFactory.create(job=job,
                                             failed=test_failed,
                                             passed=not test_failed)
            if has_failure and randint(0, 2) == 0:
                for n in range(1, 5):
                    factories.StyleViolationFactory.create(job=job)

        for n in range(randint(0, 2)):
            bundle = factories.BundleFactory.create(job=job)
            for n in range(randint(0, 4)):
                factories.BundleAssetFactory.create(bundle=bundle, job=job)

        artifact_count = (randrange(3) if job.status == Status.finished
                          and job.result == Result.passed else 0)
        for n in range(0, artifact_count):
            factories.ArtifactFactory.create(job=job,
                                             repository=repo,
                                             finished=True)

        db.session.commit()

        aggregate_build_stats_for_job(job_id=job.id)

        result = build_schema.dump(build)
        publish("builds", "build.update", result.data)
        click.echo("Created {!r}".format(job))

    db.session.commit()
    return build
コード例 #7
0
def test_try_create_with_defaults_existing_instance(default_user):
    instance = try_create(User, {"id": default_user.id},
                          {"email": "*****@*****.**"})
    assert not instance
コード例 #8
0
def test_try_create_only_where_existing_instance(default_user):
    instance = try_create(User, {"email": default_user.email})
    assert not instance
コード例 #9
0
def test_try_create_only_where_new_instance():
    instance = try_create(User, {"email": "*****@*****.**"})
    assert instance
    assert instance.email == "*****@*****.**"