chore: remove moved files #62
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: Create Blog Post | |
| on: | |
| push: | |
| branches: [main] | |
| paths: | |
| - 'src/ver2/**' | |
| jobs: | |
| create-blog-post: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: code checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # 전체 히스토리 가져오기 | |
| - name: setup for Korean filenames | |
| run: | | |
| git config --global core.quotepath false | |
| echo "LANG=C.UTF-8" >> $GITHUB_ENV | |
| echo "LC_ALL=C.UTF-8" >> $GITHUB_ENV | |
| - name: get latest problem info | |
| id: problem_info | |
| run: | | |
| python3 << 'EOF' | |
| import os | |
| import re | |
| import subprocess | |
| # 현재 push에 포함된 커밋만 확인 | |
| result = subprocess.run( | |
| ['git', 'log', '--format=%H %s', '${{ github.event.before }}..${{ github.event.after }}'], | |
| capture_output=True, | |
| text=True | |
| ) | |
| lines = result.stdout.strip().split('\n') | |
| if not lines or lines[0] == '': | |
| print("❌ No commits in this push") | |
| exit(0) | |
| # feat으로 시작하는 첫 번째 커밋 찾기 | |
| feat_commit = None | |
| feat_msg = None | |
| for line in lines: | |
| parts = line.split(' ', 1) | |
| if len(parts) == 2: | |
| commit_hash, commit_msg = parts | |
| if commit_msg.startswith('feat:'): | |
| feat_commit = commit_hash | |
| feat_msg = commit_msg | |
| break | |
| if not feat_commit: | |
| print("❌ No feat commit found in this push") | |
| exit(0) | |
| print(f"✅ feat commit: {feat_commit}") | |
| print(f"✅ feat message: {feat_msg}") | |
| # 커밋 메시지에서 문제 이름 추출 | |
| problem_name_from_commit = feat_msg.replace('feat:', '').strip() | |
| print(f"✅ Problem name from commit: {problem_name_from_commit}") | |
| # Git에서 추가된 파일만 확인 (--diff-filter=A) | |
| result = subprocess.run( | |
| ['git', 'diff-tree', '--no-commit-id', '--name-only', '--diff-filter=A', '-r', feat_commit], | |
| capture_output=True, | |
| text=True, | |
| encoding='utf-8' | |
| ) | |
| files = result.stdout.strip().split('\n') | |
| # README 찾기 - 문제 이름과 매칭되는 것만 | |
| readme_path = None | |
| for f in files: | |
| if 'src/ver2/' in f and 'README.md' in f: | |
| # 경로에서 문제 이름 추출 | |
| path_parts = f.split('/') | |
| if len(path_parts) >= 3: | |
| dir_name = path_parts[2] # src/ver2/[문제이름]/README.md | |
| # 커밋 메시지의 문제 이름과 디렉토리 이름이 일치하는지 확인 | |
| if problem_name_from_commit in f or dir_name == problem_name_from_commit: | |
| readme_path = f.strip('"') | |
| break | |
| if not readme_path: | |
| print("❌ No README found in commit") | |
| exit(0) | |
| print(f"✅ Found README: {readme_path}") | |
| # Git에서 직접 파일 내용 읽기 | |
| result = subprocess.run( | |
| ['git', 'show', f'{feat_commit}:{readme_path}'], | |
| capture_output=True, | |
| text=True, | |
| encoding='utf-8' | |
| ) | |
| if result.returncode != 0: | |
| print(f"❌ Cannot read from git: {readme_path}") | |
| exit(0) | |
| content = result.stdout | |
| print(f"✅ Successfully read {len(content)} bytes from git") | |
| # 제목 추출 | |
| title_match = re.search(r'^# (.+)$', content, re.MULTILINE) | |
| title = title_match.group(1) if title_match else '' | |
| # 카테고리 추출 | |
| cat_match = re.search(r'## 카테고리\s*\n([^\n#]+)', content) | |
| if cat_match: | |
| categories = re.findall(r'`([^`]+)`', cat_match.group(1)) | |
| first_category = categories[0] if categories else '기타' | |
| all_categories = '\n'.join(categories) | |
| else: | |
| first_category = '기타' | |
| all_categories = '기타' | |
| # README 저장 | |
| with open('/tmp/problem_readme.md', 'w', encoding='utf-8') as f: | |
| f.write(content) | |
| # GitHub Output | |
| with open(os.environ['GITHUB_OUTPUT'], 'a', encoding='utf-8') as f: | |
| f.write(f"problem_name={title}\n") | |
| f.write(f"first_category={first_category}\n") | |
| f.write(f"all_categories<<EOF\n{all_categories}\nEOF\n") | |
| f.write("readme_exists=true\n") | |
| print(f"✅ problem_name={title}") | |
| print(f"✅ first_category={first_category}") | |
| EOF | |
| - name: checkout blog repo | |
| if: steps.problem_info.outputs.readme_exists == 'true' | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: choijw1004/choijw1004.github.io | |
| token: ${{ secrets.BLOG_PAT }} | |
| path: blog-repo | |
| - name: create blog post | |
| if: steps.problem_info.outputs.readme_exists == 'true' | |
| run: | | |
| DATE=$(date +%Y-%m-%d) | |
| DATETIME=$(date +"%Y-%m-%d %H:%M:%S +0900") | |
| cd blog-repo/_posts | |
| LAST_NUM=$(ls ${DATE}-*.md 2>/dev/null | \ | |
| sed "s/${DATE}-\([0-9]*\)\.md/\1/" | \ | |
| sort -n | \ | |
| tail -1) | |
| if [ -z "$LAST_NUM" ]; then | |
| NEXT_NUM=0 | |
| else | |
| NEXT_NUM=$((LAST_NUM + 1)) | |
| fi | |
| FILENAME="${DATE}-${NEXT_NUM}.md" | |
| cd ../.. | |
| cat > blog-repo/_posts/${FILENAME} << EOF | |
| --- | |
| layout: post | |
| title: "[Algorithm] ${{ steps.problem_info.outputs.problem_name }}" | |
| description: "${{ steps.problem_info.outputs.problem_name }} 접근방식" | |
| date: ${DATETIME} | |
| tags: | |
| - Algorithm | |
| EOF | |
| echo "${{ steps.problem_info.outputs.all_categories }}" | while IFS= read -r category; do | |
| if [ ! -z "$category" ]; then | |
| echo " - ${category}" >> blog-repo/_posts/${FILENAME} | |
| fi | |
| done | |
| cat >> blog-repo/_posts/${FILENAME} << EOF | |
| author: choijangwoo | |
| toc: true | |
| published: true | |
| categories: [Algorithm, ${{ steps.problem_info.outputs.first_category }}] | |
| --- | |
| EOF | |
| cat /tmp/problem_readme.md >> blog-repo/_posts/${FILENAME} | |
| echo "✅ Created: ${FILENAME}" | |
| - name: commit to blog repo | |
| if: steps.problem_info.outputs.readme_exists == 'true' | |
| run: | | |
| cd blog-repo | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add . | |
| git diff --staged --quiet || git commit -m "docs: add algorithm problem post" | |
| git pull --rebase --autostash || git pull --no-rebase | |
| git push |