1
|
3 |
|
import re |
2
|
3 |
|
import shlex |
3
|
|
|
|
4
|
3 |
|
import attr |
|
|
|
|
5
|
3 |
|
import semantic_version |
|
|
|
|
6
|
3 |
|
import uritemplate |
|
|
|
|
7
|
3 |
|
import requests |
8
|
3 |
|
import giturlparse |
|
|
|
|
9
|
|
|
from plumbum.cmd import git |
|
|
|
|
10
|
3 |
|
|
11
|
|
|
MERGED_PULL_REQUEST = re.compile( |
12
|
|
|
r'^([0-9a-f]{5,40}) Merge pull request #(\w+)' |
13
|
|
|
) |
14
|
3 |
|
|
15
|
|
|
GITHUB_PULL_REQUEST_API = ( |
16
|
|
|
'https://api.github.com/repos{/owner}{/repo}/issues{/number}' |
17
|
3 |
|
) |
18
|
3 |
|
|
19
|
3 |
|
|
20
|
3 |
|
def changes_to_release_type(repository): |
21
|
3 |
|
pull_request_labels = set() |
22
|
|
|
changes = repository.changes_since_last_version |
23
|
3 |
|
|
24
|
|
|
for change in changes: |
25
|
3 |
|
for label in change.labels: |
26
|
3 |
|
pull_request_labels.add(label) |
27
|
3 |
|
|
28
|
3 |
|
change_descriptions = [ |
29
|
3 |
|
'\n'.join([change.title, change.description]) for change in changes |
30
|
|
|
] |
31
|
|
|
|
32
|
|
|
current_version = repository.latest_version |
33
|
|
|
if 'BREAKING CHANGE' in change_descriptions: |
34
|
|
|
return 'major', Release.BREAKING_CHANGE, current_version.next_major() |
35
|
3 |
|
elif 'enhancement' in pull_request_labels: |
36
|
3 |
|
return 'minor', Release.FEATURE, current_version.next_minor() |
37
|
|
|
elif 'bug' in pull_request_labels: |
38
|
3 |
|
return 'patch', Release.FIX, current_version.next_patch() |
39
|
|
|
else: |
40
|
3 |
|
return None, Release.NO_CHANGE, current_version |
41
|
3 |
|
|
42
|
|
|
|
43
|
|
|
class Release: |
44
|
|
|
NO_CHANGE = 'nochanges' |
45
|
|
|
BREAKING_CHANGE = 'breaking' |
46
|
|
|
FEATURE = 'feature' |
47
|
|
|
FIX = 'fix' |
48
|
|
|
|
49
|
|
|
version = '<current_version>' |
50
|
|
|
name = None |
51
|
3 |
|
title = "{formatted string}" |
52
|
|
|
title_format = '' |
53
|
|
|
description = "(optional)Release description" |
54
|
|
|
changes = [] |
55
|
|
|
|
56
|
|
|
@property |
57
|
|
|
def title(self): |
58
|
|
|
return |
59
|
3 |
|
|
60
|
|
|
@attr.s |
61
|
|
|
class PullRequest: |
62
|
3 |
|
number = attr.ib() |
63
|
|
|
title = attr.ib() |
64
|
3 |
|
description = attr.ib() |
65
|
|
|
# default is 'body' key |
66
|
3 |
|
author = attr.ib() |
67
|
3 |
|
labels = attr.ib(default=attr.Factory(list)) |
68
|
3 |
|
|
69
|
3 |
|
@classmethod |
70
|
3 |
|
def from_github(cls, api_response): |
71
|
3 |
|
return PullRequest( |
72
|
3 |
|
number = api_response['number'], |
|
|
|
|
73
|
|
|
title = api_response['title'], |
|
|
|
|
74
|
3 |
|
description = api_response['body'], |
|
|
|
|
75
|
3 |
|
author = api_response['user']['login'], |
|
|
|
|
76
|
3 |
|
labels = [ |
|
|
|
|
77
|
|
|
label['name'] |
78
|
3 |
|
for label in api_response['labels'] |
79
|
3 |
|
# label['colour'] => https://gist.github.com/MicahElliott/719710 |
|
|
|
|
80
|
3 |
|
], |
81
|
|
|
# labels need a description => map for default github tags |
82
|
3 |
|
) |
83
|
|
|
|
84
|
3 |
|
|
85
|
|
|
@attr.s |
86
|
|
|
class GitRepository: |
87
|
3 |
|
VERSION_ZERO = semantic_version.Version('0.0.0') |
88
|
|
|
# TODO: handle multiple remotes (cookiecutter [non-owner maintainer]) |
89
|
|
|
REMOTE_NAME = 'origin' |
90
|
3 |
|
|
91
|
|
|
auth_token = attr.ib(default=None) |
92
|
3 |
|
|
93
|
|
|
@property |
94
|
3 |
|
def parsed_repo(self): |
95
|
3 |
|
return giturlparse.parse(self.remote_url) |
96
|
|
|
|
97
|
3 |
|
@property |
98
|
3 |
|
def remote_url(self): |
99
|
|
|
return git(shlex.split('config --get remote.{}.url'.format( |
100
|
3 |
|
self.REMOTE_NAME |
101
|
3 |
|
))) |
102
|
|
|
|
103
|
|
|
@property |
104
|
3 |
|
def commit_history(self): |
105
|
|
|
return [ |
106
|
|
|
commit_message |
107
|
3 |
|
for commit_message in git(shlex.split( |
108
|
3 |
|
'log --oneline --no-color' |
109
|
|
|
)).split('\n') |
110
|
|
|
if commit_message |
111
|
|
|
] |
112
|
|
|
|
113
|
|
|
@property |
114
|
|
|
def first_commit_sha(self): |
115
|
|
|
return git( |
116
|
|
|
'rev-list', '--max-parents=0', 'HEAD' |
117
|
3 |
|
) |
118
|
|
|
|
119
|
|
|
@property |
120
|
|
|
def tags(self): |
121
|
|
|
return git(shlex.split('tag --list')).split('\n') |
122
|
|
|
|
123
|
|
|
@property |
124
|
3 |
|
def versions(self): |
125
|
|
|
versions = [] |
126
|
3 |
|
for tag in self.tags: |
127
|
|
|
try: |
128
|
3 |
|
versions.append(semantic_version.Version(tag)) |
129
|
|
|
except ValueError: |
130
|
3 |
|
pass |
131
|
|
|
return versions |
132
|
3 |
|
|
133
|
|
|
@property |
134
|
3 |
|
def latest_version(self) -> semantic_version.Version: |
135
|
|
|
return max(self.versions) if self.versions else self.VERSION_ZERO |
136
|
3 |
|
|
137
|
|
|
def merges_since(self, version=None): |
138
|
|
|
if version == semantic_version.Version('0.0.0'): |
139
|
|
|
version = self.first_commit_sha |
140
|
|
|
|
141
|
|
|
revision_range = ' {}..HEAD'.format(version) if version else '' |
142
|
|
|
|
143
|
|
|
merge_commits = git(shlex.split( |
144
|
|
|
'log --oneline --merges --no-color{}'.format(revision_range) |
145
|
|
|
)).split('\n') |
146
|
|
|
return merge_commits |
147
|
|
|
|
148
|
|
|
# TODO: pull_requests_since(version=None) |
149
|
|
|
@property |
150
|
|
|
def changes_since_last_version(self): |
151
|
|
|
pull_requests = [] |
152
|
|
|
|
153
|
|
|
for index, commit_msg in enumerate(self.merges_since(self.latest_version)): |
|
|
|
|
154
|
|
|
matches = MERGED_PULL_REQUEST.findall(commit_msg) |
155
|
|
|
|
156
|
|
|
if matches: |
157
|
|
|
_, pull_request_number = matches[0] |
158
|
|
|
|
159
|
|
|
pull_requests.append(PullRequest.from_github( |
160
|
|
|
self.github_pull_request(pull_request_number) |
161
|
|
|
)) |
162
|
|
|
return pull_requests |
163
|
|
|
|
164
|
|
|
def github_labels(self): |
165
|
|
|
# GET /repos/:owner/:repo/labels |
166
|
|
|
""" |
167
|
|
|
[ |
168
|
|
|
{ |
169
|
|
|
"id": 208045946, |
170
|
|
|
"url": "https://api.github.com/repos/octocat/Hello-World/labels/bug", |
|
|
|
|
171
|
|
|
"name": "bug", |
172
|
|
|
"color": "f29513", |
173
|
|
|
"default": true |
174
|
|
|
} |
175
|
|
|
] |
176
|
|
|
""" |
177
|
|
|
pass |
178
|
|
|
|
179
|
|
|
def github_pull_request(self, pr_num): |
180
|
|
|
pull_request_api_url = uritemplate.expand( |
181
|
|
|
GITHUB_PULL_REQUEST_API, |
182
|
|
|
dict( |
183
|
|
|
owner=self.owner, |
184
|
|
|
repo=self.repo, |
185
|
|
|
number=pr_num |
186
|
|
|
), |
187
|
|
|
) |
188
|
|
|
|
189
|
|
|
return requests.get( |
190
|
|
|
pull_request_api_url, |
191
|
|
|
headers={ |
192
|
|
|
'Authorization': 'token {}'.format(self.auth_token) |
193
|
|
|
}, |
194
|
|
|
).json() |
195
|
|
|
|
196
|
|
|
@property |
197
|
|
|
def repo(self): |
198
|
|
|
return self.parsed_repo.repo |
199
|
|
|
|
200
|
|
|
@property |
201
|
|
|
def owner(self): |
202
|
|
|
return self.parsed_repo.owner |
203
|
|
|
|
204
|
|
|
@property |
205
|
|
|
def github(self): |
206
|
|
|
return self.parsed_repo.github |
207
|
|
|
|
208
|
|
|
@property |
209
|
|
|
def bitbucket(self): |
210
|
|
|
return self.parsed_repo.bitbucket |
211
|
|
|
|
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.