def validate(self, attrs: Mapping[str, Any]) -> Mapping[str, Any]: # If it already exists, set default attrs with existing values if self.instance: attrs = { "raw": self.instance.raw, "code_mapping_id": self.instance.repository_project_path_config, **attrs, } if not attrs.get("raw", "").strip(): return attrs # Ignore association errors and continue parsing CODEOWNERS for valid lines. # Allow users to incrementally fix association errors; for CODEOWNERS with many external mappings. associations, _ = ProjectCodeOwners.validate_codeowners_associations( attrs["raw"], self.context["project"] ) issue_owner_rules = convert_codeowners_syntax( attrs["raw"], associations, attrs["code_mapping_id"] ) # Convert IssueOwner syntax into schema syntax try: validated_data = create_schema_from_issue_owners( issue_owners=issue_owner_rules, project_id=self.context["project"].id ) return { **attrs, "schema": validated_data, } except ValidationError as e: raise serializers.ValidationError(e)
def serialize(self, obj, attrs, user): data = { "id": str(obj.id), "raw": obj.raw, "dateCreated": obj.date_added, "dateUpdated": obj.date_updated, "codeMappingId": str(obj.repository_project_path_config_id), "provider": "unknown", } data["provider"] = attrs.get("provider", "unknown") if "codeMapping" in self.expand: config = attrs.get("codeMapping", {}) data["codeMapping"] = serialize( config, user=user, serializer=RepositoryProjectPathConfigSerializer()) if "ownershipSyntax" in self.expand: data["ownershipSyntax"] = convert_schema_to_rules_text(obj.schema) if "errors" in self.expand: _, errors = ProjectCodeOwners.validate_codeowners_associations( obj.raw, obj.project) data["errors"] = errors return data
def validate(self, attrs: Mapping[str, Any]) -> Mapping[str, Any]: # If it already exists, set default attrs with existing values if self.instance: attrs = { "raw": self.instance.raw, "code_mapping_id": self.instance.repository_project_path_config, **attrs, } if not attrs.get("raw", "").strip(): return attrs # We want to limit `raw` to a reasonable length, so that people don't end up with values # that are several megabytes large. To not break this functionality for existing customers # we temporarily allow rows that already exceed this limit to still be updated. # We do something similar with ProjectOwnership at the API level. existing_raw = self.instance.raw if self.instance else "" if len(attrs["raw"]) > MAX_RAW_LENGTH and len( existing_raw) <= MAX_RAW_LENGTH: raise serializers.ValidationError({ "raw": f"Raw needs to be <= {MAX_RAW_LENGTH} characters in length" }) # Ignore association errors and continue parsing CODEOWNERS for valid lines. # Allow users to incrementally fix association errors; for CODEOWNERS with many external mappings. associations, _ = ProjectCodeOwners.validate_codeowners_associations( attrs["raw"], self.context["project"]) issue_owner_rules = convert_codeowners_syntax(attrs["raw"], associations, attrs["code_mapping_id"]) # Convert IssueOwner syntax into schema syntax try: validated_data = create_schema_from_issue_owners( issue_owners=issue_owner_rules, project_id=self.context["project"].id) return { **attrs, "schema": validated_data, } except ValidationError as e: raise serializers.ValidationError(e)