Completed
Push — fix-ci-failures ( 4b308a...3c0b05 )
by Michael
02:37
created

GitRepository.latest_version()   A

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 2
rs 10
1 3
import re
2 3
import shlex
3
4 3
import semantic_version
0 ignored issues
show
Configuration introduced by
The import semantic_version could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5 3
import uritemplate
0 ignored issues
show
Configuration introduced by
The import uritemplate could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
6 3
import requests
7 3
import giturlparse
0 ignored issues
show
Configuration introduced by
The import giturlparse could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8 3
from plumbum.cmd import git
0 ignored issues
show
Configuration introduced by
The import plumbum.cmd could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
9
10 3
MERGED_PULL_REQUEST = re.compile(
11
    r'^([0-9a-f]{5,40}) Merge pull request #(\w+)'
12
)
13
14 3
PULL_REQUEST_API = 'https://api.github.com/repos{/owner}{/repo}/issues{/number}'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (80/79).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
15
16
17 3
class PullRequest:
18 3
    title = None
19 3
    description = None
20 3
    author = None
21 3
    labels = []
22
23 3
    def __init__(self, **kwargs):
24
        # github
25 3
        self.number = kwargs['number']
26 3
        self.title = kwargs['title']
27 3
        self.description = kwargs['body']
28 3
        self.author = kwargs['user']['login']
29 3
        self.labels = [
30
            label['name']
31
            for label in kwargs['labels']
32
        ]
33
34
35 3
class GitRepository:
36 3
    VERSION_ZERO = semantic_version.Version('0.0.0')
37
38 3
    auth_token = None
39
40 3
    def __init__(self, url=None):
41 3
        self.parsed_repo = (
42
            url or
43
            # TODO: handle multiple remotes (cookiecutter [non-owner maintainer])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (81/79).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
44
            # giturlparse.parse(
45
            #     git(shlex.split('config --get remote.upstream.url'))
46
            # ) or
47
            giturlparse.parse(
48
                git(shlex.split('config --get remote.origin.url'))
49
            )
50
        )
51 3
        self.commit_history = [
52
            commit_message
53
            for commit_message in git(shlex.split(
54
                'log --oneline --no-color'
55
            )).split('\n')
56
            if commit_message
57
        ]
58
59 3
        self.first_commit_sha = git(
60
            'rev-list', '--max-parents=0', 'HEAD'
61
        )
62 3
        self.tags = git(shlex.split('tag --list')).split('\n')
63
64 3
    @property
65
    def versions(self):
66 3
        versions = []
67 3
        for tag in self.tags:
68 3
            try:
69 3
                versions.append(semantic_version.Version(tag))
70 3
            except ValueError:
71 3
                pass
72 3
        return versions
73
74 3
    @property
75 3
    def latest_version(self) -> semantic_version.Version:
76 3
        return max(self.versions) if self.versions else self.VERSION_ZERO
77
78 3
    def merges_since(self, version=None):
79 3
        if version == semantic_version.Version('0.0.0'):
80 3
            version = self.first_commit_sha
81
82 3
        revision_range = ' {}..HEAD'.format(version) if version else ''
83
84 3
        merge_commits = git(shlex.split(
85
            'log --oneline --merges --no-color{}'.format(revision_range)
86
        )).split('\n')
87 3
        return merge_commits
88
89
    # TODO: pull_requests_since(version=None)
90 3
    @property
91
    def changes_since_last_version(self):
92 3
        pull_requests = []
93
94 3
        for index, commit_msg in enumerate(self.merges_since(self.latest_version)):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (83/79).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
95 3
            matches = MERGED_PULL_REQUEST.findall(commit_msg)
96
97 3
            if matches:
98 3
                _, pull_request_number = matches[0]
99
100 3
                pr = self.get_pull_request(pull_request_number)
0 ignored issues
show
Coding Style Naming introduced by
The name pr does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
101 3
                pull_requests.append(
102
                    PullRequest(**pr)
103
                )
104 3
        return pull_requests
105
106
    # github api
107 3
    def get_pull_request(self, pr_num):
108 3
        pull_request_api_url = uritemplate.expand(
109
            PULL_REQUEST_API,
110
            dict(
111
                owner=self.owner,
112
                repo=self.repo,
113
                number=pr_num
114
            ),
115
        )
116
117 3
        return requests.get(
118
            pull_request_api_url,
119
            headers={
120
                'Authorization': 'token {}'.format(self.auth_token)
121
            },
122
        ).json()
123
124 3
    @property
125
    def repo(self):
126 3
        return self.parsed_repo.repo
127
128 3
    @property
129
    def owner(self):
130 3
        return self.parsed_repo.owner
131
132 3
    @property
133
    def github(self):
134 3
        return self.parsed_repo.github
135
136 3
    @property
137
    def bitbucket(self):
138
        return self.parsed_repo.bitbucket
139