Skip to content

Commit 5cd7f1a

Browse files
authoredNov 25, 2024··
Stop using __LINE_BOT_SDK_PYTHON_VERSION__ (#710)
The string `__LINE_BOT_SDK_PYTHON_VERSION__ ` is used in `linebot/__about__.py` to make it easier to replace with the new version at the time of release. However, this string is inappropriate as a version, so it needs to be replaced when testing locally... This is cumbersome for us, so this PR addresses that issue. At the time of release, the file that need to be updated will be directly modified anyway. `./tools/update_version.py` will handle this. Since 1 lines across 1 files need to be changed, the CI will fail if this condition is not met. Reviewers can verify that the version is changed locally, but it's up to them whether to do so.
1 parent f1218b1 commit 5cd7f1a

File tree

4 files changed

+83
-17
lines changed

4 files changed

+83
-17
lines changed
 

‎.github/workflows/auto-testing.yml

-13
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ jobs:
3333
- name: Ensure generate-code.py doesn't throw any error
3434
run: |
3535
python generate-code.py
36-
- name: Update version in linebot/__about__.py
37-
run: |
38-
VERSION="12.3.0"
39-
VERSION=${VERSION#v}
40-
sed -i "s/__version__ = '__LINE_BOT_SDK_PYTHON_VERSION__'/__version__ = '$VERSION'/g" linebot/__about__.py
41-
cat linebot/__about__.py
4236
- name: Test with pytest
4337
run: |
4438
tox
@@ -64,13 +58,6 @@ jobs:
6458
uses: actions/setup-python@v5
6559
with:
6660
python-version: ${{ matrix.python-version }}
67-
- name: Update version in linebot/__about__.py
68-
run: |
69-
VERSION="12.3.0"
70-
VERSION=${VERSION#v}
71-
sed -i "s/__version__ = '__LINE_BOT_SDK_PYTHON_VERSION__'/__version__ = '$VERSION'/g" linebot/__about__.py
72-
cat linebot/__about__.py
73-
7461
- name: Install dependencies & lib
7562
run: |
7663
python -m pip install --upgrade pip

‎.github/workflows/publish-to-pypi.yml

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ name: Upload Python Package
66
on:
77
release:
88
types: [published]
9+
workflow_dispatch:
10+
inputs:
11+
version:
12+
description: 'The version to release'
13+
required: true
914

1015
jobs:
1116
deploy:
@@ -26,10 +31,14 @@ jobs:
2631
pip install setuptools wheel twine
2732
- name: Update version in linebot/__about__.py
2833
run: |
29-
VERSION=${{ github.event.release.tag_name }}
34+
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
35+
VERSION=${{ github.event.inputs.version }}
36+
else
37+
VERSION=${{ github.event.release.tag_name }}
38+
fi
39+
3040
VERSION=${VERSION#v}
31-
sed -i "s/__version__ = '__LINE_BOT_SDK_PYTHON_VERSION__'/__version__ = '$VERSION'/g" linebot/__about__.py
32-
cat linebot/__about__.py
41+
python tools/update_version.py $VERSION
3342
- name: Build and publish
3443
env:
3544
TWINE_USERNAME: ${{ secrets.PYPI_API_USER }}

‎linebot/__about__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Meta data of line-bot-sdk."""
1616

1717

18-
__version__ = '__LINE_BOT_SDK_PYTHON_VERSION__'
18+
__version__ = '1.0.0a1'
1919
__author__ = 'LINE Corporation'
2020
__copyright__ = 'Copyright 2016, LINE Corporation'
2121
__license__ = 'Apache 2.0'

‎tools/update_version.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import sys
2+
import re
3+
import subprocess
4+
5+
def update_and_verify_version(new_version):
6+
file_path = 'linebot/__about__.py'
7+
8+
# Update version
9+
with open(file_path, 'r') as file:
10+
content = file.read()
11+
12+
new_content = re.sub(
13+
r"__version__ = '.*?'",
14+
f"__version__ = '{new_version}'",
15+
content
16+
)
17+
18+
with open(file_path, 'w') as file:
19+
file.write(new_content)
20+
21+
print(f"Updated version to {new_version} in {file_path}")
22+
23+
# verify version
24+
match = re.search(r"__version__ = '(.*?)'", new_content)
25+
if not match:
26+
raise ValueError("Version string not found in the file.")
27+
28+
actual_version = match.group(1)
29+
if actual_version != new_version:
30+
raise ValueError(f"Version mismatch: expected {new_version}, found {actual_version}")
31+
32+
print(f"Version verified: {actual_version}")
33+
34+
# diff check just in case
35+
try:
36+
result = subprocess.run(['git', 'diff', '--numstat', file_path], capture_output=True, text=True, check=True)
37+
changed_lines = result.stdout.strip().split('\n')
38+
added_lines = 0
39+
deleted_lines = 0
40+
41+
for line in changed_lines:
42+
added, deleted = map(int, line.split('\t')[:2])
43+
added_lines += added
44+
deleted_lines += deleted
45+
46+
if added_lines != 1 or deleted_lines != 1:
47+
raise ValueError(f"Unexpected number of changed lines: expected 1 added and 1 deleted, found {added_lines} added and {deleted_lines} deleted")
48+
49+
print('Git diff verification passed: 1 line added and 1 line deleted.')
50+
51+
# Show diff
52+
diff_result = subprocess.run(['git', 'diff', '--color=always', file_path], capture_output=True, text=True, check=True)
53+
print('Git diff output:\n', diff_result.stdout)
54+
55+
except subprocess.CalledProcessError as e:
56+
print(f"Error during git diff verification: {e}")
57+
sys.exit(1)
58+
59+
if __name__ == "__main__":
60+
if len(sys.argv) != 2:
61+
print("Usage: python update_version.py <new_version>")
62+
sys.exit(1)
63+
64+
new_version = sys.argv[1]
65+
66+
try:
67+
update_and_verify_version(new_version)
68+
except ValueError as e:
69+
print(e)
70+
sys.exit(1)

0 commit comments

Comments
 (0)
Please sign in to comment.