mirror of
https://github.com/alibaba/higress.git
synced 2026-06-09 04:37:31 +08:00
feat: update translate-readme action (#2208)
This commit is contained in:
112
.github/workflows/helm-docs.yaml
vendored
112
.github/workflows/helm-docs.yaml
vendored
@@ -6,11 +6,13 @@ on:
|
|||||||
- "*"
|
- "*"
|
||||||
paths:
|
paths:
|
||||||
- 'helm/**'
|
- 'helm/**'
|
||||||
|
- '!helm/higress/README.zh.md'
|
||||||
workflow_dispatch: ~
|
workflow_dispatch: ~
|
||||||
push:
|
push:
|
||||||
branches: [ main ]
|
branches: [ main ]
|
||||||
paths:
|
paths:
|
||||||
- 'helm/**'
|
- 'helm/**'
|
||||||
|
- '!helm/higress/README.zh.md'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
helm:
|
helm:
|
||||||
@@ -31,7 +33,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
GOBIN=$PWD GO111MODULE=on go install github.com/norwoodj/helm-docs/cmd/helm-docs@v1.14.2
|
GOBIN=$PWD GO111MODULE=on go install github.com/norwoodj/helm-docs/cmd/helm-docs@v1.14.2
|
||||||
./helm-docs -c ${GITHUB_WORKSPACE}/helm/higress -f ../core/values.yaml
|
./helm-docs -c ${GITHUB_WORKSPACE}/helm/higress -f ../core/values.yaml
|
||||||
DIFF=$(git diff ${GITHUB_WORKSPACE}/helm/higress/*md)
|
DIFF=$(git diff ${GITHUB_WORKSPACE}/helm/higress/README.md)
|
||||||
if [ ! -z "$DIFF" ]; then
|
if [ ! -z "$DIFF" ]; then
|
||||||
echo "Please use helm-docs in your clone, of your fork, of the project, and commit a updated README.md for the chart."
|
echo "Please use helm-docs in your clone, of your fork, of the project, and commit a updated README.md for the chart."
|
||||||
fi
|
fi
|
||||||
@@ -55,18 +57,19 @@ jobs:
|
|||||||
id: compare_readme
|
id: compare_readme
|
||||||
run: |
|
run: |
|
||||||
cd ./helm/higress
|
cd ./helm/higress
|
||||||
BASE_BRANCH=main
|
|
||||||
UPSTREAM_REPO=https://github.com/alibaba/higress.git
|
BASE_BRANCH=${GITHUB_BASE_REF:-main}
|
||||||
|
git fetch origin $BASE_BRANCH
|
||||||
TEMP_DIR=$(mktemp -d)
|
|
||||||
git clone --depth 1 --branch $BASE_BRANCH $UPSTREAM_REPO $TEMP_DIR
|
if git diff --quiet origin/$BASE_BRANCH -- README.md; then
|
||||||
|
echo "README.md has no local changes compared to $BASE_BRANCH. Skipping translation."
|
||||||
if diff -q "$TEMP_DIR/README.md" README.md > /dev/null; then
|
|
||||||
echo "README.md has no changes in comparison to base branch. Skipping translation."
|
|
||||||
echo "skip_translation=true" >> $GITHUB_ENV
|
echo "skip_translation=true" >> $GITHUB_ENV
|
||||||
else
|
else
|
||||||
echo "README.md has changed in comparison to base branch. Proceeding with translation."
|
echo "README.md has local changes compared to $BASE_BRANCH. Proceeding with translation."
|
||||||
echo "skip_translation=false" >> $GITHUB_ENV
|
echo "skip_translation=false" >> $GITHUB_ENV
|
||||||
|
echo "--------- diff ---------"
|
||||||
|
git diff origin/$BASE_BRANCH -- README.md
|
||||||
|
echo "------------------------"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Translate README.md to Chinese
|
- name: Translate README.md to Chinese
|
||||||
@@ -76,39 +79,74 @@ jobs:
|
|||||||
API_KEY: ${{ secrets.HIGRESS_OPENAI_API_KEY }}
|
API_KEY: ${{ secrets.HIGRESS_OPENAI_API_KEY }}
|
||||||
API_MODEL: ${{ secrets.HIGRESS_OPENAI_API_MODEL }}
|
API_MODEL: ${{ secrets.HIGRESS_OPENAI_API_MODEL }}
|
||||||
run: |
|
run: |
|
||||||
cd ./helm/higress
|
cat << 'EOF' > translate_readme.py
|
||||||
FILE_CONTENT=$(cat README.md)
|
import os
|
||||||
|
import json
|
||||||
|
import requests
|
||||||
|
|
||||||
PAYLOAD=$(jq -n \
|
API_URL = os.environ["API_URL"]
|
||||||
--arg model "$API_MODEL" \
|
API_KEY = os.environ["API_KEY"]
|
||||||
--arg content "$FILE_CONTENT" \
|
API_MODEL = os.environ["API_MODEL"]
|
||||||
'{
|
README_PATH = "./helm/higress/README.md"
|
||||||
model: $model,
|
OUTPUT_PATH = "./helm/higress/README.zh.md"
|
||||||
messages: [
|
|
||||||
{"role": "system", "content": "You are a translation assistant that translates English Markdown text to Chinese."},
|
|
||||||
{"role": "user", "content": $content}
|
|
||||||
],
|
|
||||||
temperature: 1.1,
|
|
||||||
stream: false
|
|
||||||
}')
|
|
||||||
|
|
||||||
RESPONSE=$(curl -s -X POST "$API_URL" \
|
def stream_translation(api_url, api_key, payload):
|
||||||
-H "Content-Type: application/json" \
|
headers = {
|
||||||
-H "Authorization: Bearer $API_KEY" \
|
"Content-Type": "application/json",
|
||||||
-d "$PAYLOAD")
|
"Authorization": f"Bearer {api_key}",
|
||||||
|
}
|
||||||
|
response = requests.post(api_url, headers=headers, json=payload, stream=True)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
echo "Response: $RESPONSE"
|
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)
|
||||||
|
|
||||||
echo "$RESPONSE" | jq -c -r '.choices[] | .message.content' > README.zh.new.md
|
def main():
|
||||||
|
if not os.path.exists(README_PATH):
|
||||||
|
print("README.md not found!")
|
||||||
|
return
|
||||||
|
|
||||||
if [ -f "README.zh.new.md" ]; then
|
with open(README_PATH, "r", encoding="utf-8") as f:
|
||||||
echo "Translation completed and saved to README.zh.new.md."
|
content = f.read()
|
||||||
else
|
|
||||||
echo "Translation failed or no content returned!"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
mv README.zh.new.md README.zh.md
|
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
|
- name: Create Pull Request
|
||||||
if: env.skip_translation == 'false'
|
if: env.skip_translation == 'false'
|
||||||
|
|||||||
Reference in New Issue
Block a user