d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
214 lines
6.8 KiB
TypeScript
214 lines
6.8 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import {
|
|
confluenceCreateCommentContract,
|
|
confluenceListCommentsContract,
|
|
} from '@/lib/api/contracts/selectors/confluence'
|
|
import { parseRequest } from '@/lib/api/server'
|
|
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
|
import { validateAlphanumericId, validateJiraCloudId } from '@/lib/core/security/input-validation'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { getConfluenceCloudId } from '@/tools/confluence/utils'
|
|
import { parseAtlassianErrorMessage } from '@/tools/jira/utils'
|
|
|
|
const logger = createLogger('ConfluenceCommentsAPI')
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
// Create a comment
|
|
export const POST = withRouteHandler(async (request: NextRequest) => {
|
|
try {
|
|
const auth = await checkSessionOrInternalAuth(request)
|
|
if (!auth.success || !auth.userId) {
|
|
return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const parsed = await parseRequest(confluenceCreateCommentContract, request, {})
|
|
if (!parsed.success) return parsed.response
|
|
|
|
const { domain, accessToken, cloudId: providedCloudId, pageId, comment } = parsed.data.body
|
|
|
|
if (!domain) {
|
|
return NextResponse.json({ error: 'Domain is required' }, { status: 400 })
|
|
}
|
|
|
|
if (!accessToken) {
|
|
return NextResponse.json({ error: 'Access token is required' }, { status: 400 })
|
|
}
|
|
|
|
if (!pageId) {
|
|
return NextResponse.json({ error: 'Page ID is required' }, { status: 400 })
|
|
}
|
|
|
|
if (!comment) {
|
|
return NextResponse.json({ error: 'Comment is required' }, { status: 400 })
|
|
}
|
|
|
|
const pageIdValidation = validateAlphanumericId(pageId, 'pageId', 255)
|
|
if (!pageIdValidation.isValid) {
|
|
return NextResponse.json({ error: pageIdValidation.error }, { status: 400 })
|
|
}
|
|
|
|
const cloudId = providedCloudId || (await getConfluenceCloudId(domain, accessToken))
|
|
|
|
const cloudIdValidation = validateJiraCloudId(cloudId, 'cloudId')
|
|
if (!cloudIdValidation.isValid) {
|
|
return NextResponse.json({ error: cloudIdValidation.error }, { status: 400 })
|
|
}
|
|
|
|
const url = `https://api.atlassian.com/ex/confluence/${cloudId}/wiki/api/v2/footer-comments`
|
|
|
|
logger.info('Calling Confluence API', { url })
|
|
|
|
const body = {
|
|
pageId,
|
|
body: {
|
|
representation: 'storage',
|
|
value: comment,
|
|
},
|
|
}
|
|
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
body: JSON.stringify(body),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text()
|
|
logger.error('Confluence API error response:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: errorText,
|
|
})
|
|
return NextResponse.json(
|
|
{ error: parseAtlassianErrorMessage(response.status, response.statusText, errorText) },
|
|
{ status: response.status }
|
|
)
|
|
}
|
|
|
|
const data = await response.json()
|
|
return NextResponse.json({ ...data, pageId })
|
|
} catch (error) {
|
|
logger.error('Error creating Confluence comment:', error)
|
|
return NextResponse.json(
|
|
{ error: (error as Error).message || 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
})
|
|
|
|
// List comments on a page
|
|
export const GET = withRouteHandler(async (request: NextRequest) => {
|
|
try {
|
|
const auth = await checkSessionOrInternalAuth(request)
|
|
if (!auth.success || !auth.userId) {
|
|
return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const parsed = await parseRequest(confluenceListCommentsContract, request, {})
|
|
if (!parsed.success) return parsed.response
|
|
|
|
const {
|
|
domain,
|
|
accessToken,
|
|
pageId,
|
|
cloudId: providedCloudId,
|
|
limit,
|
|
bodyFormat,
|
|
cursor,
|
|
} = parsed.data.query
|
|
|
|
if (!domain) {
|
|
return NextResponse.json({ error: 'Domain is required' }, { status: 400 })
|
|
}
|
|
|
|
if (!accessToken) {
|
|
return NextResponse.json({ error: 'Access token is required' }, { status: 400 })
|
|
}
|
|
|
|
if (!pageId) {
|
|
return NextResponse.json({ error: 'Page ID is required' }, { status: 400 })
|
|
}
|
|
|
|
const pageIdValidation = validateAlphanumericId(pageId, 'pageId', 255)
|
|
if (!pageIdValidation.isValid) {
|
|
return NextResponse.json({ error: pageIdValidation.error }, { status: 400 })
|
|
}
|
|
|
|
const cloudId = providedCloudId || (await getConfluenceCloudId(domain, accessToken))
|
|
|
|
const cloudIdValidation = validateJiraCloudId(cloudId, 'cloudId')
|
|
if (!cloudIdValidation.isValid) {
|
|
return NextResponse.json({ error: cloudIdValidation.error }, { status: 400 })
|
|
}
|
|
|
|
const queryParams = new URLSearchParams()
|
|
queryParams.append('limit', String(Math.min(Number(limit), 250)))
|
|
queryParams.append('body-format', bodyFormat)
|
|
if (cursor) {
|
|
queryParams.append('cursor', cursor)
|
|
}
|
|
const url = `https://api.atlassian.com/ex/confluence/${cloudId}/wiki/api/v2/pages/${pageId}/footer-comments?${queryParams.toString()}`
|
|
|
|
const response = await fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text()
|
|
logger.error('Confluence API error response:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: errorText,
|
|
})
|
|
return NextResponse.json(
|
|
{ error: parseAtlassianErrorMessage(response.status, response.statusText, errorText) },
|
|
{ status: response.status }
|
|
)
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
const comments = (data.results || []).map((comment: any) => {
|
|
const bodyValue = comment.body?.storage?.value || comment.body?.view?.value || ''
|
|
return {
|
|
id: comment.id,
|
|
body: {
|
|
value: bodyValue,
|
|
representation: bodyFormat,
|
|
},
|
|
createdAt: comment.createdAt || '',
|
|
authorId: comment.authorId || '',
|
|
status: comment.status ?? null,
|
|
title: comment.title ?? null,
|
|
pageId: comment.pageId ?? null,
|
|
blogPostId: comment.blogPostId ?? null,
|
|
parentCommentId: comment.parentCommentId ?? null,
|
|
version: comment.version ?? null,
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({
|
|
comments,
|
|
nextCursor: data._links?.next
|
|
? new URL(data._links.next, 'https://placeholder').searchParams.get('cursor')
|
|
: null,
|
|
})
|
|
} catch (error) {
|
|
logger.error('Error listing Confluence comments:', error)
|
|
return NextResponse.json(
|
|
{ error: (error as Error).message || 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
})
|