Completed
Push — init ( a47ce7...8d8544 )
by Michael
05:32
created

GitRepository.latest_version()   A

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
import re
2
import shlex
3
4
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
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
import requests
7
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
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
MERGED_PULL_REQUEST = re.compile(
11
    r'^([0-9a-f]{5,40}) Merge pull request #(\w+)'
12
)
13
14
PULL_REQUEST_API = 'https://api.github.com/repos{/owner}{/repo}/pulls{/number}'
15
16
17
class PullRequest:
18
    title = None
19
    description = None
20
    author = None
21
    labels = []
22
23
    def __init__(self, pr_number, committish, title, description, author):
24
        self.number = pr_number
25
        self.committish = committish
26
        self.title = title
27
        self.description = description
28
        self.author = author
29
30
31
class GitRepository:
32
    auth_token = None
33
34
    def __init__(self, url=None):
35
        self.parsed_repo = url or giturlparse.parse(
36
            git(shlex.split('config --get remote.origin.url'))
37
        )
38
        self.commit_history = git(shlex.split(
39
            'log --oneline --merges --no-color'
40
        )).split('\n')
41
42
        self.tags = git(shlex.split('tag --list')).split('\n')
43
44
        self.versions = sorted([
45
            semantic_version.Version(tag)
46
            for tag in self.tags
47
            if tag
48
        ])
49
50
    @property
51
    def latest_version(self):
52
        return self.versions[-1] if self.versions else semantic_version.Version('0.0.0')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (88/79).

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

Loading history...
53
54
    def get_pull_request(self, pr_num):
55
        return requests.get(
56
            uritemplate.expand(
57
                PULL_REQUEST_API,
58
                dict(
59
                    owner=self.owner,
60
                    repo=self.repo,
61
                    number=pr_num
62
                ),
63
            ),
64
            headers={
65
                'Authorization': 'token {}'.format(self.auth_token)
66
            },
67
        ).json()
68
69
    @property
70
    def pull_requests(self):
71
        pull_requests = []
72
73
        for index, commit_msg in enumerate(self.commit_history):
74
            matches = MERGED_PULL_REQUEST.findall(commit_msg)
75
            if matches:
76
                committish, pr_number = matches[0]
77
                title = description = author = None
78
                if self.auth_token:
79
                    pr = self.get_pull_request(pr_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...
80
                    title = pr['title']
81
                    description = pr['body']
82
                    author = pr['user']['login']
83
84
                pull_requests.append(
85
                    PullRequest(
86
                        pr_number,
87
                        committish,
88
                        title,
89
                        description,
90
                        author,
91
                    )
92
                )
93
94
        return pull_requests
95
96
    @property
97
    def repo(self):
98
        return self.parsed_repo.repo
99
100
    @property
101
    def owner(self):
102
        return self.parsed_repo.owner
103
104
    @property
105
    def github(self):
106
        return self.parsed_repo.github
107
108
    @property
109
    def bitbucket(self):
110
        return self.parsed_repo.bitbucket
111