Discussion Check to Issue Close #16
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: Discussion Check to Issue Close | |
| on: | |
| discussion: | |
| types: [edited] | |
| permissions: | |
| issues: write | |
| discussions: read | |
| jobs: | |
| check_discussion_todos: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check TODO changes and close corresponding issues | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const discussion = context.payload.discussion; | |
| // ํ์๋ก ์นดํ ๊ณ ๋ฆฌ๊ฐ ์๋๋ฉด ์ข ๋ฃ | |
| if (discussion.category.name !== "ํ์๋ก") { | |
| console.log("์นดํ ๊ณ ๋ฆฌ๊ฐ ํ์๋ก์ด ์๋๋ฏ๋ก ์ข ๋ฃํฉ๋๋ค."); | |
| return; | |
| } | |
| // Discussion ๋ณธ๋ฌธ์์ ์ฒดํฌ๋ TODO ์ฐพ๊ธฐ | |
| const lines = discussion.body.split('\n'); | |
| const checkedTodos = []; | |
| for (const line of lines) { | |
| // ์ฒดํฌ๋ TODO ์ฐพ๊ธฐ: - [x] ํ ์คํธ | |
| const checkedMatch = line.match(/^(\s*)- \[x\] (.+)/); | |
| if (checkedMatch) { | |
| const todoText = checkedMatch[2].trim(); | |
| if (todoText) { | |
| checkedTodos.push(todoText); | |
| } | |
| } | |
| } | |
| console.log(`์ฒดํฌ๋ TODO ๊ฐ์: ${checkedTodos.length}`); | |
| // ๊ฐ ์ฒดํฌ๋ TODO์ ๋ํด ํด๋นํ๋ ์ด์ ์ฐพ๊ธฐ ๋ฐ ๋ซ๊ธฐ | |
| for (const todoText of checkedTodos) { | |
| try { | |
| // ์ด์ ๊ฒ์ (์ ๋ชฉ์ด TODO ํ ์คํธ์ ์ผ์นํ๋ ๊ฒ) | |
| const issues = await github.rest.search.issuesAndPullRequests({ | |
| q: `repo:${context.repo.owner}/${context.repo.repo} is:issue is:open "${todoText}"` | |
| }); | |
| if (issues.data.items.length > 0) { | |
| // ํ์ฌ Discussion๊ณผ ์ฐ๊ฒฐ๋ ์ด์ ์ฐพ๊ธฐ | |
| let targetIssue = null; | |
| for (const issue of issues.data.items) { | |
| // ๊ณ ์ TODO_ID๋ก ์ ํํ ๋งค์นญ | |
| const todoId = `<!-- TODO_ID: ${discussion.id}_${todoText.replace(/[^a-zA-Z0-9]/g, '_')} -->`; | |
| if (issue.body && issue.body.includes(todoId)) { | |
| targetIssue = issue; | |
| break; | |
| } | |
| } | |
| // Discussion ๋งํฌ๊ฐ ์๋ ์ด์๊ฐ ์์ผ๋ฉด ์ฒซ ๋ฒ์งธ ์ด์ ์ ํ | |
| if (!targetIssue) { | |
| targetIssue = issues.data.items[0]; | |
| console.log(`TODO_ID๊ฐ ์ผ์นํ๋ ์ด์๊ฐ ์์ด์ ์ฒซ ๋ฒ์งธ ์ด์๋ฅผ ์ ํํฉ๋๋ค: #${targetIssue.number}`); | |
| } | |
| // ์ด๋ฏธ ๋ซํ ์ด์๋ ๊ฑด๋๋ฐ๊ธฐ | |
| if (targetIssue.state === 'closed') { | |
| console.log(`์ด์ #${targetIssue.number}๋ ์ด๋ฏธ ๋ซํ์์ต๋๋ค.`); | |
| continue; | |
| } | |
| console.log(`์ด์ #${targetIssue.number} ๋ซ๋ ์ค: ${todoText}`); | |
| // ์ด์ ๋ซ๊ธฐ | |
| await github.rest.issues.update({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: targetIssue.number, | |
| state: 'closed', | |
| state_reason: 'completed' | |
| }); | |
| console.log(`์ด์ #${targetIssue.number} ๋ซ๊ธฐ ์๋ฃ`); | |
| } else { | |
| console.log(`TODO "${todoText}"์ ํด๋นํ๋ ์ด๋ฆฐ ์ด์๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.`); | |
| } | |
| } catch (error) { | |
| console.error(`TODO "${todoText}" ์ฒ๋ฆฌ ์ค ์ค๋ฅ:`, error); | |
| } | |
| } |