0

List Task Stories

by
Published Oct 31, 2023

List a task's stories: its comments plus the activity log (assignments, completions, field changes). Set `comments_only` to keep only comments; the filter applies to the returned page. Pass `next_page.offset` from a response as `offset` to get the next page.

Script asana Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * List Task Stories
5
 * List a task's stories: its comments plus the activity log (assignments, completions, field changes). Set `comments_only` to keep only comments; the filter applies to the returned page. Pass `next_page.offset` from a response as `offset` to get the next page.
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  task_gid: string,
10
  comments_only: boolean | undefined,
11
  opt_fields: string | undefined,
12
  limit: number | undefined,
13
  offset: string | undefined
14
) {
15
  const url = new URL(`https://app.asana.com/api/1.0/tasks/${task_gid}/stories`)
16
  url.searchParams.append("limit", String(limit ?? 100))
17
  if (offset != null && offset !== "") {
18
    url.searchParams.append("offset", offset)
19
  }
20
  if (opt_fields != null && opt_fields !== "") {
21
    // the comments_only filter reads resource_subtype, so keep it in the response
22
    url.searchParams.append("opt_fields", `${opt_fields},resource_subtype`)
23
  }
24

25
  const response = await fetch(url, {
26
    headers: {
27
      Authorization: `Bearer ${auth.token}`,
28
      Accept: "application/json",
29
    },
30
  })
31

32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  const result = await response.json()
37
  if (comments_only) {
38
    result.data = result.data.filter(
39
      (story: { resource_subtype: string }) =>
40
        story.resource_subtype === "comment_added"
41
    )
42
  }
43
  return result
44
}
45

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get stories from a task
    6
     * Returns the compact records for all stories on the task.
    7
     */
    8
    export async function main(
    9
      auth: Asana,
    10
      task_gid: string,
    11
      opt_pretty: string | undefined,
    12
      opt_fields: string | undefined,
    13
      limit: string | undefined,
    14
      offset: string | undefined
    15
    ) {
    16
      const url = new URL(
    17
        `https://app.asana.com/api/1.0/tasks/${task_gid}/stories`
    18
      );
    19
      for (const [k, v] of [
    20
        ["opt_pretty", opt_pretty],
    21
        ["opt_fields", opt_fields],
    22
        ["limit", limit],
    23
        ["offset", offset],
    24
      ]) {
    25
        if (v !== undefined && v !== "") {
    26
          url.searchParams.append(k, v);
    27
        }
    28
      }
    29
      const response = await fetch(url, {
    30
        method: "GET",
    31
        headers: {
    32
          Authorization: "Bearer " + auth.token,
    33
        },
    34
        body: undefined,
    35
      });
    36
      if (!response.ok) {
    37
        const text = await response.text();
    38
        throw new Error(`${response.status} ${text}`);
    39
      }
    40
      return await response.json();
    41
    }
    42