|
| 1 | +# Copyright 2024-2026 the original author or authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +name: Bidirectional Issue Translation and Update |
| 16 | + |
| 17 | +on: |
| 18 | + issues: |
| 19 | + types: [opened] |
| 20 | +env: |
| 21 | + ANTHROPIC_BASE_URL: "https://open.bigmodel.cn/api/anthropic" |
| 22 | +jobs: |
| 23 | + translate_and_update: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + permissions: |
| 26 | + contents: read |
| 27 | + issues: write |
| 28 | + id-token: write |
| 29 | + steps: |
| 30 | + - name: Checkout repository |
| 31 | + uses: actions/checkout@v4 |
| 32 | + with: |
| 33 | + fetch-depth: 1 |
| 34 | + |
| 35 | + - name: Translate issue body with Claude (auto detect) |
| 36 | + id: translate |
| 37 | + uses: anthropics/claude-code-action@v1 |
| 38 | + with: |
| 39 | + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} |
| 40 | + prompt: | |
| 41 | + 你是一名专业的双语翻译。请自动检测下面文本的主要语言(只会是中文或英文): |
| 42 | + - 如果主要语言是中文,请将其准确、地道地翻译为英文,只返回英文翻译。 |
| 43 | + - 如果主要语言是英文,请将其准确、地道地翻译为中文,只返回中文翻译。 |
| 44 | + - 不要解释,不要输出原文,只返回翻译内容。 |
| 45 | + --- |
| 46 | + ${{ github.event.issue.body }} |
| 47 | +
|
| 48 | + - name: Update issue body with original and translation |
| 49 | + uses: actions/github-script@v7 |
| 50 | + with: |
| 51 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 52 | + script: | |
| 53 | + const issue_number = context.issue.number; |
| 54 | + const owner = context.repo.owner; |
| 55 | + const repo = context.repo.repo; |
| 56 | + const orig = context.payload.issue.body || ''; |
| 57 | +
|
| 58 | + // Get the translation from the previous step |
| 59 | + // Note: The exact output property may vary, trying common ones |
| 60 | + const stepOutputs = ${{ toJson(steps.translate.outputs) }}; |
| 61 | + const translated = (stepOutputs.response || stepOutputs.result || stepOutputs.output || '').trim(); |
| 62 | +
|
| 63 | + // Only update if we have a translation and it's different from original |
| 64 | + if (translated && translated !== orig && translated !== 'undefined') { |
| 65 | + const newBody = `## 原文 / Original\n${orig}\n\n## 翻译 / Translation\n${translated}`; |
| 66 | + await github.rest.issues.update({ |
| 67 | + owner, |
| 68 | + repo, |
| 69 | + issue_number, |
| 70 | + body: newBody, |
| 71 | + }); |
| 72 | + console.log('Successfully updated issue with translation'); |
| 73 | + } else { |
| 74 | + console.log('No valid translation received or translation same as original'); |
| 75 | + } |
| 76 | +
|
| 77 | + - name: Post translation workflow summary |
| 78 | + if: always() |
| 79 | + uses: actions/github-script@v7 |
| 80 | + with: |
| 81 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 82 | + script: | |
| 83 | + const issue_number = context.issue.number; |
| 84 | + const owner = context.repo.owner; |
| 85 | + const repo = context.repo.repo; |
| 86 | +
|
| 87 | + // Get translation result |
| 88 | + const stepOutputs = ${{ toJson(steps.translate.outputs) }}; |
| 89 | + const translated = (stepOutputs.response || stepOutputs.result || stepOutputs.output || '').trim(); |
| 90 | + const original = context.payload.issue.body || ''; |
| 91 | +
|
| 92 | + // Get workflow run URL |
| 93 | + const runUrl = `https://github.com/${owner}/${repo}/actions/runs/${{ github.run_id }}`; |
| 94 | +
|
| 95 | + let commentBody = `## 🌐 Translation Workflow Summary\n\n`; |
| 96 | +
|
| 97 | + if (translated && translated !== original && translated !== 'undefined') { |
| 98 | + // Detect language |
| 99 | + const isChineseOriginal = /[\u4e00-\u9fff]/.test(original); |
| 100 | + const targetLang = isChineseOriginal ? 'English' : 'Chinese'; |
| 101 | + commentBody += `✅ **Translation completed successfully**\n`; |
| 102 | + commentBody += `- **Source language:** ${isChineseOriginal ? 'Chinese' : 'English'}\n`; |
| 103 | + commentBody += `- **Target language:** ${targetLang}\n`; |
| 104 | + commentBody += `- **Issue body has been updated** with both original and translated content\n\n`; |
| 105 | + } else { |
| 106 | + commentBody += `ℹ️ **Translation skipped**\n`; |
| 107 | + commentBody += `- Reason: Translation same as original or no valid translation received\n\n`; |
| 108 | + } |
| 109 | +
|
| 110 | + commentBody += `**Workflow Run:** [View Details](${runUrl})\n`; |
| 111 | + commentBody += `**Time:** ${new Date().toISOString()}\n\n`; |
| 112 | + commentBody += `---\n*This comment was automatically generated by the Translation workflow.*`; |
| 113 | +
|
| 114 | + await github.rest.issues.createComment({ |
| 115 | + owner, |
| 116 | + repo, |
| 117 | + issue_number, |
| 118 | + body: commentBody |
| 119 | + }); |
| 120 | + console.log('Posted translation summary to issue #' + issue_number); |
0 commit comments