Completed
Push — ux ( fb8692 )
by Michael
09:14
created

pull_request_numbers_since_latest_version()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
1
import re
2
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
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 shlex
6
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
from cached_property import cached_property
0 ignored issues
show
Configuration introduced by
The import cached_property 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 halo import Halo
0 ignored issues
show
Configuration introduced by
The import halo 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
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...
10
11
from changes import services
0 ignored issues
show
Bug introduced by
The name services does not seem to exist in module changes.
Loading history...
12
13
GITHUB_MERGED_PULL_REQUEST = re.compile(
14
    r'^([0-9a-f]{5,40}) Merge pull request #(\w+)'
15
)
16
17
# def gitx(args):
18
#     if changes.debug:
19
#         print('git {}'.format(args))
20
#     return git(shlex.split(args))
21
22
23
@attr.s
24
class GitRepository(object):
25
    VERSION_ZERO = semantic_version.Version('0.0.0')
26
    # TODO: handle multiple remotes (cookiecutter [non-owner maintainer])
27
    REMOTE_NAME = 'origin'
28
29
    auth_token = attr.ib(default=None)
30
31
    @property
32
    def remote_url(self):
33
        return git(shlex.split('config --get remote.{}.url'.format(
34
            self.REMOTE_NAME
35
        )))
36
37
    @property
38
    def parsed_repo(self):
39
        return giturlparse.parse(self.remote_url)
40
41
    @property
42
    def repo(self):
43
        return self.parsed_repo.repo
44
45
    @property
46
    def owner(self):
47
        return self.parsed_repo.owner
48
49
    @property
50
    def platform(self):
51
        return self.parsed_repo.platform
52
53
    @property
54
    def is_github(self):
55
        return self.parsed_repo.github
56
57
    @property
58
    def is_bitbucket(self):
59
        return self.parsed_repo.bitbucket
60
61
    @property
62
    def commit_history(self):
63
        return [
64
            commit_message
65
            for commit_message in git(shlex.split(
66
                'log --oneline --no-color'
67
            )).split('\n')
68
            if commit_message
69
        ]
70
71
    @property
72
    def first_commit_sha(self):
73
        return git(
74
            'rev-list', '--max-parents=0', 'HEAD'
75
        )
76
77
    @property
78
    def tags(self):
79
        return git(shlex.split('tag --list')).split('\n')
80
81
    @property
82
    def versions(self):
83
        versions = []
84
        for tag in self.tags:
85
            try:
86
                versions.append(semantic_version.Version(tag))
87
            except ValueError:
88
                pass
89
        return versions
90
91
    @property
92
    def latest_version(self) -> semantic_version.Version:
93
        return max(self.versions) if self.versions else self.VERSION_ZERO
94
95
    def merges_since(self, version=None):
96
        if version == semantic_version.Version('0.0.0'):
97
            version = self.first_commit_sha
98
99
        revision_range = ' {}..HEAD'.format(version) if version else ''
100
101
        merge_commits = git(shlex.split(
102
            'log --oneline --merges --no-color{}'.format(revision_range)
103
        )).split('\n')
104
        return merge_commits
105
106
    @property
107
    def merges_since_latest_version(self):
108
        return self.merges_since(self.latest_version)
109
110
    @property
111
    def files_modified_in_last_commit(self):
112
        return git(shlex.split('diff --name -only --diff -filter=d'))
113
114
    @property
115
    def dirty_files(self):
116
        return [
117
            modified_path
118
            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...
119
            if modified_path.startswith(' M')
120
        ]
121
122
    @staticmethod
123
    def add(files_to_add):
124
        return git(['add'] + files_to_add)
125
126
    @staticmethod
127
    def commit(message):
128
        return git(shlex.split(
129
            'commit --message="{}" '.format(message)
130
        ))
131
132
    @staticmethod
133
    def discard(file_paths):
134
        git(['checkout', '--'] + file_paths)
135
136
    @staticmethod
137
    def tag(version):
138
        # TODO: signed tags
139
        return git(
140
            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...
141
                version=version
142
            ))
143
        )
144
145
    @staticmethod
146
    def push():
147
        return git(shlex.split('push --tags'))
148
149
150
@attr.s
151
class GitHubRepository(GitRepository):
152
    api = attr.ib(default=None)
153
154
    def __attrs_post_init__(self):
155
        self.api = services.GitHub(self)
156
157
    @cached_property
158
    def labels(self):
159
        return self.api.labels()
160
161
    @cached_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 method 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
        title = 'Fetching pull requests'
164
        with Halo(text=title, spinner='hearts') as spinner:
165
            pull_requests = []
166
            pull_request_numbers = 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 (81/79).

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

Loading history...
167
            for i, pull_request_number in enumerate(pull_request_numbers):
168
                pull_requests.append(
169
                    PullRequest.from_github(
170
                        self.api.pull_request(
171
                            pull_request_number
172
                        )
173
                    )
174
                )
175
                spinner.text = '{} [{}/{}]'.format(
176
                    title,
177
                    i,
178
                    len(pull_request_numbers)
179
                )
180
                # TODO: catch connection failures
181
182
        spinner.succeed('{title} [{num_pr}/{num_pr}]'.format(
183
            title=title,
184
            num_pr=len(pull_request_numbers)
185
        ))
186
        return pull_requests
187
188
    @property
189
    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...
190
        pull_request_numbers = []
191
192
        for commit_msg in self.merges_since(self.latest_version):
193
194
            matches = GITHUB_MERGED_PULL_REQUEST.findall(commit_msg)
195
196
            if matches:
197
                _, pull_request_number = matches[0]
198
                pull_request_numbers.append(pull_request_number)
199
200
        return pull_request_numbers
201
202
    def create_release(self, release):
203
        return self.api.create_release(release)
204
205
206
@attr.s
207
class PullRequest(object):
208
    number = attr.ib()
209
    title = attr.ib()
210
    description = attr.ib()
211
    author = attr.ib()
212
    body = attr.ib()
213
    user = attr.ib()
214
    labels = attr.ib(default=attr.Factory(list))
215
216
    @property
217
    def description(self):
218
        return self.body
219
220
    @property
221
    def author(self):
222
        return self.user['login']
223
224
    @property
225
    def label_names(self):
226
        return [
227
            label['name']
228
            for label in self.labels
229
        ]
230
231
    @classmethod
232
    def from_github(cls, api_response):
233
        return cls(**{
234
            k.name: api_response[k.name]
235
            for k in attr.fields(cls)
236
        })
237
238
    @classmethod
239
    def from_number(cls, number):
240
        pass
241