Completed
Pull Request — master (#423)
by
unknown
02:54 queued 50s
created

GetScanExecutions()   C

Complexity

Conditions 8

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 53
rs 5.8625
cc 8

How to fix   Long Method   

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:

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 requests
17
import datetime
18
19
def GetScanExecutions(config, scan_id):
20
   """
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
21
   The template class for
22
23
   Returns: An blank Dict.
24
   
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
25
   Raises:
26
   ValueError: On lack of key in config.
27
   """
28
   
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
29
   results = {}
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
30
31
   url = "https://{}/api/scan/v1/scans/{}".format(config['api_host'], scan_id)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
32
   payload = None
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
Unused Code introduced by
The variable payload seems to be unused.
Loading history...
33
   headers = { "Accept": "application/json" }
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
Coding Style introduced by
No space allowed after bracket
headers = "Accept": "application/json"
^
Loading history...
Coding Style introduced by
No space allowed before bracket
headers = "Accept": "application/json"
^
Loading history...
34
35
   try:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
36
      r = requests.get(url,
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 6 were found.
Loading history...
37
                       headers=headers,
38
                       auth=(config['api_key'], ''))
39
      r.raise_for_status()
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 6 were found.
Loading history...
40
   except:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
41
      raise ValueError("HTTP error: %s on %s" % (r.status_code, r.url))
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 6 were found.
Loading history...
42
43
   try:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
44
      data = r.json()
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 6 were found.
Loading history...
45
   except:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
46
      raise ValueError("Invalid JSON")
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 6 were found.
Loading history...
47
   else:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
48
      results = { 'latest_complete': None, 'scans': [] }
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 6 were found.
Loading history...
Coding Style introduced by
No space allowed after bracket
results = 'latest_complete': None, 'scans': []
^
Loading history...
Coding Style introduced by
No space allowed before bracket
results = 'latest_complete': None, 'scans': []
^
Loading history...
49
      for item in data:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 6 were found.
Loading history...
50
         create_date = datetime.datetime.fromtimestamp(item['create_date'])
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 9 were found.
Loading history...
51
         finish_date = datetime.datetime.fromtimestamp(item['finish_date'])
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 9 were found.
Loading history...
52
         duration = finish_date - create_date
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 9 were found.
Loading history...
53
54
         results['scans'].append({ "id": item['id'],
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 9 were found.
Loading history...
Coding Style introduced by
No space allowed after bracket
results['scans'].append({ "id": item['id'],
^
Loading history...
55
                                   "active":      item['active'],
56
                                   "create_date": create_date.strftime('%Y-%m-%d %H:%M:%S'),
57
                                   "finish_date": finish_date.strftime('%Y-%m-%d %H:%M:%S'),
58
                                   "duration":    str(duration)
59
                                })
60
         
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
61
   # This list can be very large, limit to the last 10.
62
   results['scans'].sort(reverse=True)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
63
   results['scans'] = results['scans'][0:10]
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
64
65
   # Find the latest ccmpleted scan..
66
   for item in results['scans']:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
67
      if item['active'] is False:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 6 were found.
Loading history...
68
         results['latest_complete'] = item['id']
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 9 were found.
Loading history...
69
         break
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 9 were found.
Loading history...
70
71
   return results
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 3 were found.
Loading history...
72
73