This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
forked from etsy/boundary-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.py
executable file
·208 lines (163 loc) · 6.15 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
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
#!/usr/bin/env python3
"""
This script is used for releasing boundary-layer. It can only be run by
administrators of the repository at github.com/etsy/boundary-layer.
To create a release, run:
./release.py
This will:
1. Fetch the remote state of the repository, to make sure that you have an up-to-date copy
2. Run some checks on your local state, comparing it to the remote state, to help prevent you from executing a release that fails to capture your desired changes
3. Check out the local copy of the remote state
4. Create a tag
5. Push the tag
6. Return you to whatever branch you were on previously
Once the tag is created remotely, a github actions workflow should run the publish build, and release the new version of boundary-layer to pypi.
"""
import argparse
from collections import namedtuple
import re
import shlex
import subprocess
import semver
import versioneer
def build_parser():
parser = argparse.ArgumentParser()
bump_type = parser.add_mutually_exclusive_group()
bump_type.add_argument(
'--bump',
choices=['major', 'minor', 'patch'],
default='patch',
help='Select the portion of the version string to bump. default: `patch`'
)
bump_type.add_argument(
'--force-version',
type=semver.VersionInfo.parse,
help='Force the new version to this value. Must be a valid semver.'
)
parser.add_argument(
'--git-remote-name',
default='origin',
help='Name of the git remote from which to release. default: `origin`'
)
parser.add_argument(
'--remote-branch-name',
default='master',
help='Name of the remote branch to use as the basis for the release. default: `master`'
)
return parser
def bump_version(version, bump_type):
bumpers = {
'major': semver.bump_major,
'minor': semver.bump_minor,
'patch': semver.bump_patch,
}
return bumpers[bump_type](str(version))
def check_remote(remote_name):
"""
Print a warning if we are trying to deploy from a local clone of a fork, rather
than from a clone of the primary repository.
"""
output = subprocess.check_output(
shlex.split(f'git remote get-url {remote_name}'),
encoding='utf-8'
).strip()
# let's check this the dumbest way possible
if output not in [
'git@github.com:etsy/boundary-layer',
'git@github.com:etsy/boundary-layer.git',
'https://github.com/etsy/boundary-layer',
'https://www.github.com/etsy/boundary-layer',
]:
print(f'''
WARNING: Remote `{remote_name}` corresponding to `{output}` will not trigger the release build!
We recommend releasing to `git@github.com:etsy/boundary-layer`''')
def fetch_latest(remote_name, branch_name):
print(f'Fetching latest commits from {remote_name}/{branch_name}')
subprocess.check_call(
shlex.split(
f'git fetch {remote_name} {branch_name}'
)
)
def git_checkout(remote_name, branch_name):
ref = f'{remote_name}/{branch_name}' if remote_name else branch_name
print(f'Checking out {ref}')
subprocess.check_call(shlex.split(f'git checkout {ref}'))
def get_git_state():
output_lines = subprocess.check_output(
shlex.split('git status -b --porcelain'),
encoding='utf-8'
).strip().split('\n')
return namedtuple('GitState', ['branch', 'files'])(
branch=output_lines[0],
files=output_lines[1:],
)
def check_git_state(state):
if state.files:
raise Exception(
f'Cannot release: unclean git state:\n{state.files}'
)
def create_and_push_tag(remote_name, tag_name):
print(f'Creating tag {tag_name}')
subprocess.check_call(
shlex.split(
f'''
git tag -a -m "Bumping to version {tag_name}" {tag_name}
'''
)
)
print(f'Pushing tag {tag_name}')
subprocess.check_call(
shlex.split(
f'''
git push --tags {remote_name}
'''
)
)
print('Tag pushed successfully.')
def parse_current_branch(state):
# state.branch has the form
# `## <branch-name>...<upstream> [<ahead|behind> <number-of-commits>]`
# bu the upstream portion and the ahead/behind are all optional
pattern = r'## (?P<branch_name>[a-zA-Z0-9_/-]+)(\.\.\.(?P<remote>[a-z0-9A-Z_/-]+)( \[(?P<ahead_behind>(ahead|behind) \d+)\])?)?'
m = re.match(pattern, state.branch).groupdict()
return namedtuple('GitBranch', ['name', 'remote', 'ahead_behind'])(
m.get('branch_name'),
m.get('remote'),
m.get('ahead_behind')
)
def get_version(bump_type, force_version):
current_version = semver.VersionInfo.parse(versioneer.get_version())
print(f'Current version is: {current_version}')
new_version = str(force_version) if force_version else bump_version(current_version, bump_type)
print('New version: {}'.format(new_version))
okay = input('Continue? [y/N] ')
if not okay.lower().startswith('y'):
raise Exception('Aborted by user')
return new_version
def do_release(*, remote_name, branch_name, bump_type, force_version):
check_remote(remote_name)
fetch_latest(remote_name, branch_name)
current_branch = parse_current_branch(get_git_state())
if current_branch.remote == f'{remote_name}/{branch_name}' and current_branch.ahead_behind:
raise Exception(
f'Local changes found on branch `{current_branch.name}` which tracks `{remote_name}/{branch_name}`! This is probably unintended. Please reconcile your local state before proceeding.'
)
try:
git_checkout(remote_name, branch_name)
check_git_state(get_git_state())
create_and_push_tag(
remote_name,
tag_name=get_version(bump_type, force_version),
)
finally:
if current_branch.name:
git_checkout(None, current_branch.name)
if __name__ == '__main__':
parser = build_parser()
args = parser.parse_args()
do_release(
remote_name=args.git_remote_name,
branch_name=args.remote_branch_name,
bump_type=args.bump,
force_version=args.force_version,
)