@@ -20,6 +20,12 @@ export async function POST(request: Request) {
2020 cloudId : providedCloudId ,
2121 issueType,
2222 parent,
23+ labels,
24+ duedate,
25+ reporter,
26+ environment,
27+ customFieldId,
28+ customFieldValue,
2329 } = await request . json ( )
2430
2531 if ( ! domain ) {
@@ -94,17 +100,61 @@ export async function POST(request: Request) {
94100 }
95101
96102 if ( priority !== undefined && priority !== null && priority !== '' ) {
97- fields . priority = {
98- name : priority ,
103+ // Check if priority is a numeric ID or a name
104+ const isNumericId = / ^ \d + $ / . test ( priority )
105+ fields . priority = isNumericId ? { id : priority } : { name : priority }
106+ }
107+
108+ if ( labels !== undefined && labels !== null && Array . isArray ( labels ) && labels . length > 0 ) {
109+ fields . labels = labels
110+ }
111+
112+ if ( duedate !== undefined && duedate !== null && duedate !== '' ) {
113+ fields . duedate = duedate
114+ }
115+
116+ if ( reporter !== undefined && reporter !== null && reporter !== '' ) {
117+ fields . reporter = {
118+ id : reporter ,
99119 }
100120 }
101121
102- if ( assignee !== undefined && assignee !== null && assignee !== '' ) {
103- fields . assignee = {
104- id : assignee ,
122+ if ( environment !== undefined && environment !== null && environment !== '' ) {
123+ fields . environment = {
124+ type : 'doc' ,
125+ version : 1 ,
126+ content : [
127+ {
128+ type : 'paragraph' ,
129+ content : [
130+ {
131+ type : 'text' ,
132+ text : environment ,
133+ } ,
134+ ] ,
135+ } ,
136+ ] ,
105137 }
106138 }
107139
140+ // Handle team assignment via custom field
141+ if (
142+ customFieldId !== undefined &&
143+ customFieldId !== null &&
144+ customFieldId !== '' &&
145+ customFieldValue !== undefined &&
146+ customFieldValue !== null &&
147+ customFieldValue !== ''
148+ ) {
149+ // Normalize the field ID (ensure it starts with customfield_)
150+ const fieldId = customFieldId . startsWith ( 'customfield_' )
151+ ? customFieldId
152+ : `customfield_${ customFieldId } `
153+
154+ // Use the team UUID directly
155+ fields [ fieldId ] = customFieldValue
156+ }
157+
108158 const body = { fields }
109159
110160 const response = await fetch ( url , {
@@ -132,16 +182,47 @@ export async function POST(request: Request) {
132182 }
133183
134184 const responseData = await response . json ( )
135- logger . info ( 'Successfully created Jira issue:' , responseData . key )
185+ const issueKey = responseData . key || 'unknown'
186+ logger . info ( 'Successfully created Jira issue:' , issueKey )
187+
188+ let assigneeId : string | undefined
189+ if ( assignee !== undefined && assignee !== null && assignee !== '' ) {
190+ const assignUrl = `https://api.atlassian.com/ex/jira/${ cloudId } /rest/api/3/issue/${ issueKey } /assignee`
191+ logger . info ( 'Assigning issue to:' , assignee )
192+
193+ const assignResponse = await fetch ( assignUrl , {
194+ method : 'PUT' ,
195+ headers : {
196+ Authorization : `Bearer ${ accessToken } ` ,
197+ Accept : 'application/json' ,
198+ 'Content-Type' : 'application/json' ,
199+ } ,
200+ body : JSON . stringify ( {
201+ accountId : assignee ,
202+ } ) ,
203+ } )
204+
205+ if ( ! assignResponse . ok ) {
206+ const assignErrorText = await assignResponse . text ( )
207+ logger . warn ( 'Failed to assign issue (issue was created successfully):' , {
208+ status : assignResponse . status ,
209+ error : assignErrorText ,
210+ } )
211+ } else {
212+ assigneeId = assignee
213+ logger . info ( 'Successfully assigned issue to:' , assignee )
214+ }
215+ }
136216
137217 return NextResponse . json ( {
138218 success : true ,
139219 output : {
140220 ts : new Date ( ) . toISOString ( ) ,
141- issueKey : responseData . key || 'unknown' ,
221+ issueKey : issueKey ,
142222 summary : responseData . fields ?. summary || 'Issue created' ,
143223 success : true ,
144- url : `https://${ domain } /browse/${ responseData . key } ` ,
224+ url : `https://${ domain } /browse/${ issueKey } ` ,
225+ ...( assigneeId && { assigneeId } ) ,
145226 } ,
146227 } )
147228 } catch ( error : any ) {
0 commit comments