Search for code in a team's repositories
One script reply has been approved by the moderators Verified

Search for code in the repositories of the specified team.

Created by hugo697 783 days ago
Submitted by hugo697 Typescript (fetch-only)
Verified 222 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Search for code in a team's repositories
7
 * Search for code in the repositories of the specified team.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  username: string,
12
  search_query: string | undefined,
13
  page: string | undefined,
14
  pagelen: string | undefined
15
) {
16
  const url = new URL(
17
    `https://api.bitbucket.org/2.0/teams/${username}/search/code`
18
  );
19
  for (const [k, v] of [
20
    ["search_query", search_query],
21
    ["page", page],
22
    ["pagelen", pagelen],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41