Skip to content

Commit 9aafe20

Browse files
authored
Merge pull request KuekHaoYang#59 from sky06walker/main
feat: add automated upstream sync workflow
2 parents 950b05f + 1b3e241 commit 9aafe20

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Upstream Sync
2+
3+
permissions:
4+
contents: write
5+
pull-requests: write # Required to create the PR
6+
7+
env:
8+
UPSTREAM_REPO: KuekHaoYang/KVideo
9+
UPSTREAM_BRANCH: main
10+
TARGET_BRANCH: main
11+
SYNC_BRANCH: upstream-sync-conflict # The branch name used if a conflict occurs
12+
13+
on:
14+
schedule:
15+
- cron: "0 */6 * * *"
16+
workflow_dispatch:
17+
18+
jobs:
19+
sync_latest_from_upstream:
20+
name: Sync latest commits from upstream repo
21+
runs-on: ubuntu-latest
22+
if: ${{ github.event.repository.fork }}
23+
24+
steps:
25+
- name: Checkout target repo
26+
uses: actions/checkout@v4
27+
with:
28+
fetch-depth: 0 # Required to access full history for merging
29+
30+
- name: Sync and Merge (with PR Fallback)
31+
env:
32+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
run: |
34+
# 1. Configure Git
35+
git config user.name "github-actions[bot]"
36+
git config user.email "github-actions[bot]@users.noreply.github.com"
37+
38+
# 2. Add Upstream Remote
39+
git remote add upstream "https://github.com/$UPSTREAM_REPO.git"
40+
git fetch upstream
41+
42+
# 3. Attempt Merge
43+
echo "Attempting to merge upstream/$UPSTREAM_BRANCH into $TARGET_BRANCH..."
44+
45+
if git merge upstream/$UPSTREAM_BRANCH; then
46+
echo "✅ Merge successful. Pushing changes..."
47+
git push origin $TARGET_BRANCH
48+
else
49+
echo "⚠️ Merge conflict detected. Aborting merge and creating PR..."
50+
git merge --abort
51+
52+
# 4. Create a clean branch from upstream content
53+
git checkout -B $SYNC_BRANCH upstream/$UPSTREAM_BRANCH
54+
git push -f origin $SYNC_BRANCH
55+
56+
# 5. Create Pull Request using GitHub CLI
57+
# The '|| true' prevents failure if the PR already exists
58+
gh pr create \
59+
--title "🚨 Manual Sync Required: Conflicts with Upstream" \
60+
--body "The automatic sync failed due to merge conflicts. This PR contains the latest upstream changes. Please resolve conflicts and merge manually." \
61+
--head $SYNC_BRANCH \
62+
--base $TARGET_BRANCH \
63+
|| echo "PR already exists or could not be created."
64+
fi
65+
66+
- name: Delete workflow runs
67+
uses: Mattraks/delete-workflow-runs@v2
68+
with:
69+
token: ${{ secrets.GITHUB_TOKEN }}
70+
repository: ${{ github.repository }}
71+
retain_days: 1
72+
keep_minimum_runs: 3

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Upstream Sync](https://github.com/sky06walker/KVideo/actions/workflows/Github_Upstream_Sync.yml/badge.svg)](https://github.com/sky06walker/KVideo/actions/workflows/Github_Upstream_Sync.yml)
2+
13
# 视频聚合平台 (KVideo)
24

35
![KVideo Banner](public/icon.png)
@@ -480,4 +482,4 @@ npm start
480482

481483
## Star History
482484

483-
[![Star History Chart](https://api.star-history.com/svg?repos=KuekHaoYang/KVideo&type=date&legend=top-left)](https://www.star-history.com/#KuekHaoYang/KVideo&type=date&legend=top-left)
485+
[![Star History Chart](https://api.star-history.com/svg?repos=KuekHaoYang/KVideo&type=date&legend=top-left)](https://www.star-history.com/#KuekHaoYang/KVideo&type=date&legend=top-left)

0 commit comments

Comments
 (0)