|
| 1 | +import common from "../common/common.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "asana-list-task-stories", |
| 5 | + name: "List Task Stories", |
| 6 | + description: "List stories (including comments) for a task. [See the documentation](https://developers.asana.com/reference/getstoriesfortask)", |
| 7 | + version: "0.0.1", |
| 8 | + type: "action", |
| 9 | + props: { |
| 10 | + ...common.props, |
| 11 | + taskId: { |
| 12 | + label: "Task GID", |
| 13 | + description: "The ID of the task to retrieve stories for", |
| 14 | + type: "string", |
| 15 | + propDefinition: [ |
| 16 | + common.props.asana, |
| 17 | + "tasks", |
| 18 | + (c) => ({ |
| 19 | + project: c.project, |
| 20 | + }), |
| 21 | + ], |
| 22 | + }, |
| 23 | + commentsOnly: { |
| 24 | + type: "boolean", |
| 25 | + label: "Comments Only", |
| 26 | + description: "Only return comments", |
| 27 | + optional: true, |
| 28 | + }, |
| 29 | + optFields: { |
| 30 | + type: "string[]", |
| 31 | + label: "Opt Fields", |
| 32 | + description: "This endpoint returns a resource which excludes some properties by default. To include those optional properties, set this query parameter to a comma-separated list of the properties you wish to include. See the [documentation](https://developers.asana.com/reference/stories) for available fields.", |
| 33 | + optional: true, |
| 34 | + }, |
| 35 | + maxResults: { |
| 36 | + type: "integer", |
| 37 | + label: "Max Results", |
| 38 | + description: "The maximum number of results to return", |
| 39 | + default: 100, |
| 40 | + optional: true, |
| 41 | + }, |
| 42 | + }, |
| 43 | + methods: { |
| 44 | + async getStoriesForTask({ |
| 45 | + taskId, ...opts |
| 46 | + }) { |
| 47 | + return this.asana._makeRequest({ |
| 48 | + path: `tasks/${taskId}/stories`, |
| 49 | + ...opts, |
| 50 | + }); |
| 51 | + }, |
| 52 | + }, |
| 53 | + async run({ $ }) { |
| 54 | + let hasMore, count = 0; |
| 55 | + |
| 56 | + const params = { |
| 57 | + limit: this.maxResults, |
| 58 | + opt_fields: this.optFields |
| 59 | + ? this.optFields?.join(",") |
| 60 | + : undefined, |
| 61 | + }; |
| 62 | + |
| 63 | + const results = []; |
| 64 | + |
| 65 | + do { |
| 66 | + const { |
| 67 | + data, next_page: next, |
| 68 | + } = await this.getStoriesForTask({ |
| 69 | + $, |
| 70 | + taskId: this.taskId, |
| 71 | + params, |
| 72 | + }); |
| 73 | + |
| 74 | + hasMore = next; |
| 75 | + params.offset = next?.offset; |
| 76 | + |
| 77 | + if (data.length === 0) { |
| 78 | + break; |
| 79 | + } |
| 80 | + |
| 81 | + for (const story of data) { |
| 82 | + if (this.commentsOnly && story.type !== "comment") { |
| 83 | + continue; |
| 84 | + } |
| 85 | + results.push(story); |
| 86 | + if (++count >= this.maxResults) { |
| 87 | + hasMore = false; |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + } while (hasMore); |
| 92 | + |
| 93 | + $.export("$summary", `Found ${results.length} stories`); |
| 94 | + return results; |
| 95 | + }, |
| 96 | +}; |
0 commit comments