Skip to content

Sync Upstream and Create PR #44

Sync Upstream and Create PR

Sync Upstream and Create PR #44

Workflow file for this run

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