Passed
Push — main ( 05fcff...04a574 )
by Eran
01:28
created

git_commits.create_graph_model()   C

Complexity

Conditions 10

Size

Total Lines 35
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 35
rs 5.9999
c 0
b 0
f 0
cc 10
nop 1

How to fix   Complexity   

Complexity

Complex classes like git_commits.create_graph_model() 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
import operator
2
from pathlib import Path
3
from tempfile import TemporaryDirectory
4
5
import git
6
7
import graphinate
8
9
10
def create_graph_model(repo: git.Repo):
11
    # Fetch all branches from the remote
12
    repo.git.fetch('--all')
13
14
    graph_model = graphinate.GraphModel(name='Git Repository Graph')
15
16
    @graph_model.node(operator.itemgetter('type'), key=operator.itemgetter('id'), label=operator.itemgetter('label'))
17
    def commit():
18
        for b in repo.remote().refs:
19
            for c in repo.iter_commits(b):
20
                branch = b.name.replace('origin/', '')
21
                for char in '-/. ':
22
                    if char in branch:
23
                        branch = branch.replace(char, '_')
24
25
                yield {'id': c.hexsha,
26
                       'type': branch,
27
                       'branch': b.name,
28
                       'label': c.summary}
29
                for f in c.stats.files:
30
                    yield {'id': f,
31
                           'type': 'file',
32
                           'branch': b.name,
33
                           'label': f}
34
35
    @graph_model.edge()
36
    def branch():
37
        for b in repo.remote().refs:
38
            for c in repo.iter_commits(b):
39
                if c.parents:
40
                    yield {'source': c.parents[0].hexsha, 'target': c.hexsha}
41
                    for f in c.stats.files:
42
                        yield {'source': c.hexsha, 'target': f}
43
44
    return graph_model
45
46
47
def git_commits(repo_url: str):
48
    with TemporaryDirectory() as temp_dir:
49
        repo_path = Path(temp_dir) / 'magika'
50
51
        with git.Repo.clone_from(repo_url, repo_path) as repo:
52
            model = create_graph_model(repo)
53
            schema = graphinate.builders.GraphQLBuilder(model).build()
54
            graphinate.graphql.server(schema)
55
56
57
if __name__ == '__main__':
58
    # git_commits(repo_url='https://github.com/google/magika.git')
59
    git_commits(repo_url='https://github.com/erivlis/mappingtools.git')
60