Sync Upstream and Create PR #44
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Sync Upstream and Create PR | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: "0 0 * * *" # ๋งค์ผ ๋ฐค 12์ ์ ๊ฐ์ ์คํ | |
jobs: | |
sync: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Checkout Fork | |
uses: actions/checkout@v3 | |
with: | |
repository: ${{ github.repository }} | |
token: ${{ secrets.GITHUB_TOKEN }} | |
fetch-depth: 0 # ๋ชจ๋ ํ์คํ ๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ | |
- name: Configure Git | |
run: | | |
git config user.name "minchodang" | |
git config user.email "[email protected]" | |
- name: Add Upstream Remote | |
run: | | |
git remote add upstream https://github.com/react-hook-form/documentation | |
git fetch upstream | |
- name: Checkout and Set Up Master Branch | |
run: | | |
git checkout -B master origin/master | |
- name: Merge Upstream/master into master | |
run: | | |
git merge upstream/master --no-edit | |
# .github ๋๋ ํ ๋ฆฌ ํ์ ๋ชจ๋ ํ์ผ ์ญ์ | |
git rm -r --cached .github || true | |
rm -rf .github | |
# ๋ณ๊ฒฝ ์ฌํญ ์คํ ์ด์ง | |
git add . | |
# ๋ก๊ทธ ์ถ๋ ฅ: ํ์ฌ ์คํ ์ด์ง์ ์๋ ํ์ผ ํ์ธ | |
echo "Staged files:" | |
git diff --cached --name-only | |
# ์ปค๋ฐ (๋ณ๊ฒฝ ์ฌํญ์ด ์์ ๊ฒฝ์ฐ) | |
git commit -m "Sync with upstream (removing .github directory)" || echo "No changes to commit" | |
shell: bash | |
- name: Check for Changes | |
id: changes | |
run: | | |
# .github ๋๋ ํ ๋ฆฌ ์ ์ธํ ๋ณ๊ฒฝ ์ฌํญ ํ์ธ | |
git diff upstream/master master -- . ':(exclude).github' > changes.diff | |
if [ -s changes.diff ]; then | |
echo "changes=true" >> $GITHUB_ENV | |
else | |
echo "changes=false" >> $GITHUB_ENV | |
fi | |
echo "Changes detected (excluding .github):" | |
cat changes.diff | |
shell: bash | |
- name: Check Existing PRs | |
id: existing-prs | |
run: | | |
# ๊ธฐ์กด PR ๊ฐ์ ธ์ค๊ธฐ | |
RESPONSE=$(curl -s \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/pulls) | |
# "sync-upstream-"์ผ๋ก ์์ํ๋ PR ํํฐ๋ง | |
EXISTING_PR=$(echo "$RESPONSE" | jq -r '.[] | select(.head.ref | startswith("sync-upstream-")) | .head.ref' | tr '\n' ' ') | |
if [[ -n "$EXISTING_PR" ]]; then | |
echo "existing_pr=true" >> $GITHUB_ENV | |
echo "existing_pr_ref=$EXISTING_PR" >> $GITHUB_ENV | |
else | |
echo "existing_pr=false" >> $GITHUB_ENV | |
fi | |
echo "Existing PRs: $EXISTING_PR" | |
shell: bash | |
- name: Compare Changes with Existing PRs | |
id: compare-changes | |
if: env.existing_pr == 'true' && env.changes == 'true' | |
run: | | |
ALL_EXISTING_INCLUDED=true | |
for pr_ref in ${{ env.existing_pr_ref }} | |
do | |
echo "Fetching branch: $pr_ref" | |
git fetch origin "$pr_ref:$pr_ref-branch" | |
echo "Comparing with upstream/master excluding .github" | |
if ! git diff --quiet upstream/master "$pr_ref-branch" -- . ':(exclude).github'; then | |
ALL_EXISTING_INCLUDED=false | |
break | |
fi | |
done | |
if [ "$ALL_EXISTING_INCLUDED" = true ]; then | |
echo "diff=false" >> $GITHUB_ENV | |
else | |
echo "diff=true" >> $GITHUB_ENV | |
# ๊ธฐ์กด PR ๋ซ๊ธฐ | |
for pr_ref in ${{ env.existing_pr_ref }} | |
do | |
PR_NUMBER=$(curl -s \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
${{ github.repository }}/pulls \ | |
| jq -r '.[] | select(.head.ref=="'$pr_ref'") | .number') | |
echo "Closing PR #$PR_NUMBER" | |
curl -X PATCH \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
${{ github.repository }}/pulls/$PR_NUMBER \ | |
-d '{"state":"closed"}' | |
done | |
fi | |
shell: bash | |
- name: Create Pull Request | |
if: env.diff == 'true' || env.existing_pr == 'false' | |
run: | | |
# ์๋ก์ด ๋ธ๋์น ์์ฑ | |
BRANCH_NAME=sync-upstream-$(date +%Y%m%d%H%M%S) | |
git checkout -b $BRANCH_NAME | |
# ๋ธ๋์น ํธ์ | |
git push origin $BRANCH_NAME | |
# PR ์์ฑ | |
curl -X POST \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/pulls \ | |
-d '{ | |
"title": "Sync with upstream (removing .github directory)", | |
"body": "This PR syncs the repository with upstream changes and removes the entire .github directory.", | |
"head": "'"${BRANCH_NAME}"'", | |
"base": "master" | |
}' | |
shell: bash |