From 78598b36ca9ff2ae230114603ec4f4d1a3397a32 Mon Sep 17 00:00:00 2001 From: Jeongan Lee <84510455+fkdl0048@users.noreply.github.com> Date: Tue, 15 Oct 2024 15:53:55 +0900 Subject: [PATCH] Update and rename LeetCode.yml to IssueCloseAction.yml --- .github/workflows/IssueCloseAction.yml | 74 ++++++++++++++++++++++++++ .github/workflows/LeetCode.yml | 73 ------------------------- 2 files changed, 74 insertions(+), 73 deletions(-) create mode 100644 .github/workflows/IssueCloseAction.yml delete mode 100644 .github/workflows/LeetCode.yml diff --git a/.github/workflows/IssueCloseAction.yml b/.github/workflows/IssueCloseAction.yml new file mode 100644 index 0000000..d9ba3b7 --- /dev/null +++ b/.github/workflows/IssueCloseAction.yml @@ -0,0 +1,74 @@ +name: 이슈 닫힘 시 라벨별로 파일 생성 + +on: + issues: + types: [closed] + +permissions: + contents: write + +jobs: + create_files_by_label: + runs-on: ubuntu-latest + + steps: + - name: 레포지토리 체크아웃 + uses: actions/checkout@v3 + with: + fetch-depth: 0 # 전체 커밋 기록을 가져옵니다. + + - name: 이슈 정보 가져오기 + id: issue_data + uses: actions/github-script@v6 + with: + script: | + const issue = context.payload.issue; + core.setOutput('title', issue.title || 'untitled'); + core.setOutput('body', issue.body || ''); + core.setOutput('number', issue.number || ''); + const labels = issue.labels.map(label => label.name); + core.setOutput('labels', labels.join(',')); + + - name: 라벨이 있는지 확인 + if: ${{ steps.issue_data.outputs.labels == '' }} + run: | + echo "이슈에 라벨이 없습니다. 작업을 종료합니다." + exit 0 + + - name: 이슈 본문에서 코드 추출 + id: extract_code + run: | + echo '이슈 본문에서 코드를 추출합니다...' + echo "${{ steps.issue_data.outputs.body }}" > issue_body.txt + code=$(grep -zoP '```(?:cpp)?\s*\n([\s\S]*?)\n```' issue_body.txt | sed -e 's/```cpp//' -e 's/```//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') + if [ -z "$code" ]; then + echo "코드 블록을 찾을 수 없습니다. 작업을 종료합니다." + exit 1 + fi + echo "$code" > code.cpp + + - name: 라벨별로 파일 생성 + run: | + labels="${{ steps.issue_data.outputs.labels }}" + IFS=',' read -ra LABEL_ARRAY <<< "$labels" + title="${{ steps.issue_data.outputs.title }}" + sanitized_title=$(echo "$title" | tr '[:upper:]' '[:lower:]' | tr ' ' '_' | tr -cd '[:alnum:]_.-') + for label in "${LABEL_ARRAY[@]}"; do + sanitized_label=$(echo "$label" | tr '[:upper:]' '[:lower:]' | tr ' ' '_' | tr -cd '[:alnum:]_.-') + folder="$sanitized_label" + mkdir -p "$folder" + cp code.cpp "$folder/$sanitized_title.cpp" + echo "파일 생성: $folder/$sanitized_title.cpp" + done + + - name: 변경 사항 커밋 및 푸시 + uses: EndBug/add-and-commit@v9 + with: + message: "이슈 #${{ steps.issue_data.outputs.number }}에서 솔루션 추가" + add: "*/*.cpp" + author_name: GitHub Action + author_email: action@github.com + committer_name: GitHub Action + committer_email: action@github.com + pull_strategy: NO_REBASE # 푸시 전에 git pull을 수행합니다. + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/LeetCode.yml b/.github/workflows/LeetCode.yml deleted file mode 100644 index 27a24b8..0000000 --- a/.github/workflows/LeetCode.yml +++ /dev/null @@ -1,73 +0,0 @@ -name: 이슈 닫힘 시 LeetCode 파일 생성 - -on: - issues: - types: [closed] - -permissions: - contents: write - -jobs: - create_leetcode_file: - runs-on: ubuntu-latest - - steps: - - name: 이슈에 LeetCode 라벨이 있는지 확인 - id: check_label - uses: actions/github-script@v6 - with: - script: | - const labels = context.payload.issue.labels; - const hasLabel = labels.some(label => label.name === 'LeetCode'); - core.setOutput('hasLabel', hasLabel); - - - name: LeetCode 라벨이 없으면 중지 - if: steps.check_label.outputs.hasLabel != 'true' - run: | - echo "이슈에 LeetCode 라벨이 없습니다. 작업을 종료합니다." - exit 0 - - - name: 레포지토리 체크아웃 - uses: actions/checkout@v3 - - - name: 이슈 본문에서 코드 추출 및 파일 생성 - id: create_file - uses: actions/github-script@v6 - with: - script: | - const fs = require('fs'); - const issueBody = context.payload.issue.body || ''; - console.log('이슈 본문:', issueBody); - - const codeBlockRegex = /```(?:cpp)?\s*([\s\S]*?)\s*```/g; - let code = ''; - let match; - while ((match = codeBlockRegex.exec(issueBody)) !== null) { - console.log('추출된 코드 블록:', match[1]); - code += match[1].trim() + '\n'; - } - - if (!code) { - console.log('이슈 본문에서 코드 블록을 찾을 수 없습니다.'); - code = issueBody.trim(); // 코드 블록이 없을 경우 전체 이슈 본문을 저장 - } - - const title = context.payload.issue.title || 'untitled'; - const sanitizedTitle = title.toLowerCase().replace(/ /g, '_').replace(/[^a-z0-9_.-]/g, ''); - const filepath = `LeetCode/${sanitizedTitle}.cpp`; - fs.mkdirSync('LeetCode', { recursive: true }); - fs.writeFileSync(filepath, code); - console.log('생성된 파일 경로:', filepath); - core.setOutput('filepath', filepath); - core.setOutput('issue_number', context.payload.issue.number); - - - name: 변경 사항 커밋 및 푸시 - uses: EndBug/add-and-commit@v9 - with: - message: "이슈 #${{ steps.create_file.outputs.issue_number }}에서 LeetCode 솔루션 추가" - add: "${{ steps.create_file.outputs.filepath }}" - author_name: GitHub Action - author_email: action@github.com - committer_name: GitHub Action - committer_email: action@github.com - token: ${{ secrets.GITHUB_TOKEN }}