GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( 00c567...aeb2b9 )
by Plexxi
12:28 queued 06:35
created

normalize_pack_version()   A

Complexity

Conditions 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import os
17
import re
18
19
import jsonschema
20
21
from st2common.util import schema as util_schema
22
from st2common.constants.pack import MANIFEST_FILE_NAME
23
from st2common.constants.pack import PACK_REF_WHITELIST_REGEX
24
from st2common.content.loader import MetaLoader
25
26
__all__ = [
27
    'get_pack_ref_from_metadata',
28
    'get_pack_metadata',
29
30
    'validate_config_against_schema',
31
32
    'normalize_pack_version'
33
]
34
35
36
def get_pack_ref_from_metadata(metadata, pack_directory_name=None):
37
    """
38
    Utility function which retrieves pack "ref" attribute from the pack metadata file.
39
40
    If this attribute is not provided, an attempt is made to infer "ref" from the "name" attribute.
41
42
    :rtype: ``str``
43
    """
44
    pack_ref = None
45
46
    # The rules for the pack ref are as follows:
47
    # 1. If ref attribute is available, we used that
48
    # 2. If pack_directory_name is available we use that (this only applies to packs
49
    # which are in sub-directories)
50
    # 2. If attribute is not available, but pack name is and pack name meets the valid name
51
    # criteria, we use that
52
    if metadata.get('ref', None):
53
        pack_ref = metadata['ref']
54
    elif pack_directory_name and re.match(PACK_REF_WHITELIST_REGEX, pack_directory_name):
55
        pack_ref = pack_directory_name
56
    else:
57
        if re.match(PACK_REF_WHITELIST_REGEX, metadata['name']):
58
            pack_ref = metadata['name']
59
        else:
60
            raise ValueError('Pack name "%s" contains invalid characters and "ref" '
61
                             'attribute is not available' % (metadata['name']))
62
63
    return pack_ref
64
65
66
def get_pack_metadata(pack_dir):
67
    """
68
    Return parsed metadata for a particular pack directory.
69
70
    :rtype: ``dict``
71
    """
72
    manifest_path = os.path.join(pack_dir, MANIFEST_FILE_NAME)
73
74
    if not os.path.isfile(manifest_path):
75
        raise ValueError('Pack "%s" is missing %s file' % (pack_dir, MANIFEST_FILE_NAME))
76
77
    meta_loader = MetaLoader()
78
    content = meta_loader.load(manifest_path)
79
    if not content:
80
        raise ValueError('Pack "%s" metadata file is empty' % (pack_dir))
81
82
    return content
83
84
85
def validate_config_against_schema(config_schema, config_object, config_path,
86
                                  pack_name=None):
87
    """
88
    Validate provided config dictionary against the provided config schema
89
    dictionary.
90
    """
91
    pack_name = pack_name or 'unknown'
92
93
    schema = util_schema.get_schema_for_resource_parameters(parameters_schema=config_schema,
94
                                                            allow_additional_properties=True)
95
    instance = config_object
96
97
    try:
98
        cleaned = util_schema.validate(instance=instance, schema=schema,
99
                                       cls=util_schema.CustomValidator, use_default=True,
100
                                       allow_default_none=True)
101
    except jsonschema.ValidationError as e:
102
        attribute = getattr(e, 'path', [])
103
        attribute = '.'.join(attribute)
104
105
        msg = ('Failed validating attribute "%s" in config for pack "%s" (%s): %s' %
106
               (attribute, pack_name, config_path, str(e)))
107
        raise jsonschema.ValidationError(msg)
108
109
    return cleaned
110
111
112
def normalize_pack_version(version):
113
    """
114
    Normalize old, pre StackStorm v2.1 non valid semver version string (e.g. 0.2) to a valid
115
    semver version string (0.2.0).
116
117
    :rtype: ``str``
118
    """
119
    version = str(version)
120
121
    version_seperator_count = version.count('.')
122
    if version_seperator_count == 1:
123
        version = version + '.0'
124
125
    return version
126