Skip to content

Commit 78a2e7b

Browse files
authored
Merge pull request #34 initial autopublish
2 parents f8ee382 + 723d3c4 commit 78a2e7b

File tree

5 files changed

+302
-1
lines changed

5 files changed

+302
-1
lines changed

.github/scripts/__init__.py

Whitespace-only changes.

.github/scripts/increment_version.py

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

.github/workflows/python-publish.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# This workflow will upload a Python Package using Twine when a release is created
2+
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
3+
4+
# This workflow uses actions that are not certified by GitHub.
5+
# They are provided by a third-party and are governed by
6+
# separate terms of service, privacy policy, and support
7+
# documentation.
8+
9+
name: Publish package release
10+
11+
on:
12+
workflow_dispatch:
13+
inputs:
14+
version-change:
15+
description: Version part
16+
required: true
17+
type: choice
18+
default: minor
19+
options:
20+
- minor
21+
- patch
22+
beta:
23+
description: Is beta version
24+
required: true
25+
type: boolean
26+
default: True
27+
jobs:
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
35+
36+
runs-on: ubuntu-latest
37+
38+
steps:
39+
- uses: actions/checkout@v3
40+
# with:
41+
# token: ${{ secrets.YDB_PLATFORM_BOT_TOKEN_REPO }}
42+
43+
- name: Set up Python
44+
uses: actions/setup-python@v3
45+
with:
46+
python-version: '3.8'
47+
48+
- name: Install dependencies
49+
run: |
50+
python -m pip install --upgrade pip
51+
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+
- name: Build package
73+
run: python -m build
74+
75+
- name: Publish release on github
76+
run: |
77+
if [[ -z "$CHANGELOG" ]]
78+
then
79+
echo "CHANGELOG empty"
80+
exit 1;
81+
fi;
82+
83+
TAG="${{ steps.increment-version.outputs.NEW_VERSION }}"
84+
85+
# Get previous version from changelog
86+
# pre-incremented version not used for consistent changelog with release notes
87+
# for example changelog may be rewrited when switch from beta to release
88+
# and remove internal beta changes
89+
LAST_TAG=$(cat $CHANGELOG_FILE | grep '^## .* ##$' | head -n 2 | tail -n 1 | cut -d ' ' -f 2)
90+
91+
git config --global user.email "robot@umbrella";
92+
git config --global user.name "robot";
93+
git commit -am "Release: $TAG";
94+
95+
git tag "$TAG"
96+
git push && git push --tags
97+
98+
CHANGELOG="$CHANGELOG
99+
100+
Full Changelog: [$LAST_TAG...$TAG](https://github.com/ydb-platform/ydb-sqlalchemy/compare/$LAST_TAG...$TAG)"
101+
if [ "$WITH_BETA" = true ]
102+
then
103+
gh release create --prerelease $TAG --title "$TAG" --notes "$CHANGELOG"
104+
else
105+
gh release create $TAG --title "$TAG" --notes "$CHANGELOG"
106+
fi;
107+
108+
- name: Publish package
109+
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
110+
with:
111+
user: __token__
112+
password: ${{ secrets.PYPI_API_TOKEN }}

setup.py

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

1414
setuptools.setup(
1515
name="ydb-sqlalchemy",
16-
version="0.1.0", # AUTOVERSION
16+
version="0.0.0", # AUTOVERSION
1717
description="YDB Dialect for SQLAlchemy",
1818
author="Yandex LLC",
1919
author_email="[email protected]",

0 commit comments

Comments
 (0)