@@ -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,57 @@ export async function POST(request: Request) {
94100 }
95101
96102 if ( priority !== undefined && priority !== null && priority !== '' ) {
97- fields . priority = {
98- name : priority ,
103+ const isNumericId = / ^ \d + $ / . test ( priority )
104+ fields . priority = isNumericId ? { id : priority } : { name : priority }
105+ }
106+
107+ if ( labels !== undefined && labels !== null && Array . isArray ( labels ) && labels . length > 0 ) {
108+ fields . labels = labels
109+ }
110+
111+ if ( duedate !== undefined && duedate !== null && duedate !== '' ) {
112+ fields . duedate = duedate
113+ }
114+
115+ if ( reporter !== undefined && reporter !== null && reporter !== '' ) {
116+ fields . reporter = {
117+ id : reporter ,
99118 }
100119 }
101120
102- if ( assignee !== undefined && assignee !== null && assignee !== '' ) {
103- fields . assignee = {
104- id : assignee ,
121+ if ( environment !== undefined && environment !== null && environment !== '' ) {
122+ fields . environment = {
123+ type : 'doc' ,
124+ version : 1 ,
125+ content : [
126+ {
127+ type : 'paragraph' ,
128+ content : [
129+ {
130+ type : 'text' ,
131+ text : environment ,
132+ } ,
133+ ] ,
134+ } ,
135+ ] ,
105136 }
106137 }
107138
139+ if (
140+ customFieldId !== undefined &&
141+ customFieldId !== null &&
142+ customFieldId !== '' &&
143+ customFieldValue !== undefined &&
144+ customFieldValue !== null &&
145+ customFieldValue !== ''
146+ ) {
147+ const fieldId = customFieldId . startsWith ( 'customfield_' )
148+ ? customFieldId
149+ : `customfield_${ customFieldId } `
150+
151+ fields [ fieldId ] = customFieldValue
152+ }
153+
108154 const body = { fields }
109155
110156 const response = await fetch ( url , {
@@ -132,16 +178,47 @@ export async function POST(request: Request) {
132178 }
133179
134180 const responseData = await response . json ( )
135- logger . info ( 'Successfully created Jira issue:' , responseData . key )
181+ const issueKey = responseData . key || 'unknown'
182+ logger . info ( 'Successfully created Jira issue:' , issueKey )
183+
184+ let assigneeId : string | undefined
185+ if ( assignee !== undefined && assignee !== null && assignee !== '' ) {
186+ const assignUrl = `https://api.atlassian.com/ex/jira/${ cloudId } /rest/api/3/issue/${ issueKey } /assignee`
187+ logger . info ( 'Assigning issue to:' , assignee )
188+
189+ const assignResponse = await fetch ( assignUrl , {
190+ method : 'PUT' ,
191+ headers : {
192+ Authorization : `Bearer ${ accessToken } ` ,
193+ Accept : 'application/json' ,
194+ 'Content-Type' : 'application/json' ,
195+ } ,
196+ body : JSON . stringify ( {
197+ accountId : assignee ,
198+ } ) ,
199+ } )
200+
201+ if ( ! assignResponse . ok ) {
202+ const assignErrorText = await assignResponse . text ( )
203+ logger . warn ( 'Failed to assign issue (issue was created successfully):' , {
204+ status : assignResponse . status ,
205+ error : assignErrorText ,
206+ } )
207+ } else {
208+ assigneeId = assignee
209+ logger . info ( 'Successfully assigned issue to:' , assignee )
210+ }
211+ }
136212
137213 return NextResponse . json ( {
138214 success : true ,
139215 output : {
140216 ts : new Date ( ) . toISOString ( ) ,
141- issueKey : responseData . key || 'unknown' ,
217+ issueKey : issueKey ,
142218 summary : responseData . fields ?. summary || 'Issue created' ,
143219 success : true ,
144- url : `https://${ domain } /browse/${ responseData . key } ` ,
220+ url : `https://${ domain } /browse/${ issueKey } ` ,
221+ ...( assigneeId && { assigneeId } ) ,
145222 } ,
146223 } )
147224 } catch ( error : any ) {
0 commit comments