Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions pages/api/repos/[owner]/[repo]/issues/[issueNumber]/comments.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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,
Expand All @@ -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' })
}
}
}