0
Create a project in a workspace
One script reply has been approved by the moderators Verified

Creates a new project.

Note that the avatar has to be embedded as either a data-url or a URL to an external image as shown in the examples below:

$ body=$(cat 
Created by hugo697 177 days ago Viewed 3961 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 177 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create a project in a workspace
7
 * Creates a new project.
8

9
Note that the avatar has to be embedded as either a data-url
10
or a URL to an external image as shown in the examples below:
11

12
```
13
$ body=$(cat 
14
 */
15
export async function main(
16
  auth: Bitbucket,
17
  workspace: string,
18
  body: { type: string; [k: string]: unknown } & {
19
    links?: {
20
      html?: { href?: string; name?: string };
21
      avatar?: { href?: string; name?: string };
22
    };
23
    uuid?: string;
24
    key?: string;
25
    owner?: ({ type: string; [k: string]: unknown } & {
26
      links?: {
27
        avatar?: { href?: string; name?: string };
28
        [k: string]: unknown;
29
      };
30
      created_on?: string;
31
      display_name?: string;
32
      username?: string;
33
      uuid?: string;
34
      [k: string]: unknown;
35
    }) & {
36
      links?: {
37
        avatar?: { href?: string; name?: string };
38
        [k: string]: unknown;
39
      } & {
40
        self?: { href?: string; name?: string };
41
        html?: { href?: string; name?: string };
42
        members?: { href?: string; name?: string };
43
        projects?: { href?: string; name?: string };
44
        repositories?: { href?: string; name?: string };
45
        [k: string]: unknown;
46
      };
47
      [k: string]: unknown;
48
    };
49
    name?: string;
50
    description?: string;
51
    is_private?: boolean;
52
    created_on?: string;
53
    updated_on?: string;
54
    has_publicly_visible_repos?: boolean;
55
    [k: string]: unknown;
56
  }
57
) {
58
  const url = new URL(
59
    `https://api.bitbucket.org/2.0/workspaces/${workspace}/projects`
60
  );
61

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