From e42e6eeee608e780725abdc1b264e960a546efed Mon Sep 17 00:00:00 2001 From: zty98751 Date: Wed, 11 Jun 2025 09:52:41 +0800 Subject: [PATCH] split translae-readme from helm-docs action --- .github/workflows/helm-docs.yaml | 123 ---------------------- .github/workflows/translate-readme.yaml | 131 ++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 123 deletions(-) create mode 100644 .github/workflows/translate-readme.yaml diff --git a/.github/workflows/helm-docs.yaml b/.github/workflows/helm-docs.yaml index 3fd0cfeab..e127ec7e2 100644 --- a/.github/workflows/helm-docs.yaml +++ b/.github/workflows/helm-docs.yaml @@ -39,126 +39,3 @@ jobs: fi git diff --exit-code rm -f ./helm-docs - - translate-readme: - needs: helm - runs-on: ubuntu-latest - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get install -y jq - - - name: Compare README.md - id: compare_readme - run: | - cd ./helm/higress - - BASE_BRANCH=${GITHUB_BASE_REF:-main} - git fetch origin $BASE_BRANCH - - if git diff --quiet origin/$BASE_BRANCH -- README.md; then - echo "README.md has no local changes compared to $BASE_BRANCH. Skipping translation." - echo "skip_translation=true" >> $GITHUB_ENV - else - echo "README.md has local changes compared to $BASE_BRANCH. Proceeding with translation." - echo "skip_translation=false" >> $GITHUB_ENV - echo "--------- diff ---------" - git diff origin/$BASE_BRANCH -- README.md - echo "------------------------" - fi - - - name: Translate README.md to Chinese - if: env.skip_translation == 'false' - env: - API_URL: ${{ secrets.HIGRESS_OPENAI_API_URL }} - API_KEY: ${{ secrets.HIGRESS_OPENAI_API_KEY }} - API_MODEL: ${{ secrets.HIGRESS_OPENAI_API_MODEL }} - run: | - cat << 'EOF' > translate_readme.py - import os - import json - import requests - - API_URL = os.environ["API_URL"] - API_KEY = os.environ["API_KEY"] - API_MODEL = os.environ["API_MODEL"] - README_PATH = "./helm/higress/README.md" - OUTPUT_PATH = "./helm/higress/README.zh.md" - - def stream_translation(api_url, api_key, payload): - headers = { - "Content-Type": "application/json", - "Authorization": f"Bearer {api_key}", - } - response = requests.post(api_url, headers=headers, json=payload, stream=True) - response.raise_for_status() - - with open(OUTPUT_PATH, "w", encoding="utf-8") as out_file: - for line in response.iter_lines(decode_unicode=True): - if line.strip() == "" or not line.startswith("data: "): - continue - data = line[6:] - if data.strip() == "[DONE]": - break - try: - chunk = json.loads(data) - content = chunk["choices"][0]["delta"].get("content", "") - if content: - out_file.write(content) - except Exception as e: - print("Error parsing chunk:", e) - - def main(): - if not os.path.exists(README_PATH): - print("README.md not found!") - return - - with open(README_PATH, "r", encoding="utf-8") as f: - content = f.read() - - payload = { - "model": API_MODEL, - "messages": [ - { - "role": "system", - "content": "You are a translation assistant that translates English Markdown text to Chinese. Preserve original Markdown formatting and line breaks." - }, - { - "role": "user", - "content": content - } - ], - "temperature": 0.3, - "stream": True - } - - print("Streaming translation started...") - stream_translation(API_URL, API_KEY, payload) - print(f"Translation completed and saved to {OUTPUT_PATH}.") - - if __name__ == "__main__": - main() - EOF - - python3 translate_readme.py - rm -rf translate_readme.py - - - name: Create Pull Request - if: env.skip_translation == 'false' - uses: peter-evans/create-pull-request@v7 - with: - token: ${{ secrets.GITHUB_TOKEN }} - commit-message: "Update helm translated README.zh.md" - branch: update-helm-readme-zh - title: "Update helm translated README.zh.md" - body: | - This PR updates the translated README.zh.md file. - - - Automatically generated by GitHub Actions - labels: translation, automated - base: main \ No newline at end of file diff --git a/.github/workflows/translate-readme.yaml b/.github/workflows/translate-readme.yaml new file mode 100644 index 000000000..0af04bd39 --- /dev/null +++ b/.github/workflows/translate-readme.yaml @@ -0,0 +1,131 @@ +name: "Helm Docs" + +on: + workflow_dispatch: ~ + push: + branches: [ main ] + paths: + - 'helm/higress/README.md' + +jobs: + translate-readme: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y jq + + - name: Compare README.md + id: compare_readme + run: | + cd ./helm/higress + + BASE_BRANCH=${GITHUB_BASE_REF:-main} + git fetch origin $BASE_BRANCH + + if git diff --quiet origin/$BASE_BRANCH -- README.md; then + echo "README.md has no local changes compared to $BASE_BRANCH. Skipping translation." + echo "skip_translation=true" >> $GITHUB_ENV + else + echo "README.md has local changes compared to $BASE_BRANCH. Proceeding with translation." + echo "skip_translation=false" >> $GITHUB_ENV + echo "--------- diff ---------" + git diff origin/$BASE_BRANCH -- README.md + echo "------------------------" + fi + + - name: Translate README.md to Chinese + if: env.skip_translation == 'false' + env: + API_URL: ${{ secrets.HIGRESS_OPENAI_API_URL }} + API_KEY: ${{ secrets.HIGRESS_OPENAI_API_KEY }} + API_MODEL: ${{ secrets.HIGRESS_OPENAI_API_MODEL }} + run: | + cat << 'EOF' > translate_readme.py + import os + import json + import requests + + API_URL = os.environ["API_URL"] + API_KEY = os.environ["API_KEY"] + API_MODEL = os.environ["API_MODEL"] + README_PATH = "./helm/higress/README.md" + OUTPUT_PATH = "./helm/higress/README.zh.md" + + def stream_translation(api_url, api_key, payload): + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {api_key}", + } + response = requests.post(api_url, headers=headers, json=payload, stream=True) + response.raise_for_status() + + with open(OUTPUT_PATH, "w", encoding="utf-8") as out_file: + for line in response.iter_lines(decode_unicode=True): + if line.strip() == "" or not line.startswith("data: "): + continue + data = line[6:] + if data.strip() == "[DONE]": + break + try: + chunk = json.loads(data) + content = chunk["choices"][0]["delta"].get("content", "") + if content: + out_file.write(content) + except Exception as e: + print("Error parsing chunk:", e) + + def main(): + if not os.path.exists(README_PATH): + print("README.md not found!") + return + + with open(README_PATH, "r", encoding="utf-8") as f: + content = f.read() + + payload = { + "model": API_MODEL, + "messages": [ + { + "role": "system", + "content": "You are a translation assistant that translates English Markdown text to Chinese. Preserve original Markdown formatting and line breaks." + }, + { + "role": "user", + "content": content + } + ], + "temperature": 0.3, + "stream": True + } + + print("Streaming translation started...") + stream_translation(API_URL, API_KEY, payload) + print(f"Translation completed and saved to {OUTPUT_PATH}.") + + if __name__ == "__main__": + main() + EOF + + python3 translate_readme.py + rm -rf translate_readme.py + + - name: Create Pull Request + if: env.skip_translation == 'false' + uses: peter-evans/create-pull-request@v7 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: "Update helm translated README.zh.md" + branch: update-helm-readme-zh + title: "Update helm translated README.zh.md" + body: | + This PR updates the translated README.zh.md file. + + - Automatically generated by GitHub Actions + labels: translation, automated + base: main