Skip to content

Commit

Permalink
Stop using __LINE_BOT_SDK_PYTHON_VERSION__ (#710)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Yang-33 authored Nov 25, 2024
1 parent f1218b1 commit 5cd7f1a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 17 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/auto-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ jobs:
- name: Ensure generate-code.py doesn't throw any error
run: |
python generate-code.py
- name: Update version in linebot/__about__.py
run: |
VERSION="12.3.0"
VERSION=${VERSION#v}
sed -i "s/__version__ = '__LINE_BOT_SDK_PYTHON_VERSION__'/__version__ = '$VERSION'/g" linebot/__about__.py
cat linebot/__about__.py
- name: Test with pytest
run: |
tox
Expand All @@ -64,13 +58,6 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Update version in linebot/__about__.py
run: |
VERSION="12.3.0"
VERSION=${VERSION#v}
sed -i "s/__version__ = '__LINE_BOT_SDK_PYTHON_VERSION__'/__version__ = '$VERSION'/g" linebot/__about__.py
cat linebot/__about__.py
- name: Install dependencies & lib
run: |
python -m pip install --upgrade pip
Expand Down
15 changes: 12 additions & 3 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ name: Upload Python Package
on:
release:
types: [published]
workflow_dispatch:
inputs:
version:
description: 'The version to release'
required: true

jobs:
deploy:
Expand All @@ -26,10 +31,14 @@ jobs:
pip install setuptools wheel twine
- name: Update version in linebot/__about__.py
run: |
VERSION=${{ github.event.release.tag_name }}
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
VERSION=${{ github.event.inputs.version }}
else
VERSION=${{ github.event.release.tag_name }}
fi
VERSION=${VERSION#v}
sed -i "s/__version__ = '__LINE_BOT_SDK_PYTHON_VERSION__'/__version__ = '$VERSION'/g" linebot/__about__.py
cat linebot/__about__.py
python tools/update_version.py $VERSION
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_API_USER }}
Expand Down
2 changes: 1 addition & 1 deletion linebot/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""Meta data of line-bot-sdk."""


__version__ = '__LINE_BOT_SDK_PYTHON_VERSION__'
__version__ = '1.0.0a1'
__author__ = 'LINE Corporation'
__copyright__ = 'Copyright 2016, LINE Corporation'
__license__ = 'Apache 2.0'
Expand Down
70 changes: 70 additions & 0 deletions tools/update_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import sys
import re
import subprocess

def update_and_verify_version(new_version):
file_path = 'linebot/__about__.py'

# Update version
with open(file_path, 'r') as file:
content = file.read()

new_content = re.sub(
r"__version__ = '.*?'",
f"__version__ = '{new_version}'",
content
)

with open(file_path, 'w') as file:
file.write(new_content)

print(f"Updated version to {new_version} in {file_path}")

# verify version
match = re.search(r"__version__ = '(.*?)'", new_content)
if not match:
raise ValueError("Version string not found in the file.")

actual_version = match.group(1)
if actual_version != new_version:
raise ValueError(f"Version mismatch: expected {new_version}, found {actual_version}")

print(f"Version verified: {actual_version}")

# diff check just in case
try:
result = subprocess.run(['git', 'diff', '--numstat', file_path], capture_output=True, text=True, check=True)
changed_lines = result.stdout.strip().split('\n')
added_lines = 0
deleted_lines = 0

for line in changed_lines:
added, deleted = map(int, line.split('\t')[:2])
added_lines += added
deleted_lines += deleted

if added_lines != 1 or deleted_lines != 1:
raise ValueError(f"Unexpected number of changed lines: expected 1 added and 1 deleted, found {added_lines} added and {deleted_lines} deleted")

print('Git diff verification passed: 1 line added and 1 line deleted.')

# Show diff
diff_result = subprocess.run(['git', 'diff', '--color=always', file_path], capture_output=True, text=True, check=True)
print('Git diff output:\n', diff_result.stdout)

except subprocess.CalledProcessError as e:
print(f"Error during git diff verification: {e}")
sys.exit(1)

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python update_version.py <new_version>")
sys.exit(1)

new_version = sys.argv[1]

try:
update_and_verify_version(new_version)
except ValueError as e:
print(e)
sys.exit(1)

0 comments on commit 5cd7f1a

Please sign in to comment.