process_labels()   F
last analyzed

Complexity

Conditions 12

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
c 1
b 0
f 0
dl 0
loc 45
rs 2.7855

How to fix   Complexity   

Complexity

Complex classes like process_labels() 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
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Copyright (c) The Spyder Development Team
4
#
5
# Licensed under the terms of the MIT License
6
# (See LICENSE.txt for details)
7
# -----------------------------------------------------------------------------
8
"""Get/Create github labels from a text file."""
9
10
# yapf: disable
11
12
# Local imports
13
from loghub.core.repo import GitHubRepo
14
15
# yapf: enable
16
17
18
def process_labels(username, password, token, action, repo, filename):
19
    """Get or update labels on a given repository."""
20
    # Separator
21
    s = ';'
22
23
    # Instantiate Github API
24
    gh = GitHubRepo(
25
        username=username,
26
        password=password,
27
        token=token,
28
        repo=repo, )
29
30
    labels = gh.labels()
31
32
    if action == 'get':
33
        print('Getting labels from {0}\n'.format(repo))
34
        labels = sorted((l.name, l.color) for l in labels)
35
        data = ''.join('{0}{1}{2}\n'.format(l, s, c) for (l, c) in labels)
36
37
        with open(filename, 'wt') as f:
38
            f.write(data[:-1])
39
40
    elif action == 'update':
41
        print('Updating labels on {0}\n'.format(repo))
42
        with open(filename, 'r') as f:
43
            data = f.read()
44
45
        lines = [line for line in data.split('\n') if line]
46
        labels = []
47
        for line in lines:
48
            parts = [p for p in line.strip().split(s)]
49
            if len(parts) == 3:
50
                old_name, new_name, color = parts
51
            else:
52
                new_name, color = parts
53
                old_name = new_name
54
55
            label_dict = {
56
                'new_name': new_name,
57
                'old_name': old_name,
58
                'color': color
59
            }
60
            labels.append(label_dict)
61
62
        gh.set_labels(labels)
63