Time: 3 ms (40.61%), Space: 20.3 MB (15.78%) - LeetHub #18
This file contains hidden or 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: LeetHub Setting | ||
| on: | ||
| push: | ||
| branches: [ "main" ] | ||
| permissions: | ||
| contents: write | ||
| jobs: | ||
| detect: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| should_run: ${{ steps.check_leethub.outputs.should_run }} | ||
| package_name: ${{ steps.detect_package.outputs.PACKAGE_NAME }} | ||
| commit_msg: ${{ steps.check_leethub.outputs.commit_msg }} | ||
| level: ${{ steps.extract_level.outputs.leetcode_level }} | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v3 | ||
| with: | ||
| fetch-depth: 2 | ||
| - name: Check if LeetHub Commit | ||
| id: check_leethub | ||
| run: | | ||
| COMMIT_MSG=$(git log -1 --pretty=%B | tail -n 1) | ||
| echo "Latest commit message: $COMMIT_MSG" | ||
| if echo "$COMMIT_MSG" | grep -q "LeetHub"; then | ||
| echo "should_run=true" >> $GITHUB_OUTPUT | ||
| echo "commit_msg=${COMMIT_MSG}" >> $GITHUB_OUTPUT | ||
| echo "LeetHub 커밋 확인, 패키지 이사 작업 준비 ✅" | ||
| else | ||
| echo "should_run=false" >> $GITHUB_OUTPUT | ||
| echo "commit_msg=" >> $GITHUB_OUTPUT | ||
| echo "작업 없이 패스하면 됩니다 ❌" | ||
| fi | ||
| - name: Detect New Solve Package | ||
| id: detect_package | ||
| if: steps.check_leethub.outputs.should_run == 'true' | ||
| run: | | ||
| EXCLUDE_DIRS=("LeetCode" "백준" "프로그래머스" ".github") | ||
| NEW_DIRS=$(git diff --name-only HEAD~1 HEAD | awk -F/ 'NF==2 {print $1}' | sort -u) | ||
| for DIR in $NEW_DIRS; do | ||
| SKIP=false | ||
| for EX in "${EXCLUDE_DIRS[@]}"; do | ||
| if [[ "$DIR" == "$EX" ]]; then | ||
| SKIP=true | ||
| break | ||
| fi | ||
| done | ||
| if [[ "$SKIP" == false ]]; then | ||
| echo "새로 추가된 문제풀이 패키지명: $DIR" | ||
| echo "PACKAGE_NAME=$DIR" >> $GITHUB_OUTPUT | ||
| break | ||
| fi | ||
| done | ||
| - name: Extract Level from README or stats.json | ||
| id: extract_level | ||
| if: steps.check_leethub.outputs.should_run == 'true' && steps.detect_package.outputs.PACKAGE_NAME != '' | ||
| run: | | ||
| PACKAGE_NAME="${{ steps.detect_package.outputs.PACKAGE_NAME }}" | ||
| README_PATH="$PACKAGE_NAME/README.md" | ||
| STATS_PATH="stats.json" | ||
| LEVEL="" | ||
| # 1순위: README에서 추출 | ||
| if [ -f "$README_PATH" ]; then | ||
| LEVEL=$(grep -oP '(?<=<h3>).*?(?=</h3>)' "$README_PATH" | head -n 1) | ||
| echo "README에서 추출: $LEVEL" | ||
| fi | ||
| # 2순위: stats.json에서 추출 | ||
| if [ -z "$LEVEL" ] && [ -f "$STATS_PATH" ]; then | ||
| LEVEL=$(cat "$STATS_PATH" | python3 -c " | ||
| import sys, json | ||
| data = json.load(sys.stdin) | ||
| pkg = '$PACKAGE_NAME' | ||
| difficulty = data.get('leetcode', {}).get('shas', {}).get(pkg, {}).get('difficulty', '') | ||
| print(difficulty.capitalize() if difficulty else '') | ||
| ") | ||
| echo "stats.json에서 추출: $LEVEL" | ||
| fi | ||
| # 최종 fallback | ||
| if [ -z "$LEVEL" ]; then | ||
| LEVEL="Unknown" | ||
| fi | ||
| echo "LeetCode 문제 난이도: $LEVEL" | ||
| echo "leetcode_level=$LEVEL" >> $GITHUB_OUTPUT | ||
| move: | ||
| needs: detect | ||
| if: ${{ needs.detect.outputs.should_run == 'true' && needs.detect.outputs.package_name != '' }} | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v3 | ||
| with: | ||
| token: ${{ secrets.GITHUB_TOKEN }} | ||
| - name: Move Directory | ||
| run: | | ||
| PACKAGE_NAME="${{ needs.detect.outputs.package_name }}" | ||
| LEVEL_DIRECTORY="${{ needs.detect.outputs.level }}" | ||
| ROOT_DIR="LeetCode" | ||
| echo "옮겨야 할 패키지 확인: $PACKAGE_NAME" | ||
| echo "감지된 레벨: $LEVEL_DIRECTORY" | ||
| FOUND_EXISTING_PATH="" | ||
| for dir in "$ROOT_DIR"/*; do | ||
| if [ -d "$dir/$PACKAGE_NAME" ]; then | ||
| FOUND_EXISTING_PATH="$dir" | ||
| echo "기존 디렉토리 발견: $dir/$PACKAGE_NAME" | ||
| break | ||
| fi | ||
| done | ||
| if [ -n "$FOUND_EXISTING_PATH" ]; then | ||
| DEST="$FOUND_EXISTING_PATH/$PACKAGE_NAME" | ||
| echo "→ 기존 디렉토리로 병합 이동: $DEST" | ||
| mv "$PACKAGE_NAME"/* "$DEST"/ | ||
| rmdir "$PACKAGE_NAME" || true | ||
| else | ||
| DEST="$ROOT_DIR/$LEVEL_DIRECTORY/$PACKAGE_NAME" | ||
| echo "→ 새 디렉토리로 이동: $DEST" | ||
| mkdir -p "$ROOT_DIR/$LEVEL_DIRECTORY" | ||
| mv "$PACKAGE_NAME" "$ROOT_DIR/$LEVEL_DIRECTORY/" | ||
| fi | ||
| - name: Commit and Push | ||
| run: | | ||
| COMMIT_MSG="${{ needs.detect.outputs.commit_msg }}" | ||
| echo "커밋 메세지 확인: $COMMIT_MSG" | ||
| git config --global user.name "woolnd" | ||
| git config --global user.email "wodnd0418@gmail.com" | ||
| git add . | ||
| git commit -m "$COMMIT_MSG" || echo "No changes to commit" | ||
| git pull --rebase origin main | ||
| git push | ||