From 30341bc6b20ae93b1301400a509b17b3b8d9fb8d Mon Sep 17 00:00:00 2001 From: sagitchu <601096721@qq.com> Date: Tue, 18 Mar 2025 16:54:22 +0800 Subject: [PATCH] fix: access pr --- .github/workflows/sync-upstream.yml | 30 ++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/.github/workflows/sync-upstream.yml b/.github/workflows/sync-upstream.yml index 77226cd0..e58faa0e 100644 --- a/.github/workflows/sync-upstream.yml +++ b/.github/workflows/sync-upstream.yml @@ -14,14 +14,15 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 - + with: + fetch-depth: 0 # 获取完整历史 + - name: Sync upstream pull requests uses: actions/github-script@v6 with: - github-token: ${{ secrets.GITHUB_TOKEN }} # 使用默认的 GITHUB_TOKEN - # 或者使用您的自定义 token: ${{ secrets.YOUR_PAT }} + github-token: ${{ secrets.GITHUB_TOKEN }} script: | - const upstream = 'songquanpeng/one-api'; // 替换为实际的上游仓库 + const upstream = 'songquanpeng/one-api'; const [owner, repo] = upstream.split('/'); // 获取上游仓库的PR @@ -34,22 +35,37 @@ jobs: // 为每个PR创建或更新对应的PR for (const pull of pulls.data) { try { + const branchName = `upstream-pr-${pull.number}`; + + // 获取PR详细信息 + const prDetails = await github.rest.pulls.get({ + owner, + repo, + pull_number: pull.number + }); + + // 创建新分支 + await exec.exec('git', ['fetch', `https://github.com/${owner}/${repo}.git`, `pull/${pull.number}/head:${branchName}`]); + // 检查是否已存在相同的PR const existingPulls = await github.rest.pulls.list({ owner: context.repo.owner, repo: context.repo.repo, - head: `${owner}:${pull.head.ref}`, + head: `${context.repo.owner}:${branchName}`, }); if (existingPulls.data.length === 0) { + // 推送分支 + await exec.exec('git', ['push', 'origin', branchName]); + // 创建新的PR await github.rest.pulls.create({ owner: context.repo.owner, repo: context.repo.repo, title: `[Upstream] ${pull.title}`, body: `Synced from upstream PR: ${pull.html_url}\n\n${pull.body || ''}`, - head: `${owner}:${pull.head.ref}`, - base: pull.base.ref, + head: branchName, + base: 'main' // 根据实际情况修改目标分支 }); } } catch (error) {