Skip to content

Commit cd70e93

Browse files
authored
Merge pull request #2 from ydb-platform/publish_ci
Publich python package CI
2 parents d2f1dd1 + 39a4192 commit cd70e93

File tree

8 files changed

+299
-61
lines changed

8 files changed

+299
-61
lines changed
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_mcp/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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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: patch
19+
options:
20+
- minor
21+
- patch
22+
beta:
23+
description: Is beta version
24+
required: true
25+
type: boolean
26+
default: False
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: Install dependencies
53+
run: |
54+
python -m pip install --upgrade pip
55+
pip install packaging build
56+
57+
- name: read changelog
58+
id: read-changelog
59+
run: |
60+
CHANGELOG=$(cat $CHANGELOG_FILE | sed -e '/^## .*$/,$d')
61+
echo "CHANGELOG<<CHANGELOGEOF_MARKER" >> $GITHUB_ENV
62+
echo "$CHANGELOG" >> $GITHUB_ENV
63+
echo "CHANGELOGEOF_MARKER" >> $GITHUB_ENV
64+
echo "# Changelog" >> $GITHUB_STEP_SUMMARY
65+
echo "$CHANGELOG" >> $GITHUB_STEP_SUMMARY
66+
67+
68+
- name: Increment version
69+
id: increment-version
70+
run: |
71+
NEW_VERSION=$(python3 ./.github/scripts/increment_version.py --inc-type=$VERSION_CHANGE --beta=$WITH_BETA)
72+
echo new version: $NEW_VERSION
73+
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT
74+
echo "New version: $NEW_VERSION" >> $GITHUB_STEP_SUMMARY
75+
76+
- name: Build package
77+
run: python -m build
78+
79+
- name: Publish release on github
80+
run: |
81+
if [[ -z "$CHANGELOG" ]]
82+
then
83+
echo "CHANGELOG empty"
84+
exit 1;
85+
fi;
86+
87+
TAG="${{ steps.increment-version.outputs.NEW_VERSION }}"
88+
89+
# Get previous version from changelog
90+
# pre-incremented version not used for consistent changelog with release notes
91+
# for example changelog may be rewrited when switch from beta to release
92+
# and remove internal beta changes
93+
LAST_TAG=$(cat $CHANGELOG_FILE | grep '^## .* ##$' | head -n 2 | tail -n 1 | cut -d ' ' -f 2)
94+
95+
git config --global user.email "robot@umbrella";
96+
git config --global user.name "robot";
97+
git commit -am "Release: $TAG";
98+
99+
git tag "$TAG"
100+
git push && git push --tags
101+
102+
CHANGELOG="$CHANGELOG
103+
104+
Full Changelog: [$LAST_TAG...$TAG](https://github.com/ydb-platform/ydb-sqlalchemy/compare/$LAST_TAG...$TAG)"
105+
if [ "$WITH_BETA" = true ]
106+
then
107+
gh release create --prerelease $TAG --title "$TAG" --notes "$CHANGELOG"
108+
else
109+
gh release create $TAG --title "$TAG" --notes "$CHANGELOG"
110+
fi;
111+
112+
- name: Publish package
113+
uses: pypa/gh-action-pypi-publish@release/v1.8

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* Update package
2+
3+
# 0.1.0
4+
* First version

pyproject.toml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,25 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "ydb-mcp"
7-
version = "0.1.0"
7+
version = "0.1.0" # AUTOVERSION
88
description = "Model Context Protocol server for YDB DBMS"
99
readme = "README.md"
1010
authors = [
1111
{name = "YDB MCP Team", email = "[email protected]"}
1212
]
1313
license = {text = "Apache 2.0"}
14-
requires-python = ">=3.8"
14+
requires-python = ">=3.10"
1515
classifiers = [
1616
"Development Status :: 3 - Alpha",
1717
"Intended Audience :: Developers",
1818
"Programming Language :: Python :: 3",
19-
"Programming Language :: Python :: 3.8",
20-
"Programming Language :: Python :: 3.9",
2119
"Programming Language :: Python :: 3.10",
20+
"Programming Language :: Python :: 3.11",
21+
"Programming Language :: Python :: 3.12",
2222
]
2323
dependencies = [
24-
"ydb>=3.0.0",
25-
"mcp-server>=0.1.0",
26-
"httpx>=0.25.0",
24+
"ydb>=3.21.0",
25+
"mcp>=1.6.0",
2726
]
2827

2928
[project.optional-dependencies]

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
ydb>=3.0.0
2-
mcp-server>=0.1.0
1+
ydb>=3.21.0
2+
mcp>=1.6.0

setup.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

ydb_mcp/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""YDB MCP - Model Context Protocol server for YDB."""
2+
from .version import VERSION
23

3-
__version__ = "0.1.0"
4+
5+
__version__ = VERSION
46

57
# Import order matters to avoid circular imports
68
from ydb_mcp.connection import YDBConnection

ydb_mcp/version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VERSION = "0.1.0"

0 commit comments

Comments
 (0)