Skip to content

Commit 9d2da73

Browse files
authored
Jira - adding pagination to search-issues-with-jql (#18437)
* Adding pagination to search-issues-with-jql * Page adjustments * Typo fix
1 parent 78ebe2f commit 9d2da73

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

components/jira/actions/search-issues-with-jql/search-issues-with-jql.mjs

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
name: "Search Issues with JQL",
55
description: "Search for issues using JQL (Jira Query Language). [See the documentation](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-search/#api-rest-api-3-search-jql-get)",
66
key: "jira-search-issues-with-jql",
7-
version: "0.0.4",
7+
version: "0.1.0",
88
type: "action",
99
props: {
1010
jira,
@@ -17,16 +17,16 @@ export default {
1717
jql: {
1818
type: "string",
1919
label: "JQL Query",
20-
description: "The [JQL](https://support.atlassian.com/jira-software-cloud/docs/what-is-advanced-search-in-jira-cloud/) query to search for issues",
20+
description: "The JQL query to search for issues. [See the documentation for syntax and examples](https://support.atlassian.com/jira-software-cloud/docs/what-is-advanced-search-in-jira-cloud/)",
2121
},
2222
maxResults: {
2323
type: "integer",
2424
label: "Max Results",
25-
description: "Maximum number of issues to return (default: 50, max: 100)",
25+
description: "Maximum number of issues to return (default: 50)",
2626
optional: true,
2727
default: 50,
2828
min: 1,
29-
max: 100,
29+
max: 5000,
3030
},
3131
fields: {
3232
type: "string",
@@ -90,14 +90,62 @@ export default {
9090
optional: true,
9191
},
9292
},
93+
methods: {
94+
getMaxResultsPerPage() {
95+
return Math.min(this.maxResults, 1000);
96+
},
97+
async paginate({
98+
params, maxResults, ...otherArgs
99+
}) {
100+
const results = [];
101+
let nextPageToken;
102+
let response;
103+
104+
do {
105+
response = await this.jira.searchIssues({
106+
...otherArgs,
107+
params: {
108+
...params,
109+
...(nextPageToken && {
110+
nextPageToken,
111+
}),
112+
},
113+
});
114+
115+
const pageResults = response.issues;
116+
const pageLength = pageResults?.length;
117+
if (!pageLength) {
118+
break;
119+
}
120+
121+
// If maxResults is specified, only add what we need
122+
if (maxResults && results.length + pageLength > maxResults) {
123+
const remainingSlots = maxResults - results.length;
124+
results.push(...pageResults.slice(0, remainingSlots));
125+
break;
126+
} else {
127+
results.push(...pageResults);
128+
}
129+
130+
// Also break if we've reached maxResults exactly
131+
if (maxResults && results.length >= maxResults) {
132+
break;
133+
}
134+
135+
nextPageToken = response.nextPageToken;
136+
} while (nextPageToken && !response.isLast);
137+
138+
return results;
139+
},
140+
},
93141
async run({ $ }) {
94142
try {
95-
const response = await this.jira.searchIssues({
143+
const issues = await this.paginate({
96144
$,
97145
cloudId: this.cloudId,
98146
params: {
99147
jql: this.jql,
100-
maxResults: this.maxResults,
148+
maxResults: this.getMaxResultsPerPage(),
101149
fields: this.fields,
102150
expand: this.expand
103151
? this.expand.join(",")
@@ -106,9 +154,12 @@ export default {
106154
fieldsByKeys: this.fieldsByKeys,
107155
failFast: this.failFast,
108156
},
157+
maxResults: this.maxResults,
109158
});
110-
$.export("$summary", `Successfully retrieved ${response.issues.length} issues`);
111-
return response;
159+
$.export("$summary", `Successfully retrieved ${issues.length} issues`);
160+
return {
161+
issues,
162+
};
112163
} catch (error) {
113164
throw new Error(`Failed to search issues: ${error.message}`);
114165
}

components/jira/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/jira",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "Pipedream Jira Components",
55
"main": "jira.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)