@@ -48,7 +48,6 @@ export default class JiraService {
4848 // Process worklog comments
4949 if ( fields . includes ( "worklog" ) ) {
5050 await this . validateForWorklogs ( issues , worklogStartDate , worklogEndDate ) ;
51-
5251 }
5352
5453 // Handle pagination using v3 API tokens
@@ -59,6 +58,62 @@ export default class JiraService {
5958
6059 return issues ;
6160
61+ } catch ( err ) {
62+ if ( ! nextPageToken ) { // Fallback to v2 only for the failure in first call.
63+ console . error ( 'JQL v3 search failed. Fallback to v2 search' , err ) ;
64+ return this . searchTicketsFallback_V2 ( jql , fields , undefined , opts ) ;
65+ }
66+
67+ if ( opts ?. ignoreErrors !== true ) {
68+ const messages = err . error ?. errorMessages ;
69+ if ( messages ?. length > 0 ) {
70+ this . $message . error ( messages . join ( '<br/>' ) , "Error fetching ticket details" ) ;
71+ }
72+ }
73+ throw err ;
74+ }
75+ }
76+
77+ // This function can be deleted after all Jira instances started supporting v3 API.
78+ // eslint-disable-next-line complexity
79+ async searchTicketsFallback_V2 ( jql , fields , startAt , opts ) {
80+ startAt = startAt || 0 ;
81+ fields = fields || defaultJiraFields ;
82+ const { worklogStartDate, worklogEndDate } = opts || { } ;
83+
84+ const postData = { jql, fields, maxResults : opts ?. maxResults || 1000 } ;
85+ if ( opts ?. expand ?. length ) {
86+ postData . expand = opts . expand ;
87+ }
88+
89+ if ( startAt > 0 ) {
90+ postData . startAt = startAt ;
91+ }
92+
93+ try {
94+ const result = await this . $ajax . get ( prepareUrlWithQueryString ( ApiUrls . searchLegacyV2 , postData ) ) ;
95+ const issues = result . issues ;
96+ issues . total = result . total ;
97+
98+ if ( opts ?. ignoreWarnings !== true ) {
99+ if ( result . warningMessages ?. length ) {
100+ const msg = result . warningMessages . join ( '\r\n' ) ;
101+ this . $message . warning ( msg , 'Query Error' ) ;
102+ }
103+ }
104+
105+ if ( fields . includes ( "worklog" ) ) {
106+ await this . validateForWorklogs ( issues , worklogStartDate , worklogEndDate ) ;
107+ }
108+
109+ const searchResult = { total : result . total , issues : issues , startAt : startAt } ;
110+
111+ if ( ! opts ?. maxResults && ( ( issues . length + searchResult . startAt ) < searchResult . total && issues . length > 0 ) ) {
112+ const res = await this . searchTicketsFallback_V2 ( jql , fields , searchResult . startAt + issues . length , opts ) ;
113+ issues . addRange ( res ) ;
114+ }
115+
116+ return issues ;
62117 } catch ( err ) {
63118 if ( opts ?. ignoreErrors !== true ) {
64119 const messages = err . error ?. errorMessages ;
0 commit comments