Completed
Pull Request — master (#535)
by W
02:47
created

ListPackagesAction.run()   F

Complexity

Conditions 17

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
dl 0
loc 31
rs 2.7204
c 1
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like ListPackagesAction.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 pkg_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 requests
17
import six.moves.http_client as http_client
18
19
from st2actions.runners import pythonrunner
20
21
22
class ListPackagesAction(pythonrunner.Action):
23
24
    def run(self, repo, package, distro_version, version, release, api_token, url):
25
        page = 1
26
        packages = []
27
28
        while page < 100:
29
            page_url = url + '?page=' + str(page)
30
            response = requests.get(page_url)
31
32
            if response.status_code != http_client.OK:
33
                raise Exception(response.text)
34
35
            packages += response.json()
36
37
            if len(packages) >= int(response.headers.get('Total', 0)):
38
                break
39
40
            page += 1
41
42
        if package:
43
            packages = [pkg_info for pkg_info in packages if pkg_info['name'] == package]
44
45
        if distro_version:
46
            packages = [pkg_info for pkg_info in packages if pkg_info['distro_version'] == distro_version]
47
48
        if version:
49
            packages = [pkg_info for pkg_info in packages if pkg_info['version'] == version]
50
51
        if release:
52
            packages = [pkg_info for pkg_info in packages if pkg_info['release'] ==release]
53
54
        return sorted(packages, key=lambda x: (x['version'], x['release']), reverse=True)
55