Skip to content

Commit 33dfa97

Browse files
authored
Merge pull request #57 from hamsurang/fix/yml
fix: compare-src, sync-upstream yml 재수정
2 parents 556a84d + f2b4bac commit 33dfa97

File tree

2 files changed

+179
-64
lines changed

2 files changed

+179
-64
lines changed

.github/workflows/compare-src.yml

+91-42
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: Update origin-src for Translation
22

33
on:
4-
push:
5-
branches:
6-
- master
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 0 * * *" # 매일 밤 12시 정각
77

88
jobs:
99
update-origin-src:
@@ -21,60 +21,109 @@ jobs:
2121
- name: Fetch All Branches
2222
run: git fetch --all
2323

24+
- name: Configure Git
25+
run: |
26+
git config --global user.name "minchodang"
27+
git config --global user.email "minsu910725@gmail.com"
28+
29+
# 1) origin/master의 src와 현재 repo의 origin-src 폴더 비교
2430
- name: Compare origin-src with src from origin/master
31+
id: compare
2532
run: |
26-
# 만약 origin-src 폴더가 없으면 업데이트가 필요한 것으로 간주합니다.
33+
# 만약 origin-src 폴더가 없으면 업데이트가 필요한 것으로 간주
2734
if [ ! -d origin-src ]; then
2835
echo "CHANGED" > diff_status.txt
2936
else
30-
# 임시 폴더를 만들어 origin/master의 src를 추출합니다.
37+
# 임시 폴더를 만들어 origin/master의 src를 추출
3138
mkdir tmp-src
3239
git archive origin/master src | tar -x -C tmp-src
33-
# origin-src와 tmp-src/src를 재귀적으로 비교합니다.
40+
# origin-src와 tmp-src/src 재귀 비교
3441
diff -r origin-src tmp-src/src > /dev/null || echo "CHANGED" > diff_status.txt
3542
fi
36-
if grep -q "CHANGED" diff_status.txt; then
43+
44+
# GitHub 환경변수에 반영
45+
if [ -f diff_status.txt ]; then
46+
echo "pr_needed=true" >> $GITHUB_ENV
3747
echo "Differences detected between origin-src and origin/master's src."
3848
else
49+
echo "pr_needed=false" >> $GITHUB_ENV
3950
echo "No differences detected."
4051
fi
4152
42-
- name: Update origin-src in master-ko Branch if Needed
53+
# 2) pr_needed이면 -> origin-src 갱신
54+
- name: Prepare master-ko
55+
if: env.pr_needed == 'true'
4356
run: |
44-
if grep -q "CHANGED" diff_status.txt; then
45-
BRANCH_NAME=update-origin-src-$(date +%Y%m%d%H%M%S)
46-
# master-ko 브랜치가 존재하면 해당 브랜치에서, 없으면 새로 생성합니다.
47-
if git rev-parse --verify origin/master-ko; then
48-
git checkout -B master-ko origin/master-ko
49-
else
50-
git checkout -B master-ko
51-
fi
52-
# 업데이트를 위한 새 브랜치 생성
53-
git checkout -b $BRANCH_NAME
54-
# 기존 origin-src 폴더 제거
55-
rm -rf origin-src
56-
# origin/master의 src 디렉토리 내용을 추출하여 origin-src로 복사합니다.
57-
git archive origin/master src | tar -x
58-
mv src origin-src
59-
# 변경사항이 있는 경우 커밋 및 PR 생성
60-
if ! git diff --quiet; then
61-
git add origin-src
62-
git commit -m "Update origin-src with latest changes from origin/master's src"
63-
git push origin $BRANCH_NAME
64-
curl -X POST \
65-
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
66-
-H "Accept: application/vnd.github.v3+json" \
67-
https://api.github.com/repos/${{ github.repository }}/pulls \
68-
-d '{
69-
"title": "Update origin-src for translation",
70-
"body": "This PR updates the origin-src directory in master-ko with the latest src changes from origin/master.",
71-
"head": "'"${BRANCH_NAME}"'",
72-
"base": "master-ko"
73-
}'
74-
else
75-
echo "No changes to commit after updating origin-src."
76-
fi
57+
# master-ko 브랜치가 존재하면 체크아웃, 없으면 새로
58+
if git rev-parse --verify origin/master-ko; then
59+
git checkout -B master-ko origin/master-ko
7760
else
78-
echo "No changes detected. Skipping update."
61+
git checkout -B master-ko
62+
fi
63+
64+
# 우선, 기존 origin-src 폴더 제거
65+
rm -rf origin-src
66+
# origin/master의 src 디렉토리 내용을 추출 -> origin-src로 복사
67+
git archive origin/master src | tar -x
68+
mv src origin-src
69+
70+
# 변경사항 커밋 (변경이 있으면)
71+
if ! git diff --quiet; then
72+
git add origin-src
73+
git commit -m "Update origin-src with latest changes from origin/master's src"
7974
fi
8075
shell: bash
76+
77+
# 3) 만약 pr_needed=true면, 기존 "update-origin-src-" 브랜치가 있는지 확인
78+
- name: Check Existing update-origin-src PR
79+
id: check-pr
80+
if: env.pr_needed == 'true'
81+
run: |
82+
RESPONSE=$(curl -s \
83+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
84+
-H "Accept: application/vnd.github.v3+json" \
85+
"https://api.github.com/repos/${{ github.repository }}/pulls")
86+
87+
# update-origin-src- 으로 시작하는 head.ref 찾기
88+
EXISTING_BRANCHES=$(echo "$RESPONSE" | jq -r '.[] | select(.head.ref | startswith("update-origin-src-")) | .head.ref' | tr '\n' ' ')
89+
90+
if [[ -n "$EXISTING_BRANCHES" ]]; then
91+
# 여러 개가 있을 수도 있지만, 여기서는 첫 번째만 사용 (원하시면 로직 수정 가능)
92+
FIRST_BRANCH=$(echo "$EXISTING_BRANCHES" | awk '{print $1}')
93+
echo "found_branch=$FIRST_BRANCH" >> $GITHUB_ENV
94+
echo "existing_pr_src=true" >> $GITHUB_ENV
95+
echo "Existing branch: $FIRST_BRANCH"
96+
else
97+
echo "existing_pr_src=false" >> $GITHUB_ENV
98+
echo "No existing update-origin-src branches."
99+
fi
100+
101+
# 4) 기존 브랜치가 있으면 -> force push 로 갱신(중복 PR 생기지 않음)
102+
- name: Force Push to Existing Branch
103+
if: env.pr_needed == 'true' && env.existing_pr_src == 'true'
104+
run: |
105+
BRANCH_NAME=${{ env.found_branch }}
106+
echo "Force pushing master-ko -> $BRANCH_NAME"
107+
git push origin master-ko:"$BRANCH_NAME" --force
108+
109+
# 5) 기존 브랜치가 없으면 -> 새 브랜치 & PR 생성
110+
- name: Create New PR Branch
111+
if: env.pr_needed == 'true' && env.existing_pr_src == 'false'
112+
run: |
113+
BRANCH_NAME=update-origin-src-$(date +%Y%m%d%H%M%S)
114+
git checkout master-ko
115+
git checkout -b $BRANCH_NAME
116+
git push origin $BRANCH_NAME
117+
118+
# 새 PR 생성
119+
curl -X POST \
120+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
121+
-H "Accept: application/vnd.github.v3+json" \
122+
"https://api.github.com/repos/${{ github.repository }}/pulls" \
123+
-d "{
124+
\"title\": \"Update origin-src for translation\",
125+
\"body\": \"This PR updates the origin-src directory in master-ko with the latest src changes from origin/master.\",
126+
\"head\": \"${BRANCH_NAME}\",
127+
\"base\": \"master-ko\"
128+
}"
129+
shell: bash

.github/workflows/sync-upstream.yml

+88-22
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
with:
1919
repository: ${{ github.repository }}
2020
token: ${{ secrets.GITHUB_TOKEN }}
21-
fetch-depth: 0 # 모든 히스토리 가져오기
21+
fetch-depth: 0 # 모든 히스토리 가져오기
2222

2323
- name: Configure Git
2424
run: |
@@ -34,22 +34,26 @@ jobs:
3434
run: |
3535
git checkout -B master origin/master
3636
37+
# ***********************
38+
# 충돌 자동 해결을 위해 -X theirs 추가
39+
# ***********************
3740
- name: Merge Upstream/master into master
3841
run: |
39-
git merge upstream/master --no-edit
42+
# 업스트림 변경을 우선으로(충돌 발생 시 upstream/master 변경사항 사용)
43+
git merge upstream/master -X theirs --no-edit
4044
4145
# .github 디렉토리 하위 모든 파일 삭제
4246
git rm -r --cached .github || true
4347
rm -rf .github
4448
45-
# 변경 사항 스테이지
49+
# 변경 사항 스테이징
4650
git add .
4751
48-
# 로그 출력: 현재 스테이지에 있는 파일 확인
52+
# 로그 출력: 현재 스테이지에 있는 파일
4953
echo "Staged files:"
5054
git diff --cached --name-only
5155
52-
# 커밋 (변경 사항이 있을 경우)
56+
# 변경 사항이 있을 경우에만 커밋
5357
git commit -m "Sync with upstream (removing .github directory)" || echo "No changes to commit"
5458
shell: bash
5559

@@ -67,28 +71,24 @@ jobs:
6771
cat changes.diff
6872
shell: bash
6973

70-
- name: Check Existing PRs
74+
- name: Check Existing PRs for Master Sync
7175
id: existing-prs
7276
run: |
73-
# 기존 PR 가져오기
7477
RESPONSE=$(curl -s \
7578
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
7679
-H "Accept: application/vnd.github.v3+json" \
7780
https://api.github.com/repos/${{ github.repository }}/pulls)
78-
79-
# "sync-upstream-"으로 시작하는 PR 필터링
8081
EXISTING_PR=$(echo "$RESPONSE" | jq -r '.[] | select(.head.ref | startswith("sync-upstream-")) | .head.ref' | tr '\n' ' ')
81-
8282
if [[ -n "$EXISTING_PR" ]]; then
8383
echo "existing_pr=true" >> $GITHUB_ENV
8484
echo "existing_pr_ref=$EXISTING_PR" >> $GITHUB_ENV
8585
else
8686
echo "existing_pr=false" >> $GITHUB_ENV
8787
fi
88-
echo "Existing PRs: $EXISTING_PR"
88+
echo "Existing PRs for master sync: $EXISTING_PR"
8989
shell: bash
9090

91-
- name: Compare Changes with Existing PRs
91+
- name: Compare Changes with Existing PRs for Master Sync
9292
id: compare-changes
9393
if: env.existing_pr == 'true' && env.changes == 'true'
9494
run: |
@@ -116,14 +116,13 @@ jobs:
116116
PR_NUMBER=$(curl -s \
117117
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
118118
-H "Accept: application/vnd.github.v3+json" \
119-
${{ github.repository }}/pulls \
119+
https://api.github.com/repos/${{ github.repository }}/pulls \
120120
| jq -r '.[] | select(.head.ref=="'$pr_ref'") | .number')
121-
122121
echo "Closing PR #$PR_NUMBER"
123122
curl -X PATCH \
124123
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
125124
-H "Accept: application/vnd.github.v3+json" \
126-
${{ github.repository }}/pulls/$PR_NUMBER \
125+
https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER \
127126
-d '{"state":"closed"}'
128127
done
129128
fi
@@ -152,26 +151,93 @@ jobs:
152151
}'
153152
shell: bash
154153

155-
- name: Update master-ko Branch (excluding src)
154+
# ===== master-ko 업데이트 파트 =====
155+
156+
- name: Check Existing PRs for master-ko Update
157+
id: existing-prs-ko
158+
run: |
159+
RESPONSE=$(curl -s \
160+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
161+
-H "Accept: application/vnd.github.v3+json" \
162+
"https://api.github.com/repos/${{ github.repository }}/pulls")
163+
EXISTING_KO_PR=$(echo "$RESPONSE" | jq -r '.[] | select(.head.ref | startswith("update-master-ko-")) | .head.ref' | tr '\n' ' ')
164+
if [[ -n "$EXISTING_KO_PR" ]]; then
165+
echo "existing_pr_ko=true" >> $GITHUB_ENV
166+
echo "existing_pr_ko_ref=$EXISTING_KO_PR" >> $GITHUB_ENV
167+
else
168+
echo "existing_pr_ko=false" >> $GITHUB_ENV
169+
fi
170+
echo "Existing PRs for master-ko update: $EXISTING_KO_PR"
171+
shell: bash
172+
173+
- name: Prepare master-ko Update
156174
run: |
157175
# 최신 master 브랜치 체크아웃
158176
git checkout master
159-
# master-ko 브랜치가 원격에 존재하면 체크아웃, 없으면 새로 생성
177+
# 원격의 master-ko 브랜치가 있으면 가져오고, 없으면 새로 생성
160178
git fetch origin master-ko || true
161179
git checkout -B master-ko origin/master-ko || git checkout -B master-ko
162180
163181
# master 브랜치의 모든 파일을 가져오되, src는 제외하고 master-ko의 기존 src 유지
164-
git checkout master -- .
165-
git checkout master-ko -- src
182+
git checkout master -- . ":(exclude)src"
166183
167184
git add .
168185
git commit -m "Update master-ko with upstream changes (excluding src)" || echo "No changes to commit"
186+
shell: bash
169187

170-
# 새로운 브랜치로 PR 생성
188+
- name: Compare Changes with Existing master-ko PRs
189+
id: compare-changes-ko
190+
if: env.existing_pr_ko == 'true'
191+
run: |
192+
ALL_INCLUDED=true
193+
EXISTING_BRANCH=""
194+
for pr_ref in ${{ env.existing_pr_ko_ref }}
195+
do
196+
echo "Fetching branch: $pr_ref"
197+
git fetch origin "$pr_ref:$pr_ref-branch"
198+
echo "Comparing updated master-ko with existing PR branch (excluding src)"
199+
if ! git diff --quiet master-ko "$pr_ref-branch" -- . ':(exclude)src'; then
200+
ALL_INCLUDED=false
201+
EXISTING_BRANCH=$pr_ref
202+
break
203+
fi
204+
done
205+
206+
if [ "$ALL_INCLUDED" = true ]; then
207+
echo "ko_diff=false" >> $GITHUB_ENV
208+
else
209+
echo "ko_diff=true" >> $GITHUB_ENV
210+
if [ -n "$EXISTING_BRANCH" ]; then
211+
PR_NUMBER=$(curl -s \
212+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
213+
-H "Accept: application/vnd.github.v3+json" \
214+
"https://api.github.com/repos/${{ github.repository }}/pulls" \
215+
| jq -r '.[] | select(.head.ref=="'$EXISTING_BRANCH'") | .number')
216+
echo "Closing master-ko PR #$PR_NUMBER"
217+
curl -X PATCH \
218+
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
219+
-H "Accept: application/vnd.github.v3+json" \
220+
https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER \
221+
-d '{"state":"closed"}'
222+
fi
223+
fi
224+
shell: bash
225+
226+
- name: Update Existing master-ko PR Branch
227+
if: env.existing_pr_ko == 'true' && env.ko_diff == 'false'
228+
run: |
229+
# 기존 PR 브랜치(첫번째 값)를 사용하여 업데이트
230+
EXISTING_BRANCH=$(echo "${{ env.existing_pr_ko_ref }}" | awk '{print $1}')
231+
echo "Pushing updated changes to existing master-ko branch: $EXISTING_BRANCH"
232+
git push origin master-ko:"$EXISTING_BRANCH" --force
233+
shell: bash
234+
235+
- name: Create New PR for master-ko Update
236+
if: env.ko_diff == 'true' || env.existing_pr_ko == 'false'
237+
run: |
171238
BRANCH_NAME=update-master-ko-$(date +%Y%m%d%H%M%S)
172239
git checkout -b $BRANCH_NAME
173240
git push origin $BRANCH_NAME
174-
175241
curl -X POST \
176242
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
177243
-H "Accept: application/vnd.github.v3+json" \
@@ -182,4 +248,4 @@ jobs:
182248
"head": "'"${BRANCH_NAME}"'",
183249
"base": "master-ko"
184250
}'
185-
shell: bash
251+
shell: bash

0 commit comments

Comments
 (0)