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