diff --git a/pages/api/repos/[owner]/[repo]/issues/[issueNumber]/comments.js b/pages/api/repos/[owner]/[repo]/issues/[issueNumber]/comments.js index 8f2d824..237c93a 100644 --- a/pages/api/repos/[owner]/[repo]/issues/[issueNumber]/comments.js +++ b/pages/api/repos/[owner]/[repo]/issues/[issueNumber]/comments.js @@ -1,16 +1,44 @@ import { HttpClient } from '@actions/http-client' -const createComment = async (http, params) => { +const createOrUpdateComment = async (http, params) => { const { repoToken, owner, repo, issueNumber, body } = params - return http.postJson( + // Fetch existing comments + const commentsResponse = await http.getJson( `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}/comments`, - { body }, { accept: 'application/vnd.github.v3+json', authorization: `token ${repoToken}`, } ) + + // Check if a comment already exists + const existingComment = commentsResponse.result.find(comment => + comment.user.login === "tscircuitbot" + ) + + + if (existingComment) { + // Update the existing comment + return http.patchJson( + `https://api.github.com/repos/${owner}/${repo}/issues/comments/${existingComment.id}`, + { body }, + { + accept: 'application/vnd.github.v3+json', + authorization: `token ${repoToken}`, + } + ) + } else { + // Create a new comment if no existing one is found + return http.postJson( + `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}/comments`, + { body }, + { + accept: 'application/vnd.github.v3+json', + authorization: `token ${repoToken}`, + } + ) + } } const checkToken = async (http, token) => { @@ -54,7 +82,7 @@ export default async function handler(req, res) { } const { owner, repo, issueNumber } = req.query - const response = await createComment(httpClient, { + const response = await createOrUpdateComment(httpClient, { owner, repo, issueNumber, @@ -67,4 +95,4 @@ export default async function handler(req, res) { console.error(JSON.stringify(err)) return res.status(500).json({ error: 'Internal server error' }) } -} +} \ No newline at end of file