Skip to content

Commit 3b42f55

Browse files
committed
Adding pagination to search-issues-with-jql
1 parent 6e80efa commit 3b42f55

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

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

Lines changed: 58 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,15 @@ 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 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,
3029
},
3130
fields: {
3231
type: "string",
@@ -90,14 +89,62 @@ export default {
9089
optional: true,
9190
},
9291
},
92+
methods: {
93+
getMaxResultsPerPage() {
94+
return Math.min(this.maxResults, 500);
95+
},
96+
async paginate({
97+
params, maxResults, ...otherArgs
98+
}) {
99+
const results = [];
100+
let nextPageToken;
101+
let response;
102+
103+
do {
104+
response = await this.jira.searchIssues({
105+
...otherArgs,
106+
params: {
107+
...params,
108+
...(nextPageToken && {
109+
nextPageToken,
110+
}),
111+
},
112+
});
113+
114+
const pageResults = response.issues;
115+
const pageLength = pageResults?.length;
116+
if (!pageLength) {
117+
break;
118+
}
119+
120+
// If maxResults is specified, only add what we need
121+
if (maxResults && results.length + pageLength > maxResults) {
122+
const remainingSlots = maxResults - results.length;
123+
results.push(...pageResults.slice(0, remainingSlots));
124+
break;
125+
} else {
126+
results.push(...pageResults);
127+
}
128+
129+
// Also break if we've reached maxResults exactly
130+
if (maxResults && results.length >= maxResults) {
131+
break;
132+
}
133+
134+
nextPageToken = response.nextPageToken;
135+
} while (nextPageToken && !response.isLast);
136+
137+
return results;
138+
},
139+
},
93140
async run({ $ }) {
94141
try {
95-
const response = await this.jira.searchIssues({
142+
const issues = await this.paginate({
96143
$,
97144
cloudId: this.cloudId,
98145
params: {
99146
jql: this.jql,
100-
maxResults: this.maxResults,
147+
maxResults: this.getMaxResultsPerPage(),
101148
fields: this.fields,
102149
expand: this.expand
103150
? this.expand.join(",")
@@ -106,9 +153,12 @@ export default {
106153
fieldsByKeys: this.fieldsByKeys,
107154
failFast: this.failFast,
108155
},
156+
maxResults: this.maxResults,
109157
});
110-
$.export("$summary", `Successfully retrieved ${response.issues.length} issues`);
111-
return response;
158+
$.export("$summary", `Successfully retrieved ${issues.length} issues`);
159+
return {
160+
issues,
161+
};
112162
} catch (error) {
113163
throw new Error(`Failed to search issues: ${error.message}`);
114164
}

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)