def update_schema(self): """ Updating the schema goes through the following steps: 1. parsing the original codeowner file to get the associations 2. convert the codeowner file to the ownership syntax 3. convert the ownership syntax to the schema """ associations, _ = self.validate_codeowners_associations( self.raw, self.project) issue_owner_rules = convert_codeowners_syntax( codeowners=self.raw, associations=associations, code_mapping=self.repository_project_path_config, ) # Convert IssueOwner syntax into schema syntax try: schema = create_schema_from_issue_owners( issue_owners=issue_owner_rules, project_id=self.project.id) # Convert IssueOwner syntax into schema syntax if schema: self.schema = schema self.save() except ValidationError: return
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 validate(self, attrs): if "raw" not in attrs: return attrs schema = create_schema_from_issue_owners( attrs["raw"], self.context["ownership"].project_id) self._validate_no_codeowners(schema["rules"]) attrs["schema"] = schema return attrs
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, _ = 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(str(e))
def validate(self, attrs): if "raw" not in attrs: 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. existing_raw = self.context["ownership"].raw or "" 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" }) schema = create_schema_from_issue_owners( attrs["raw"], self.context["ownership"].project_id) self._validate_no_codeowners(schema["rules"]) attrs["schema"] = schema return attrs