@@ -2,6 +2,8 @@ name: Sync Upstream and Create PR
2
2
3
3
on :
4
4
workflow_dispatch :
5
+ schedule :
6
+ - cron : ' 0 0 * * *' # 매일 밤 12시 정각에 실행
5
7
6
8
jobs :
7
9
sync :
@@ -30,54 +32,72 @@ jobs:
30
32
- name : Check for Changes
31
33
id : changes
32
34
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
36
39
fi
37
40
38
41
- name : Check Existing PRs
39
42
id : existing-prs
40
43
run : |
41
- EXISTING_PR=$(curl -s \
44
+ # Fetch existing PRs
45
+ RESPONSE=$(curl -s \
42
46
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
43
47
-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' ' ')
46
52
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
50
56
else
51
- echo "existing_pr=false" >> $GITHUB_ENV
57
+ echo "existing_pr=false" >> $GITHUB_ENV
52
58
fi
53
59
54
- - name : Compare Changes with Existing PR
60
+ - name : Compare Changes with Each Existing PR
55
61
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'
57
63
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"
63
69
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
72
76
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
78
98
79
99
- 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'
81
101
run : |
82
102
BRANCH_NAME=sync-upstream-$(date +%Y%m%d%H%M%S)
83
103
git checkout -b $BRANCH_NAME origin/main
86
106
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
87
107
88
108
- 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'
90
110
run : |
91
111
curl -X POST \
92
112
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
0 commit comments