summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/meta_incorrect.py
blob: 160b8ec08a3167e087e568a3fed5b643c13b272d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Implementation of meta-incorrect rule."""
# Copyright (c) 2018, Ansible Project
from __future__ import annotations

from typing import TYPE_CHECKING

from ansiblelint.constants import LINE_NUMBER_KEY
from ansiblelint.rules import AnsibleLintRule

if TYPE_CHECKING:
    from typing import Any

    from ansiblelint.errors import MatchError
    from ansiblelint.file_utils import Lintable


class MetaChangeFromDefaultRule(AnsibleLintRule):
    """meta/main.yml default values should be changed."""

    id = "meta-incorrect"
    field_defaults = [
        ("author", "your name"),
        ("description", "your description"),
        ("company", "your company (optional)"),
        ("license", "license (GPLv2, CC-BY, etc)"),
        ("license", "license (GPL-2.0-or-later, MIT, etc)"),
    ]
    values = ", ".join(sorted({f[0] for f in field_defaults}))
    description = (
        f"You should set appropriate values in meta/main.yml for these fields: {values}"
    )
    severity = "HIGH"
    tags = ["metadata"]
    version_added = "v4.0.0"

    def matchyaml(self, file: Lintable) -> list[MatchError]:
        if file.kind != "meta" or not file.data:
            return []

        galaxy_info = file.data.get("galaxy_info", None)
        if not galaxy_info:
            return []

        results = []
        for field, default in self.field_defaults:
            value = galaxy_info.get(field, None)
            if value and value == default:
                results.append(
                    self.create_matcherror(
                        filename=file,
                        linenumber=file.data[LINE_NUMBER_KEY],
                        message=f"Should change default metadata: {field}",
                    )
                )

        return results