Update an explicit group permission for a repository

Updates the group permission, or grants a new permission if one does not already exist. Only users with admin permission for the repository may access this resource. The only authentication method supported for this endpoint is via app passwords. Permissions can be: * `admin` * `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 repository
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 repository may access this resource.
10

11
The only authentication method supported 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
  group_slug: string,
22
  repo_slug: 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/groups/${group_slug}`
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