Skip to content

Commit 8a4393c

Browse files
authored
Fix: sync fork yaml final (#24)
* Fix: sync-fork yaml check pr logic3 * Fix: sync-fork yaml check pr logic4 * Fix: sync-fork yaml check pr logic debugging * Fix: sync-fork yaml check pr logic debugging2 * Fix: sync-fork yaml check pr logic debugging3 * Fix: sync-fork yaml check pr logic debugging4 * Fix: sync_fork_yml 추가 수정6 * Fix: sync-fork yaml check pr logic debugging5 * Fix: sync-fork yaml check pr logic debugging6 * Fix: sync-fork yaml check pr logic debugging7 * Fix: sync_fork_yml 추가 수정7 * Fix: sync_fork_yml 추가 수정8 * Fix: sync_fork_yml 추가 수정9 * Fix: sync_fork_yml 추가 수정10 * Fix: sync_fork_yml 추가 수정10 * Fix: sync_fork_yml 추가 수정11 * Fix: sync_fork_yml 추가 수정11
1 parent 30c7e62 commit 8a4393c

File tree

1 file changed

+52
-32
lines changed

1 file changed

+52
-32
lines changed

.github/workflows/sync_fork.yml

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: Sync Upstream and Create PR
22

33
on:
44
workflow_dispatch:
5+
schedule:
6+
- cron: '0 0 * * *' # 매일 밤 12시 정각에 실행
57

68
jobs:
79
sync:
@@ -30,54 +32,72 @@ jobs:
3032
- name: Check for Changes
3133
id: changes
3234
run: |
33-
git diff --exit-code origin/main upstream/main -- . ':(exclude).github/workflows/sync-upstream.yml' || echo "changes=true" >> $GITHUB_OUTPUT
34-
if [ ! -f $GITHUB_OUTPUT ]; then
35-
echo "changes=false" >> $GITHUB_OUTPUT
35+
if git diff --quiet origin/main upstream/main; then
36+
echo "changes=false" >> $GITHUB_ENV
37+
else
38+
echo "changes=true" >> $GITHUB_ENV
3639
fi
3740
3841
- name: Check Existing PRs
3942
id: existing-prs
4043
run: |
41-
EXISTING_PR=$(curl -s \
44+
# Fetch existing PRs
45+
RESPONSE=$(curl -s \
4246
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
4347
-H "Accept: application/vnd.github.v3+json" \
44-
https://api.github.com/repos/${{ github.repository }}/pulls \
45-
| jq -r '.[] | select(.head.ref | startswith("sync-upstream-")) | .head.ref')
48+
https://api.github.com/repos/${{ github.repository }}/pulls)
49+
50+
# Filter the response to get PRs starting with "sync-upstream-"
51+
EXISTING_PR=$(echo "$RESPONSE" | jq -r '.[] | select(.head.ref | startswith("sync-upstream-")) | .head.ref' | tr '\n' ' ')
4652
47-
if [ -n "$EXISTING_PR" ]; then
48-
echo "existing_pr=true" >> $GITHUB_ENV
49-
echo "existing_pr_ref=$EXISTING_PR" >> $GITHUB_ENV
53+
if [[ -n "$EXISTING_PR" ]]; then
54+
echo "existing_pr=true" >> $GITHUB_ENV
55+
echo "existing_pr_ref=$EXISTING_PR" >> $GITHUB_ENV
5056
else
51-
echo "existing_pr=false" >> $GITHUB_ENV
57+
echo "existing_pr=false" >> $GITHUB_ENV
5258
fi
5359
54-
- name: Compare Changes with Existing PR
60+
- name: Compare Changes with Each Existing PR
5561
id: compare-changes
56-
if: steps.existing-prs.outputs.existing_pr == 'true' && steps.changes.outputs.changes == 'true'
62+
if: env.existing_pr == 'true' && env.changes == 'true'
5763
run: |
58-
git fetch origin ${{ steps.existing-prs.outputs.existing_pr_ref }}:existing-pr-branch
59-
git diff --exit-code origin/main existing-pr-branch -- . ':(exclude).github/workflows/sync-upstream.yml' || echo "diff=true" >> $GITHUB_OUTPUT
60-
if [ ! -f $GITHUB_OUTPUT ]; then
61-
echo "diff=false" >> $GITHUB_OUTPUT
62-
fi
64+
ALL_EXISTING_INCLUDED=true
65+
for pr_ref in ${{ env.existing_pr_ref }}
66+
do
67+
echo "Fetching branch: $pr_ref"
68+
git fetch origin "$pr_ref:$pr_ref-branch"
6369
64-
- name: Close Existing PR
65-
if: steps.compare-changes.outputs.diff == 'false'
66-
run: |
67-
PR_NUMBER=$(curl -s \
68-
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
69-
-H "Accept: application/vnd.github.v3+json" \
70-
https://api.github.com/repos/${{ github.repository }}/pulls \
71-
| jq -r '.[] | select(.head.ref=="${{ steps.existing-prs.outputs.existing_pr_ref }}") | .number')
70+
echo "Comparing with upstream/main"
71+
if ! git diff --quiet upstream/main "$pr_ref-branch"; then
72+
ALL_EXISTING_INCLUDED=false
73+
break
74+
fi
75+
done
7276
73-
curl -X PATCH \
74-
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
75-
-H "Accept: application/vnd.github.v3+json" \
76-
https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER \
77-
-d '{"state":"closed"}'
77+
if [ "$ALL_EXISTING_INCLUDED" = true ]; then
78+
echo "diff=false" >> $GITHUB_ENV
79+
else
80+
echo "diff=true" >> $GITHUB_ENV
81+
82+
# Close all existing PRs
83+
for pr_ref in ${{ env.existing_pr_ref }}
84+
do
85+
PR_NUMBER=$(curl -s \
86+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
87+
-H "Accept: application/vnd.github.v3+json" \
88+
https://api.github.com/repos/${{ github.repository }}/pulls \
89+
| jq -r '.[] | select(.head.ref=="'$pr_ref'") | .number')
90+
91+
curl -X PATCH \
92+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
93+
-H "Accept: application/vnd.github.v3+json" \
94+
https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER \
95+
-d '{"state":"closed"}'
96+
done
97+
fi
7898
7999
- name: Create Branch and Rebase Changes
80-
if: steps.compare-changes.outputs.diff == 'true' || steps.existing-prs.outputs.existing_pr == 'false'
100+
if: env.diff == 'true' || env.existing_pr == 'false'
81101
run: |
82102
BRANCH_NAME=sync-upstream-$(date +%Y%m%d%H%M%S)
83103
git checkout -b $BRANCH_NAME origin/main
@@ -86,7 +106,7 @@ jobs:
86106
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
87107
88108
- name: Create Pull Request
89-
if: steps.compare-changes.outputs.diff == 'true' || steps.existing-prs.outputs.existing_pr == 'false'
109+
if: env.diff == 'true' || env.existing_pr == 'false'
90110
run: |
91111
curl -X POST \
92112
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \

0 commit comments

Comments
 (0)