Update an explicit user permission for a project

Updates the explicit user permission for a given user and project. The selected user must be a member of the workspace, and cannot be the workspace owner. 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 user permission for a project
7
 * Updates the explicit user permission for a given user and project. The selected
8
user must be a member of the workspace, and cannot be the workspace owner.
9

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

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

15
Permissions can be:
16

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

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