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