0
Update an explicit user permission for a repository
One script reply has been approved by the moderators Verified

Updates the explicit user permission for a given user and repository. The selected user must be a member of the workspace, and cannot be the workspace owner. Only users with admin permission for the repository may access this resource.

The only authentication method for this endpoint is via app passwords.

Permissions can be:

  • admin
  • write
  • read
Created by hugo697 645 days ago Viewed 22518 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 645 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Update an explicit user permission for a repository
7
 * Updates the explicit user permission for a given user and repository. The selected user must be a member of
8
the workspace, and cannot be the workspace owner.
9
Only users with admin permission for the repository may access this resource.
10

11
The only authentication method for this endpoint is via app passwords.
12

13
Permissions can be:
14

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

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