@@ -19,82 +19,85 @@ export default class JiraService {
1919 this . runningRequests = { } ;
2020 }
2121
22- searchTickets ( jql , fields , startAt , opts ) {
23- startAt = startAt || 0 ;
22+ // eslint-disable-next-line complexity
23+ async searchTickets ( jql , fields , nextPageToken , opts ) {
2424 fields = fields || defaultJiraFields ;
2525 const { worklogStartDate, worklogEndDate } = opts || { } ;
26- return new Promise ( ( resolve , reject ) => {
26+
27+ try {
2728 const postData = { jql, fields, maxResults : opts ?. maxResults || 1000 } ;
2829 if ( opts ?. expand ?. length ) {
2930 postData . expand = opts . expand ;
3031 }
3132
32- if ( startAt > 0 ) {
33- postData . startAt = startAt ;
33+ if ( nextPageToken ) {
34+ postData . nextPageToken = nextPageToken ;
3435 }
35- this . $ajax . get ( prepareUrlWithQueryString ( ApiUrls . search , postData ) )
36- . then ( ( result ) => {
37- const issues = result . issues ;
38- issues . total = result . total ;
39- if ( opts ?. ignoreWarnings !== true ) {
40- if ( result . warningMessages ?. length ) {
41- const msg = result . warningMessages . join ( '\r\n' ) ;
42- this . $message . warning ( msg , 'Query Error' ) ;
43- }
44- }
45- //if (result.maxResults < result.total) {
46- // this.$message.warning("Your filter returned " + result.total + " tickets but only first " + result.maxResults + " were fetched!");
47- //}
48- if ( fields . indexOf ( "worklog" ) > - 1 ) {
49- let prevCount = issues . length ;
50- let retryCount = 3 ;
51- const cback = ( remaining , isus ) => {
52- if ( remaining === 0 ) {
53- resolve ( { total : result . total , issues : issues , startAt : startAt } ) ;
54- }
55- else if ( prevCount > remaining || -- retryCount >= 0 ) {
56- prevCount = remaining ;
57- this . fillWorklogs ( isus , worklogStartDate , worklogEndDate , cback ) ;
58- }
59- else {
60- reject ( null ) ;
61- }
62- } ;
63- cback ( false , issues ) ;
64- retryCount = 3 ;
65- }
66- else {
67- resolve ( { total : result . total , issues : issues , startAt : startAt } ) ;
68- }
69- } , ( err ) => {
70- if ( opts ?. ignoreErrors !== true ) {
71- const messages = err . error ?. errorMessages ;
72- if ( messages ?. length > 0 ) {
73- this . $message . error ( messages . join ( '<br/>' ) , "Error fetching ticket details" ) ;
74- }
75- }
76- reject ( err ) ;
77- } ) ;
78- } ) . then ( ( result ) => {
36+
37+ const result = await this . $ajax . get ( prepareUrlWithQueryString ( ApiUrls . search , postData ) ) ;
38+
7939 const issues = result . issues ;
80- if ( ! opts ?. maxResults && ( ( issues . length + result . startAt ) < result . total && issues . length > 0 ) ) {
81- return this . searchTickets ( jql , fields , result . startAt + issues . length , opts ) . then ( res => issues . addRange ( res ) ) ;
40+
41+ if ( opts ?. ignoreWarnings !== true ) {
42+ if ( result . warningMessages ?. length ) {
43+ const msg = result . warningMessages . join ( '\r\n' ) ;
44+ this . $message . warning ( msg , 'Query Error' ) ;
45+ }
8246 }
83- else {
84- return issues ;
47+
48+ // Process worklog comments
49+ if ( fields . includes ( "worklog" ) ) {
50+ await this . validateForWorklogs ( issues , worklogStartDate , worklogEndDate ) ;
51+
52+ }
53+
54+ // Handle pagination using v3 API tokens
55+ if ( ! opts ?. maxResults && ! result . isLast && issues . length > 0 ) {
56+ const nextResults = await this . searchTickets ( jql , fields , result . nextPageToken , opts ) ;
57+ issues . addRange ( nextResults ) ;
58+ }
59+
60+ return issues ;
61+
62+ } catch ( err ) {
63+ if ( opts ?. ignoreErrors !== true ) {
64+ const messages = err . error ?. errorMessages ;
65+ if ( messages ?. length > 0 ) {
66+ this . $message . error ( messages . join ( '<br/>' ) , "Error fetching ticket details" ) ;
67+ }
68+ }
69+ throw err ;
70+ }
71+ }
72+
73+ async validateForWorklogs ( issues , worklogStartDate , worklogEndDate ) {
74+ await new Promise ( ( resolve , reject ) => {
75+ let prevCount = issues . length ;
76+ let retryCount = 3 ;
77+ const cback = ( remaining , isus ) => {
78+ if ( remaining === 0 ) {
79+ resolve ( ) ;
80+ }
81+ else if ( prevCount > remaining || -- retryCount >= 0 ) {
82+ prevCount = remaining ;
83+ this . fillWorklogs ( isus , worklogStartDate , worklogEndDate , cback ) ;
84+ }
85+ else {
86+ reject ( null ) ;
87+ }
88+ } ;
89+ cback ( false , issues ) ;
90+ retryCount = 3 ;
91+ } ) ;
92+
93+
94+ issues . forEach ( issue => {
95+ const worklogs = issue . fields ?. worklog ?. worklogs ;
96+ if ( Array . isArray ( worklogs ) && worklogs . length ) {
97+ worklogs . forEach ( w => {
98+ w . comment = stringifyComment ( w . comment ) ;
99+ } ) ;
85100 }
86- } ) . then ( ( issues ) => {
87- if ( fields . includes ( "worklog" ) ) {
88- issues . forEach ( issue => {
89- const worklogs = issue . fields ?. worklog ?. worklogs ;
90- if ( Array . isArray ( worklogs ) && worklogs . length ) {
91- worklogs . forEach ( w => {
92- w . comment = stringifyComment ( w . comment ) ;
93- } ) ;
94- }
95- } ) ;
96- }
97- return issues ;
98101 } ) ;
99102 }
100103
0 commit comments