Create a task on a pull request

Creates a new pull request task. Returns the newly created pull request task. Tasks can optionally be created in relation to a comment specified by the comment's ID which will cause the task to appear below the comment on a pull request when viewed in Bitbucket.

Script bitbucket Verified

by hugo697 ยท 3/6/2024

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 375 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create a task on a pull request
7
 * Creates a new pull request task.
8

9
Returns the newly created pull request task.
10

11
Tasks can optionally be created in relation to a comment specified by the comment's ID which
12
will cause the task to appear below the comment on a pull request when viewed in Bitbucket.
13
 */
14
export async function main(
15
  auth: Bitbucket,
16
  pull_request_id: string,
17
  repo_slug: string,
18
  workspace: string,
19
  body: {
20
    content: { raw: string };
21
    comment?: { type: string; [k: string]: unknown } & {
22
      id?: number;
23
      created_on?: string;
24
      updated_on?: string;
25
      content?: {
26
        raw?: string;
27
        markup?: "markdown" | "creole" | "plaintext";
28
        html?: string;
29
      };
30
      user?: { type: string; [k: string]: unknown } & {
31
        links?: {
32
          avatar?: { href?: string; name?: string };
33
          [k: string]: unknown;
34
        };
35
        created_on?: string;
36
        display_name?: string;
37
        username?: string;
38
        uuid?: string;
39
        [k: string]: unknown;
40
      };
41
      deleted?: boolean;
42
      parent?: unknown;
43
      inline?: { from?: number; to?: number; path: string };
44
      links?: {
45
        self?: { href?: string; name?: string };
46
        html?: { href?: string; name?: string };
47
        code?: { href?: string; name?: string };
48
      };
49
      [k: string]: unknown;
50
    };
51
    pending?: boolean;
52
  }
53
) {
54
  const url = new URL(
55
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pullrequests/${pull_request_id}/tasks`
56
  );
57

58
  const response = await fetch(url, {
59
    method: "POST",
60
    headers: {
61
      "Content-Type": "application/json",
62
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
63
    },
64
    body: JSON.stringify(body),
65
  });
66
  if (!response.ok) {
67
    const text = await response.text();
68
    throw new Error(`${response.status} ${text}`);
69
  }
70
  return await response.json();
71
}
72