Completed
Pull Request — master (#127)
by Michael
06:54 queued 06:20
created

PullRequest.from_github()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 2
rs 9.4285
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 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 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
from changes import services
0 ignored issues
show
Bug introduced by
The name services does not seem to exist in module changes.
Loading history...
11
12 3
GITHUB_MERGED_PULL_REQUEST = re.compile(
13
    r'^([0-9a-f]{5,40}) Merge pull request #(\w+)'
14
)
15
16
# def gitx(args):
17
#     if changes.debug:
18
#         print('git {}'.format(args))
19
#     return git(shlex.split(args))
20
21
22 3
@attr.s
23 3
class GitRepository(object):
24 3
    VERSION_ZERO = semantic_version.Version('0.0.0')
25
    # TODO: handle multiple remotes (cookiecutter [non-owner maintainer])
26 3
    REMOTE_NAME = 'origin'
27
28 3
    auth_token = attr.ib(default=None)
29
30 3
    @property
31
    def remote_url(self):
32 3
        return git(shlex.split('config --get remote.{}.url'.format(
33
            self.REMOTE_NAME
34
        )))
35
36 3
    @property
37
    def parsed_repo(self):
38 3
        return giturlparse.parse(self.remote_url)
39
40 3
    @property
41
    def repo(self):
42 3
        return self.parsed_repo.repo
43
44 3
    @property
45
    def owner(self):
46 3
        return self.parsed_repo.owner
47
48 3
    @property
49
    def platform(self):
50 3
        return self.parsed_repo.platform
51
52 3
    @property
53
    def is_github(self):
54 3
        return self.parsed_repo.github
55
56 3
    @property
57
    def is_bitbucket(self):
58
        return self.parsed_repo.bitbucket
59
60 3
    @property
61
    def commit_history(self):
62
        return [
63
            commit_message
64
            for commit_message in git(shlex.split(
65
                'log --oneline --no-color'
66
            )).split('\n')
67
            if commit_message
68
        ]
69
70 3
    @property
71
    def first_commit_sha(self):
72
        return git(
73
            'rev-list', '--max-parents=0', 'HEAD'
74
        )
75
76 3
    @property
77
    def tags(self):
78 3
        return git(shlex.split('tag --list')).split('\n')
79
80 3
    @property
81
    def versions(self):
82 3
        versions = []
83 3
        for tag in self.tags:
84 3
            try:
85 3
                versions.append(semantic_version.Version(tag))
86 3
            except ValueError:
87 3
                pass
88 3
        return versions
89
90 3
    @property
91 3
    def latest_version(self) -> semantic_version.Version:
92 3
        return max(self.versions) if self.versions else self.VERSION_ZERO
93
94 3
    def merges_since(self, version=None):
95 3
        if version == semantic_version.Version('0.0.0'):
96
            version = self.first_commit_sha
97
98 3
        revision_range = ' {}..HEAD'.format(version) if version else ''
99
100 3
        merge_commits = git(shlex.split(
101
            'log --oneline --merges --no-color{}'.format(revision_range)
102
        )).split('\n')
103 3
        return merge_commits
104
105 3
    @property
106
    def merges_since_latest_version(self):
107
        return self.merges_since(self.latest_version)
108
109 3
    @property
110
    def files_modified_in_last_commit(self):
111
        return git(shlex.split('diff --name -only --diff -filter=d'))
112
113 3
    @property
114
    def dirty_files(self):
115
        return [
116
            modified_path
117
            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...
118
            if modified_path.startswith(' M')
119
        ]
120
121 3
    @staticmethod
122
    def add(files_to_add):
123 3
        return git(['add'] + files_to_add)
124
125 3
    @staticmethod
126
    def commit(message):
127 3
        return git(shlex.split(
128
            'commit --message="{}" '.format(message)
129
        ))
130
131 3
    @staticmethod
132
    def discard(file_paths):
133 3
        git(['checkout', '--'] + file_paths)
134
135 3
    @staticmethod
136
    def tag(version):
137
        # TODO: signed tags
138 3
        return git(
139
            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...
140
                version=version
141
            ))
142
        )
143
144 3
    @staticmethod
145
    def push():
146 3
        return git(shlex.split('push --tags'))
147
148
149 3
@attr.s
150 3
class GitHubRepository(GitRepository):
151 3
    api = attr.ib(default=None)
152
153 3
    def __attrs_post_init__(self):
154 3
        self.api = services.GitHub(self)
155
156 3
    @cached_property
157
    def labels(self):
158 3
        return self.api.labels()
159
160 3
    @cached_property
161
    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...
162 3
        return [
163
            PullRequest.from_github(self.api.pull_request(pull_request_number))
164
            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...
165
        ]
166
167 3
    @property
168
    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...
169 3
        pull_request_numbers = []
170
171 3
        for commit_msg in self.merges_since(self.latest_version):
172
173 3
            matches = GITHUB_MERGED_PULL_REQUEST.findall(commit_msg)
174
175 3
            if matches:
176 3
                _, pull_request_number = matches[0]
177 3
                pull_request_numbers.append(pull_request_number)
178
179 3
        return pull_request_numbers
180
181 3
    def create_release(self, release):
182 3
        return self.api.create_release(release)
183
184
185 3
@attr.s
186 3
class PullRequest(object):
187 3
    number = attr.ib()
188 3
    title = attr.ib()
189 3
    description = attr.ib()
190 3
    author = attr.ib()
191 3
    body = attr.ib()
192 3
    user = attr.ib()
193 3
    labels = attr.ib(default=attr.Factory(list))
194
195 3
    @property
196
    def description(self):
197 3
        return self.body
198
199 3
    @property
200
    def author(self):
201 3
        return self.user['login']
202
203 3
    @property
204
    def label_names(self):
205 3
        return [
206
            label['name']
207
            for label in self.labels
208
        ]
209
210 3
    @classmethod
211
    def from_github(cls, api_response):
212 3
        return cls(**{
213
            k.name: api_response[k.name]
214
            for k in attr.fields(cls)
215
        })
216
217 3
    @classmethod
218
    def from_number(cls, number):
219
        pass
220