Update an explicit group permission for a project

Updates the group permission, or grants a new permission if one does not already exist. Only users with admin permission for the project may access this resource. Due to security concerns, the JWT and OAuth authentication methods are unsupported. This is to ensure integrations and add-ons are not allowed to change permissions. Permissions can be: * `admin` * `create-repo` * `write` * `read`

Script bitbucket Verified

by hugo697 ยท 10/24/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 375 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Update an explicit group permission for a project
7
 * Updates the group permission, or grants a new permission if one does not already exist.
8

9
Only users with admin permission for the project may access this resource.
10

11
Due to security concerns, the JWT and OAuth authentication methods are unsupported.
12
This is to ensure integrations and add-ons are not allowed to change permissions.
13

14
Permissions can be:
15

16
* `admin`
17
* `create-repo`
18
* `write`
19
* `read`
20
 */
21
export async function main(
22
  auth: Bitbucket,
23
  group_slug: string,
24
  project_key: string,
25
  workspace: string,
26
  body: { permission: "read" | "write" | "create-repo" | "admin" }
27
) {
28
  const url = new URL(
29
    `https://api.bitbucket.org/2.0/workspaces/${workspace}/projects/${project_key}/permissions-config/groups/${group_slug}`
30
  );
31

32
  const response = await fetch(url, {
33
    method: "PUT",
34
    headers: {
35
      "Content-Type": "application/json",
36
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
37
    },
38
    body: JSON.stringify(body),
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46