Skip to content

Commit de14b64

Browse files
authored
Merge pull request #1 from ydb-platform/new_dbapi
DBAPI based on Query Service
2 parents 81676a4 + 9346679 commit de14b64

20 files changed

+3198
-380
lines changed

.github/scripts/increment_version.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#!/bin/env python
2+
import argparse
3+
from dataclasses import dataclass
4+
5+
from packaging.version import Version
6+
7+
PYPROJECT_PATH = "pyproject.toml"
8+
DEFAULT_CHANGELOG_PATH = "CHANGELOG.md"
9+
DEFAULT_YDB_VERSION_FILE = "ydb_dbapi/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(pyproject_content: str):
83+
version_line = ""
84+
for line in pyproject_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_pyproject(
101+
pyproject_path: str, inc_type: str, with_beta: bool
102+
) -> str:
103+
with open(pyproject_path, "rt") as f:
104+
setup_content = f.read()
105+
106+
version = extract_version(setup_content)
107+
version.increment(inc_type, with_beta)
108+
setup_content = setup_content.replace(
109+
version.old_line, version.version_line_with_mark()
110+
)
111+
112+
with open(pyproject_path, "w") as f:
113+
f.write(setup_content)
114+
115+
return str(version)
116+
117+
118+
def add_changelog_version(changelog_path, version: str):
119+
with open(changelog_path, "rt") as f:
120+
content = f.read()
121+
content = content.strip()
122+
123+
if content.startswith("##"):
124+
return
125+
126+
content = """## %s ##
127+
%s
128+
""" % (version, content)
129+
with open(changelog_path, "w") as f:
130+
f.write(content)
131+
132+
133+
def set_version_in_version_file(file_path: str, version: str):
134+
with open(file_path, "w") as f:
135+
f.write('VERSION = "%s"\n' % version)
136+
137+
138+
def main():
139+
parser = argparse.ArgumentParser()
140+
parser.add_argument(
141+
"--inc-type",
142+
default="minor",
143+
help="increment version type: patch or minor",
144+
choices=["minor", "patch"],
145+
)
146+
parser.add_argument(
147+
"--beta", choices=["true", "false"], help="is beta version"
148+
)
149+
parser.add_argument(
150+
"--changelog-path",
151+
default=DEFAULT_CHANGELOG_PATH,
152+
help="path to changelog",
153+
type=str,
154+
)
155+
parser.add_argument("--pyproject-path", default=PYPROJECT_PATH)
156+
157+
args = parser.parse_args()
158+
159+
is_beta = args.beta == "true"
160+
161+
new_version = increment_version_at_pyproject(
162+
args.pyproject_path, args.inc_type, is_beta
163+
)
164+
add_changelog_version(args.changelog_path, new_version)
165+
set_version_in_version_file(DEFAULT_YDB_VERSION_FILE, new_version)
166+
print(new_version)
167+
168+
169+
if __name__ == "__main__":
170+
main()
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+
6+
@pytest.mark.parametrize(
7+
"source,inc_type,with_beta,result",
8+
[
9+
("0.0.0", "patch", False, "0.0.1"),
10+
("0.0.1", "patch", False, "0.0.2"),
11+
("0.0.1b1", "patch", False, "0.0.1"),
12+
("0.0.0", "patch", True, "0.0.1b1"),
13+
("0.0.1", "patch", True, "0.0.2b1"),
14+
("0.0.2b1", "patch", True, "0.0.2b2"),
15+
("0.0.1", "minor", False, "0.1.0"),
16+
("0.0.1b1", "minor", False, "0.1.0"),
17+
("0.1.0b1", "minor", False, "0.1.0"),
18+
("0.1.0", "minor", True, "0.2.0b1"),
19+
("0.1.0b1", "minor", True, "0.1.0b2"),
20+
("0.1.1b1", "minor", True, "0.2.0b1"),
21+
("3.0.0b1", "patch", True, "3.0.0b2"),
22+
],
23+
)
24+
def test_increment_version(source, inc_type, with_beta, result):
25+
version = VersionLine("", source)
26+
version.increment(inc_type, with_beta)
27+
incremented = str(version)
28+
assert incremented == result

.github/workflows/lint.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
python-version: [3.9]
17+
poetry-version: [1.8]
18+
19+
steps:
20+
- uses: actions/checkout@v3
21+
with:
22+
persist-credentials: false
23+
fetch-depth: 0
24+
25+
- name: Set up Python ${{ matrix.python-version }}
26+
uses: actions/setup-python@v4
27+
with:
28+
python-version: ${{ matrix.python-version }}
29+
30+
- name: Install Poetry ${{ matrix.poetry-version }}
31+
run: |
32+
pip install "poetry~=${{ matrix.poetry-version }}.0"
33+
34+
# Ensure that Poetry is not upgraded past the version we are testing
35+
poetry add "poetry@~${{ matrix.poetry-version }}" --lock
36+
37+
- name: Install packages
38+
run: |
39+
poetry install
40+
41+
- name: Run tests
42+
run: |
43+
poetry run poe lint

.github/workflows/python-publish.yml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
GH_TOKEN: ${{ secrets.YDB_PLATFORM_BOT_TOKEN_REPO }}
33+
CHANGELOG_FILE: CHANGELOG.md
34+
PYPROJECT_PATH: pyproject.toml
35+
36+
permissions:
37+
contents: write
38+
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
39+
40+
runs-on: ubuntu-latest
41+
42+
steps:
43+
- uses: actions/checkout@v3
44+
with:
45+
token: ${{ secrets.YDB_PLATFORM_BOT_TOKEN_REPO }}
46+
47+
- name: Set up Python
48+
uses: actions/setup-python@v3
49+
with:
50+
python-version: '3.9'
51+
52+
- name: read changelog
53+
id: read-changelog
54+
run: |
55+
CHANGELOG=$(cat $CHANGELOG_FILE | sed -e '/^## .*$/,$d')
56+
echo "CHANGELOG<<CHANGELOGEOF_MARKER" >> $GITHUB_ENV
57+
echo "$CHANGELOG" >> $GITHUB_ENV
58+
echo "CHANGELOGEOF_MARKER" >> $GITHUB_ENV
59+
echo "# Changelog" >> $GITHUB_STEP_SUMMARY
60+
echo "$CHANGELOG" >> $GITHUB_STEP_SUMMARY
61+
62+
63+
- name: Increment version
64+
id: increment-version
65+
run: |
66+
NEW_VERSION=$(python3 ./.github/scripts/increment_version.py --inc-type=$VERSION_CHANGE --beta=$WITH_BETA)
67+
echo new version: $NEW_VERSION
68+
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT
69+
echo "New version: $NEW_VERSION" >> $GITHUB_STEP_SUMMARY
70+
71+
- name: Install Poetry
72+
run: pip install poetry
73+
74+
- name: Build package
75+
run: poetry build
76+
77+
- name: Publish release on github
78+
run: |
79+
if [[ -z "$CHANGELOG" ]]
80+
then
81+
echo "CHANGELOG empty"
82+
exit 1;
83+
fi;
84+
85+
TAG="${{ steps.increment-version.outputs.NEW_VERSION }}"
86+
87+
# Get previous version from changelog
88+
# pre-incremented version not used for consistent changelog with release notes
89+
# for example changelog may be rewrited when switch from beta to release
90+
# and remove internal beta changes
91+
LAST_TAG=$(cat $CHANGELOG_FILE | grep '^## .* ##$' | head -n 2 | tail -n 1 | cut -d ' ' -f 2)
92+
93+
git config --global user.email "robot@umbrella";
94+
git config --global user.name "robot";
95+
git commit -am "Release: $TAG";
96+
97+
git tag "$TAG"
98+
git push && git push --tags
99+
100+
CHANGELOG="$CHANGELOG
101+
102+
Full Changelog: [$LAST_TAG...$TAG](https://github.com/ydb-platform/ydb-sqlalchemy/compare/$LAST_TAG...$TAG)"
103+
if [ "$WITH_BETA" = true ]
104+
then
105+
gh release create --prerelease $TAG --title "$TAG" --notes "$CHANGELOG"
106+
else
107+
gh release create $TAG --title "$TAG" --notes "$CHANGELOG"
108+
fi;
109+
110+
- name: Publish package
111+
uses: pypa/gh-action-pypi-publish@release/v1.8

0 commit comments

Comments
 (0)