Completed
Push — master ( 7bfe45...907785 )
by Tomaz
04:15
created

issue_to_dict()   C

Complexity

Conditions 9

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 9
dl 0
loc 55
rs 5.4159

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
from st2common.util import isotime
2
3
__all__ = [
4
    'issue_to_dict',
5
    'label_to_dict'
6
]
7
8
9
def issue_to_dict(issue):
10
    result = {}
11
12
    if issue.closed_by:
13
        closed_by = issue.closed_by.name
14
    else:
15
        closed_by = None
16
17
    if issue.user:
18
        author = issue.user.name
19
    else:
20
        author = None
21
22
    if issue.assignee:
23
        assignee = issue.assigne.name
24
    else:
25
        assignee = None
26
27
    if issue.pull_request:
28
        is_pull_request = True
29
    else:
30
        is_pull_request = False
31
32
    result['id'] = issue.id
33
    result['repository'] = issue.repository.name
34
    result['author'] = author
35
    result['assign'] = assignee
36
    result['title'] = issue.title
37
    result['body'] = issue.body
38
    result['url'] = issue.html_url
39
    result['state'] = issue.state
40
    result['is_pull_request'] = is_pull_request
41
42
    if issue.labels:
43
        labels = [label_to_dict(label) for label in issue.labels]
44
    else:
45
        labels = []
46
47
    result['labels'] = labels
48
49
    # Note: We convert it to a serialize type (string)
50
    if issue.created_at:
51
        created_at = isotime.format(issue.created_at)
52
    else:
53
        created_at = None
54
55
    if issue.closed_at:
56
        closed_at = isotime.format(issue.closed_at)
57
    else:
58
        closed_at = None
59
60
    result['created_at'] = created_at
61
    result['closed_at'] = closed_at
62
    result['closed_by'] = closed_by
63
    return result
64
65
66
def label_to_dict(label):
67
    result = {}
68
69
    result['name'] = label.name
70
    result['color'] = label.color
71
    result['url'] = label.url
72
73
    return result
74