Completed
Push — status ( 924687 )
by Michael
09:41
created

GitRepository.versions()   A

Complexity

Conditions 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
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}/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
class PullRequest:
18
    title = None
19
    description = None
20
    author = None
21
    labels = []
22
23
    def __init__(self, **kwargs):
24
        # github
25
        self.number = kwargs['number']
26
        self.title = kwargs['title']
27
        self.description = kwargs['body']
28
        self.author = kwargs['user']['login']
29
        self.labels = [
30
            label['name']
31
            for label in kwargs['labels']
32
        ]
33
34
35
class GitRepository:
36
    auth_token = None
37
38
    def __init__(self, url=None):
39
        self.parsed_repo = (
40
            url or
41
            # 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...
42
            # giturlparse.parse(
43
            #     git(shlex.split('config --get remote.upstream.url'))
44
            # ) or
45
            giturlparse.parse(
46
                git(shlex.split('config --get remote.origin.url'))
47
            )
48
        )
49
        self.commit_history = [
50
            commit_message
51
            for commit_message in git(shlex.split(
52
                'log --oneline --no-color'
53
            )).split('\n')
54
            if commit_message
55
        ]
56
57
        self.first_commit_sha = git(
58
            'rev-list', '--max-parents=0', 'HEAD'
59
        )
60
        self.tags = git(shlex.split('tag --list')).split('\n')
61
62
    @property
63
    def versions(self):
64
        versions = []
65
        for tag in self.tags:
66
            try:
67
                versions.append(semantic_version.Version(tag))
68
            except ValueError:
69
                pass
70
        return versions
71
72
    @property
73
    def latest_version(self) -> semantic_version.Version:
74
        return max(self.versions) 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 (89/79).

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

Loading history...
75
76
    def merges_since(self, version=None):
77
        print('merged since')
78
        print(version)
79
        
80
        if version == semantic_version.Version('0.0.0'):
81
            print('not a real version')
82
        #     version = self.first_commit_sha
83
84
        revision_range = ' {}..HEAD'.format(version) if version else ''
85
86
        merge_commits = git(shlex.split(
87
            'log --oneline --merges --no-color{}'.format(revision_range)
88
        )).split('\n')
89
        return merge_commits
90
91
    # TODO: pull_requests_since(version=None)
92
    @property
93
    def changes_since_last_version(self):
94
        pull_requests = []
95
96
        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...
97
            matches = MERGED_PULL_REQUEST.findall(commit_msg)
98
99
            if matches:
100
                _, pull_request_number = matches[0]
101
102
                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...
103
                pull_requests.append(
104
                    PullRequest(
105
                        **pr
106
                    )
107
                )
108
        return pull_requests
109
110
    # github api
111
    def get_pull_request(self, pr_num):
112
        return requests.get(
113
            uritemplate.expand(
114
                PULL_REQUEST_API,
115
                dict(
116
                    owner=self.owner,
117
                    repo=self.repo,
118
                    number=pr_num
119
                ),
120
            ),
121
            headers={
122
                'Authorization': 'token {}'.format(self.auth_token)
123
            },
124
        ).json()
125
126
    @property
127
    def repo(self):
128
        return self.parsed_repo.repo
129
130
    @property
131
    def owner(self):
132
        return self.parsed_repo.owner
133
134
    @property
135
    def github(self):
136
        return self.parsed_repo.github
137
138
    @property
139
    def bitbucket(self):
140
        return self.parsed_repo.bitbucket
141