List pull requests

Returns all pull requests on the specified repository. By default only open pull requests are returned. This can be controlled using the `state` query parameter. To retrieve pull requests that are in one of multiple states, repeat the `state` parameter for each individual state. This endpoint also supports filtering and sorting of the results. See filtering and sorting for more details.

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
 * List pull requests
7
 * Returns all pull requests on the specified repository.
8

9
By default only open pull requests are returned. This can be controlled
10
using the `state` query parameter. To retrieve pull requests that are
11
in one of multiple states, repeat the `state` parameter for each
12
individual state.
13

14
This endpoint also supports filtering and sorting of the results. See
15
filtering and sorting for more details.
16
 */
17
export async function main(
18
  auth: Bitbucket,
19
  repo_slug: string,
20
  workspace: string,
21
  state: "OPEN" | "MERGED" | "DECLINED" | "SUPERSEDED" | undefined
22
) {
23
  const url = new URL(
24
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pullrequests`
25
  );
26
  for (const [k, v] of [["state", state]]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
35
    },
36
    body: undefined,
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