Skip to content

Commit f4ebfe5

Browse files
committed
Add script to update and verify version
1 parent eae415f commit f4ebfe5

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

.github/workflows/auto-testing.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ jobs:
4444
- name: Update version in linebot/__about__.py
4545
run: |
4646
VERSION="123.456.789"
47-
sed -i "s/__version__ = '.*'/__version__ = '$VERSION'/g" linebot/__about__.py
48-
cat linebot/__about__.py
47+
python tools/update_version.py $VERSION
4948
5049
check-import:
5150
runs-on: ubuntu-latest

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

Lines changed: 12 additions & 3 deletions
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__ = '.*'/__version__ = '$VERSION'/g" linebot/__about__.py
32-
cat linebot/__about__.py
41+
python tools/update_and_verify_version.py $VERSION
3342
- name: Build and publish
3443
env:
3544
TWINE_USERNAME: ${{ secrets.PYPI_API_USER }}

tools/update_version.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
except subprocess.CalledProcessError as e:
52+
print(f"Error during git diff verification: {e}")
53+
sys.exit(1)
54+
55+
if __name__ == "__main__":
56+
if len(sys.argv) != 2:
57+
print("Usage: python update_and_verify_version.py <new_version>")
58+
sys.exit(1)
59+
60+
new_version = sys.argv[1]
61+
62+
try:
63+
update_and_verify_version(new_version)
64+
except ValueError as e:
65+
print(e)
66+
sys.exit(1)

0 commit comments

Comments
 (0)