-
Notifications
You must be signed in to change notification settings - Fork 236
/
vcs_helpers.py
256 lines (205 loc) · 7.08 KB
/
vcs_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""VCS Helpers
"""
import logging
import os
import re
from datetime import date
from functools import wraps
from pathlib import Path, PurePath
from typing import Optional, Tuple
from urllib.parse import urlsplit
from git import GitCommandError, InvalidGitRepositoryError, Repo, TagObject
from git.exc import BadName
from .errors import GitError, HvcsRepoParseError
from .helpers import LoggedFunction
from .settings import config
try:
repo = Repo(".", search_parent_directories=True)
except InvalidGitRepositoryError:
repo = None
logger = logging.getLogger(__name__)
def check_repo(func):
"""Decorator which checks that we are in a git repository."""
@wraps(func)
def function_wrapper(*args, **kwargs):
if repo is None:
raise GitError("Not in a valid git repository")
return func(*args, **kwargs)
return function_wrapper
@check_repo
def get_commit_log(from_rev=None):
"""Yield all commit messages from last to first."""
rev = None
if from_rev:
try:
repo.commit(from_rev)
rev = f"...{from_rev}"
except BadName:
logger.debug(
f"Reference {from_rev} does not exist, considering entire history"
)
for commit in repo.iter_commits(rev):
yield (commit.hexsha, commit.message.replace("\r\n", "\n"))
@check_repo
@LoggedFunction(logger)
def get_last_version(skip_tags=None) -> Optional[str]:
"""
Find the latest version using repo tags.
:return: A string containing a version number.
"""
skip_tags = skip_tags or []
def version_finder(tag):
if isinstance(tag.commit, TagObject):
return tag.tag.tagged_date
return tag.commit.committed_date
for i in sorted(repo.tags, reverse=True, key=version_finder):
match = re.search(r"\d+\.\d+\.\d+", i.name)
if match and i.name not in skip_tags:
return match.group(0) # Return only numeric vesion like 1.2.3
return None
@check_repo
@LoggedFunction(logger)
def get_version_from_tag(tag_name: str) -> Optional[str]:
"""
Get the git commit hash corresponding to a tag name.
:param tag_name: Name of the git tag (i.e. 'v1.0.0')
:return: sha1 hash of the commit
"""
for i in repo.tags:
if i.name == tag_name:
return i.commit.hexsha
return None
@check_repo
@LoggedFunction(logger)
def get_repository_owner_and_name() -> Tuple[str, str]:
"""
Check the 'origin' remote to get the owner and name of the remote repository.
:return: A tuple of the owner and name.
"""
url = repo.remote("origin").url
split_url = urlsplit(url)
# Select the owner and name as regex groups
parts = re.search(r"[:/]([^:]+)/([^/]*?)(.git)?$", split_url.path)
if not parts:
raise HvcsRepoParseError
return parts.group(1), parts.group(2)
@check_repo
def get_current_head_hash() -> str:
"""
Get the commit hash of the current HEAD.
:return: The commit hash.
"""
return repo.head.commit.name_rev.split(" ")[0]
@check_repo
@LoggedFunction(logger)
def commit_new_version(version: str):
"""
Commit the file containing the version number variable.
The commit message will be generated from the configured template.
:param version: Version number to be used in the commit message.
"""
from .history import load_version_declarations
commit_subject = config.get("commit_subject")
message = commit_subject.format(version=version)
# Add an extended message if one is configured
commit_message = config.get("commit_message")
if commit_message:
message += "\n\n"
message += commit_message.format(version=version)
commit_author = config.get(
"commit_author",
"semantic-release <semantic-release>",
)
for declaration in load_version_declarations():
git_path = PurePath(os.getcwd(), declaration.path).relative_to(repo.working_dir)
repo.git.add(str(git_path))
return repo.git.commit(m=message, author=commit_author)
@check_repo
@LoggedFunction(logger)
def update_changelog_file(version: str, content_to_add: str):
"""
Update changelog file with changelog for the release.
:param version: The release version number, as a string.
:param content_to_add: The release notes for the version.
"""
changelog_file = config.get("changelog_file")
changelog_placeholder = config.get("changelog_placeholder")
git_path = Path(os.getcwd(), changelog_file)
if not git_path.exists():
original_content = f"# Changelog\n\n{changelog_placeholder}\n"
logger.warning(f"Changelog file not found: {git_path} - creating it.")
else:
original_content = git_path.read_text()
if changelog_placeholder not in original_content:
logger.warning(
f"Placeholder '{changelog_placeholder}' not found "
f"in changelog file {git_path} - skipping change."
)
return
updated_content = original_content.replace(
changelog_placeholder,
"\n".join(
[
changelog_placeholder,
"",
f"## v{version} ({date.today():%Y-%m-%d})",
content_to_add,
]
),
)
git_path.write_text(updated_content)
repo.git.add(str(git_path.relative_to(repo.working_dir)))
@check_repo
@LoggedFunction(logger)
def tag_new_version(version: str):
"""
Create a new tag with the version number, prefixed with v by default.
:param version: The version number used in the tag as a string.
"""
tag_format = config.get("tag_format")
tag = tag_format.format(version=version)
return repo.git.tag("-a", tag, m=tag)
@check_repo
@LoggedFunction(logger)
def push_new_version(
auth_token: str = None,
owner: str = None,
name: str = None,
branch: str = "master",
domain: str = "github.com",
):
"""
Run git push and git push --tags.
:param auth_token: Authentication token used to push.
:param owner: Organisation or user that owns the repository.
:param name: Name of repository.
:param branch: Branch to push to
:param server_url: Name of the server. Will be used to identify a gitlab instance.
:raises GitError: if GitCommandError is raised
"""
server = "origin"
if auth_token:
token = auth_token
if config.get("hvcs") == "gitlab":
token = "gitlab-ci-token:" + token
actor = os.environ.get("GITHUB_ACTOR")
if actor:
server = f"https://{actor}:{token}@{domain}/{owner}/{name}.git"
else:
server = f"https://{token}@{domain}/{owner}/{name}.git"
try:
repo.git.push(server, branch)
repo.git.push("--tags", server, branch)
except GitCommandError as error:
message = str(error)
if auth_token:
message = message.replace(auth_token, "[AUTH_TOKEN]")
raise GitError(message)
@check_repo
@LoggedFunction(logger)
def checkout(branch: str):
"""
Check out the given branch in the local repository.
:param branch: The branch to checkout.
"""
return repo.git.checkout(branch)