Skip to content

Commit 905bf40

Browse files
committed
#401 - Added fallback to v2 API when v3 jql search fails. This is to support datacenter version of JA
(cherry picked from commit f57dbf7)
1 parent 315b22f commit 905bf40

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/constants/api-urls.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const ApiUrls = {
22
getAllProjects: "~/rest/api/2/project",
33
search: "~/rest/api/3/search/jql",
4+
searchLegacyV2: "~/rest/api/2/search",
45
createIssue: "~/rest/api/2/issue/",
56
cloneIssue: "~/rest/internal/2/issue/{0}/clone",
67
taskStatus: "~/rest/api/3/task/{0}",

src/services/jira-service.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)