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.
Test Failed
Push — develop-v1.6.0 ( 9d5181...7efb31 )
by
unknown
04:49
created

get_params_view()   F

Complexity

Conditions 14

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
dl 0
loc 30
rs 2.7581
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A is_immutable() 0 2 1
A is_required() 0 2 1

How to fix   Complexity   

Complexity

Complex classes like get_params_view() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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 copy
17
import six
18
19
from st2common import log as logging
20
from st2common.util import action_db as action_db_util
21
from st2common.util.casts import get_cast
22
23
LOG = logging.getLogger(__name__)
24
25
26
def _merge_param_meta_values(action_meta=None, runner_meta=None):
27
    runner_meta_keys = runner_meta.keys() if runner_meta else []
28
    action_meta_keys = action_meta.keys() if action_meta else []
29
    all_keys = set(runner_meta_keys).union(set(action_meta_keys))
30
31
    merged_meta = {}
32
33
    # ?? Runner immutable param's meta shouldn't be allowed to be modified by action whatsoever.
34
    if runner_meta and runner_meta.get('immutable', False):
35
        merged_meta = runner_meta
36
37
    for key in all_keys:
38
        if key in action_meta_keys and key not in runner_meta_keys:
39
            merged_meta[key] = action_meta[key]
40
        elif key in runner_meta_keys and key not in action_meta_keys:
41
            merged_meta[key] = runner_meta[key]
42
        else:
43
            if key in ['immutable']:
44
                merged_meta[key] = runner_meta.get(key, False) or action_meta.get(key, False)
45
            else:
46
                merged_meta[key] = action_meta.get(key)
47
    return merged_meta
48
49
50
def get_params_view(action_db=None, runner_db=None, merged_only=False):
51
    runner_params = copy.deepcopy(runner_db.runner_parameters) if runner_db else {}
52
    action_params = copy.deepcopy(action_db.parameters) if action_db else {}
53
54
    parameters = set(runner_params.keys()).union(set(action_params.keys()))
55
56
    merged_params = {}
57
    for param in parameters:
58
        merged_params[param] = _merge_param_meta_values(action_meta=action_params.get(param),
59
                                                        runner_meta=runner_params.get(param))
60
61
    if merged_only:
62
        return merged_params
63
64
    def is_required(param_meta):
65
        return param_meta.get('required', False)
66
67
    def is_immutable(param_meta):
68
        return param_meta.get('immutable', False)
69
70
    immutable = {param for param in parameters if is_immutable(merged_params.get(param))}
71
    required = {param for param in parameters if is_required(merged_params.get(param))}
72
    required = required - immutable
73
    optional = parameters - required - immutable
74
75
    required_params = {k: merged_params[k] for k in required}
76
    optional_params = {k: merged_params[k] for k in optional}
77
    immutable_params = {k: merged_params[k] for k in immutable}
78
79
    return (required_params, optional_params, immutable_params)
80
81
82
def cast_params(action_ref, params, cast_overrides=None):
0 ignored issues
show
Documentation introduced by
Empty function docstring
Loading history...
83
    """
84
    """
85
    params = params or {}
86
    action_db = action_db_util.get_action_by_ref(action_ref)
87
88
    if not action_db:
89
        raise ValueError('Action with ref "%s" doesn\'t exist' % (action_ref))
90
91
    action_parameters_schema = action_db.parameters
92
    runnertype_db = action_db_util.get_runnertype_by_name(action_db.runner_type['name'])
93
    runner_parameters_schema = runnertype_db.runner_parameters
94
    # combine into 1 list of parameter schemas
95
    parameters_schema = {}
96
    if runner_parameters_schema:
97
        parameters_schema.update(runner_parameters_schema)
98
    if action_parameters_schema:
99
        parameters_schema.update(action_parameters_schema)
100
    # cast each param individually
101
    for k, v in six.iteritems(params):
102
        parameter_schema = parameters_schema.get(k, None)
103
        if not parameter_schema:
104
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No schema.', k, v)
105
            continue
106
        parameter_type = parameter_schema.get('type', None)
107
        if not parameter_type:
108
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No type.', k, v)
109
            continue
110
        # Pick up cast from teh override and then from the system suppied ones.
111
        cast = cast_overrides.get(parameter_type, None) if cast_overrides else None
112
        if not cast:
113
            cast = get_cast(cast_type=parameter_type)
114
        if not cast:
115
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No cast for %s.', k, v,
116
                      parameter_type)
117
            continue
118
        LOG.debug('Casting param: %s of type %s to type: %s', v, type(v), parameter_type)
119
120
        try:
121
            params[k] = cast(v)
122
        except Exception as e:
123
            v_type = type(v).__name__
124
            msg = ('Failed to cast value "%s" (type: %s) for parameter "%s" of type "%s": %s. '
125
                   'Perhaphs the value is of an invalid type?' %
126
                   (v, v_type, k, parameter_type, str(e)))
127
            raise ValueError(msg)
128
129
    return params
130
131
132
def validate_action_parameters(action_ref, inputs):
133
    input_set = set(inputs.keys())
134
135
    # Get the list of action and runner parameters.
136
    parameters = action_db_util.get_action_parameters_specs(action_ref)
137
138
    # Check required parameters that have no default defined.
139
    required = set([param for param, meta in six.iteritems(parameters)
140
                    if meta.get('required', False) and 'default' not in meta])
141
142
    requires = sorted(required.difference(input_set))
143
144
    # Check unexpected parameters:
145
    unexpected = sorted(input_set.difference(set(parameters.keys())))
146
147
    return requires, unexpected
148