Check for duplicates before adding tickets #12

Merged
ztimson merged 6 commits from ticket-duplicates into master 2025-12-31 00:02:18 -05:00
Owner
No description provided.
ztimson added 1 commit 2025-12-30 23:18:16 -05:00
Check for duplicates before adding tickets
Some checks failed
Publish Library / Build NPM Project (push) Successful in 14s
Code review / review (pull_request) Failing after 28s
Publish Library / Tag Version (push) Successful in 27s
8add830d2b
ztimson added 1 commit 2025-12-30 23:25:38 -05:00
Merge branch 'master' of git.zakscode.com:ztimson/ai-agents into ticket-duplicates
Some checks failed
Code review / review (pull_request) Failing after 15s
Publish Library / Build NPM Project (push) Successful in 3s
Publish Library / Tag Version (push) Successful in 11s
e62e11fb75
ztimson reviewed 2025-12-30 23:28:12 -05:00
ztimson left a comment
Author
Owner

Looks good!

Looks good!
ztimson reviewed 2025-12-30 23:29:07 -05:00
ztimson left a comment
Author
Owner

Test

Test
ztimson added 1 commit 2025-12-30 23:35:01 -05:00
Merge branch 'master' of git.zakscode.com:ztimson/ai-agents into ticket-duplicates
All checks were successful
Publish Library / Build NPM Project (push) Successful in 3s
Publish Library / Tag Version (push) Successful in 16s
Code review / review (pull_request) Successful in 48s
f3df34ec47
assistant reviewed 2025-12-30 23:35:49 -05:00
assistant left a comment
First-time contributor

Review Summary

This code change introduces multiple critical bugs that will prevent the code from running:

Critical Issues:

  1. Undefined Variable References: Throughout the new code, updateRes is referenced in error messages before it's defined (lines 31, 172, 176, 181, 189, 194). This will cause ReferenceError exceptions.

  2. Syntax Errors: The ternary expression !resp.ok ? throw new Error(...) : null is invalid JavaScript syntax. The throw statement cannot be used directly in a ternary expression without wrapping.

  3. Logic Error on Line 31-32: The fetch promise handler returns null when not ok, but then the code immediately tries to call issueRes.json(), which will fail with "Cannot read property 'json' of null".

  4. Breaking Label Check (Line 33): The label validation was changed from checking objects with a name property to direct string comparison, which will always fail since labels are objects, not strings.

Other Concerns:

  • The label naming scheme changed from Kind/ to Reviewed/ without clear justification
  • Error handling pattern is inconsistent and makes debugging harder

Recommendation: This PR should not be merged in its current state. The code needs significant fixes to even run without throwing exceptions. The original error handling approach was more reliable and should be restored or properly refactored.

## Review Summary This code change introduces **multiple critical bugs** that will prevent the code from running: ### Critical Issues: 1. **Undefined Variable References**: Throughout the new code, `updateRes` is referenced in error messages before it's defined (lines 31, 172, 176, 181, 189, 194). This will cause `ReferenceError` exceptions. 2. **Syntax Errors**: The ternary expression `!resp.ok ? throw new Error(...) : null` is invalid JavaScript syntax. The `throw` statement cannot be used directly in a ternary expression without wrapping. 3. **Logic Error on Line 31-32**: The fetch promise handler returns `null` when not ok, but then the code immediately tries to call `issueRes.json()`, which will fail with "Cannot read property 'json' of null". 4. **Breaking Label Check (Line 33)**: The label validation was changed from checking objects with a `name` property to direct string comparison, which will always fail since labels are objects, not strings. ### Other Concerns: - The label naming scheme changed from `Kind/` to `Reviewed/` without clear justification - Error handling pattern is inconsistent and makes debugging harder **Recommendation**: This PR should not be merged in its current state. The code needs significant fixes to even run without throwing exceptions. The original error handling approach was more reliable and should be restored or properly refactored.
src/refine.mjs Outdated
@@ -30,3 +30,2 @@
headers: {'Authorization': `token ${auth}`}
});
if(!issueRes.ok) throw new Error(`${issueRes.status} ${await issueRes.text()}`);
}).then(async resp => !resp.ok ? throw new Error(`${updateRes.status} ${await updateRes.text()}`) : null);
First-time contributor

Critical bug: updateRes is referenced before it's defined. This variable is only defined later on line 189. The error message should reference resp.status and resp.text() instead of updateRes.status and updateRes.text().

Critical bug: `updateRes` is referenced before it's defined. This variable is only defined later on line 189. The error message should reference `resp.status` and `resp.text()` instead of `updateRes.status` and `updateRes.text()`.
First-time contributor

Syntax error: Cannot use throw in a ternary expression without wrapping it in a function or using proper syntax. Should be throw new Error(...) wrapped in parentheses or use a different approach like .then(async resp => { if (!resp.ok) throw new Error(...); return resp; }).

Syntax error: Cannot use `throw` in a ternary expression without wrapping it in a function or using proper syntax. Should be `throw new Error(...)` wrapped in parentheses or use a different approach like `.then(async resp => { if (!resp.ok) throw new Error(...); return resp; })`.
First-time contributor

Logic error: After throwing an error or returning null, the code tries to call issueRes.json() on line 32. If the response is not ok, issueRes will be null, causing a runtime error when trying to call .json() on it.

Logic error: After throwing an error or returning null, the code tries to call `issueRes.json()` on line 32. If the response is not ok, `issueRes` will be null, causing a runtime error when trying to call `.json()` on it.
ztimson marked this conversation as resolved
src/refine.mjs Outdated
@@ -33,2 +31,3 @@
}).then(async resp => !resp.ok ? throw new Error(`${updateRes.status} ${await updateRes.text()}`) : null);
const issueData = await issueRes.json();
if(!issueData.labels?.some(l => l.name === 'Review/AI')) {
if(issueData.labels[0] !== 'Review/AI' || issueData.labels.length !== 1) {
First-time contributor

Bug: Unsafe array access. The code assumes issueData.labels[0] exists and is a string, but labels is an array of objects (with properties like name). The original code correctly checked issueData.labels?.some(l => l.name === 'Review/AI'). This will always fail since labels[0] is an object, not a string.

Bug: Unsafe array access. The code assumes `issueData.labels[0]` exists and is a string, but labels is an array of objects (with properties like `name`). The original code correctly checked `issueData.labels?.some(l => l.name === 'Review/AI')`. This will always fail since `labels[0]` is an object, not a string.
ztimson marked this conversation as resolved
src/refine.mjs Outdated
@@ -149,0 +169,4 @@
await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}/comments`, {
method: 'POST',
headers: {'Authorization': `token ${auth}`, 'Content-Type': 'application/json'},
body: `{"body": "Duplicate of #${dupeId}"}`
First-time contributor

Same critical bug as line 31: updateRes is referenced before it's defined. Should reference resp.status and resp.text() instead.

Same critical bug as line 31: `updateRes` is referenced before it's defined. Should reference `resp.status` and `resp.text()` instead.
First-time contributor

Same syntax error as line 31: Cannot use throw in a ternary expression without proper syntax.

Same syntax error as line 31: Cannot use `throw` in a ternary expression without proper syntax.
ztimson marked this conversation as resolved
@@ -149,0 +173,4 @@
}).then(async resp => !resp.ok ? throw new Error(`${updateRes.status} ${await updateRes.text()}`) : null);
await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}/labels`, {
method: 'POST',
headers: {'Authorization': `token ${auth}`, 'Content-Type': 'application/json'},
First-time contributor

Same bugs as line 172: updateRes referenced before definition and invalid throw syntax in ternary expression.

Same bugs as line 172: `updateRes` referenced before definition and invalid throw syntax in ternary expression.
ztimson marked this conversation as resolved
@@ -149,0 +178,4 @@
}).then(async resp => !resp.ok ? throw new Error(`${updateRes.status} ${await updateRes.text()}`) : null);
await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}`, {
method: 'PATCH',
headers: {'Authorization': `token ${auth}`, 'Content-Type': 'application/json'},
First-time contributor

Same bugs as line 172: updateRes referenced before definition and invalid throw syntax in ternary expression.

Same bugs as line 172: `updateRes` referenced before definition and invalid throw syntax in ternary expression.
ztimson marked this conversation as resolved
src/refine.mjs Outdated
@@ -149,2 +186,4 @@
}
// Update ticket
const updateRes = await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}`, {
First-time contributor

Same bugs: updateRes is used in its own error handler before being assigned, and invalid throw syntax in ternary expression. This creates a circular reference issue.

Same bugs: `updateRes` is used in its own error handler before being assigned, and invalid throw syntax in ternary expression. This creates a circular reference issue.
ztimson marked this conversation as resolved
src/refine.mjs Outdated
@@ -163,1 +191,3 @@
const resp = await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}/labels`, {
headers: {'Authorization': `token ${auth}`, 'Content-Type': 'application/json'},
body: JSON.stringify({title, body})
}).then(async resp => !resp.ok ? throw new Error(`${updateRes.status} ${await updateRes.text()}`) : null);
First-time contributor

The label name changed from Kind/${type} to Reviewed/${type} without explanation. This could break existing label filtering logic. Verify this is intentional and consistent with the system's labeling scheme.

The label name changed from `Kind/${type}` to `Reviewed/${type}` without explanation. This could break existing label filtering logic. Verify this is intentional and consistent with the system's labeling scheme.
ztimson marked this conversation as resolved
src/refine.mjs Outdated
@@ -164,0 +191,4 @@
headers: {'Authorization': `token ${auth}`, 'Content-Type': 'application/json'},
body: JSON.stringify({title, body})
}).then(async resp => !resp.ok ? throw new Error(`${updateRes.status} ${await updateRes.text()}`) : null);
if(type) { // Label
First-time contributor

Same bugs as above: updateRes referenced in error handler instead of resp, and invalid throw syntax.

Same bugs as above: `updateRes` referenced in error handler instead of `resp`, and invalid throw syntax.
ztimson marked this conversation as resolved
ztimson added 1 commit 2025-12-30 23:42:21 -05:00
Fixed bugs
All checks were successful
Publish Library / Build NPM Project (push) Successful in 3s
Publish Library / Tag Version (push) Successful in 15s
Code review / review (pull_request) Successful in 1m30s
eb4486f196
assistant reviewed 2025-12-30 23:43:54 -05:00
assistant left a comment
First-time contributor

Summary

This code review identified 13 additional issues beyond those already flagged in the existing comments:

Critical Bugs:

  1. Label comparison error (line 35): Comparing label object to string instead of accessing .name property
  2. Wrong API endpoint (line 153): Missing owner/repo in search URL
  3. Wrong parameter type (line 158): Using repo name instead of repo ID
  4. Manual JSON construction (lines 174, 199): Security risk and error-prone
  5. Potential runtime error (line 195): No validation before accessing type[0]

Logic Issues:

  1. Overly restrictive label filter (line 35): Changed from "any label matches" to "exactly one label"
  2. Race condition (line 166): Using title before AI may have set it
  3. Substring matching bug (line 170): Could cause false positive duplicate detection
  4. Unused variable (line 189): updateRes assigned but not used

Error Handling Concerns:

  1. Silent failure (line 162): Search errors ignored, skipping duplicate detection
  2. Empty context (line 164): AI duplicate check ineffective when search fails
  3. Behavioral change (line 150): Throws instead of graceful exit
  4. Inconsistent types (line 162): Fallback creates type ambiguity

The most critical issues are the label comparison bug (line 35) which will cause the script to never process any issues, the API endpoint error (line 153), and the manual JSON string construction which poses security risks. These should be addressed immediately before deployment.

## Summary This code review identified **13 additional issues** beyond those already flagged in the existing comments: ### Critical Bugs: 1. **Label comparison error (line 35)**: Comparing label object to string instead of accessing `.name` property 2. **Wrong API endpoint (line 153)**: Missing owner/repo in search URL 3. **Wrong parameter type (line 158)**: Using repo name instead of repo ID 4. **Manual JSON construction (lines 174, 199)**: Security risk and error-prone 5. **Potential runtime error (line 195)**: No validation before accessing `type[0]` ### Logic Issues: 6. **Overly restrictive label filter (line 35)**: Changed from "any label matches" to "exactly one label" 7. **Race condition (line 166)**: Using `title` before AI may have set it 8. **Substring matching bug (line 170)**: Could cause false positive duplicate detection 9. **Unused variable (line 189)**: `updateRes` assigned but not used ### Error Handling Concerns: 10. **Silent failure (line 162)**: Search errors ignored, skipping duplicate detection 11. **Empty context (line 164)**: AI duplicate check ineffective when search fails 12. **Behavioral change (line 150)**: Throws instead of graceful exit 13. **Inconsistent types (line 162)**: Fallback creates type ambiguity The most critical issues are the label comparison bug (line 35) which will cause the script to never process any issues, the API endpoint error (line 153), and the manual JSON string construction which poses security risks. These should be addressed immediately before deployment.
src/refine.mjs Outdated
@@ -32,3 +35,1 @@
if(!issueRes.ok) throw new Error(`${issueRes.status} ${await issueRes.text()}`);
const issueData = await issueRes.json();
if(!issueData.labels?.some(l => l.name === 'Review/AI')) {
if(issueData.labels.length !== 1 || issueData.labels?.[0] !== 'Review/AI') {
First-time contributor

Logic error: The check issueData.labels?.[0] !== 'Review/AI' compares a label object to a string. Labels are objects with properties like name, not strings. This should be issueData.labels?.[0]?.name !== 'Review/AI' to correctly access the label's name property.

Logic error: The check `issueData.labels?.[0] !== 'Review/AI'` compares a label object to a string. Labels are objects with properties like `name`, not strings. This should be `issueData.labels?.[0]?.name !== 'Review/AI'` to correctly access the label's name property.
First-time contributor

Logic error: The condition checks if labels.length !== 1 which will skip processing for issues with 0 or 2+ labels. However, the original code used .some() which would process issues that have the 'Review/AI' label among other labels. This change makes the filter more restrictive and may break existing workflows where issues have multiple labels.

Logic error: The condition checks if `labels.length !== 1` which will skip processing for issues with 0 or 2+ labels. However, the original code used `.some()` which would process issues that have the 'Review/AI' label among other labels. This change makes the filter more restrictive and may break existing workflows where issues have multiple labels.
ztimson marked this conversation as resolved
@@ -146,3 +150,1 @@
if(!body) {
console.log('Invalid response from AI');
return process.exit(1);
if(!body) throw new Error('Invalid response from AI');
First-time contributor

Error handling change: Changed from graceful exit with exit code 1 to throwing an error. This is a behavioral change that could affect how the script is used in CI/CD pipelines. Ensure this is intentional and that calling code can handle the exception.

Error handling change: Changed from graceful exit with exit code 1 to throwing an error. This is a behavioral change that could affect how the script is used in CI/CD pipelines. Ensure this is intentional and that calling code can handle the exception.
ztimson marked this conversation as resolved
src/refine.mjs Outdated
@@ -149,0 +150,4 @@
if(!body) throw new Error('Invalid response from AI');
// Check for duplicates
const search = await fetch(`${git}/api/v1/repos/issues/search`, {
First-time contributor

Potential bug: The search API endpoint ${git}/api/v1/repos/issues/search is missing the ${owner}/${repo} path segment. This should likely be ${git}/api/v1/repos/${owner}/${repo}/issues/search to search within the specific repository, otherwise it may search globally or fail.

Potential bug: The search API endpoint `${git}/api/v1/repos/issues/search` is missing the `${owner}/${repo}` path segment. This should likely be `${git}/api/v1/repos/${owner}/${repo}/issues/search` to search within the specific repository, otherwise it may search globally or fail.
ztimson marked this conversation as resolved
src/refine.mjs Outdated
@@ -149,0 +155,4 @@
headers: {'Authorization': `token ${auth}`, 'Content-Type': 'application/json'},
body: JSON.stringify({
priority_repo_id: repo,
type: 'issues',
First-time contributor

Bug: The priority_repo_id parameter expects a repository ID (numeric), but repo variable contains the repository name (string). This will likely cause the search to fail or return incorrect results.

Bug: The `priority_repo_id` parameter expects a repository ID (numeric), but `repo` variable contains the repository name (string). This will likely cause the search to fail or return incorrect results.
ztimson marked this conversation as resolved
@@ -149,0 +159,4 @@
limit: 3,
q: title
})
}).then(resp => resp.ok ? resp.json() : []);
First-time contributor

Error handling issue: If the search API returns an error, the code defaults to an empty array []. This silently fails and skips duplicate detection. Consider logging a warning or handling the error more explicitly to avoid missing duplicates due to API failures.

Error handling issue: If the search API returns an error, the code defaults to an empty array `[]`. This silently fails and skips duplicate detection. Consider logging a warning or handling the error more explicitly to avoid missing duplicates due to API failures.
First-time contributor

Type safety issue: The fallback [] when resp.ok is false means search could be an empty array or the actual response data. This inconsistency could cause issues if the API response structure is unexpected. Consider explicitly handling the error case.

Type safety issue: The fallback `[]` when `resp.ok` is false means `search` could be an empty array or the actual response data. This inconsistency could cause issues if the API response structure is unexpected. Consider explicitly handling the error case.
ztimson marked this conversation as resolved
@@ -149,0 +161,4 @@
})
}).then(resp => resp.ok ? resp.json() : []);
let dupeId = null;
const dupeIds = search.map(t => t.id);
First-time contributor

Potential null reference: If search is an empty array (from error or no results), search.map() will work but dupes will be empty. The AI duplicate check on line 166 will then have no context to compare against, making the duplicate detection ineffective when it fails silently.

Potential null reference: If `search` is an empty array (from error or no results), `search.map()` will work but `dupes` will be empty. The AI duplicate check on line 166 will then have no context to compare against, making the duplicate detection ineffective when it fails silently.
ztimson marked this conversation as resolved
@@ -149,0 +163,4 @@
let dupeId = null;
const dupeIds = search.map(t => t.id);
const dupes = search.map(t => `ID: ${t.id}\nTitle: ${t.title}\n\`\`\`markdown\n${t.body}\n\`\`\``).join('\n\n');
const hasDuplicates = (await ai.language.ask(`${title}\n\`\`\`markdown\n${body}\n\`\`\``, {
First-time contributor

Race condition risk: The duplicate check uses title which is set by the AI tool call. If the AI hasn't called the title tool yet or it's still empty, the search query will be empty or invalid, potentially returning incorrect results or all issues.

Race condition risk: The duplicate check uses `title` which is set by the AI tool call. If the AI hasn't called the `title` tool yet or it's still empty, the search query will be empty or invalid, potentially returning incorrect results or all issues.
ztimson marked this conversation as resolved
@@ -149,0 +167,4 @@
system: `Your job is to identify duplicates. Respond with the ID number of the duplicate or nothing if there are no matches \n\n${dupes}`
}))?.pop()?.content;
// Handle duplicates
if(!!hasDuplicates && (dupeId = dupeIds.find(id => hasDuplicates.includes(id.toString()))) != null) {
First-time contributor

Logic issue: The duplicate detection uses hasDuplicates.includes(id.toString()) which is a substring match. This could cause false positives (e.g., ID 123 would match content containing "1234"). Consider using a more precise matching strategy or parsing the AI response more carefully.

Logic issue: The duplicate detection uses `hasDuplicates.includes(id.toString())` which is a substring match. This could cause false positives (e.g., ID 123 would match content containing "1234"). Consider using a more precise matching strategy or parsing the AI response more carefully.
ztimson marked this conversation as resolved
@@ -149,0 +171,4 @@
await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}/comments`, {
method: 'POST',
headers: {'Authorization': `token ${auth}`, 'Content-Type': 'application/json'},
body: `{"body": "Duplicate of #${dupeId}"}`
First-time contributor

Bug: JSON string is manually constructed instead of using JSON.stringify(). The body should be body: JSON.stringify({body: \Duplicate of #${dupeId}`})` to properly escape special characters and prevent JSON injection vulnerabilities.

Bug: JSON string is manually constructed instead of using `JSON.stringify()`. The body should be `body: JSON.stringify({body: \`Duplicate of #${dupeId}\`})` to properly escape special characters and prevent JSON injection vulnerabilities.
ztimson marked this conversation as resolved
@@ -149,1 +186,4 @@
console.log('Duplicate');
return process.exit();
}
First-time contributor

Unused variable: updateRes is assigned but never used. The .then() chain doesn't return the response, so updateRes will be a Promise that resolves to undefined. If you need the response, remove the .then() or return the response from it.

Unused variable: `updateRes` is assigned but never used. The `.then()` chain doesn't return the response, so `updateRes` will be a Promise that resolves to undefined. If you need the response, remove the `.then()` or return the response from it.
ztimson marked this conversation as resolved
src/refine.mjs Outdated
@@ -163,1 +193,3 @@
const resp = await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}/labels`, {
headers: {'Authorization': `token ${auth}`, 'Content-Type': 'application/json'},
body: JSON.stringify({title, body})
}).then(async resp => { if(!resp.ok) throw new Error(`${resp.status} ${await resp.text()}`); });
First-time contributor

Missing error handling: If type is an empty string or undefined, the type[0].toUpperCase() will throw a runtime error. Add a check to ensure type is valid before attempting to transform it.

Missing error handling: If `type` is an empty string or undefined, the `type[0].toUpperCase()` will throw a runtime error. Add a check to ensure `type` is valid before attempting to transform it.
ztimson marked this conversation as resolved
@@ -169,3 +199,1 @@
body: JSON.stringify({labels: [`Kind/${type[0].toUpperCase() + type.slice(1).toLowerCase()}`]})
});
if(!resp.ok) throw new Error(`${resp.status} ${await resp.text()}`);
headers: {'Authorization': `token ${auth}`, 'Content-Type': 'application/json'},
First-time contributor

Bug: JSON string is manually constructed with template literal instead of using JSON.stringify(). This is error-prone and could break if type contains special characters. Should be body: JSON.stringify({labels: [\Reviewed/${type[0].toUpperCase() + type.slice(1).toLowerCase()}`]})`.

Bug: JSON string is manually constructed with template literal instead of using `JSON.stringify()`. This is error-prone and could break if `type` contains special characters. Should be `body: JSON.stringify({labels: [\`Reviewed/${type[0].toUpperCase() + type.slice(1).toLowerCase()}\`]})`.
ztimson marked this conversation as resolved
ztimson added 1 commit 2025-12-30 23:55:24 -05:00
Fixed more bugs
All checks were successful
Publish Library / Build NPM Project (push) Successful in 3s
Publish Library / Tag Version (push) Successful in 13s
Code review / review (pull_request) Successful in 58s
9e5372f37b
assistant reviewed 2025-12-30 23:56:24 -05:00
assistant left a comment
First-time contributor

Review Summary

This code review identified 10 critical issues in the refactoring of src/refine.mjs:

Critical Bugs (Must Fix):

  1. Line 35: Logic error comparing label name to number 1 instead of checking length, and comparing label object to string - this will cause the label filtering to fail completely
  2. Line 153: Missing repository path in API endpoint will cause search requests to fail
  3. Line 158: Null reference risk when repoInfo is null from failed fetch
  4. Line 162: Race condition where title may be empty when used in search query

Security Issues:

  1. Line 174: JSON injection vulnerability from manual string construction
  2. Line 199: JSON injection vulnerability (lower risk due to enum constraint)

Runtime Safety:

  1. Line 199: Potential TypeError if type is undefined when accessing type[0]

Breaking Changes:

  1. Line 199: Label prefix changed from Kind/ to Reviewed/ - verify intentional and update dependent systems

Logic Issues:

  1. Line 170: Substring matching in regex could cause false positive duplicate detection
  2. Line 164: Silent failure when search API fails makes duplicate detection ineffective

Positive Changes:

  • Improved error handling with try-catch wrapper at the top level
  • Better code organization with comments
  • Consistent use of .then() chains for error handling
  • Version bump to 0.1.0 reflects significant changes

Recommendation: Address the critical bugs (especially lines 35, 153, 158) before deployment as they will cause runtime failures. The security issues should also be fixed to prevent potential JSON injection attacks.

## Review Summary This code review identified **10 critical issues** in the refactoring of `src/refine.mjs`: ### Critical Bugs (Must Fix): 1. **Line 35**: Logic error comparing label name to number `1` instead of checking length, and comparing label object to string - this will cause the label filtering to fail completely 2. **Line 153**: Missing repository path in API endpoint will cause search requests to fail 3. **Line 158**: Null reference risk when `repoInfo` is null from failed fetch 4. **Line 162**: Race condition where `title` may be empty when used in search query ### Security Issues: 5. **Line 174**: JSON injection vulnerability from manual string construction 6. **Line 199**: JSON injection vulnerability (lower risk due to enum constraint) ### Runtime Safety: 7. **Line 199**: Potential TypeError if `type` is undefined when accessing `type[0]` ### Breaking Changes: 8. **Line 199**: Label prefix changed from `Kind/` to `Reviewed/` - verify intentional and update dependent systems ### Logic Issues: 9. **Line 170**: Substring matching in regex could cause false positive duplicate detection 10. **Line 164**: Silent failure when search API fails makes duplicate detection ineffective ### Positive Changes: - Improved error handling with try-catch wrapper at the top level - Better code organization with comments - Consistent use of `.then()` chains for error handling - Version bump to 0.1.0 reflects significant changes **Recommendation**: Address the critical bugs (especially lines 35, 153, 158) before deployment as they will cause runtime failures. The security issues should also be fixed to prevent potential JSON injection attacks.
src/refine.mjs Outdated
@@ -32,3 +35,1 @@
if(!issueRes.ok) throw new Error(`${issueRes.status} ${await issueRes.text()}`);
const issueData = await issueRes.json();
if(!issueData.labels?.some(l => l.name === 'Review/AI')) {
if(issueData.labels?.[0]?.name !== 1 || issueData.labels?.[0] !== 'Review/AI') {
First-time contributor

Critical logic error: The condition issueData.labels?.[0]?.name !== 1 compares a label name (string) to the number 1, which will always be true. This appears to be a typo and should likely be checking the length: issueData.labels?.length !== 1. Additionally, the second part issueData.labels?.[0] !== 'Review/AI' compares a label object to a string, which will always be true. The correct check should be: issueData.labels?.length !== 1 || issueData.labels?.[0]?.name !== 'Review/AI'

Critical logic error: The condition `issueData.labels?.[0]?.name !== 1` compares a label name (string) to the number 1, which will always be true. This appears to be a typo and should likely be checking the length: `issueData.labels?.length !== 1`. Additionally, the second part `issueData.labels?.[0] !== 'Review/AI'` compares a label object to a string, which will always be true. The correct check should be: `issueData.labels?.length !== 1 || issueData.labels?.[0]?.name !== 'Review/AI'`
ztimson marked this conversation as resolved
@@ -156,0 +150,4 @@
if(!body) throw new Error('Invalid response from AI');
// Check for duplicates
const repoInfo = await fetch(`${git}/api/v1/repos/${owner}/${repo}`, {headers: {'Authorization': `token ${auth}`},}).then(resp => resp.ok ? resp.json() : null);
First-time contributor

Critical bug: The search API endpoint is missing the repository path segment. This should be ${git}/api/v1/repos/${owner}/${repo}/issues/search instead of ${git}/api/v1/repos/issues/search. Without the proper path, the API call will likely fail or search globally instead of within the specific repository.

Critical bug: The search API endpoint is missing the repository path segment. This should be `${git}/api/v1/repos/${owner}/${repo}/issues/search` instead of `${git}/api/v1/repos/issues/search`. Without the proper path, the API call will likely fail or search globally instead of within the specific repository.
ztimson marked this conversation as resolved
@@ -156,3 +157,2 @@
body: JSON.stringify({
title,
body,
owner,
First-time contributor

Bug: The priority_repo_id parameter expects a numeric repository ID, but repoInfo.id could be null if the fetch on line 152 failed (returns null when !resp.ok). This will cause the search to fail. Add proper error handling: if(!repoInfo) throw new Error('Failed to fetch repository info'); before using repoInfo.id.

Bug: The `priority_repo_id` parameter expects a numeric repository ID, but `repoInfo.id` could be null if the fetch on line 152 failed (returns null when !resp.ok). This will cause the search to fail. Add proper error handling: `if(!repoInfo) throw new Error('Failed to fetch repository info');` before using repoInfo.id.
ztimson marked this conversation as resolved
@@ -159,0 +159,4 @@
priority_repo_id: repoInfo.id,
type: 'issues',
limit: 3,
q: title
First-time contributor

Race condition: The duplicate search uses the title variable which is set by the AI tool call. If the AI hasn't called the title tool yet at this point in execution, title will be an empty string, causing the search query to be empty or invalid. Consider moving the duplicate check after ensuring the AI has completed and set the title, or add validation: if(!title) throw new Error('AI failed to set title');

Race condition: The duplicate search uses the `title` variable which is set by the AI tool call. If the AI hasn't called the `title` tool yet at this point in execution, `title` will be an empty string, causing the search query to be empty or invalid. Consider moving the duplicate check after ensuring the AI has completed and set the title, or add validation: `if(!title) throw new Error('AI failed to set title');`
ztimson marked this conversation as resolved
@@ -161,3 +164,1 @@
if(!updateRes.ok) throw new Error(`${updateRes.status} ${await updateRes.text()}`);
if(type) {
const resp = await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}/labels`, {
}).then(resp => resp.ok ? resp.json() : []);
First-time contributor

Silent failure risk: When the search API fails (line 153 returns []), the dupes variable becomes an empty string, and the AI duplicate check has no context to compare against. The duplicate detection becomes ineffective but continues silently. Consider logging a warning or throwing an error when search fails to ensure duplicate detection is working properly.

Silent failure risk: When the search API fails (line 153 returns []), the `dupes` variable becomes an empty string, and the AI duplicate check has no context to compare against. The duplicate detection becomes ineffective but continues silently. Consider logging a warning or throwing an error when search fails to ensure duplicate detection is working properly.
ztimson marked this conversation as resolved
@@ -164,0 +167,4 @@
const dupes = search.map(t => `ID: ${t.id}\nTitle: ${t.title}\n\`\`\`markdown\n${t.body}\n\`\`\``).join('\n\n');
const hasDuplicates = (await ai.language.ask(`${title}\n\`\`\`markdown\n${body}\n\`\`\``, {
system: `Your job is to identify duplicates. Respond with the ID number of the duplicate or nothing if there are no matches \n\n${dupes}`
}))?.pop()?.content;
First-time contributor

Logic issue: The regex pattern (^| )${id}( |$) performs substring matching which could cause false positives. For example, if the AI response contains "123" it would match issue ID 12 or 23. Consider using word boundaries or more precise parsing of the AI response to extract the exact ID number.

Logic issue: The regex pattern `(^| )${id}( |$)` performs substring matching which could cause false positives. For example, if the AI response contains "123" it would match issue ID 12 or 23. Consider using word boundaries or more precise parsing of the AI response to extract the exact ID number.
@@ -164,1 +171,4 @@
// Handle duplicates
if(!!hasDuplicates && (dupeId = dupeIds.find(id => new RegExp(`(^| )${id}( |$)`, 'm').test(hasDuplicates)))) {
await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}/comments`, {
method: 'POST',
First-time contributor

JSON injection vulnerability: The JSON body is manually constructed using a template literal instead of JSON.stringify(). If dupeId contains special characters like quotes or backslashes, this will produce invalid JSON or allow injection. Change to: body: JSON.stringify({body: \Duplicate of #${dupeId}`})`

JSON injection vulnerability: The JSON body is manually constructed using a template literal instead of JSON.stringify(). If `dupeId` contains special characters like quotes or backslashes, this will produce invalid JSON or allow injection. Change to: `body: JSON.stringify({body: \`Duplicate of #${dupeId}\`})`
ztimson marked this conversation as resolved
@@ -172,0 +196,4 @@
body: JSON.stringify({title, body})
}).then(async resp => { if(!resp.ok) throw new Error(`${resp.status} ${await resp.text()}`); });
if(type) { // Label
await fetch(`${git}/api/v1/repos/${owner}/${repo}/issues/${ticket}/labels`, {
First-time contributor

JSON injection vulnerability: The JSON body is manually constructed using a template literal. If type contains special characters, this could produce invalid JSON. While type is constrained by the enum in the tool definition, it's still better practice to use JSON.stringify(): body: JSON.stringify({labels: [\Reviewed/${type[0].toUpperCase() + type.slice(1).toLowerCase()}`]})`

JSON injection vulnerability: The JSON body is manually constructed using a template literal. If `type` contains special characters, this could produce invalid JSON. While `type` is constrained by the enum in the tool definition, it's still better practice to use JSON.stringify(): `body: JSON.stringify({labels: [\`Reviewed/${type[0].toUpperCase() + type.slice(1).toLowerCase()}\`]})`
First-time contributor

Potential runtime error: If type is an empty string or undefined (e.g., if the AI fails to call the type tool), then type[0].toUpperCase() will throw a TypeError. Add validation before this line: if(!type) throw new Error('AI failed to set type');

Potential runtime error: If `type` is an empty string or undefined (e.g., if the AI fails to call the type tool), then `type[0].toUpperCase()` will throw a TypeError. Add validation before this line: `if(!type) throw new Error('AI failed to set type');`
First-time contributor

Breaking change: Label prefix changed from Kind/${type} to Reviewed/${type} without explanation. This could break existing workflows, filters, or automation that rely on the Kind/ label prefix. Verify this is intentional and update any dependent systems accordingly.

Breaking change: Label prefix changed from `Kind/${type}` to `Reviewed/${type}` without explanation. This could break existing workflows, filters, or automation that rely on the `Kind/` label prefix. Verify this is intentional and update any dependent systems accordingly.
ztimson marked this conversation as resolved
ztimson added 1 commit 2025-12-31 00:01:58 -05:00
Fixed more bugs
Some checks failed
Publish Library / Build NPM Project (push) Successful in 3s
Code review / review (pull_request) Has been cancelled
Publish Library / Tag Version (push) Successful in 7s
23cb66544e
ztimson merged commit 99a1e55471 into master 2025-12-31 00:02:18 -05:00
ztimson deleted branch ticket-duplicates 2025-12-31 00:02:18 -05:00
Sign in to join this conversation.