mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-06 04:35:03 -05:00
* feat(confluence): added more confluence endpoints * update license * updated * updated docs
194 lines
6.2 KiB
TypeScript
194 lines
6.2 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
|
import { validateAlphanumericId, validateJiraCloudId } from '@/lib/core/security/input-validation'
|
|
import { getConfluenceCloudId } from '@/tools/confluence/utils'
|
|
|
|
const logger = createLogger('ConfluenceLabelsAPI')
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
// Add a label to a page
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const auth = await checkSessionOrInternalAuth(request)
|
|
if (!auth.success || !auth.userId) {
|
|
return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const {
|
|
domain,
|
|
accessToken,
|
|
cloudId: providedCloudId,
|
|
pageId,
|
|
labelName,
|
|
prefix: labelPrefix,
|
|
} = await request.json()
|
|
|
|
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 (!labelName) {
|
|
return NextResponse.json({ error: 'Label name 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/rest/api/content/${pageId}/label`
|
|
|
|
const body = [
|
|
{
|
|
prefix: labelPrefix || 'global',
|
|
name: labelName,
|
|
},
|
|
]
|
|
|
|
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 errorData = await response.json().catch(() => null)
|
|
logger.error('Confluence API error response:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: JSON.stringify(errorData, null, 2),
|
|
})
|
|
const errorMessage =
|
|
errorData?.message || `Failed to add Confluence label (${response.status})`
|
|
return NextResponse.json({ error: errorMessage }, { status: response.status })
|
|
}
|
|
|
|
const data = await response.json()
|
|
const addedLabel = data.results?.[0] || data[0] || data
|
|
return NextResponse.json({
|
|
id: addedLabel.id ?? '',
|
|
name: addedLabel.name ?? labelName,
|
|
prefix: addedLabel.prefix ?? labelPrefix ?? 'global',
|
|
pageId,
|
|
labelName,
|
|
})
|
|
} catch (error) {
|
|
logger.error('Error adding Confluence label:', error)
|
|
return NextResponse.json(
|
|
{ error: (error as Error).message || 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// List labels on a page
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const auth = await checkSessionOrInternalAuth(request)
|
|
if (!auth.success || !auth.userId) {
|
|
return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const domain = searchParams.get('domain')
|
|
const accessToken = searchParams.get('accessToken')
|
|
const pageId = searchParams.get('pageId')
|
|
const providedCloudId = searchParams.get('cloudId')
|
|
const limit = searchParams.get('limit') || '25'
|
|
const cursor = searchParams.get('cursor')
|
|
|
|
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)))
|
|
if (cursor) {
|
|
queryParams.append('cursor', cursor)
|
|
}
|
|
const url = `https://api.atlassian.com/ex/confluence/${cloudId}/wiki/api/v2/pages/${pageId}/labels?${queryParams.toString()}`
|
|
|
|
const response = await fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => null)
|
|
logger.error('Confluence API error response:', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: JSON.stringify(errorData, null, 2),
|
|
})
|
|
const errorMessage =
|
|
errorData?.message || `Failed to list Confluence labels (${response.status})`
|
|
return NextResponse.json({ error: errorMessage }, { status: response.status })
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
const labels = (data.results || []).map((label: any) => ({
|
|
id: label.id,
|
|
name: label.name,
|
|
prefix: label.prefix || 'global',
|
|
}))
|
|
|
|
return NextResponse.json({
|
|
labels,
|
|
nextCursor: data._links?.next
|
|
? new URL(data._links.next, 'https://placeholder').searchParams.get('cursor')
|
|
: null,
|
|
})
|
|
} catch (error) {
|
|
logger.error('Error listing Confluence labels:', error)
|
|
return NextResponse.json(
|
|
{ error: (error as Error).message || 'Internal server error' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|