|
| 1 | +name: Translate new posts to English |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [closed] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + translate: |
| 12 | + if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout main |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + ref: main |
| 19 | + |
| 20 | + - name: Get changed files from PR |
| 21 | + id: changes |
| 22 | + uses: actions/github-script@v7 |
| 23 | + with: |
| 24 | + script: | |
| 25 | + const pr = context.payload.pull_request; |
| 26 | + const { owner, repo } = context.repo; |
| 27 | + const pull_number = pr.number; |
| 28 | + const per_page = 100; |
| 29 | + let page = 1; |
| 30 | + const files = []; |
| 31 | + while (true) { |
| 32 | + const res = await github.rest.pulls.listFiles({ owner, repo, pull_number, per_page, page }); |
| 33 | + if (res.data.length === 0) break; |
| 34 | + for (const f of res.data) files.push(f.filename); |
| 35 | + if (res.data.length < per_page) break; |
| 36 | + page += 1; |
| 37 | + } |
| 38 | + const posts = files.filter(f => f.startsWith('_posts/') && f.endsWith('.md')); |
| 39 | + core.setOutput('posts', posts.join('\n')); |
| 40 | +
|
| 41 | + - name: Set ONLY_FILES env |
| 42 | + if: steps.changes.outputs.posts != '' |
| 43 | + run: | |
| 44 | + echo 'ONLY_FILES<<EOF' >> $GITHUB_ENV |
| 45 | + echo '${{ steps.changes.outputs.posts }}' >> $GITHUB_ENV |
| 46 | + echo 'EOF' >> $GITHUB_ENV |
| 47 | +
|
| 48 | + - name: Set up Python |
| 49 | + if: steps.changes.outputs.posts != '' |
| 50 | + uses: actions/setup-python@v5 |
| 51 | + with: |
| 52 | + python-version: '3.11' |
| 53 | + |
| 54 | + - name: Install dependencies |
| 55 | + if: steps.changes.outputs.posts != '' |
| 56 | + run: | |
| 57 | + python -m pip install --upgrade pip |
| 58 | + pip install openai PyYAML python-frontmatter python-slugify |
| 59 | +
|
| 60 | + - name: Generate English translations |
| 61 | + if: steps.changes.outputs.posts != '' |
| 62 | + env: |
| 63 | + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} |
| 64 | + TRANSLATION_MODEL: ${{ vars.TRANSLATION_MODEL }} |
| 65 | + run: | |
| 66 | + python scripts/translate_to_en.py |
| 67 | +
|
| 68 | + - name: Commit and push translations |
| 69 | + if: steps.changes.outputs.posts != '' |
| 70 | + uses: stefanzweifel/git-auto-commit-action@v5 |
| 71 | + with: |
| 72 | + commit_message: "chore: add English translations for PR #${{ github.event.pull_request.number }}" |
| 73 | + file_pattern: "_posts_en/**/*.md" |
| 74 | + |
| 75 | + |
0 commit comments