Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] deprecated_chatter check for >= v18 #125

Merged
merged 1 commit into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ Check syntax error for CSV files declared in the manifest
* Check xml-deprecated-data-node
Deprecated <data> node inside <odoo> xml node

* Check xml-deprecated-oe-chatter

Odoo 18 introduced a new XML tag `<chatter/>` which replaces the old way to declare
chatters on form views. For more information, see:
https://github.com/odoo/odoo/pull/156463

* Check xml-deprecated-openerp-node
deprecated <openerp> xml node

Expand Down Expand Up @@ -322,6 +328,10 @@ options:
- https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.1.1/test_repo/broken_module/skip_xml_check_2.xml#L3 Deprecated `<data>` node
- https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.1.1/test_repo/test_module/model_view.xml#L3 Deprecated `<data>` node

* xml-deprecated-oe-chatter

- https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.1.1/test_repo/odoo18_module/views/deprecated_chatter.xml#L6 Please replace old style chatters with the new tag <chatter/>.

* xml-deprecated-openerp-node

- https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.1.1/test_repo/broken_module/model_view.xml#L2 Deprecated `<openerp>` xml node
Expand Down Expand Up @@ -379,8 +389,8 @@ options:

* xml-record-missing-id

- https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.1.1/test_repo/broken_module/model_view.xml#L21 Record has no id, add a unique one to create a new record, use an existing one to update it
- https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.1.1/test_repo/broken_module/model_view.xml#L24 Record has no id, add a unique one to create a new record, use an existing one to update it
- https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.1.1/test_repo/broken_module/model_view.xml#L25 Record has no id, add a unique one to create a new record, use an existing one to update it
- https://github.com/OCA/odoo-pre-commit-hooks/blob/v0.1.1/test_repo/broken_module/model_view.xml#L28 Record has no id, add a unique one to create a new record, use an existing one to update it

* xml-redundant-module-name

Expand Down
2 changes: 2 additions & 0 deletions src/oca_pre_commit_hooks/base_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ def __init__(
enable: Set[str],
disable: Set[str],
module_name: Union[str, None] = None,
module_version: Union[str, None] = None,
autofix: bool = False,
):
self.enable = enable
self.disable = disable
self.autofix = autofix
self.module_name = module_name
self.module_version = module_version

self.checks_errors = []
self.needs_autofix = False
Expand Down
4 changes: 2 additions & 2 deletions src/oca_pre_commit_hooks/checks_odoo_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class ChecksOdooModule(BaseChecker):
def __init__(self, manifest_path, enable, disable, changed=None, verbose=True, autofix=False):
super().__init__(enable, disable, autofix=autofix)
super().__init__(enable, disable, autofix=autofix, module_version=utils.manifest_version(manifest_path))
if not os.path.isfile(manifest_path) or os.path.basename(manifest_path) not in MANIFEST_NAMES:
raise UserWarning( # pragma: no cover
f"Not valid manifest file name {manifest_path} file expected {MANIFEST_NAMES}"
Expand Down Expand Up @@ -136,7 +136,7 @@ def check_xml(self):
if not manifest_datas:
return
checks_obj = checks_odoo_module_xml.ChecksOdooModuleXML(
manifest_datas, self.odoo_addon_name, self.enable, self.disable
manifest_datas, self.odoo_addon_name, self.enable, self.disable, module_version=self.module_version
)
for check_meth in utils.getattr_checks(checks_obj):
check_meth()
Expand Down
26 changes: 24 additions & 2 deletions src/oca_pre_commit_hooks/checks_odoo_module_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Dict, List

from lxml import etree
from packaging.version import Version

from oca_pre_commit_hooks import utils
from oca_pre_commit_hooks.base_checker import BaseChecker
Expand Down Expand Up @@ -42,6 +43,7 @@ class ChecksOdooModuleXML(BaseChecker):
xpath_comment = etree.XPath("//comment()")
xpath_openerp = etree.XPath("/openerp")
xpath_xpath = etree.XPath("//xpath")
xpath_oe_chatter = etree.XPath("//div[hasclass('oe_chatter')]")

tree_deprecate_attrs = {"string", "colors", "fonts"}
xpath_tree_deprecated = etree.XPath(f'.//tree[{"|".join(f"@{a}" for a in tree_deprecate_attrs)}]')
Expand All @@ -56,8 +58,8 @@ class ChecksOdooModuleXML(BaseChecker):
f"/odoo//template//*[{qweb_deprecated_attrs}] | " f"/openerp//template//*[{qweb_deprecated_attrs}]"
)

def __init__(self, manifest_datas, module_name, enable, disable):
super().__init__(enable, disable, module_name)
def __init__(self, manifest_datas, module_name, enable, disable, module_version):
super().__init__(enable, disable, module_name, module_version)
self.manifest_datas = manifest_datas or []
for manifest_data in self.manifest_datas:
try:
Expand Down Expand Up @@ -466,3 +468,23 @@ def check_xml_oe_structure(self):
filepath=manifest_data["filename_short"],
line=xpath_node.sourceline,
)

@utils.only_required_for_checks("xml-deprecated-oe-chatter")
def check_xml_deprecated_oe_chatter(self):
"""* Check xml-deprecated-oe-chatter

Odoo 18 introduced a new XML tag `<chatter/>` which replaces the old way to declare
chatters on form views. For more information, see:
https://github.com/odoo/odoo/pull/156463
"""
if not self.module_version or (self.module_version and self.module_version < Version("18")):
return

for manifest_data in self.manifest_datas:
for xpath_node in self.xpath_oe_chatter(manifest_data["node"]):
self.register_error(
code="xml-deprecated-oe-chatter",
message=("Please replace old style chatters with the new tag <chatter/>."),
filepath=manifest_data["filename_short"],
line=xpath_node.sourceline,
)
16 changes: 16 additions & 0 deletions src/oca_pre_commit_hooks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
import re
import subprocess
import sys
from ast import literal_eval
from contextlib import contextmanager
from functools import lru_cache
from inspect import getmembers, isfunction
from itertools import chain
from pathlib import Path

from packaging.version import InvalidVersion, Version

from oca_pre_commit_hooks.base_checker import BaseChecker

CHECKS_DISABLED_REGEX = re.compile(re.escape("oca-hooks:disable=") + r"([a-z\-,]+)")
Expand Down Expand Up @@ -159,3 +162,16 @@ def get_checks_docstring(check_classes):
checks_found |= set(re.findall(RE_CHECK_DOCSTRING, checks_docstring))
checks_docstring = re.sub(r"( )+\*", "*", checks_docstring)
return checks_found, checks_docstring


def manifest_version(manifest_path):
with open(manifest_path, encoding="utf-8") as manifest_fd:
try:
manifest = literal_eval(manifest_fd.read())
except (ValueError, SyntaxError):
return None

try:
return Version(manifest.get("version"))
except InvalidVersion:
return None
4 changes: 4 additions & 0 deletions test_repo/broken_module/model_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<xpath expr="//span[@t-esc='o.amount_to_text()']/.." position="after">
<attribute name="t-if">o.value</attribute>
</xpath>
<div class="oe_chatter">
<field name="message_follower_ids" groups="base.group_user"/>
<field name="message_ids"/>
</div>
</form>
</field>
</record>
Expand Down
Empty file.
12 changes: 12 additions & 0 deletions test_repo/odoo18_module/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
'name': 'Odoo 18 Module For Tests',
'license': 'AGPL-3',
'author': 'Odoo Community Association (OCA)',
'version': '18.0.1.0.0',
'depends': [
'base',
],
'data': [
'views/deprecated_chatter.xml',
],
}
11 changes: 11 additions & 0 deletions test_repo/odoo18_module/views/deprecated_chatter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<record id="deprecated_chatter" model="ir.ui.view">
<field name="name">deprecated.chatter.view</field>
<field name="model">random.model</field>
<field name="arch" type="xml">
<div class="oe_chatter">
<field name="message_follower_ids" groups="base.group_user"/>
<field name="message_ids"/>
</div>
</field>
</record>
1 change: 1 addition & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"xml-oe-structure-missing-id": 6,
"xml-record-missing-id": 2,
"xml-duplicate-template-id": 9,
"xml-deprecated-oe-chatter": 1,
}


Expand Down