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 ( e931fe...f756b6 )
by Plexxi
17:01 queued 11:01
created

_get_action_alias_db_by_name()   B

Complexity

Conditions 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 31
rs 8.5806
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
18
from st2common.content.loader import ContentPackLoader
19
from st2common.content.loader import MetaLoader
20
from st2common.constants.pack import MANIFEST_FILE_NAME
21
from st2common.util.pack import get_pack_ref_from_metadata
22
from st2common.exceptions.content import ParseException
23
from st2common.bootstrap.aliasesregistrar import AliasesRegistrar
24
from st2common.models.utils.action_alias_utils import extract_parameters_for_action_alias_db
25
from st2common.models.utils.action_alias_utils import extract_parameters
26
from st2tests.pack_resource import BasePackResourceTestCase
27
28
__all__ = [
29
    'BaseActionAliasTestCase'
30
]
31
32
33
class BaseActionAliasTestCase(BasePackResourceTestCase):
34
    """
35
    Base class for testing action aliases.
36
    """
37
38
    action_alias_name = None
39
    action_alias_db = None
40
41
    def setUp(self):
42
        super(BaseActionAliasTestCase, self).setUp()
43
44
        if not self.action_alias_name:
45
            raise ValueError('"action_alias_name" class attribute needs to be provided')
46
47
        self.action_alias_db = self._get_action_alias_db_by_name(name=self.action_alias_name)
48
49
    def assertCommandMatchesExactlyOneFormatString(self, format_strings, command):
50
        """
51
        Assert that the provided command matches exactly one format string from the provided list.
52
        """
53
        matched_format_strings = []
54
55
        for format_string in format_strings:
56
            try:
57
                extract_parameters(format_str=format_string,
58
                                   param_stream=command)
59
            except ParseException:
60
                continue
61
62
            matched_format_strings.append(format_string)
63
64
        if len(matched_format_strings) == 0:
65
            msg = ('Command "%s" didn\'t match any of the provided format strings' % (command))
66
            raise AssertionError(msg)
67
        elif len(matched_format_strings) > 1:
68
            msg = ('Command "%s" matched multiple format strings: %s' %
69
                   (command, ', '.join(matched_format_strings)))
70
            raise AssertionError(msg)
71
72
    def assertExtractedParametersMatch(self, format_string, command, parameters):
73
        """
74
        Assert that the provided command matches the format string.
75
76
        In addition to that, also assert that the parameters which have been extracted from the
77
        user input (command) also match the provided parameters.
78
        """
79
        extracted_params = extract_parameters_for_action_alias_db(
80
            action_alias_db=self.action_alias_db,
81
            format_str=format_string,
82
            param_stream=command)
83
84
        if extracted_params != parameters:
85
            msg = ('Extracted parameters from command string "%s" against format string "%s"'
86
                   ' didn\'t match the provided parameters: ' % (command, format_string))
87
88
            # Note: We intercept the exception so we can can include diff for the dictionaries
89
            try:
90
                self.assertEqual(extracted_params, parameters)
91
            except AssertionError as e:
92
                msg += str(e)
93
94
            raise AssertionError(msg)
95
96
    def _get_action_alias_db_by_name(self, name):
97
        """
98
        Retrieve ActionAlias DB object for the provided alias name.
99
        """
100
        base_pack_path = self._get_base_pack_path()
101
        pack_yaml_path = os.path.join(base_pack_path, MANIFEST_FILE_NAME)
102
103
        if os.path.isfile(pack_yaml_path):
104
            # 1. 1st try to infer pack name from pack metadata file
105
            meta_loader = MetaLoader()
106
            pack_metadata = meta_loader.load(pack_yaml_path)
107
            pack = get_pack_ref_from_metadata(metadata=pack_metadata)
108
        else:
109
            # 2. If pack.yaml is not available, fail back to directory name
110
            # Note: For this to work, directory name needs to match pack name
111
            _, pack = os.path.split(base_pack_path)
112
113
        pack_loader = ContentPackLoader()
114
        registrar = AliasesRegistrar(use_pack_cache=False)
115
116
        aliases_path = pack_loader.get_content_from_pack(pack_dir=base_pack_path,
117
                                                         content_type='aliases')
118
        aliases = registrar._get_aliases_from_pack(aliases_dir=aliases_path)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _get_aliases_from_pack was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
119
        for alias_path in aliases:
120
            action_alias_db = registrar._get_action_alias_db(pack=pack,
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _get_action_alias_db was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
121
                                                             action_alias=alias_path)
122
123
            if action_alias_db.name == name:
124
                return action_alias_db
125
126
        raise ValueError('Alias with name "%s" not found' % (name))
127