Passed
Pull Request — master (#3425)
by Anthony
05:11
created

MatchAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 14
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 5 1
A run() 0 7 1
1
import os
2
3
from st2common.runners.base_action import Action
4
from st2client.models.action_alias import ActionAliasMatch
5
from st2client.client import Client
6
7
8
class MatchAction(Action):
9
    def __init__(self, config=None):
10
        super(MatchAction, self).__init__(config=config)
11
        api_url = os.environ.get('ST2_ACTION_API_URL', None)
12
        token = os.environ.get('ST2_ACTION_AUTH_TOKEN', None)
13
        self.client = Client(api_url=api_url, token=token)
14
15
    def run(self, text):
16
        alias_match = ActionAliasMatch()
17
        alias_match.command = text
18
        matches = self.client.managers['ActionAlias'].match(alias_match)
19
        return {
20
            'alias': _format_match(matches[0]),
21
            'representation': matches[1]
22
        }
23
24
25
def _format_match(match):
26
    return {
27
        'name': match.name,
28
        'pack': match.pack,
29
        'action_ref': match.action_ref
30
    }
31