def build_instance(self, data, **kwargs): revision = self.context.get("resolved_revision") build = Build( repository=self.context.get("repository"), revision_sha=revision.sha if revision else None, **data ) if build.data is None: build.data = {} build.data["required_hook_ids"] = Hook.get_required_hook_ids( build.repository.id ) if revision: if not build.label: build.label = revision.message.split("\n")[0] if not build.authors and revision.authors: build.authors = revision.authors return build
def test_get_required_hook_ids(default_repo): hook = factories.HookFactory.create(repository=default_repo, is_required=True) factories.HookFactory.create(repository=default_repo, is_required=False) assert Hook.get_required_hook_ids(default_repo.id) == [str(hook.id)]
def post(self, repo: Repository): """ Create a new build. """ schema = BuildCreateSchema(strict=True, context={"repository": repo}) result = self.schema_from_request(schema, partial=True) if result.errors: return self.respond(result.errors, 403) data = result.data # TODO(dcramer): only if we create a source via a patch will we need the author # author_data = data.pop('author') # if author_data.get('email'): # author = Author.query.filter( # Author.repository_id == repo.id, Author.email == author_data['email'] # ).first() # else: # author = None # if not author: # author = Author(repository_id=repo.id, **author_data) # db.session.add(author) # db.session.flush() # TODO(dcramer): need to handle patch case yet source = (Source.query.options( joinedload("author"), joinedload("revision")).filter( Source.revision_sha == data.pop("ref"), Source.repository_id == repo.id).one_or_none()) assert source # we need to write/sync the required hook IDs in case they've changed required_hook_ids = Hook.get_required_hook_ids(repo.id) if (source.data or {}).get("required_hook_ids") != required_hook_ids: if source.data is None: source.data = {} source.data["required_hook_ids"] = required_hook_ids db.session.add(source) build = Build(repository=repo, **data) # TODO(dcramer): we should convert source in the schema build.source = source # build.source_id = source.id build.author = source.author if not source.patch_id: if not build.label: build.label = source.revision.message.split("\n")[0] if not build.label: return self.error("missing build label") db.session.add(build) try: db.session.commit() except IntegrityError: db.session.rollback() return self.respond(status=422) result = build_schema.dump(build) assert not result.errors, "this should never happen" publish("builds", "build.create", result.data) return self.respond(result.data, 200)