|
| 1 | +name: Duplicate discussion check |
| 2 | + |
| 3 | +on: |
| 4 | + discussion: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +permissions: |
| 8 | + discussions: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-duplicate: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + timeout-minutes: 5 |
| 14 | + steps: |
| 15 | + - name: Find similar discussions |
| 16 | + uses: actions/github-script@v7 |
| 17 | + with: |
| 18 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 19 | + script: | |
| 20 | + const discussion = context.payload.discussion; |
| 21 | + const title = discussion.title || ''; |
| 22 | +
|
| 23 | + // Extract meaningful keywords from the title |
| 24 | + const STOP_WORDS = new Set([ |
| 25 | + 'is', 'a', 'the', 'for', 'in', 'on', 'with', 'how', 'do', |
| 26 | + 'does', 'can', 'to', 'of', 'and', 'or', 'it', 'at', 'by', |
| 27 | + 'an', 'be', 'as', 'from', 'that', 'this', 'what', 'why', |
| 28 | + 'when', 'where', 'which', 'who', 'will', 'are', 'was', |
| 29 | + 'not', 'my', 'we', 'i', 'you', |
| 30 | + ]); |
| 31 | +
|
| 32 | + const keywords = title |
| 33 | + .toLowerCase() |
| 34 | + .replace(/[^a-z0-9\s]/g, ' ') |
| 35 | + .split(/\s+/) |
| 36 | + .filter(w => w.length > 1 && !STOP_WORDS.has(w)) |
| 37 | + .slice(0, 5); |
| 38 | +
|
| 39 | + if (keywords.length < 2) { |
| 40 | + core.info('Title too short or no meaningful keywords; skipping duplicate check.'); |
| 41 | + return; |
| 42 | + } |
| 43 | +
|
| 44 | + const searchQuery = `repo:opendecree/decree is:discussion ${keywords.join(' ')}`; |
| 45 | + core.info(`Search query: ${searchQuery}`); |
| 46 | +
|
| 47 | + let items; |
| 48 | + try { |
| 49 | + const results = await github.rest.search.issuesAndPullRequests({ |
| 50 | + q: searchQuery, |
| 51 | + per_page: 5, |
| 52 | + }); |
| 53 | + items = results.data.items; |
| 54 | + } catch (err) { |
| 55 | + core.warning(`Search API error (skipping): ${err.message}`); |
| 56 | + return; |
| 57 | + } |
| 58 | +
|
| 59 | + // Filter out the current discussion |
| 60 | + const matches = items |
| 61 | + .filter(item => item.number !== discussion.number) |
| 62 | + .slice(0, 3); |
| 63 | +
|
| 64 | + if (matches.length === 0) { |
| 65 | + core.info('No similar discussions found; no comment posted.'); |
| 66 | + return; |
| 67 | + } |
| 68 | +
|
| 69 | + const bulletList = matches |
| 70 | + .map(item => `- [${item.title}](${item.html_url})`) |
| 71 | + .join('\n'); |
| 72 | +
|
| 73 | + const commentBody = [ |
| 74 | + '👋 Before the community answers, here are some existing discussions that might cover your question:', |
| 75 | + '', |
| 76 | + bulletList, |
| 77 | + '', |
| 78 | + 'If none of these answer your question, feel free to continue the discussion!', |
| 79 | + ].join('\n'); |
| 80 | +
|
| 81 | + await github.graphql(` |
| 82 | + mutation AddComment($discussionId: ID!, $body: String!) { |
| 83 | + addDiscussionComment(input: {discussionId: $discussionId, body: $body}) { |
| 84 | + comment { id } |
| 85 | + } |
| 86 | + } |
| 87 | + `, { |
| 88 | + discussionId: discussion.node_id, |
| 89 | + body: commentBody, |
| 90 | + }); |
| 91 | +
|
| 92 | + core.info(`Posted duplicate-discussion comment on discussion #${discussion.number}`); |
0 commit comments