예제 #1
0
def resolve_ref_for_change_request(change_request_id: UUID):
    lock_key = f"resolve-cr-ref:{change_request_id}"
    with redis.lock(lock_key, timeout=60.0, nowait=True):
        cr = ChangeRequest.query.unrestricted_unsafe().get(change_request_id)
        if not cr:
            raise ValueError(
                "Unable to find change request with id = {}".format(
                    change_request_id))

        auth.set_current_tenant(
            auth.RepositoryTenant(repository_id=cr.repository_id))

        if not cr.parent_revision_sha and cr.parent_ref:
            try:
                revision = revisions.identify_revision(cr.repository,
                                                       cr.parent_ref,
                                                       with_vcs=True)
            except InvalidPublicKey:
                raise
            cr.parent_revision_sha = revision.sha
            db.session.add(cr)
            db.session.commit()

        if not cr.head_revision_sha and cr.head_ref:
            revision = revisions.identify_revision(cr.repository,
                                                   cr.head_ref,
                                                   with_vcs=True)
            cr.head_revision_sha = revision.sha
            if not cr.authors and revision.authors:
                cr.authors = revision.authors
            db.session.add(cr)
            db.session.commit()
예제 #2
0
def test_identity_revision_unknown(default_repo, default_revision,
                                   mock_vcs_server):
    mock_vcs_server.replace(
        mock_vcs_server.GET,
        "http://localhost:8070/stmt/resolve",
        status=400,
        json={
            "error": "invalid_ref",
            "ref": default_revision.sha[:7]
        },
    )

    with pytest.raises(UnknownRevision):
        identify_revision(default_repo, ref=default_revision.sha[:7])
예제 #3
0
    def _deserialize(self, value, attr, data):
        repo = self.context.get("repository")
        if repo:
            try:
                revision = revisions.identify_revision(repo, value)
            except UnknownRevision as e:
                current_app.logger.warn("invalid ref received", exc_info=True)
                raise ValidationError(
                    "unknown revision: {}".format(value)) from e

            return revision.sha

        return value
예제 #4
0
def resolve_ref_for_build(build_id: UUID):
    lock_key = f"resolve-build-ref:{build_id}"
    with redis.lock(lock_key, timeout=60.0, nowait=True):
        build = Build.query.unrestricted_unsafe().get(build_id)
        if not build:
            raise ValueError(
                "Unable to find build with id = {}".format(build_id))

        if build.revision_sha:
            return

        auth.set_current_tenant(
            auth.RepositoryTenant(repository_id=build.repository_id))

        revision: Optional[Revision] = None
        try:
            revision = revisions.identify_revision(build.repository,
                                                   build.ref,
                                                   with_vcs=True)
        except UnknownRevision:
            build.result = Result.errored
            build.status = Status.finished
            try:
                with db.session.begin_nested():
                    db.session.add(
                        FailureReason(
                            repository_id=build.repository_id,
                            build_id=build.id,
                            reason=FailureReason.Reason.unresolvable_ref,
                        ))
                    db.session.flush()
            except IntegrityError as exc:
                if "duplicate" not in str(exc):
                    raise

        except InvalidPublicKey:
            pass

        if revision:
            build.revision_sha = revision.sha
            if not build.authors and revision.authors:
                build.authors = revision.authors
            if not build.label:
                build.label = revision.message.split("\n")[0]
        db.session.add(build)
        db.session.commit()

    data = build_schema.dump(build)
    publish("builds", "build.update", data)
예제 #5
0
def test_identify_revision_vcs_server(default_repo, default_revision,
                                      mock_vcs_server):
    mock_vcs_server.replace(
        mock_vcs_server.GET,
        "http://localhost:8070/stmt/resolve",
        json={
            "resolve": {
                "sha":
                default_revision.sha,
                "message":
                default_revision.message,
                "authors": [(
                    default_revision.authors[0].name,
                    default_revision.authors[0].email,
                )],
            }
        },
    )

    result = identify_revision(default_repo, ref=default_revision.sha[:7])
    assert result == default_revision
예제 #6
0
    def _deserialize(self, value, attr, data, **kwargs):
        repo = self.context.get("repository")
        if not repo:
            return value

        if not self.validate_ref and not self.resolve_to:
            return value

        try:
            revision = revisions.identify_revision(
                repo, value, with_vcs=self.validate_ref
            )
        except UnknownRevision as e:
            if self.validate_ref:
                current_app.logger.warn("invalid ref received", exc_info=True)
                raise ValidationError("unknown revision: {}".format(value)) from e
        else:
            if self.resolve_to:
                # XXX(dcramer): we'd prefer to make this a compound field
                # but Marshmallow wont let us bind this
                self.context["resolved_{}".format(self.resolve_to)] = revision
        return value
예제 #7
0
def test_identify_revision_database(default_repo, default_revision,
                                    mock_vcs_server):
    result = identify_revision(default_repo, ref=default_revision.sha)
    assert result == default_revision