summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/meta_no_info.py
blob: 0e3c04690fa7e3ee8d4619fe52609f050c0af53a (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Implementation of meta-no-info rule."""
# Copyright (c) 2016, Will Thames and contributors
# Copyright (c) 2018, Ansible Project
from __future__ import annotations

from typing import TYPE_CHECKING, Generator

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

if TYPE_CHECKING:
    from typing import Any, Tuple


META_STR_INFO = ("author", "description")
META_INFO = tuple(
    list(META_STR_INFO)
    + [
        "license",
        "min_ansible_version",
        "platforms",
    ]
)


def _platform_info_errors_itr(
    platforms: list[dict[str, str]],
) -> Generator[str, None, None]:
    if not isinstance(platforms, list):
        yield "Platforms should be a list of dictionaries"
        return

    for platform in platforms:
        if not isinstance(platform, dict):
            yield "Platforms should be a list of dictionaries"
        elif "name" not in platform:
            yield "Platform should contain name"


def _galaxy_info_errors_itr(
    galaxy_info: dict[str, Any],
    info_list: tuple[str, ...] = META_INFO,
    str_info_list: tuple[str, ...] = META_STR_INFO,
) -> Generator[str, None, None]:
    for info in info_list:
        g_info = galaxy_info.get(info, False)
        if g_info:
            if info in str_info_list and not isinstance(g_info, str):
                yield f"{info} should be a string"
            elif info == "platforms":
                yield from _platform_info_errors_itr(g_info)
        else:
            yield f"Role info should contain {info}"


class MetaMainHasInfoRule(AnsibleLintRule):
    """meta/main.yml should contain relevant info."""

    id = "meta-no-info"
    str_info = META_STR_INFO
    info = META_INFO
    description = f"meta/main.yml should contain: {', '.join(info)}"
    severity = "HIGH"
    tags = ["metadata"]
    version_added = "v4.0.0"

    def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]:
        if file.kind != "meta":
            return []

        # since Ansible 2.10 we can add a meta/requirements.yml but
        # we only want to match on meta/main.yml
        if file.path.name != "main.yml":
            return []

        galaxy_info = data.get("galaxy_info", False)
        if galaxy_info:
            return [
                self.create_matcherror(message=err, filename=file)
                for err in _galaxy_info_errors_itr(galaxy_info)
            ]
        return [self.create_matcherror(message="No 'galaxy_info' found", filename=file)]