Skip to content

Commit a9b6425

Browse files
Merge pull request #1146 from adamtheturtle/generic-release
Use release script from click-pathlib
2 parents d760f5f + 3247a5a commit a9b6425

File tree

4 files changed

+44
-80
lines changed

4 files changed

+44
-80
lines changed

admin/release.py

Lines changed: 39 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,64 @@
11
"""
2-
Release the next version of VWS Python.
2+
Release the next version.
33
"""
44

55
import datetime
66
import os
77
import subprocess
88
from pathlib import Path
99

10-
from dulwich.porcelain import add, commit, push, tag_list
11-
from dulwich.repo import Repo
12-
from github import Github, Repository, UnknownObjectException
10+
from github import Github, Repository
1311

1412

15-
def get_version() -> str:
13+
def get_version(github_repository: Repository) -> str:
1614
"""
17-
Return the next version of VWS Python.
15+
Return the next version.
1816
This is today’s date in the format ``YYYY.MM.DD.MICRO``.
1917
``MICRO`` refers to the number of releases created on this date,
2018
starting from ``0``.
2119
"""
2220
utc_now = datetime.datetime.utcnow()
2321
date_format = '%Y.%m.%d'
2422
date_str = utc_now.strftime(date_format)
25-
local_repository = Repo('.')
26-
tag_labels = tag_list(repo=local_repository)
27-
tag_labels = [item.decode() for item in tag_labels]
23+
tag_labels = [tag.name for tag in github_repository.get_tags()]
2824
today_tag_labels = [
2925
item for item in tag_labels if item.startswith(date_str)
3026
]
3127
micro = int(len(today_tag_labels))
32-
return '{date}.{micro}'.format(date=date_str, micro=micro)
28+
new_version = f'{date_str}.{micro}'
29+
return new_version
3330

3431

35-
def update_changelog(version: str) -> None:
32+
def update_changelog(version: str, github_repository: Repository) -> None:
3633
"""
3734
Add a version title to the changelog.
3835
"""
39-
changelog = Path('CHANGELOG.rst')
40-
changelog_contents = changelog.read_text()
36+
changelog_path = Path('CHANGELOG.rst')
37+
branch = 'master'
38+
changelog_content_file = github_repository.get_contents(
39+
path=str(changelog_path),
40+
ref=branch,
41+
)
42+
changelog_bytes = changelog_content_file.decoded_content
43+
changelog_contents = changelog_bytes.decode('utf-8')
4144
new_changelog_contents = changelog_contents.replace(
4245
'Next\n----',
43-
'Next\n----\n\n{version}\n------------'.format(version=version),
46+
f'Next\n----\n\n{version}\n------------',
4447
)
45-
changelog.write_text(new_changelog_contents)
46-
47-
48-
def create_github_release(
49-
repository: Repository,
50-
version: str,
51-
) -> None:
52-
"""
53-
Create a tag and release on GitHub.
54-
"""
55-
changelog_url = (
56-
'https://vws-python.readthedocs.io/en/latest/changelog.html'
57-
)
58-
repository.create_git_tag_and_release(
59-
tag=version,
60-
tag_message='Release ' + version,
61-
release_name='Release ' + version,
62-
release_message='See ' + changelog_url,
63-
type='commit',
64-
object=repository.get_commits()[0].sha,
48+
github_repository.update_file(
49+
path=str(changelog_path),
50+
message=f'Update for release {version}',
51+
content=new_changelog_contents,
52+
sha=changelog_content_file.sha,
6553
)
6654

6755

68-
def commit_and_push(version: str, repository: Repository) -> None:
69-
"""
70-
Commit and push all changes.
71-
"""
72-
local_repository = Repo('.')
73-
paths = ['CHANGELOG.rst']
74-
_, ignored = add(paths=paths)
75-
assert not ignored
76-
message = b'Update for release ' + version.encode('utf-8')
77-
commit(message=message)
78-
branch_name = 'master'
79-
push(
80-
repo=local_repository,
81-
remote_location=repository.ssh_url,
82-
refspecs=branch_name.encode('utf-8'),
83-
)
84-
85-
86-
def get_repo(github_token: str, github_owner: str) -> Repository:
87-
"""
88-
Get a GitHub repository.
89-
"""
90-
github_client = Github(github_token)
91-
try:
92-
github_user_or_org = github_client.get_organization(github_owner)
93-
except UnknownObjectException:
94-
github_user_or_org = github_client.get_user(github_owner)
95-
96-
return github_user_or_org.get_repo('vws-python')
97-
98-
99-
def build() -> None:
56+
def build_and_upload_to_pypi() -> None:
10057
"""
10158
Build source and binary distributions.
10259
"""
10360
for args in (
104-
['git', 'fetch'],
61+
['git', 'fetch', '--tags'],
10562
['rm', '-rf', 'build'],
10663
['python', 'setup.py', 'sdist', 'bdist_wheel'],
10764
['twine', 'upload', '-r', 'pypi', 'dist/*'],
@@ -115,15 +72,22 @@ def main() -> None:
11572
"""
11673
github_token = os.environ['GITHUB_TOKEN']
11774
github_owner = os.environ['GITHUB_OWNER']
118-
repository = get_repo(github_token=github_token, github_owner=github_owner)
119-
version_str = get_version()
120-
update_changelog(version=version_str)
121-
commit_and_push(version=version_str, repository=repository)
122-
create_github_release(
123-
repository=repository,
124-
version=version_str,
75+
github_repository_name = os.environ['GITHUB_REPOSITORY_NAME']
76+
github_client = Github(github_token)
77+
github_repository = github_client.get_repo(
78+
full_name_or_id=f'{github_owner}/{github_repository_name}',
79+
)
80+
version_str = get_version(github_repository=github_repository)
81+
update_changelog(version=version_str, github_repository=github_repository)
82+
github_repository.create_git_tag_and_release(
83+
tag=version_str,
84+
tag_message='Release ' + version_str,
85+
release_name='Release ' + version_str,
86+
release_message='See CHANGELOG.rst',
87+
type='commit',
88+
object=github_repository.get_commits()[0].sha,
12589
)
126-
build()
90+
build_and_upload_to_pypi()
12791

12892

12993
if __name__ == '__main__':

admin/release.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ set -ex
55
# Perform a release.
66
# See the release process documentation for details.
77
cd "$(mktemp -d)"
8-
git clone [email protected]:"${GITHUB_OWNER}"/vws-python.git
9-
cd vws-python
8+
git clone [email protected]:"${GITHUB_OWNER}"/"${GITHUB_REPOSITORY_NAME}".git
9+
cd "${GITHUB_REPOSITORY_NAME}"
1010
virtualenv -p python3 release
1111
source release/bin/activate
1212
pip install --editable .[dev]
13-
python admin/release.py
13+
python admin/release.py "${GITHUB_TOKEN}" "${GITHUB_OWNER}"

dev-requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ check-manifest==0.40
1111
codecov==2.0.15 # Upload coverage data
1212
doc8==0.8.0
1313
dodgy==0.1.9 # Look for uploaded secrets
14-
dulwich==0.19.14
1514
flake8-commas==2.0.0 # Require silicon valley commas
1615
flake8-quotes==2.1.1 # Require single quotes
1716
flake8==3.7.9 # Lint

docs/source/release-process.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Store your PyPI password:
6666
.. substitution-prompt:: bash
6767

6868
export GITHUB_OWNER=|github-owner|
69-
curl https://raw.githubusercontent.com/"$GITHUB_OWNER"/|github-repository|/master/admin/release.sh | bash
69+
export GITHUB_REPOSITORY_NAME=|github-repository|
70+
curl https://raw.githubusercontent.com/"$GITHUB_OWNER"/"$GITHUB_REPOSITORY_NAME"/master/admin/release.sh | bash
7071

7172
.. _GitHub access token instructions: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line/

0 commit comments

Comments
 (0)