| 1 | name: Similar Issue Search & Discussion Suggestion |
| 2 | on: |
| 3 | issues: |
| 4 | types: [opened] |
| 5 | pull_request: |
| 6 | types: [opened] |
| 7 | |
| 8 | jobs: |
| 9 | analyze: |
| 10 | # Disabling for now due to bugs |
| 11 | if: false |
| 12 | runs-on: ubuntu-latest |
| 13 | permissions: |
| 14 | issues: write |
| 15 | pull-requests: write |
| 16 | |
| 17 | steps: |
| 18 | - name: Checkout deja-view |
| 19 | uses: actions/checkout@v5 |
| 20 | with: |
| 21 | repository: "bdougie/deja-view" |
| 22 | # Option 1: Pin to specific commit (most secure) |
| 23 | ref: "45e7696" # Pin to specific commit for stability |
| 24 | |
| 25 | # Option 2: Use a tag (recommended after creating releases) |
| 26 | # ref: 'v1.0.0' |
| 27 | |
| 28 | # Option 3: Use main branch (least secure, gets latest changes) |
| 29 | # ref: 'main' |
| 30 | |
| 31 | path: deja-view |
| 32 | |
| 33 | - name: Setup Python |
| 34 | uses: actions/setup-python@v6 |
| 35 | with: |
| 36 | python-version: "3.11" |
| 37 | |
| 38 | - name: Install dependencies |
| 39 | run: | |
| 40 | cd deja-view |
| 41 | pip install -r requirements.txt |
| 42 | |
| 43 | - name: Index repository if needed |
| 44 | env: |
| 45 | CHROMA_API_KEY: ${{ secrets.CHROMA_CLOUD_API_KEY }} |
| 46 | CHROMA_TENANT: ${{ secrets.CHROMA_TENANT }} |
| 47 | CHROMA_DATABASE: ${{ secrets.CHROMA_DATABASE }} |
| 48 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 49 | run: | |
| 50 | cd deja-view |
| 51 | |
| 52 | # Index the repository (this will update existing index) |
| 53 | python cli.py index "${{ github.repository }}" --max-issues 200 || true |
| 54 | |
| 55 | - name: Search for similar issues |
| 56 | id: search |
| 57 | env: |
| 58 | CHROMA_API_KEY: ${{ secrets.CHROMA_CLOUD_API_KEY }} |
| 59 | CHROMA_TENANT: ${{ secrets.CHROMA_TENANT }} |
| 60 | CHROMA_DATABASE: ${{ secrets.CHROMA_DATABASE }} |
| 61 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 62 | run: | |
| 63 | cd deja-view |
| 64 | |
| 65 | # Determine if this is an issue or PR |
| 66 | if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 67 | ISSUE_URL="https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }}" |
| 68 | THRESHOLD="0.85" |
| 69 | TOP_K="5" |
| 70 | else |
| 71 | ISSUE_URL="https://github.com/${{ github.repository }}/issues/${{ github.event.issue.number }}" |
| 72 | THRESHOLD="0.7" |
| 73 | TOP_K="5" |
| 74 | fi |
| 75 | |
| 76 | # Create a Python script to search and format as JSON |
| 77 | cat << 'PYTHON_SCRIPT' > search_similar.py |
| 78 | import sys |
| 79 | import json |
| 80 | import os |
| 81 | from github_similarity_service import SimilarityService |
| 82 | |
| 83 | issue_url = sys.argv[1] |
| 84 | threshold = float(sys.argv[2]) |
| 85 | top_k = int(sys.argv[3]) |
| 86 | |
| 87 | # Parse issue number from URL |
| 88 | issue_number = int(issue_url.split('/')[-1]) |
| 89 | repo_parts = issue_url.split('/') |
| 90 | owner = repo_parts[3] |
| 91 | repo = repo_parts[4] |
| 92 | |
| 93 | service = SimilarityService() |
| 94 | |
| 95 | # Find similar issues |
| 96 | similar = service.find_similar_issues( |
| 97 | owner=owner, |
| 98 | repo=repo, |
| 99 | issue_number=issue_number, |
| 100 | top_k=top_k, |
| 101 | min_similarity=threshold |
| 102 | ) |
| 103 | |
| 104 | # Format as JSON |
| 105 | print(json.dumps(similar)) |
| 106 | PYTHON_SCRIPT |
| 107 | |
| 108 | # Search for similar issues |
| 109 | SIMILAR_ISSUES=$(python search_similar.py "$ISSUE_URL" "$THRESHOLD" "$TOP_K") |
| 110 | |
| 111 | # Save to output |
| 112 | echo "similar_issues<<EOF" >> $GITHUB_OUTPUT |
| 113 | echo "$SIMILAR_ISSUES" >> $GITHUB_OUTPUT |
| 114 | echo "EOF" >> $GITHUB_OUTPUT |
| 115 | |
| 116 | # Check if any similar issues found |
| 117 | if [ "$(echo "$SIMILAR_ISSUES" | jq '. | length')" -gt 0 ]; then |
| 118 | echo "has_similar=true" >> $GITHUB_OUTPUT |
| 119 | else |
| 120 | echo "has_similar=false" >> $GITHUB_OUTPUT |
| 121 | fi |
| 122 | |
| 123 | - name: Check if issue should be a discussion |
| 124 | id: discussion_check |
| 125 | if: github.event_name == 'issues' |
| 126 | env: |
| 127 | CHROMA_API_KEY: ${{ secrets.CHROMA_CLOUD_API_KEY }} |
| 128 | CHROMA_TENANT: ${{ secrets.CHROMA_TENANT }} |
| 129 | CHROMA_DATABASE: ${{ secrets.CHROMA_DATABASE }} |
| 130 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 131 | run: | |
| 132 | cd deja-view |
| 133 | |
| 134 | # Create a Python script to analyze the issue |
| 135 | cat << 'PYTHON_SCRIPT' > check_discussion.py |
| 136 | import os |
| 137 | import json |
| 138 | import re |
| 139 | from github_similarity_service import SimilarityService |
| 140 | |
| 141 | # Get issue details from environment |
| 142 | issue_title = os.environ.get('ISSUE_TITLE', '') |
| 143 | issue_body = os.environ.get('ISSUE_BODY', '') |
| 144 | issue_labels = json.loads(os.environ.get('ISSUE_LABELS', '[]')) |
| 145 | |
| 146 | # Mock Issue object for analysis |
| 147 | class MockIssue: |
| 148 | def __init__(self): |
| 149 | self.title = issue_title |
| 150 | self.body = issue_body |
| 151 | self.labels = issue_labels |
| 152 | self.state = 'open' |
| 153 | self.number = int(os.environ.get('ISSUE_NUMBER', 0)) |
| 154 | self.created_at = '' |
| 155 | self.updated_at = '' |
| 156 | self.url = '' |
| 157 | |
| 158 | service = SimilarityService() |
| 159 | issue = MockIssue() |
| 160 | |
| 161 | score, reasons = service._calculate_discussion_score(issue) |
| 162 | |
| 163 | result = { |
| 164 | 'score': score, |
| 165 | 'reasons': reasons, |
| 166 | 'should_be_discussion': score >= 0.5, |
| 167 | 'confidence': 'high' if score >= 0.7 else 'medium' if score >= 0.5 else 'low' |
| 168 | } |
| 169 | |
| 170 | print(json.dumps(result)) |
| 171 | PYTHON_SCRIPT |
| 172 | |
| 173 | # Run the analysis |
| 174 | DISCUSSION_ANALYSIS=$(ISSUE_TITLE="${{ github.event.issue.title }}" \ |
| 175 | ISSUE_BODY='${{ github.event.issue.body }}' \ |
| 176 | ISSUE_LABELS='${{ toJson(github.event.issue.labels.*.name) }}' \ |
| 177 | ISSUE_NUMBER="${{ github.event.issue.number }}" \ |
| 178 | python check_discussion.py) |
| 179 | |
| 180 | echo "analysis<<EOF" >> $GITHUB_OUTPUT |
| 181 | echo "$DISCUSSION_ANALYSIS" >> $GITHUB_OUTPUT |
| 182 | echo "EOF" >> $GITHUB_OUTPUT |
| 183 | |
| 184 | # Extract whether it should be a discussion |
| 185 | SHOULD_BE_DISCUSSION=$(echo "$DISCUSSION_ANALYSIS" | jq -r '.should_be_discussion') |
| 186 | echo "should_be_discussion=$SHOULD_BE_DISCUSSION" >> $GITHUB_OUTPUT |
| 187 | |
| 188 | # Extract confidence level |
| 189 | CONFIDENCE=$(echo "$DISCUSSION_ANALYSIS" | jq -r '.confidence') |
| 190 | echo "confidence=$CONFIDENCE" >> $GITHUB_OUTPUT |
| 191 | |
| 192 | - name: Comment with analysis |
| 193 | uses: actions/github-script@v8 |
| 194 | with: |
| 195 | script: | |
| 196 | let comment = ''; |
| 197 | const hasSimilar = '${{ steps.search.outputs.has_similar }}' === 'true'; |
| 198 | const isPR = '${{ github.event_name }}' === 'pull_request'; |
| 199 | const shouldBeDiscussion = !isPR && '${{ steps.discussion_check.outputs.should_be_discussion }}' === 'true'; |
| 200 | |
| 201 | // Add similar issues section if found |
| 202 | if (hasSimilar) { |
| 203 | const similarIssues = JSON.parse(`${{ steps.search.outputs.similar_issues }}`); |
| 204 | |
| 205 | if (isPR) { |
| 206 | // Filter only open issues for PRs |
| 207 | const openIssues = similarIssues.filter(issue => issue.state === 'open'); |
| 208 | |
| 209 | if (openIssues.length > 0) { |
| 210 | // Simple table format for PRs |
| 211 | comment += '## 📋 Related Open Issues\n\n'; |
| 212 | comment += 'These open issues appear to be related to this PR (≥85% similarity):\n\n'; |
| 213 | comment += '| # | Title | Similarity |\n'; |
| 214 | comment += '|---|-------|------------|\n'; |
| 215 | |
| 216 | openIssues.forEach(issue => { |
| 217 | const similarity = (issue.similarity * 100).toFixed(0); |
| 218 | const title = issue.title.length > 50 ? issue.title.substring(0, 47) + '...' : issue.title; |
| 219 | comment += `| [#${issue.number}](${issue.url}) | ${title} | ${similarity}% |\n`; |
| 220 | }); |
| 221 | |
| 222 | comment += '\n'; |
| 223 | comment += '_Consider closing related issues if this PR addresses them._\n'; |
| 224 | } else { |
| 225 | // Don't add any comment if no open issues found |
| 226 | return; |
| 227 | } |
| 228 | } else { |
| 229 | // Original format for issues |
| 230 | comment += '## 🔍 Similar Issues Found\n\n'; |
| 231 | comment += 'I found some existing issues that might be related:\n\n'; |
| 232 | |
| 233 | similarIssues.forEach((issue, index) => { |
| 234 | const similarity = (issue.similarity * 100).toFixed(1); |
| 235 | const state = issue.state === 'open' ? '🟢' : '🔴'; |
| 236 | |
| 237 | comment += `${index + 1}. ${state} [#${issue.number}: ${issue.title}](${issue.url}) (${similarity}% similar)\n`; |
| 238 | |
| 239 | if (issue.summary) { |
| 240 | comment += ` > ${issue.summary.substring(0, 150)}${issue.summary.length > 150 ? '...' : ''}\n`; |
| 241 | } |
| 242 | comment += '\n'; |
| 243 | }); |
| 244 | |
| 245 | comment += '\n'; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Add discussion suggestion if applicable |
| 250 | if (shouldBeDiscussion) { |
| 251 | const analysis = JSON.parse(`${{ steps.discussion_check.outputs.analysis }}`); |
| 252 | const confidence = '${{ steps.discussion_check.outputs.confidence }}'; |
| 253 | |
| 254 | if (hasSimilar) { |
| 255 | comment += '---\n\n'; |
| 256 | } |
| 257 | |
| 258 | comment += '## 💬 Discussion Suggestion\n\n'; |
| 259 | |
| 260 | const confidenceEmoji = { |
| 261 | 'high': '🟢', |
| 262 | 'medium': '🟡', |
| 263 | 'low': '🔴' |
| 264 | }; |
| 265 | |
| 266 | comment += `This issue might be better suited as a **GitHub Discussion** ${confidenceEmoji[confidence] || ''}\n\n`; |
| 267 | comment += `**Confidence**: ${confidence.charAt(0).toUpperCase() + confidence.slice(1)} (${(analysis.score * 100).toFixed(0)}%)\n\n`; |
| 268 | |
| 269 | if (analysis.reasons && analysis.reasons.length > 0) { |
| 270 | comment += '**Reasons**:\n'; |
| 271 | analysis.reasons.forEach(reason => { |
| 272 | comment += `- ${reason}\n`; |
| 273 | }); |
| 274 | comment += '\n'; |
| 275 | } |
| 276 | |
| 277 | comment += 'GitHub Discussions are great for:\n'; |
| 278 | comment += '- Questions and help requests\n'; |
| 279 | comment += '- Feature requests and ideas\n'; |
| 280 | comment += '- General feedback and brainstorming\n'; |
| 281 | comment += '- Community conversations\n\n'; |
| 282 | |
| 283 | comment += '_Consider converting this to a discussion if it\'s not a bug report or actionable task._\n'; |
| 284 | } |
| 285 | |
| 286 | // Only comment if we have something to say |
| 287 | if (comment) { |
| 288 | comment += '\n---\n'; |
| 289 | comment += '_This analysis was automatically generated by [deja-view](https://github.com/bdougie/deja-view)._'; |
| 290 | |
| 291 | if ('${{ github.event_name }}' === 'pull_request') { |
| 292 | await github.rest.issues.createComment({ |
| 293 | owner: context.repo.owner, |
| 294 | repo: context.repo.repo, |
| 295 | issue_number: context.payload.pull_request.number, |
| 296 | body: comment |
| 297 | }); |
| 298 | } else { |
| 299 | await github.rest.issues.createComment({ |
| 300 | owner: context.repo.owner, |
| 301 | repo: context.repo.repo, |
| 302 | issue_number: context.issue.number, |
| 303 | body: comment |
| 304 | }); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | - name: Add labels |
| 309 | if: github.event_name == 'issues' |
| 310 | uses: actions/github-script@v8 |
| 311 | with: |
| 312 | script: | |
| 313 | const labels = []; |
| 314 | |
| 315 | // Check for duplicate |
| 316 | if ('${{ steps.search.outputs.has_similar }}' === 'true') { |
| 317 | const similarIssues = JSON.parse(`${{ steps.search.outputs.similar_issues }}`); |
| 318 | const hasDuplicate = similarIssues.some(issue => issue.similarity > 0.9); |
| 319 | |
| 320 | if (hasDuplicate) { |
| 321 | labels.push('potential-duplicate'); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // Check for discussion suggestion |
| 326 | if ('${{ steps.discussion_check.outputs.should_be_discussion }}' === 'true') { |
| 327 | const confidence = '${{ steps.discussion_check.outputs.confidence }}'; |
| 328 | |
| 329 | if (confidence === 'high') { |
| 330 | labels.push('should-be-discussion'); |
| 331 | } else if (confidence === 'medium') { |
| 332 | labels.push('discussion'); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // Add labels if any |
| 337 | if (labels.length > 0) { |
| 338 | await github.rest.issues.addLabels({ |
| 339 | owner: context.repo.owner, |
| 340 | repo: context.repo.repo, |
| 341 | issue_number: context.issue.number, |
| 342 | labels: labels |
| 343 | }); |
| 344 | } |