|
| 1 | +name: Sync Docs Changes from ZH PR to EN PR |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + source_pr_url: |
| 7 | + description: 'Source PR URL (Chinese docs repository)' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + default: '' |
| 11 | + target_pr_url: |
| 12 | + description: 'Target PR URL (English docs repository)' |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + default: '' |
| 16 | + ai_provider: |
| 17 | + description: 'AI Provider to use for translation' |
| 18 | + required: false |
| 19 | + type: choice |
| 20 | + options: |
| 21 | + - deepseek |
| 22 | + - gemini |
| 23 | + default: 'gemini' |
| 24 | + |
| 25 | +jobs: |
| 26 | + sync-docs: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout current repository |
| 31 | + uses: actions/checkout@v4 |
| 32 | + with: |
| 33 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + fetch-depth: 0 |
| 35 | + |
| 36 | + - name: Checkout ai-pr-translator repository |
| 37 | + uses: actions/checkout@v4 |
| 38 | + with: |
| 39 | + repository: "qiancai/ai-pr-translator" |
| 40 | + ref: "main" |
| 41 | + path: "ai-pr-translator" |
| 42 | + |
| 43 | + - name: Set up Python |
| 44 | + uses: actions/setup-python@v4 |
| 45 | + with: |
| 46 | + python-version: '3.9' |
| 47 | + |
| 48 | + - name: Install dependencies |
| 49 | + run: pip install -r ai-pr-translator/scripts/requirements.txt |
| 50 | + |
| 51 | + - name: Extract PR information |
| 52 | + id: extract_info |
| 53 | + env: |
| 54 | + SOURCE_URL: ${{ github.event.inputs.source_pr_url }} |
| 55 | + TARGET_URL: ${{ github.event.inputs.target_pr_url }} |
| 56 | + run: | |
| 57 | + if [[ ! "$SOURCE_URL" =~ ^https://github\.com/[^/]+/[^/]+/pull/[0-9]+$ ]]; then |
| 58 | + echo "❌ Invalid source PR URL format"; exit 1 |
| 59 | + fi |
| 60 | + if [[ ! "$TARGET_URL" =~ ^https://github\.com/[^/]+/[^/]+/pull/[0-9]+$ ]]; then |
| 61 | + echo "❌ Invalid target PR URL format"; exit 1 |
| 62 | + fi |
| 63 | + |
| 64 | + SOURCE_OWNER=$(echo "$SOURCE_URL" | cut -d'/' -f4) |
| 65 | + SOURCE_REPO=$(echo "$SOURCE_URL" | cut -d'/' -f5) |
| 66 | + SOURCE_PR=$(echo "$SOURCE_URL" | cut -d'/' -f7) |
| 67 | + TARGET_OWNER=$(echo "$TARGET_URL" | cut -d'/' -f4) |
| 68 | + TARGET_REPO=$(echo "$TARGET_URL" | cut -d'/' -f5) |
| 69 | + TARGET_PR=$(echo "$TARGET_URL" | cut -d'/' -f7) |
| 70 | + |
| 71 | + { |
| 72 | + echo "source_owner<<EOF"; echo "$SOURCE_OWNER"; echo "EOF" |
| 73 | + echo "source_repo<<EOF"; echo "$SOURCE_REPO"; echo "EOF" |
| 74 | + echo "source_pr<<EOF"; echo "$SOURCE_PR"; echo "EOF" |
| 75 | + echo "target_owner<<EOF"; echo "$TARGET_OWNER"; echo "EOF" |
| 76 | + echo "target_repo<<EOF"; echo "$TARGET_REPO"; echo "EOF" |
| 77 | + echo "target_pr<<EOF"; echo "$TARGET_PR"; echo "EOF" |
| 78 | + } >> $GITHUB_OUTPUT |
| 79 | + |
| 80 | + echo "Source: ${SOURCE_OWNER}/${SOURCE_REPO}#${SOURCE_PR}" |
| 81 | + echo "Target: ${TARGET_OWNER}/${TARGET_REPO}#${TARGET_PR}" |
| 82 | +
|
| 83 | + - name: Get target PR branch info |
| 84 | + id: target_branch |
| 85 | + env: |
| 86 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 87 | + TARGET_OWNER: ${{ steps.extract_info.outputs.target_owner }} |
| 88 | + TARGET_REPO: ${{ steps.extract_info.outputs.target_repo }} |
| 89 | + TARGET_PR: ${{ steps.extract_info.outputs.target_pr }} |
| 90 | + run: | |
| 91 | + PR_INFO=$(curl -s -H "Authorization: token ${GH_TOKEN}" -H "Accept: application/vnd.github.v3+json" \ |
| 92 | + "https://api.github.com/repos/${TARGET_OWNER}/${TARGET_REPO}/pulls/${TARGET_PR}") |
| 93 | + TARGET_BRANCH=$(echo "$PR_INFO" | jq -r '.head.ref') |
| 94 | + HEAD_REPO=$(echo "$PR_INFO" | jq -r '.head.repo.full_name') |
| 95 | + echo "target_branch=${TARGET_BRANCH}" >> $GITHUB_OUTPUT |
| 96 | + echo "head_repo=${HEAD_REPO}" >> $GITHUB_OUTPUT |
| 97 | + echo "Target branch: ${TARGET_BRANCH}, Head repo: ${HEAD_REPO}" |
| 98 | +
|
| 99 | + - name: Clone target repository |
| 100 | + env: |
| 101 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 102 | + HEAD_REPO: ${{ steps.target_branch.outputs.head_repo }} |
| 103 | + TARGET_BRANCH: ${{ steps.target_branch.outputs.target_branch }} |
| 104 | + run: | |
| 105 | + git clone "https://x-access-token:${GITHUB_TOKEN}@github.com/${HEAD_REPO}.git" target_repo |
| 106 | + cd target_repo && git checkout "$TARGET_BRANCH" |
| 107 | + git config user.name "github-actions[bot]" |
| 108 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 109 | +
|
| 110 | + - name: Run sync script |
| 111 | + id: sync_script |
| 112 | + env: |
| 113 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 114 | + DEEPSEEK_API_TOKEN: ${{ secrets.DEEPSEEK_API_TOKEN }} |
| 115 | + GEMINI_API_TOKEN: ${{ secrets.GEMINI_API_TOKEN }} |
| 116 | + SOURCE_PR_URL: ${{ github.event.inputs.source_pr_url }} |
| 117 | + TARGET_PR_URL: ${{ github.event.inputs.target_pr_url }} |
| 118 | + AI_PROVIDER: ${{ github.event.inputs.ai_provider }} |
| 119 | + TARGET_REPO_PATH: ${{ github.workspace }}/target_repo |
| 120 | + run: | |
| 121 | + cd ai-pr-translator/scripts |
| 122 | + if python main_workflow.py; then |
| 123 | + echo "sync_success=true" >> $GITHUB_OUTPUT |
| 124 | + echo "✅ Sync script completed successfully" |
| 125 | + else |
| 126 | + echo "sync_success=false" >> $GITHUB_OUTPUT |
| 127 | + echo "❌ Sync script failed" |
| 128 | + exit 1 |
| 129 | + fi |
| 130 | +
|
| 131 | + - name: Commit and push changes |
| 132 | + if: steps.sync_script.outputs.sync_success == 'true' |
| 133 | + env: |
| 134 | + SOURCE_PR_URL: ${{ github.event.inputs.source_pr_url }} |
| 135 | + TARGET_PR_URL: ${{ github.event.inputs.target_pr_url }} |
| 136 | + AI_PROVIDER: ${{ github.event.inputs.ai_provider }} |
| 137 | + TARGET_BRANCH: ${{ steps.target_branch.outputs.target_branch }} |
| 138 | + run: | |
| 139 | + cd target_repo && git add . |
| 140 | + if ! git diff --staged --quiet; then |
| 141 | + printf "Auto-sync: Update English docs from Chinese PR\n\nSynced from: %s\nTarget PR: %s\nAI Provider: %s\n\nCo-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>" \ |
| 142 | + "$SOURCE_PR_URL" "$TARGET_PR_URL" "$AI_PROVIDER" | git commit -F - |
| 143 | + git push origin "$TARGET_BRANCH" |
| 144 | + echo "Changes pushed to $TARGET_BRANCH" |
| 145 | + else |
| 146 | + echo "No changes to commit" |
| 147 | + fi |
| 148 | +
|
| 149 | + - name: Add success comment to target PR |
| 150 | + if: steps.sync_script.outputs.sync_success == 'true' |
| 151 | + env: |
| 152 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 153 | + SOURCE_PR_URL: ${{ github.event.inputs.source_pr_url }} |
| 154 | + TARGET_PR_URL: ${{ github.event.inputs.target_pr_url }} |
| 155 | + TARGET_OWNER: ${{ steps.extract_info.outputs.target_owner }} |
| 156 | + TARGET_REPO: ${{ steps.extract_info.outputs.target_repo }} |
| 157 | + TARGET_PR: ${{ steps.extract_info.outputs.target_pr }} |
| 158 | + run: | |
| 159 | + BODY=$(printf '%s\n\n%s\n%s\n%s\n\n%s' "**Auto-sync completed successfully**" \ |
| 160 | + "**Source PR**: ${SOURCE_PR_URL}" "**Target PR**: ${TARGET_PR_URL}" \ |
| 161 | + "English documentation has been updated based on Chinese documentation changes." ) |
| 162 | + PAYLOAD=$(jq -n --arg body "$BODY" '{body: $body}') |
| 163 | + curl -X POST -H "Authorization: token ${GITHUB_TOKEN}" \ |
| 164 | + -H "Accept: application/vnd.github.v3+json" \ |
| 165 | + "https://api.github.com/repos/${TARGET_OWNER}/${TARGET_REPO}/issues/${TARGET_PR}/comments" \ |
| 166 | + -d "$PAYLOAD" |
| 167 | +
|
| 168 | + - name: Add failure comment to target PR |
| 169 | + if: steps.sync_script.outputs.sync_success == 'false' |
| 170 | + env: |
| 171 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 172 | + SOURCE_PR_URL: ${{ github.event.inputs.source_pr_url }} |
| 173 | + TARGET_PR_URL: ${{ github.event.inputs.target_pr_url }} |
| 174 | + TARGET_OWNER: ${{ steps.extract_info.outputs.target_owner }} |
| 175 | + TARGET_REPO: ${{ steps.extract_info.outputs.target_repo }} |
| 176 | + TARGET_PR: ${{ steps.extract_info.outputs.target_pr }} |
| 177 | + run: | |
| 178 | + BODY=$(printf '%s\n\n%s\n%s\n%s\n\n%s' "**Auto-sync failed**" \ |
| 179 | + "**Source PR**: ${SOURCE_PR_URL}" "**Target PR**: ${TARGET_PR_URL}" \ |
| 180 | + "The sync process encountered an error. Please check the workflow logs for details.") |
| 181 | + PAYLOAD=$(jq -n --arg body "$BODY" '{body: $body}') |
| 182 | + curl -X POST -H "Authorization: token ${GITHUB_TOKEN}" \ |
| 183 | + -H "Accept: application/vnd.github.v3+json" \ |
| 184 | + "https://api.github.com/repos/${TARGET_OWNER}/${TARGET_REPO}/issues/${TARGET_PR}/comments" \ |
| 185 | + -d "$PAYLOAD" |
0 commit comments