From 0ae5eee3b2ea567227c0e039cd4ab02378a5d6ea Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Sat, 18 Jan 2025 13:49:15 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20ci:=20add=20workflow=20to=20auto?= =?UTF-8?q?=20sync=20latest=20version=20(#1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/mirror.yml | 39 ++++++++++++++++++++++++++++++++++++ mirror.py | 23 ++++++++++++++------- 2 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/mirror.yml diff --git a/.github/workflows/mirror.yml b/.github/workflows/mirror.yml new file mode 100644 index 0000000..e3a3609 --- /dev/null +++ b/.github/workflows/mirror.yml @@ -0,0 +1,39 @@ +name: Mirror +on: + push: + branches: [main] + workflow_dispatch: + schedule: + - cron: "0 12 * * *" + +jobs: + build: + name: Mirror + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install uv + uses: astral-sh/setup-uv@v5 + + - name: Install python + uses: actions/setup-python@v5 + with: + python-version: "3.13" + + - name: Setup git + run: | + git config --global user.name 'Github Actions' + git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com' + + - name: Run mirror + run: | + uv run --script mirror.py + + - name: Push changes + run: | + git remote set-url origin https://x-access-token:$GH_TOKEN@github.com/$GITHUB_REPOSITORY + git push origin HEAD:refs/heads/main --tags + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/mirror.py b/mirror.py index c0d9199..482144c 100644 --- a/mirror.py +++ b/mirror.py @@ -1,3 +1,13 @@ +# /// script +# requires-python = ">=3.9" +# dependencies = [ +# "tomli>=2.2.1", +# "tomli-w>=1.2.0", +# "urllib3>=2", +# "packaging>=21.0", +# ] +# /// + import subprocess from pathlib import Path @@ -9,7 +19,7 @@ def main(): - # 读取 pyproject.toml 文件 + # Load pyproject.toml with open(Path(__file__).parent / "pyproject.toml", "rb") as f: pyproject = tomli.load(f) @@ -23,7 +33,7 @@ def main(): assert typos_specs[0].operator == "==" current_version = Version(typos_specs[0].version) - # 从 PyPI 获取 typos 的所有版本 + # Get typos versions from PyPI http = urllib3.PoolManager() resp = http.request("GET", "https://pypi.org/pypi/typos/json") if resp.status != 200: @@ -33,16 +43,15 @@ def main(): versions = [v for v in versions if v > current_version and not v.is_prerelease] versions.sort() - # 如果有新版本,更新 pyproject.toml 文件并提交 + # Update typos for each version for version in versions: - # 更新 dependencies 部分 + # Update pyproject.toml + pyproject["project"]["version"] = str(version) pyproject["project"]["dependencies"] = [f"typos=={version}"] - - # 写入更新后的 pyproject.toml with open(Path(__file__).parent / "pyproject.toml", "wb") as f: tomli_w.dump(pyproject, f) - # 执行 Git 操作 + # Commit and tag subprocess.run(["git", "add", "pyproject.toml"]) subprocess.run(["git", "commit", "-m", f"Update typos to {version}"]) subprocess.run(["git", "tag", f"v{version}"])