Completed
Pull Request — master (#159)
by Michael
02:21
created

git_lines()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
ccs 2
cts 2
cp 1
crap 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 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 as git_command
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
17 3
def git(command):
18 3
    return git_command(shlex.split(command))
19
20
21 3
def git_lines(command):
22 3
    return git(command).splitlines()
23
24
25 3
@attr.s
26 3
class GitRepository(object):
27 3
    VERSION_ZERO = semantic_version.Version('0.0.0')
28
    # TODO: handle multiple remotes (for non-owner maintainer workflows)
29 3
    REMOTE_NAME = 'origin'
30
31 3
    auth_token = attr.ib(default=None)
32
33 3
    @property
34
    def remote_url(self):
35 3
        return git('config --get remote.{}.url'.format(
36
            self.REMOTE_NAME
37
        ))
38
39 3
    @property
40
    def parsed_repo(self):
41 3
        return giturlparse.parse(self.remote_url)
42
43 3
    @property
44
    def repo(self):
45 3
        return self.parsed_repo.repo
46
47 3
    @property
48
    def owner(self):
49 3
        return self.parsed_repo.owner
50
51 3
    @property
52
    def platform(self):
53 3
        return self.parsed_repo.platform
54
55 3
    @property
56
    def is_github(self):
57 3
        return self.parsed_repo.github
58
59 3
    @property
60
    def is_bitbucket(self):
61
        return self.parsed_repo.bitbucket
62
63 3
    @property
64
    def commit_history(self):
65
        return [
66
            commit_message
67
            for commit_message in git_lines(
68
                'log --oneline --no-color'
69
            )
70
            if commit_message
71
        ]
72
73 3
    @property
74
    def first_commit_sha(self):
75
        return git('rev-list --max-parents=0 HEAD')
76
77 3
    @property
78
    def tags(self):
79 3
        return git_lines('tag --list')
80
81 3
    @property
82
    def versions(self):
83 3
        versions = []
84 3
        for tag in self.tags:
85 3
            try:
86 3
                versions.append(semantic_version.Version(tag))
87
            except ValueError:
88
                pass
89 3
        return versions
90
91 3
    @property
92 3
    def latest_version(self) -> semantic_version.Version:
93 3
        return max(self.versions) if self.versions else self.VERSION_ZERO
94
95 3
    def merges_since(self, version=None):
96 3
        if version == semantic_version.Version('0.0.0'):
97
            version = self.first_commit_sha
98
99 3
        revision_range = ' {}..HEAD'.format(version) if version else ''
100
101 3
        merge_commits = git(
102
            'log --oneline --merges --no-color{}'.format(revision_range)
103
        ).split('\n')
104 3
        return merge_commits
105
106 3
    @property
107
    def merges_since_latest_version(self):
108
        return self.merges_since(self.latest_version)
109
110 3
    @property
111
    def files_modified_in_last_commit(self):
112
        return git('diff --name -only --diff -filter=d')
113
114 3
    @property
115
    def dirty_files(self):
116
        return [
117
            modified_path
118
            for modified_path in git('-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 (85/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 3
    @staticmethod
123
    def add(files_to_add):
124 3
        return git('add {}'.format(' '.join(files_to_add)))
125
126 3
    @staticmethod
127
    def commit(message):
128 3
        return git(
129
            'commit --message="{}" '.format(message)
130
        )
131
132 3
    @staticmethod
133
    def discard(file_paths):
134 3
        return git('checkout -- {}'.format(' '.join(file_paths)))
135
136 3
    @staticmethod
137
    def tag(version):
138
        # TODO: signed tags
139 3
        return git(
140
            'tag --annotate {version} --message="{version}"'.format(
141
                version=version
142
            )
143
        )
144
145 3
    @staticmethod
146
    def push():
147 3
        return git('push --tags')
148
149
150 3
@attr.s
151 3
class GitHubRepository(GitRepository):
152 3
    api = attr.ib(default=None)
153
154 3
    def __attrs_post_init__(self):
155 3
        self.api = services.GitHub(self)
156
157 3
    @cached_property
158
    def labels(self):
159 3
        return self.api.labels()
160
161 3
    @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 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
221