|
| 1 | +import { NextResponse } from 'next/server' |
| 2 | +import { Logger } from '@/lib/logs/console/logger' |
| 3 | +import { getJiraCloudId } from '@/tools/jira/utils' |
| 4 | + |
| 5 | +export const dynamic = 'force-dynamic' |
| 6 | + |
| 7 | +const logger = new Logger('JiraUpdateAPI') |
| 8 | + |
| 9 | +export async function PUT(request: Request) { |
| 10 | + try { |
| 11 | + const { |
| 12 | + domain, |
| 13 | + accessToken, |
| 14 | + issueKey, |
| 15 | + summary, |
| 16 | + title, // Support both summary and title for backwards compatibility |
| 17 | + description, |
| 18 | + status, |
| 19 | + priority, |
| 20 | + assignee, |
| 21 | + cloudId: providedCloudId, |
| 22 | + } = await request.json() |
| 23 | + |
| 24 | + // Validate required parameters |
| 25 | + if (!domain) { |
| 26 | + logger.error('Missing domain in request') |
| 27 | + return NextResponse.json({ error: 'Domain is required' }, { status: 400 }) |
| 28 | + } |
| 29 | + |
| 30 | + if (!accessToken) { |
| 31 | + logger.error('Missing access token in request') |
| 32 | + return NextResponse.json({ error: 'Access token is required' }, { status: 400 }) |
| 33 | + } |
| 34 | + |
| 35 | + if (!issueKey) { |
| 36 | + logger.error('Missing issue key in request') |
| 37 | + return NextResponse.json({ error: 'Issue key is required' }, { status: 400 }) |
| 38 | + } |
| 39 | + |
| 40 | + // Use provided cloudId or fetch it if not provided |
| 41 | + const cloudId = providedCloudId || (await getJiraCloudId(domain, accessToken)) |
| 42 | + logger.info('Using cloud ID:', cloudId) |
| 43 | + |
| 44 | + // Build the URL using cloudId for Jira API |
| 45 | + const url = `https://api.atlassian.com/ex/jira/${cloudId}/rest/api/3/issue/${issueKey}` |
| 46 | + |
| 47 | + logger.info('Updating Jira issue at:', url) |
| 48 | + |
| 49 | + // Map the summary from either summary or title field |
| 50 | + const summaryValue = summary || title |
| 51 | + const fields: Record<string, any> = {} |
| 52 | + |
| 53 | + if (summaryValue) { |
| 54 | + fields.summary = summaryValue |
| 55 | + } |
| 56 | + |
| 57 | + if (description) { |
| 58 | + fields.description = { |
| 59 | + type: 'doc', |
| 60 | + version: 1, |
| 61 | + content: [ |
| 62 | + { |
| 63 | + type: 'paragraph', |
| 64 | + content: [ |
| 65 | + { |
| 66 | + type: 'text', |
| 67 | + text: description, |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + ], |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (status) { |
| 76 | + fields.status = { |
| 77 | + name: status, |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (priority) { |
| 82 | + fields.priority = { |
| 83 | + name: priority, |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (assignee) { |
| 88 | + fields.assignee = { |
| 89 | + id: assignee, |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + const body = { fields } |
| 94 | + |
| 95 | + // Make the request to Jira API |
| 96 | + const response = await fetch(url, { |
| 97 | + method: 'PUT', |
| 98 | + headers: { |
| 99 | + Authorization: `Bearer ${accessToken}`, |
| 100 | + Accept: 'application/json', |
| 101 | + 'Content-Type': 'application/json', |
| 102 | + }, |
| 103 | + body: JSON.stringify(body), |
| 104 | + }) |
| 105 | + |
| 106 | + if (!response.ok) { |
| 107 | + const errorText = await response.text() |
| 108 | + logger.error('Jira API error:', { |
| 109 | + status: response.status, |
| 110 | + statusText: response.statusText, |
| 111 | + error: errorText, |
| 112 | + }) |
| 113 | + |
| 114 | + return NextResponse.json( |
| 115 | + { error: `Jira API error: ${response.status} ${response.statusText}`, details: errorText }, |
| 116 | + { status: response.status } |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + // Note: Jira update API typically returns 204 No Content on success |
| 121 | + const responseData = response.status === 204 ? {} : await response.json() |
| 122 | + logger.info('Successfully updated Jira issue:', issueKey) |
| 123 | + |
| 124 | + return NextResponse.json({ |
| 125 | + success: true, |
| 126 | + output: { |
| 127 | + ts: new Date().toISOString(), |
| 128 | + issueKey: responseData.key || issueKey, |
| 129 | + summary: responseData.fields?.summary || 'Issue updated', |
| 130 | + success: true, |
| 131 | + }, |
| 132 | + }) |
| 133 | + } catch (error: any) { |
| 134 | + logger.error('Error updating Jira issue:', { |
| 135 | + error: error instanceof Error ? error.message : String(error), |
| 136 | + stack: error instanceof Error ? error.stack : undefined, |
| 137 | + }) |
| 138 | + |
| 139 | + return NextResponse.json( |
| 140 | + { |
| 141 | + error: error instanceof Error ? error.message : 'Internal server error', |
| 142 | + success: false, |
| 143 | + }, |
| 144 | + { status: 500 } |
| 145 | + ) |
| 146 | + } |
| 147 | +} |
0 commit comments