-
Notifications
You must be signed in to change notification settings - Fork 11
/
release.py
157 lines (135 loc) · 4.41 KB
/
release.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
# -*- coding: utf-8 -*-
# pontos/release/release.py
# Copyright (C) 2020-2022 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from argparse import Namespace
import json
from pathlib import Path
from typing import Callable
import requests
from pontos import changelog
from pontos.terminal import error, warning, info
from pontos import version
from .helper import (
build_release_dict,
commit_files,
find_signing_key,
get_current_version,
get_next_dev_version,
get_project_name,
update_version,
)
RELEASE_TEXT_FILE = ".release.md"
def release(
shell_cmd_runner: Callable,
args: Namespace,
*,
path: Path,
version_module: version,
username: str,
token: str,
requests_module: requests,
changelog_module: changelog,
**_kwargs,
) -> bool:
project: str = (
args.project
if args.project is not None
else get_project_name(shell_cmd_runner)
)
space: str = args.space
git_signing_key: str = (
args.git_signing_key
if args.git_signing_key is not None
else find_signing_key(shell_cmd_runner)
)
git_remote_name: str = (
args.git_remote_name if args.git_remote_name is not None else ''
)
git_tag_prefix: str = args.git_tag_prefix
release_version: str = (
args.release_version
if args.release_version is not None
else get_current_version()
)
next_version: str = (
args.next_version
if args.next_version is not None
else get_next_dev_version(release_version)
)
info("Pushing changes")
shell_cmd_runner(f"git push --follow-tags {git_remote_name}")
info(f"Creating release for v{release_version}")
changelog_text: str = path(RELEASE_TEXT_FILE).read_text(encoding='utf-8')
headers = {'Accept': 'application/vnd.github.v3+json'}
base_url = f"https://api.github.com/repos/{space}/{project}/releases"
git_version = f'{git_tag_prefix}{release_version}'
response = requests_module.post(
base_url,
headers=headers,
auth=(username, token),
json=build_release_dict(
git_version,
changelog_text,
name=f"{project} {release_version}",
),
)
if response.status_code != 201:
error(f"Wrong response status code: {response.status_code}")
error(json.dumps(response.text, indent=4, sort_keys=True))
return False
path(RELEASE_TEXT_FILE).unlink()
commit_msg = (
f'Automatic adjustments after release\n\n'
f'* Update to version {next_version}\n'
)
# set to new version add skeleton
changelog_bool = False
if not args.conventional_commits:
change_log_path = path.cwd() / 'CHANGELOG.md'
if args.changelog:
tmp_path = path.cwd() / Path(args.changelog)
if tmp_path.is_file():
change_log_path = tmp_path
else:
warning(f"{tmp_path} is not a file.")
updated = changelog_module.add_skeleton(
markdown=change_log_path.read_text(encoding='utf-8'),
new_version=release_version,
project_name=project,
git_tag_prefix=git_tag_prefix,
git_space=space,
)
change_log_path.write_text(updated, encoding='utf-8')
changelog_bool = True
commit_msg += f'* Add empty changelog after {release_version}'
executed, filename = update_version(
next_version, version_module, develop=True
)
if not executed:
return False
commit_files(
filename,
commit_msg,
shell_cmd_runner,
git_signing_key=git_signing_key,
changelog=changelog_bool,
)
# pushing the new tag
shell_cmd_runner(f"git push --follow-tags {git_remote_name}")
return True