0

List pull requests for a user

by
Published Oct 24, 2023

Returns all pull requests authored by the specified user. 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

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * List pull requests for a user
7
 * Returns all pull requests authored by the specified user.
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
  selected_user: string,
20
  state: "OPEN" | "MERGED" | "DECLINED" | "SUPERSEDED" | undefined
21
) {
22
  const url = new URL(
23
    `https://api.bitbucket.org/2.0/pullrequests/${selected_user}`
24
  );
25
  for (const [k, v] of [["state", state]]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43