Skip to content

Sync Upstream PRs

Sync Upstream PRs #372

Workflow file for this run

name: Sync Upstream PRs
on:
schedule:
- cron: '0 */6 * * *' # 每6小时运行一次
workflow_dispatch: # 允许手动触发
jobs:
sync:
runs-on: ubuntu-latest
permissions: # 添加必要的权限
pull-requests: write
contents: write
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # 获取完整历史
token: ${{ secrets.PAT_TOKEN }} # 使用PAT以便推送分支
- name: Sync upstream pull requests
uses: actions/github-script@v6
with:
github-token: ${{ secrets.PAT_TOKEN }}
script: |
const upstream = 'songquanpeng/one-api';
const [owner, repo] = upstream.split('/');
// 使用分页获取所有上游仓库的PR
async function getAllPRs() {
let page = 1;
let allPulls = [];
while (true) {
const pulls = await github.rest.pulls.list({
owner,
repo,
state: 'open',
per_page: 100, // 每页获取最大数量
page: page
});
if (pulls.data.length === 0) break;
allPulls = allPulls.concat(pulls.data);
page++;
}
return allPulls;
}
// 获取所有PR
const pulls = await getAllPRs();
console.log(`Found ${pulls.length} open PRs in upstream repository`);
// 为每个PR创建或更新对应的PR
for (const pull of pulls) {
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: `${context.repo.owner}:${branchName}`,
state: 'all' // 检查所有状态的PR,包括open和closed
});
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: branchName,
base: 'main' // 根据实际情况修改目标分支
});
}
} catch (error) {
console.log(`Error processing PR #${pull.number}: ${error.message}`);
}
}