| 44 | |
| 45 | |
| 46 | class CustomizeCommitsCz(BaseCommitizen): |
| 47 | bump_pattern = defaults.BUMP_PATTERN |
| 48 | bump_map: Mapping[str, str | None] = defaults.BUMP_MAP |
| 49 | bump_map_major_version_zero: Mapping[str, str | None] = ( |
| 50 | defaults.BUMP_MAP_MAJOR_VERSION_ZERO |
| 51 | ) |
| 52 | change_type_order = defaults.CHANGE_TYPE_ORDER |
| 53 | |
| 54 | def __init__(self, config: BaseConfig) -> None: |
| 55 | super().__init__(config) |
| 56 | |
| 57 | if "customize" not in self.config.settings: |
| 58 | raise MissingCzCustomizeConfigError() |
| 59 | self.custom_settings = self.config.settings["customize"] |
| 60 | |
| 61 | for attr_name in [ |
| 62 | "bump_pattern", |
| 63 | "bump_map", |
| 64 | "bump_map_major_version_zero", |
| 65 | "change_type_order", |
| 66 | "commit_parser", |
| 67 | "changelog_pattern", |
| 68 | "change_type_map", |
| 69 | ]: |
| 70 | if value := self.custom_settings.get(attr_name): |
| 71 | setattr(self, attr_name, value) |
| 72 | |
| 73 | # When the user supplies a custom ``bump_map`` but no matching |
| 74 | # ``bump_map_major_version_zero``, derive the latter so that bumps |
| 75 | # under ``major_version_zero = true`` use the user's mapping rather |
| 76 | # than the (totally unrelated) ``defaults.BUMP_MAP_MAJOR_VERSION_ZERO`` |
| 77 | # fallback. See #1728. |
| 78 | if self.custom_settings.get("bump_map") and not self.custom_settings.get( |
| 79 | "bump_map_major_version_zero" |
| 80 | ): |
| 81 | self.bump_map_major_version_zero = _derive_major_version_zero(self.bump_map) |
| 82 | |
| 83 | def questions(self) -> list[CzQuestion]: |
| 84 | return self.custom_settings.get("questions", [{}]) # type: ignore[return-value] |
| 85 | |
| 86 | def message(self, answers: Mapping[str, Any]) -> str: |
| 87 | message_template = Template(self.custom_settings.get("message_template", "")) |
| 88 | if getattr(Template, "substitute", None): |
| 89 | return message_template.substitute(**answers) # type: ignore[attr-defined,no-any-return] # pragma: no cover # TODO: check if we can fix this |
| 90 | return message_template.render(**answers) |
| 91 | |
| 92 | def example(self) -> str: |
| 93 | return self.custom_settings.get("example") or "" |
| 94 | |
| 95 | def schema_pattern(self) -> str: |
| 96 | return self.custom_settings.get("schema_pattern") or "" |
| 97 | |
| 98 | def schema(self) -> str: |
| 99 | return self.custom_settings.get("schema") or "" |
| 100 | |
| 101 | def info(self) -> str: |
| 102 | if info_path := self.custom_settings.get("info_path"): |
| 103 | return Path(info_path).read_text(encoding=self.config.settings["encoding"]) |
no outgoing calls