forked from router-for-me/CLIProxyAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
171 lines (148 loc) · 5.9 KB
/
Copy pathauto-sync-upstream-copilot.yml
File metadata and controls
171 lines (148 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
name: auto-sync-upstream-copilot
on:
schedule:
# daily at 02:30 UTC (10:30 Asia/Singapore) — offset 30m from auto-sync-upstream
- cron: '30 2 * * *'
workflow_dispatch:
permissions:
contents: write
pull-requests: write
issues: write
actions: write
concurrency:
group: auto-sync-upstream-copilot
cancel-in-progress: false
env:
UPSTREAM_URL: https://github.com/router-for-me/CLIProxyAPI.git
UPSTREAM_BRANCH: main
TARGET_BRANCH: feat/copilot
PR_BRANCH: auto-sync/upstream-main-copilot
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout target branch
uses: actions/checkout@v4
with:
ref: ${{ env.TARGET_BRANCH }}
fetch-depth: 0
- name: Configure git identity
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Add upstream remote and fetch
run: |
git remote add upstream "$UPSTREAM_URL"
git fetch upstream "$UPSTREAM_BRANCH"
- name: Check if behind upstream
id: check
run: |
BEHIND=$(git rev-list --count "HEAD..upstream/${UPSTREAM_BRANCH}")
echo "behind=$BEHIND" >> "$GITHUB_OUTPUT"
if [ "$BEHIND" = "0" ]; then
echo "Already up to date with upstream/${UPSTREAM_BRANCH}."
else
echo "$BEHIND new commits from upstream:"
git log --oneline "HEAD..upstream/${UPSTREAM_BRANCH}"
fi
- name: Build merge commit message
if: steps.check.outputs.behind != '0'
run: |
{
echo "Merge upstream/${UPSTREAM_BRANCH} (auto-sync feat/copilot)"
echo
git log --reverse --pretty=format:'- %h %s' "HEAD..upstream/${UPSTREAM_BRANCH}"
echo
} > /tmp/merge-msg.txt
cat /tmp/merge-msg.txt
- name: Try clean merge
if: steps.check.outputs.behind != '0'
id: merge
run: |
set +e
git merge --no-ff -F /tmp/merge-msg.txt "upstream/${UPSTREAM_BRANCH}"
rc=$?
if [ "$rc" = "0" ]; then
echo "clean=true" >> "$GITHUB_OUTPUT"
else
echo "clean=false" >> "$GITHUB_OUTPUT"
git merge --abort || true
fi
exit 0
- name: Set up Go
if: steps.merge.outputs.clean == 'true'
uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: true
- name: Verify build
if: steps.merge.outputs.clean == 'true'
id: build
run: |
set +e
go build -o /tmp/cli-proxy-api ./cmd/server
rc=$?
rm -f /tmp/cli-proxy-api
if [ "$rc" = "0" ]; then
echo "ok=true" >> "$GITHUB_OUTPUT"
else
echo "ok=false" >> "$GITHUB_OUTPUT"
fi
exit 0
- name: Push merged changes to target branch
if: steps.merge.outputs.clean == 'true' && steps.build.outputs.ok == 'true'
run: git push origin "${TARGET_BRANCH}"
- name: Trigger downstream image build
if: steps.merge.outputs.clean == 'true' && steps.build.outputs.ok == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh workflow run ghcr-feat-copilot.yml --repo "${{ github.repository }}" --ref "${TARGET_BRANCH}"
- name: Prepare PR branch (build failure case)
if: steps.merge.outputs.clean == 'true' && steps.build.outputs.ok != 'true'
run: |
# HEAD is the (clean) merge commit — push it as the PR branch so reviewers
# see exactly what would land, and pr-test-build.yml runs against it.
git push --force origin "HEAD:refs/heads/${PR_BRANCH}"
- name: Prepare PR branch (conflict case)
if: steps.check.outputs.behind != '0' && steps.merge.outputs.clean != 'true'
run: |
# Reset working tree, then push raw upstream tip as the PR branch so GitHub
# surfaces the conflict against feat/copilot and offers web-based resolution.
git reset --hard "origin/${TARGET_BRANCH}"
git checkout -B "${PR_BRANCH}" "upstream/${UPSTREAM_BRANCH}"
git push --force origin "${PR_BRANCH}"
- name: Open or update sync PR
if: steps.check.outputs.behind != '0' && (steps.merge.outputs.clean != 'true' || steps.build.outputs.ok != 'true')
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
run: |
set -e
if [ "${{ steps.merge.outputs.clean }}" != "true" ]; then
REASON='conflicts with `'"${TARGET_BRANCH}"'`'
else
REASON='merge clean but `go build` failed'
fi
{
echo "Auto-sync from \`upstream/${UPSTREAM_BRANCH}\` (router-for-me/CLIProxyAPI) into \`${TARGET_BRANCH}\`."
echo
echo "**Reason for manual review:** ${REASON}"
echo
echo "### New commits"
git log --reverse --pretty=format:'- %h %s' "origin/${TARGET_BRANCH}..upstream/${UPSTREAM_BRANCH}"
} > /tmp/pr-body.md
# Idempotently ensure the label exists.
gh label create auto-sync-copilot --color FBCA04 --description "Automated upstream sync (feat/copilot)" || true
EXISTING=$(gh pr list --head "${PR_BRANCH}" --base "${TARGET_BRANCH}" --state open --json number --jq '.[0].number' || true)
TITLE="auto-sync(copilot): merge upstream/${UPSTREAM_BRANCH} (${REASON})"
if [ -n "$EXISTING" ]; then
gh pr edit "$EXISTING" --title "$TITLE" --body-file /tmp/pr-body.md
echo "Updated existing PR #$EXISTING"
else
gh pr create \
--base "${TARGET_BRANCH}" \
--head "${PR_BRANCH}" \
--title "$TITLE" \
--body-file /tmp/pr-body.md \
--label auto-sync-copilot
fi