Skip to content

Commit 212ab64

Browse files
authored
Merge pull request #109 autonumerate and publish python package
2 parents 0daa246 + 770465d commit 212ab64

File tree

5 files changed

+267
-6
lines changed

5 files changed

+267
-6
lines changed

.github/scripts/__init__.py

Whitespace-only changes.

.github/scripts/increment_version.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/bin/env python
2+
import argparse
3+
from dataclasses import dataclass
4+
5+
from packaging.version import Version
6+
7+
SETUP_PY_PATH = "setup.py"
8+
DEFAULT_CHANGELOG_PATH = "CHANGELOG.md"
9+
MARKER = "# AUTOVERSION"
10+
11+
12+
@dataclass(init=False)
13+
class VersionLine:
14+
old_line: str
15+
major: int
16+
minor: int
17+
patch: int
18+
pre: int
19+
20+
def __init__(self, old_line: str, version_str: str):
21+
self.old_line = old_line
22+
23+
version = Version(version_str)
24+
self.major = version.major
25+
self.minor = version.minor
26+
self.micro = version.micro
27+
28+
if version.pre is None:
29+
self.pre = 0
30+
else:
31+
self.pre = version.pre[1]
32+
33+
def increment(self, part_name: str, with_beta: bool):
34+
if part_name == 'minor':
35+
self.increment_minor(with_beta)
36+
elif part_name == 'patch' or part_name == 'micro':
37+
self.increment_micro(with_beta)
38+
else:
39+
raise Exception("unexpected increment type: '%s'" % part_name)
40+
41+
def increment_minor(self, with_beta: bool):
42+
if with_beta:
43+
if self.pre == 0 or self.micro != 0:
44+
self.increment_minor(False)
45+
self.pre += 1
46+
return
47+
48+
if self.micro == 0 and self.pre > 0:
49+
self.pre = 0
50+
return
51+
52+
self.minor += 1
53+
self.micro = 0
54+
self.pre = 0
55+
56+
def increment_micro(self, with_beta: bool):
57+
if with_beta:
58+
if self.pre == 0:
59+
self.increment_micro(False)
60+
self.pre += 1
61+
return
62+
63+
if self.pre > 0:
64+
self.pre = 0
65+
return
66+
67+
self.micro += 1
68+
69+
def __str__(self):
70+
if self.pre > 0:
71+
pre = "b%s" % self.pre
72+
else:
73+
pre = ""
74+
75+
return "%s.%s.%s%s" % (self.major, self.minor, self.micro, pre)
76+
77+
def version_line_with_mark(self):
78+
return 'version="%s", %s' % (str(self), MARKER)
79+
80+
81+
def extract_version(setup_py_content: str):
82+
version_line = ""
83+
for line in setup_py_content.splitlines():
84+
if MARKER in line:
85+
version_line = line
86+
break
87+
88+
if version_line == "":
89+
raise Exception("Not found version line")
90+
91+
version_line = version_line.strip()
92+
93+
parts = version_line.split('"')
94+
version_part = parts[1]
95+
96+
return VersionLine(old_line=version_line, version_str=version_part)
97+
98+
99+
def increment_version_at_setup_py(setup_py_path: str, inc_type: str, with_beta: bool) -> str:
100+
with open(setup_py_path, "rt") as f:
101+
setup_content = f.read()
102+
103+
version = extract_version(setup_content)
104+
version.increment(inc_type, with_beta)
105+
setup_content = setup_content.replace(version.old_line, version.version_line_with_mark())
106+
107+
with open(setup_py_path, "w") as f:
108+
f.write(setup_content)
109+
110+
return str(version)
111+
112+
113+
def add_changelog_version(changelog_path, version: str):
114+
with open(changelog_path, "rt") as f:
115+
content = f.read()
116+
content = content.strip()
117+
118+
if content.startswith("##"):
119+
return
120+
121+
content = """## %s ##
122+
%s
123+
""" % (version, content)
124+
with open(changelog_path, "w") as f:
125+
f.write(content)
126+
127+
128+
def main():
129+
parser = argparse.ArgumentParser()
130+
parser.add_argument(
131+
"--inc-type",
132+
default="minor",
133+
help="increment version type: patch or minor",
134+
choices=["minor", "patch"],
135+
)
136+
parser.add_argument(
137+
"--beta",
138+
choices=["true", "false"],
139+
help="is beta version"
140+
)
141+
parser.add_argument("--changelog-path", default=DEFAULT_CHANGELOG_PATH, help="path to changelog", type=str)
142+
parser.add_argument("--setup-py-path", default=SETUP_PY_PATH)
143+
144+
args = parser.parse_args()
145+
146+
is_beta = args.beta == "true"
147+
148+
new_version = increment_version_at_setup_py(args.setup_py_path, args.inc_type, is_beta)
149+
add_changelog_version(args.changelog_path, new_version)
150+
print(new_version)
151+
152+
153+
if __name__ == '__main__':
154+
main()
155+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
3+
from .increment_version import VersionLine
4+
5+
@pytest.mark.parametrize(
6+
"source,inc_type,with_beta,result",
7+
[
8+
("0.0.0", 'patch', False, "0.0.1"),
9+
("0.0.1", 'patch', False, "0.0.2"),
10+
("0.0.1a1", 'patch', False, "0.0.1"),
11+
("0.0.0", 'patch', True, "0.0.1a1"),
12+
("0.0.1", 'patch', True, "0.0.2a1"),
13+
("0.0.2a1", 'patch', True, "0.0.2a2"),
14+
("0.0.1", 'minor', False, "0.1.0"),
15+
("0.0.1a1", 'minor', False, "0.1.0"),
16+
("0.1.0a1", 'minor', False, "0.1.0"),
17+
("0.1.0", 'minor', True, "0.2.0a1"),
18+
("0.1.0a1", 'minor', True, "0.1.0a2"),
19+
("0.1.1a1", 'minor', True, "0.2.0a1"),
20+
]
21+
)
22+
def test_increment_version(source, inc_type, with_beta, result):
23+
version = VersionLine("", source)
24+
version.increment(inc_type, with_beta)
25+
incremented = str(version)
26+
assert incremented == result
27+

.github/workflows/python-publish.yml

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,108 @@
66
# separate terms of service, privacy policy, and support
77
# documentation.
88

9-
name: Upload Python Package
9+
name: Publish package release
1010

1111
on:
12-
release:
13-
types: [published]
12+
workflow_dispatch:
13+
inputs:
14+
version-change:
15+
description: Version part
16+
required: true
17+
type: choice
18+
options:
19+
- patch
20+
- minor
21+
beta:
22+
description: Is beta version
23+
required: true
24+
type: boolean
25+
default: True
1426

1527
jobs:
16-
deploy:
28+
publish:
29+
env:
30+
VERSION_CHANGE: ${{ github.event.inputs.version-change }}
31+
WITH_BETA: ${{ github.event.inputs.beta }}
32+
GITHUB_TOKEN: ${{ secrets.YDB_PLATFORM_BOT_TOKEN_REPO }}
33+
CHANGELOG_FILE: CHANGELOG.md
34+
SETUP_PY_PATH: setup.py
1735

1836
runs-on: ubuntu-latest
1937

2038
steps:
2139
- uses: actions/checkout@v3
40+
with:
41+
token: ${{ secrets.YDB_PLATFORM_BOT_TOKEN_REPO }}
42+
2243
- name: Set up Python
2344
uses: actions/setup-python@v3
2445
with:
25-
python-version: '3.x'
46+
python-version: '3.8'
47+
2648
- name: Install dependencies
2749
run: |
2850
python -m pip install --upgrade pip
2951
pip install build
52+
53+
- name: read changelog
54+
id: read-changelog
55+
run: |
56+
CHANGELOG=$(cat $CHANGELOG_FILE | sed -e '/^## .*$/,$d')
57+
echo "CHANGELOG<<CHANGELOGEOF_MARKER" >> $GITHUB_ENV
58+
echo "$CHANGELOG" >> $GITHUB_ENV
59+
echo "CHANGELOGEOF_MARKER" >> $GITHUB_ENV
60+
echo "# Changelog" >> $GITHUB_STEP_SUMMARY
61+
echo "$CHANGELOG" >> $GITHUB_STEP_SUMMARY
62+
63+
64+
- name: Increment version
65+
id: increment-version
66+
run: |
67+
NEW_VERSION=$(python3 ./.github/scripts/increment_version.py --inc-type=$VERSION_CHANGE --beta=$WITH_BETA)
68+
echo new version: $NEW_VERSION
69+
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT
70+
echo "New version: $NEW_VERSION" >> $GITHUB_STEP_SUMMARY
71+
72+
git add $SETUP_PY_PATH
73+
git add $CHANGELOG_FILE
74+
3075
- name: Build package
3176
run: python -m build
77+
78+
- name: Publish release on github
79+
run: |
80+
if [[ -z "$CHANGELOG" ]]
81+
then
82+
echo "CHANGELOG empty"
83+
exit 1;
84+
fi;
85+
86+
TAG="${{ steps.increment-version.outputs.NEW_VERSION }}"
87+
88+
# Get previous version from changelog
89+
# pre-incremented version not used for consistent changelog with release notes
90+
# for example changelog may be rewrited when switch from beta to release
91+
# and remove internal beta changes
92+
LAST_TAG=$(cat $CHANGELOG_FILE | grep '^## .* ##$' | head -n 2 | tail -n 1 | cut -d ' ' -f 2)
93+
94+
git config --global user.email "robot@umbrella";
95+
git config --global user.name "robot";
96+
git commit -m "Release: $NEW_VERSION";
97+
98+
git tag "$TAG"
99+
git push && git push --tags
100+
101+
CHANGELOG="$CHANGELOG
102+
103+
Full Changelog: [$LAST_TAG...$TAG](https://github.com/ydb-platform/ydb-go-sdk/compare/$LAST_TAG...$TAG)"
104+
if [ "$WITH_BETA" = true ]
105+
then
106+
gh release create -d $TAG -t "$TAG" --notes "$CHANGELOG"
107+
else
108+
gh release create $TAG -t "$TAG" --notes "$CHANGELOG"
109+
fi;
110+
32111
- name: Publish package
33112
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
34113
with:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setuptools.setup(
88
name="ydb",
9-
version="2.12.1",
9+
version="2.12.1", # AUTOVERSION
1010
description="YDB Python SDK",
1111
author="Yandex LLC",
1212
author_email="[email protected]",

0 commit comments

Comments
 (0)