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