File: MetaTagValidRule.py

package info (click to toggle)
ansible-lint 4.1.0%2Bdfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,096 kB
  • sloc: python: 3,373; sh: 4; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,713 bytes parent folder | download
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
# Copyright (c) 2018, Ansible Project

from ansiblelint import AnsibleLintRule
import re


class MetaTagValidRule(AnsibleLintRule):
    id = '702'
    shortdesc = 'Tags must contain lowercase letters and digits only'
    description = (
        'Tags must contain lowercase letters and digits only, '
        'and ``galaxy_tags`` is expected to be a list'
    )
    severity = 'HIGH'
    tags = ['metadata']
    version_added = 'v4.0.0'

    TAG_REGEXP = re.compile('^[a-z0-9]+$')

    def matchplay(self, file, data):
        if file['type'] != 'meta':
            return False

        galaxy_info = data.get('galaxy_info', None)
        if not galaxy_info:
            return False

        tags = []
        results = []

        if 'galaxy_tags' in galaxy_info:
            if isinstance(galaxy_info['galaxy_tags'], list):
                tags += galaxy_info['galaxy_tags']
            else:
                results.append(({'meta/main.yml': data},
                                "Expected 'galaxy_tags' to be a list"))

        if 'categories' in galaxy_info:
            results.append(({'meta/main.yml': data},
                            "Use 'galaxy_tags' rather than 'categories'"))
            if isinstance(galaxy_info['categories'], list):
                tags += galaxy_info['categories']
            else:
                results.append(({'meta/main.yml': data},
                                "Expected 'categories' to be a list"))

        for tag in tags:
            msg = self.shortdesc
            if not re.match(self.TAG_REGEXP, tag):
                results.append(({'meta/main.yml': data},
                                "{}, invalid: '{}'".format(msg, tag)))

        return results