diff --git a/.github/workflows/auto-request-copilot-review.yml b/.github/workflows/auto-request-copilot-review.yml index 3f2bb6c..97f2b9a 100644 --- a/.github/workflows/auto-request-copilot-review.yml +++ b/.github/workflows/auto-request-copilot-review.yml @@ -1,8 +1,9 @@ name: Auto-request Copilot review on: - # 仅操作主仓 PR 元数据;本流程不检出或执行 PR 代码。 - pull_request_target: + # 仅对主仓非草稿 PR 操作元数据;本流程不检出或执行 PR 代码。 + # GitHub 公仓默认禁用 pull_request_target,改用 pull_request 并仅对同仓 PR 执行写操作。 + pull_request: types: [opened, reopened, ready_for_review] permissions: @@ -10,7 +11,7 @@ permissions: jobs: request: - if: github.event.pull_request.draft == false + if: github.event.pull_request.draft == false && github.event.pull_request.head.repo.full_name == github.repository runs-on: ubuntu-latest timeout-minutes: 2 steps: diff --git a/.github/workflows/i18n-translate.yml b/.github/workflows/i18n-translate.yml index a42c17f..e266da7 100644 --- a/.github/workflows/i18n-translate.yml +++ b/.github/workflows/i18n-translate.yml @@ -10,18 +10,21 @@ name: i18n Auto-Translate # new-api gateways, vLLM, ...). Set secret TRANSLATE_API_KEY, secret or # variable TRANSLATE_ENDPOINT (API base ending in /v1), and TRANSLATE_MODEL. # -# Trigger is `pull_request_target` (NOT `pull_request`) so that fork PRs also -# get secrets and a write token — this is what allows auto-translating and -# pushing translations back to fork branches ("Allow edits by maintainers"). +# Trigger is `pull_request` (GitHub restricts `pull_request_target` on public +# repositories by default). Same-repo PRs receive secrets and write permissions +# to auto-translate and push commits back to the PR branch; fork PRs run in +# read-only `--check` mode without pushing or commenting. # # SECURITY MODEL — read before editing: -# pull_request_target runs with secrets in the BASE repo context, so this -# workflow must NEVER execute code from the PR head. We therefore: +# Even under `pull_request`, this workflow never executes code from the PR +# head when secrets/write tokens are available. We therefore: # 1. check out the trusted base branch (workflow + scripts/i18n/* run from it), # 2. fetch the PR *merge ref* and copy in DATA files only # (skills/, manifest.json, categories.json), dropping symlinks, # 3. run the trusted scripts over that data, -# 4. push resulting translations to the PR head branch with the bot token. +# 4. for same-repo PRs, push resulting translations to the PR head branch +# with the bot token; for fork PRs, fail cleanly on stale translations +# with instructions to translate locally. # Any value derived from the PR (branch name, repo name) is passed to shell # steps via `env:` — never interpolated with ${{ }} inside `run:`. # @@ -32,7 +35,7 @@ name: i18n Auto-Translate # scan unless `skill` input is supplied. on: - pull_request_target: + pull_request: workflow_dispatch: inputs: target_locale: @@ -49,8 +52,7 @@ permissions: models: read concurrency: - # Per-PR group (pull_request_target's github.ref is the base branch, which - # would otherwise serialize/cancel unrelated PRs). + # Per-PR group so concurrent pushes to the same PR cancel older runs. group: i18n-translate-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true @@ -64,13 +66,14 @@ jobs: - name: Checkout trusted base uses: actions/checkout@v4 with: - # No `ref:` on purpose — for pull_request_target this is the BASE - # branch (trusted scripts); for workflow_dispatch it's the chosen ref. + # Explicitly check out the BASE branch on pull_request (trusted + # scripts); for workflow_dispatch check out the chosen ref. + ref: ${{ github.event_name == 'pull_request' && github.base_ref || github.ref }} token: ${{ secrets.DESIRECORE_BOT_TOKEN || secrets.GITHUB_TOKEN }} fetch-depth: 0 - name: Overlay PR content (data only) - if: github.event_name == 'pull_request_target' + if: github.event_name == 'pull_request' env: PR_NUMBER: ${{ github.event.pull_request.number }} run: | @@ -107,7 +110,7 @@ jobs: # it explicitly so the `|| true` workaround on grep below is unambiguous. set -eo pipefail # workflow_dispatch: full scan (skill input handled later if provided) - if [ "${{ github.event_name }}" != "pull_request_target" ]; then + if [ "${{ github.event_name }}" != "pull_request" ]; then echo "relevant=true" >> "$GITHUB_OUTPUT" echo "skill_paths=" >> "$GITHUB_OUTPUT" exit 0 @@ -172,7 +175,7 @@ jobs: # workflow_dispatch with explicit skill input takes precedence ARGS+=("$INPUT_SKILL") elif [ -n "$SKILL_PATHS" ]; then - # pull_request_target: read space-separated paths into an array to + # pull_request: read space-separated paths into an array to # avoid word-splitting / glob expansion surprises. read -r -a SKILL_ARR <<<"$SKILL_PATHS" ARGS+=("${SKILL_ARR[@]}") @@ -186,8 +189,14 @@ jobs: fi exit 0 + - name: Fail on stale translations in fork PR + if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true' && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository + run: | + echo "::error::Stale or missing i18n translations detected in a fork PR. Under pull_request, fork PRs run with a read-only token and cannot auto-translate or push commits. Please run 'uv run scripts/i18n/translate.py' locally and commit the generated SKILL..md files." + exit 1 + - name: Translate stale locales - if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true' + if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} @@ -205,7 +214,7 @@ jobs: # workflow_dispatch input takes precedence (single specific skill) ARGS+=("$INPUT_SKILL") elif [ -n "$SKILL_PATHS" ]; then - # pull_request_target: only translate skills touched by this PR + # pull_request: only translate skills touched by this PR read -r -a SKILL_ARR <<<"$SKILL_PATHS" ARGS+=("${SKILL_ARR[@]}") fi @@ -215,12 +224,12 @@ jobs: uv run --quiet scripts/i18n/translate.py "${ARGS[@]}" - name: Validate after translation - if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true' + if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) run: uv run --quiet scripts/i18n/validate-i18n.py - name: Detect changes id: diff - if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true' + if: steps.changes.outputs.relevant == 'true' && steps.precheck.outputs.stale == 'true' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) run: | if git diff --quiet; then echo "changed=false" >> "$GITHUB_OUTPUT" @@ -230,7 +239,7 @@ jobs: fi - name: Push translations to PR branch - if: steps.diff.outputs.changed == 'true' && github.event_name == 'pull_request_target' + if: steps.diff.outputs.changed == 'true' && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository env: HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }} HEAD_REF: ${{ github.event.pull_request.head.ref }} @@ -281,7 +290,7 @@ jobs: fi - name: Commit & push translations - if: steps.diff.outputs.changed == 'true' && github.event_name != 'pull_request_target' + if: steps.diff.outputs.changed == 'true' && github.event_name != 'pull_request' run: | git config user.name "desirecore-bot" git config user.email "bot@desirecore.net" @@ -295,7 +304,7 @@ jobs: TRANSLATE_MODEL: ${{ vars.TRANSLATE_MODEL || 'openai/gpt-5-mini' }} - name: Comment on PR - if: steps.diff.outputs.changed == 'true' && github.event_name == 'pull_request_target' + if: steps.diff.outputs.changed == 'true' && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository uses: actions/github-script@v7 with: github-token: ${{ secrets.DESIRECORE_BOT_TOKEN || secrets.GITHUB_TOKEN }} @@ -325,7 +334,7 @@ jobs: TRANSLATE_MODEL: ${{ vars.TRANSLATE_MODEL || 'openai/gpt-5-mini' }} - name: Label on translation failure - if: failure() && github.event_name == 'pull_request_target' + if: failure() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository uses: actions/github-script@v7 with: github-token: ${{ secrets.DESIRECORE_BOT_TOKEN || secrets.GITHUB_TOKEN }} diff --git a/scripts/catalog/test_review_workflow.py b/scripts/catalog/test_review_workflow.py index b91b706..a4b1aa6 100644 --- a/scripts/catalog/test_review_workflow.py +++ b/scripts/catalog/test_review_workflow.py @@ -14,9 +14,12 @@ class ReviewWorkflowTests(unittest.TestCase): path = Path(__file__).resolve().parents[2] / '.github/workflows/auto-request-copilot-review.yml' text = path.read_text() workflow = yaml.load(text, Loader=yaml.BaseLoader) - self.assertIn('pull_request_target', workflow['on']) - self.assertNotIn('pull_request', workflow['on']) + self.assertIn('pull_request', workflow['on']) + self.assertNotIn('pull_request_target', workflow['on']) self.assertEqual(workflow['permissions']['pull-requests'], 'write') + job_if = workflow['jobs']['request']['if'] + self.assertIn('github.event.pull_request.draft == false', job_if) + self.assertIn('github.event.pull_request.head.repo.full_name == github.repository', job_if) for step in workflow['jobs']['request']['steps']: self.assertNotIn('uses', step) script = step.get('run', '')