1
|
3 |
|
import re |
2
|
|
|
|
3
|
3 |
|
import attr |
|
|
|
|
4
|
3 |
|
import semantic_version |
|
|
|
|
5
|
3 |
|
import shlex |
6
|
3 |
|
import giturlparse |
|
|
|
|
7
|
3 |
|
from plumbum.cmd import git |
|
|
|
|
8
|
|
|
|
9
|
3 |
|
from changes.github import GitHubApi |
10
|
|
|
|
11
|
3 |
|
GITHUB_MERGED_PULL_REQUEST = re.compile( |
12
|
|
|
r'^([0-9a-f]{5,40}) Merge pull request #(\w+)' |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
# def gitx(args): |
16
|
|
|
# if changes.debug: |
17
|
|
|
# print('git {}'.format(args)) |
18
|
|
|
# return git(shlex.split(args)) |
19
|
|
|
|
20
|
|
|
|
21
|
3 |
|
@attr.s |
22
|
3 |
|
class GitRepository(object): |
23
|
3 |
|
VERSION_ZERO = semantic_version.Version('0.0.0') |
24
|
|
|
# TODO: handle multiple remotes (cookiecutter [non-owner maintainer]) |
25
|
3 |
|
REMOTE_NAME = 'origin' |
26
|
|
|
|
27
|
3 |
|
auth_token = attr.ib(default=None) |
28
|
|
|
|
29
|
3 |
|
@property |
30
|
|
|
def remote_url(self): |
31
|
3 |
|
return git(shlex.split('config --get remote.{}.url'.format( |
32
|
|
|
self.REMOTE_NAME |
33
|
|
|
))) |
34
|
|
|
|
35
|
3 |
|
@property |
36
|
|
|
def parsed_repo(self): |
37
|
3 |
|
return giturlparse.parse(self.remote_url) |
38
|
|
|
|
39
|
3 |
|
@property |
40
|
|
|
def repo(self): |
41
|
3 |
|
return self.parsed_repo.repo |
42
|
|
|
|
43
|
3 |
|
@property |
44
|
|
|
def owner(self): |
45
|
3 |
|
return self.parsed_repo.owner |
46
|
|
|
|
47
|
3 |
|
@property |
48
|
|
|
def platform(self): |
49
|
3 |
|
return self.parsed_repo.platform |
50
|
|
|
|
51
|
3 |
|
@property |
52
|
|
|
def is_github(self): |
53
|
3 |
|
return self.parsed_repo.github |
54
|
|
|
|
55
|
3 |
|
@property |
56
|
|
|
def is_bitbucket(self): |
57
|
|
|
return self.parsed_repo.bitbucket |
58
|
|
|
|
59
|
3 |
|
@property |
60
|
|
|
def commit_history(self): |
61
|
|
|
return [ |
62
|
|
|
commit_message |
63
|
|
|
for commit_message in git(shlex.split( |
64
|
|
|
'log --oneline --no-color' |
65
|
|
|
)).split('\n') |
66
|
|
|
if commit_message |
67
|
|
|
] |
68
|
|
|
|
69
|
3 |
|
@property |
70
|
|
|
def first_commit_sha(self): |
71
|
|
|
return git( |
72
|
|
|
'rev-list', '--max-parents=0', 'HEAD' |
73
|
|
|
) |
74
|
|
|
|
75
|
3 |
|
@property |
76
|
|
|
def tags(self): |
77
|
3 |
|
return git(shlex.split('tag --list')).split('\n') |
78
|
|
|
|
79
|
3 |
|
@property |
80
|
|
|
def versions(self): |
81
|
3 |
|
versions = [] |
82
|
3 |
|
for tag in self.tags: |
83
|
3 |
|
try: |
84
|
3 |
|
versions.append(semantic_version.Version(tag)) |
85
|
3 |
|
except ValueError: |
86
|
3 |
|
pass |
87
|
3 |
|
return versions |
88
|
|
|
|
89
|
3 |
|
@property |
90
|
3 |
|
def latest_version(self) -> semantic_version.Version: |
91
|
3 |
|
return max(self.versions) if self.versions else self.VERSION_ZERO |
92
|
|
|
|
93
|
3 |
|
def merges_since(self, version=None): |
94
|
3 |
|
if version == semantic_version.Version('0.0.0'): |
95
|
|
|
version = self.first_commit_sha |
96
|
|
|
|
97
|
3 |
|
revision_range = ' {}..HEAD'.format(version) if version else '' |
98
|
|
|
|
99
|
3 |
|
merge_commits = git(shlex.split( |
100
|
|
|
'log --oneline --merges --no-color{}'.format(revision_range) |
101
|
|
|
)).split('\n') |
102
|
3 |
|
return merge_commits |
103
|
|
|
|
104
|
3 |
|
@property |
105
|
|
|
def merges_since_latest_version(self): |
106
|
|
|
return self.merges_since(self.latest_version) |
107
|
|
|
|
108
|
3 |
|
@property |
109
|
|
|
def files_modified_in_last_commit(self): |
110
|
|
|
return git(shlex.split('diff --name -only --diff -filter=d')) |
111
|
|
|
|
112
|
3 |
|
@property |
113
|
|
|
def dirty_files(self): |
114
|
|
|
return [ |
115
|
|
|
modified_path |
116
|
|
|
for modified_path in git(shlex.split('-c color.status=false status --short --branch')) |
|
|
|
|
117
|
|
|
if modified_path.startswith(' M') |
118
|
|
|
] |
119
|
|
|
|
120
|
3 |
|
@staticmethod |
121
|
|
|
def add(files_to_add): |
122
|
3 |
|
return git(['add'] + files_to_add) |
123
|
|
|
|
124
|
3 |
|
@staticmethod |
125
|
|
|
def commit(message): |
126
|
3 |
|
return git(shlex.split( |
127
|
|
|
'commit --message="{}" '.format(message) |
128
|
|
|
)) |
129
|
|
|
|
130
|
3 |
|
@staticmethod |
131
|
|
|
def discard(file_paths): |
132
|
3 |
|
git(['checkout', '--'] + file_paths) |
133
|
|
|
|
134
|
3 |
|
@staticmethod |
135
|
|
|
def tag(version): |
136
|
|
|
# TODO: signed tags |
137
|
3 |
|
return git( |
138
|
|
|
shlex.split('tag --annotate {version} --message="{version}"'.format( |
|
|
|
|
139
|
|
|
version=version |
140
|
|
|
)) |
141
|
|
|
) |
142
|
|
|
|
143
|
3 |
|
@staticmethod |
144
|
|
|
def push(): |
145
|
3 |
|
return git(shlex.split('push --tags')) |
146
|
|
|
|
147
|
|
|
|
148
|
3 |
|
@attr.s |
149
|
3 |
|
class GitHubRepository(GitRepository): |
150
|
3 |
|
api = attr.ib(default=None) |
151
|
|
|
|
152
|
3 |
|
def __attrs_post_init__(self): |
153
|
3 |
|
self.api = GitHubApi(self) |
154
|
|
|
|
155
|
|
|
# TODO: cached_property |
156
|
3 |
|
@property |
157
|
|
|
def labels(self): |
158
|
3 |
|
return self.api.labels() |
159
|
|
|
|
160
|
|
|
# TODO: cached_property |
161
|
3 |
|
@property |
162
|
|
|
def pull_requests_since_latest_version(self): |
|
|
|
|
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 |
|
|
|
|
166
|
|
|
] |
167
|
|
|
|
168
|
3 |
|
@property |
169
|
|
|
def pull_request_numbers_since_latest_version(self): |
|
|
|
|
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(**api_response) |
214
|
|
|
|
215
|
3 |
|
@classmethod |
216
|
|
|
def from_number(cls, number): |
217
|
|
|
pass |
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.