Sync Upstream and Create PR #20
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: Fetch Upstream | |
run: | | |
git remote add upstream https://github.com/react-hook-form/documentation | |
git fetch upstream | |
git fetch origin | |
- name: Check for Changes | |
id: changes | |
run: | | |
# .github/workflows/ ๋๋ ํ ๋ฆฌ๋ฅผ ์ ์ธํ๊ณ ๋ณ๊ฒฝ ์ฌํญ ํ์ธ | |
if git diff --quiet origin/master upstream/master -- . ':(exclude).github/workflows/'; then | |
echo "changes=false" >> $GITHUB_ENV | |
else | |
echo "changes=true" >> $GITHUB_ENV | |
fi | |
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 | |
shell: bash | |
- name: Compare Changes with Each Existing PR | |
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" | |
if ! git diff --quiet upstream/master "$pr_ref-branch"; 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" \ | |
https://api.github.com/repos/${{ github.repository }}/pulls \ | |
| jq -r '.[] | select(.head.ref=="'$pr_ref'") | .number') | |
curl -X PATCH \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER \ | |
-d '{"state":"closed"}' | |
done | |
fi | |
shell: bash | |
- name: Create Branch and Rebase Changes | |
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 origin/master | |
# upstream/master์ ๋ฆฌ๋ฒ ์ด์ค | |
git rebase upstream/master | |
# .github/workflows/ ๋๋ ํ ๋ฆฌ ๋ณ๊ฒฝ ์ฌํญ ๋๋๋ฆฌ๊ธฐ | |
git checkout origin/master -- .github/workflows/ | |
# ๋ณ๊ฒฝ ์ฌํญ์ด ์๋์ง ํ์ธํ๊ณ ์ปค๋ฐ | |
if [ -n "$(git status --porcelain)" ]; then | |
git add .github/workflows/ | |
git commit -m "Discard .github/workflows changes from upstream" | |
fi | |
# ๋ธ๋์น ํธ์ | |
git push origin $BRANCH_NAME --force-with-lease | |
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV | |
shell: bash | |
- name: Create Pull Request | |
if: env.diff == 'true' || env.existing_pr == 'false' | |
run: | | |
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", | |
"body": "This PR syncs the fork with upstream repository", | |
"head": "'"${{ env.BRANCH_NAME }}"'", | |
"base": "master" | |
}' | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |