CreateAlertAction.run()   F
last analyzed

Complexity

Conditions 18

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
c 0
b 0
f 0
dl 0
loc 88
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like CreateAlertAction.run() 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
15
from lib.actions import OpsGenieBaseAction
16
17
18
class CreateAlertAction(OpsGenieBaseAction):
19
    def run(self, message, teams=None, alias=None,
20
            description=None, recipients=None, actions=None,
21
            source="StackStorm", tags=None, details=None,
22
            entity=None, user=None, note=None):
23
        """
24
        Create alert in OpsGenie.
25
26
        Args:
27
        - message: Alert text limited to 130 characters
28
        - teams: List of team names which will be responsible for the alert
29
        - alias: Used for alert deduplication.
30
        - description: detailed description of the alert.
31
        - recipients: Optional user, group, schedule or escalation names.
32
        - actions: A comma separated list of actions that can be executed.
33
        - source: Field to specify source of alert.
34
        - tags: A comma separated list of labels attached to the alert.
35
        - details: Set of user defined properties.
36
        - entity: The entity the alert is related to.
37
        - user: Default owner of the execution.
38
        - note: Additional alert note.
39
40
        Returns:
41
        - dict: Data returned by OpsGenie.
42
43
        Raises:
44
        - ValueError: If description or message is too long.
45
        """
46
47
        if len(message) > 130:
48
            raise ValueError("Message length ({}) is over 130 chars".format(
49
                len(message)))
50
51
        body = {"apiKey": self.api_key,
52
                "message": message}
53
54
        if teams:
55
            body["teams"] = teams
56
57
        if alias:
58
            if len(alias) > 512:
59
                raise ValueError("alias is too long, can't be over 512 chars.")
60
            else:
61
                body["alias"] = alias
62
63
        if description:
64
            if len(description) > 15000:
65
                raise ValueError("Description is too long, can't be over 15000 chars.")
66
            else:
67
                body["description"] = description
68
69
        if recipients:
70
            body["recipients"] = recipients
71
72
        if actions:
73
            body["actions"] = actions
74
75
        if source:
76
            if len(source) > 512:
77
                raise ValueError("Source is too long, can't be over 512 chars.")
78
            else:
79
                body["source"] = source
80
81
        if tags:
82
            body["tags"] = tags
83
84
        if details:
85
            body["details"] = details
86
87
        if entity:
88
            if len(entity) > 512:
89
                raise ValueError("Entity is too long, can't be over 512 chars.")
90
            else:
91
                body["entity"] = entity
92
93
        if user:
94
            if len(user) > 100:
95
                raise ValueError("User is too long, can't be over 100 chars.")
96
            else:
97
                body['user'] = user
98
99
        if note:
100
            body['note'] = note
101
102
        data = self._req("POST",
103
                         "v1/json/alert",
104
                         body=body)
105
106
        return data
107