Completed
Pull Request — master (#127)
by Michael
07:53 queued 07:21
created

GitHubRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
ccs 19
cts 19
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A pull_request_numbers_since_latest_version() 0 13 3
A create_release() 0 2 1
A pull_requests_since_latest_version() 0 5 2
A labels() 0 3 1
A __attrs_post_init__() 0 2 1
1 3
import re
2
3 3
import attr
0 ignored issues
show
Configuration introduced by
The import attr 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...
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 shlex
6 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...
7 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...
8
9 3
from changes import services
10
11 3
GITHUB_MERGED_PULL_REQUEST = re.compile(
12
    r'^([0-9a-f]{5,40}) Merge pull request #(\w+)'
13
)
14
15
# def gitx(args):
16
#     if changes.debug:
17
#         print('git {}'.format(args))
18
#     return git(shlex.split(args))
19
20
21 3
@attr.s
22 3
class GitRepository(object):
23 3
    VERSION_ZERO = semantic_version.Version('0.0.0')
24
    # TODO: handle multiple remotes (cookiecutter [non-owner maintainer])
25 3
    REMOTE_NAME = 'origin'
26
27 3
    auth_token = attr.ib(default=None)
28
29 3
    @property
30
    def remote_url(self):
31 3
        return git(shlex.split('config --get remote.{}.url'.format(
32
            self.REMOTE_NAME
33
        )))
34
35 3
    @property
36
    def parsed_repo(self):
37 3
        return giturlparse.parse(self.remote_url)
38
39 3
    @property
40
    def repo(self):
41 3
        return self.parsed_repo.repo
42
43 3
    @property
44
    def owner(self):
45 3
        return self.parsed_repo.owner
46
47 3
    @property
48
    def platform(self):
49 3
        return self.parsed_repo.platform
50
51 3
    @property
52
    def is_github(self):
53 3
        return self.parsed_repo.github
54
55 3
    @property
56
    def is_bitbucket(self):
57
        return self.parsed_repo.bitbucket
58
59 3
    @property
60
    def commit_history(self):
61
        return [
62
            commit_message
63
            for commit_message in git(shlex.split(
64
                'log --oneline --no-color'
65
            )).split('\n')
66
            if commit_message
67
        ]
68
69 3
    @property
70
    def first_commit_sha(self):
71
        return git(
72
            'rev-list', '--max-parents=0', 'HEAD'
73
        )
74
75 3
    @property
76
    def tags(self):
77 3
        return git(shlex.split('tag --list')).split('\n')
78
79 3
    @property
80
    def versions(self):
81 3
        versions = []
82 3
        for tag in self.tags:
83 3
            try:
84 3
                versions.append(semantic_version.Version(tag))
85 3
            except ValueError:
86 3
                pass
87 3
        return versions
88
89 3
    @property
90 3
    def latest_version(self) -> semantic_version.Version:
91 3
        return max(self.versions) if self.versions else self.VERSION_ZERO
92
93 3
    def merges_since(self, version=None):
94 3
        if version == semantic_version.Version('0.0.0'):
95
            version = self.first_commit_sha
96
97 3
        revision_range = ' {}..HEAD'.format(version) if version else ''
98
99 3
        merge_commits = git(shlex.split(
100
            'log --oneline --merges --no-color{}'.format(revision_range)
101
        )).split('\n')
102 3
        return merge_commits
103
104 3
    @property
105
    def merges_since_latest_version(self):
106
        return self.merges_since(self.latest_version)
107
108 3
    @property
109
    def files_modified_in_last_commit(self):
110
        return git(shlex.split('diff --name -only --diff -filter=d'))
111
112 3
    @property
113
    def dirty_files(self):
114
        return [
115
            modified_path
116
            for modified_path in git(shlex.split('-c color.status=false status --short --branch'))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (98/79).

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

Loading history...
117
            if modified_path.startswith(' M')
118
        ]
119
120 3
    @staticmethod
121
    def add(files_to_add):
122 3
        return git(['add'] + files_to_add)
123
124 3
    @staticmethod
125
    def commit(message):
126 3
        return git(shlex.split(
127
            'commit --message="{}" '.format(message)
128
        ))
129
130 3
    @staticmethod
131
    def discard(file_paths):
132 3
        git(['checkout', '--'] + file_paths)
133
134 3
    @staticmethod
135
    def tag(version):
136
        # TODO: signed tags
137 3
        return git(
138
            shlex.split('tag --annotate {version} --message="{version}"'.format(
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...
139
                version=version
140
            ))
141
        )
142
143 3
    @staticmethod
144
    def push():
145 3
        return git(shlex.split('push --tags'))
146
147
148 3
@attr.s
149 3
class GitHubRepository(GitRepository):
150 3
    api = attr.ib(default=None)
151
152 3
    def __attrs_post_init__(self):
153 3
        self.api = services.GitHub(self)
154
155
    # TODO: cached_property
156 3
    @property
157
    def labels(self):
158 3
        return self.api.labels()
159
160
    # TODO: cached_property
161 3
    @property
162
    def pull_requests_since_latest_version(self):
0 ignored issues
show
Coding Style Naming introduced by
The name pull_requests_since_latest_version does not conform to the attribute 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...
163 3
        return [
164
            PullRequest.from_github(self.api.pull_request(pull_request_number))
165
            for pull_request_number in self.pull_request_numbers_since_latest_version
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (85/79).

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

Loading history...
166
        ]
167
168 3
    @property
169
    def pull_request_numbers_since_latest_version(self):
0 ignored issues
show
Coding Style Naming introduced by
The name pull_request_numbers_since_latest_version does not conform to the attribute 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...
170 3
        pull_request_numbers = []
171
172 3
        for commit_msg in self.merges_since(self.latest_version):
173
174 3
            matches = GITHUB_MERGED_PULL_REQUEST.findall(commit_msg)
175
176 3
            if matches:
177 3
                _, pull_request_number = matches[0]
178 3
                pull_request_numbers.append(pull_request_number)
179
180 3
        return pull_request_numbers
181
182 3
    def create_release(self, release):
183 3
        return self.api.create_release(release)
184
185
186 3
@attr.s
187 3
class PullRequest(object):
188 3
    number = attr.ib()
189 3
    title = attr.ib()
190 3
    description = attr.ib()
191 3
    author = attr.ib()
192 3
    body = attr.ib()
193 3
    user = attr.ib()
194 3
    labels = attr.ib(default=attr.Factory(list))
195
196 3
    @property
197
    def description(self):
198 3
        return self.body
199
200 3
    @property
201
    def author(self):
202 3
        return self.user['login']
203
204 3
    @property
205
    def label_names(self):
206 3
        return [
207
            label['name']
208
            for label in self.labels
209
        ]
210
211 3
    @classmethod
212
    def from_github(cls, api_response):
213 3
        return cls(**{
214
            k.name: api_response[k.name]
215
            for k in attr.fields(cls)
216
        })
217
218 3
    @classmethod
219
    def from_number(cls, number):
220
        pass