summaryrefslogtreecommitdiff
path: root/tools/validate_tags.py
blob: 5749098fb75d0ed0a15494345ba82b13fd3cd2ed (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

""" validate tag applications in projects.yaml

Validate the application of tags that can be pragmatically applied.

Note: Does not automatically update projects.yaml since doing so can reformat
and reorder projects.yaml

"""

import teamstats

import requests
import yaml

import os
import sys

from six.moves.urllib import parse
# List of modules to validate team based tags
team_validators = [
    teamstats.ValidateDiversity,
    teamstats.ValidateSingleVendor,
]

# List of modules to validate repository based tags
repo_validators = []


def main():
    script_path = os.path.abspath(os.path.dirname(__file__))
    filename = os.path.abspath(os.path.join(script_path,
                               '../reference/projects.yaml'))
    if not os.path.isfile(filename):
        sys.exit("Projects.yaml was not found at %s" % (filename))
    with open(filename, 'r') as f:
        teams = yaml.safe_load(f.read())
    for team in teams:
        # Check team based tags
        for validator in team_validators:
            validate(team, teams[team], validator)
        # Check deliverable based tags
        for name, deliverable in teams[team]['deliverables'].items():
            for repo in deliverable['repos']:
                if not repo_exists(repo):
                    continue
                for validator in repo_validators:
                    validate(repo, deliverable, validator)


def validate(name, data, validator):
    tag_name = validator.get_tag_name()
    contains_tag = any([tag_name == tag for tag in
                        data.get('tags', [])])
    if validator.validate(name):
        # should contain tag
        if not contains_tag:
            print_tag_missing(name, tag_name)
    else:
        # should not contain tag
        if contains_tag:
            print_bad_tag(name, tag_name)


def repo_exists(repo):
    """Sometimes the governance docs can get out of sync with repo names."""
    response = requests.get(
        'https://review.openstack.org:443/projects/%s/' %
        parse.quote_plus(repo))
    # strip off first few chars because 'the JSON response body starts with
    # a magic prefix line that must be stripped before feeding the rest of
    # the response body to a JSON parser'
    # https://review.openstack.org/Documentation/rest-api.html
    if response.status_code == 404:
        return False
    return True


def print_tag_missing(name, tag):
    print("* %s should have the tag '%s'" % (name, tag))


def print_bad_tag(name, tag):
    print("* %s should not have the tag '%s'" % (name, tag))

if __name__ == '__main__':
    sys.exit(main())