0
Fork a repository
One script reply has been approved by the moderators Verified

Creates a new fork of the specified repository.

Created by hugo697 394 days ago Viewed 9004 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 394 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Fork a repository
7
 * Creates a new fork of the specified repository.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  repo_slug: string,
12
  workspace: string,
13
  body: { type: string; [k: string]: unknown } & {
14
    links?: {
15
      self?: { href?: string; name?: string };
16
      html?: { href?: string; name?: string };
17
      avatar?: { href?: string; name?: string };
18
      pullrequests?: { href?: string; name?: string };
19
      commits?: { href?: string; name?: string };
20
      forks?: { href?: string; name?: string };
21
      watchers?: { href?: string; name?: string };
22
      downloads?: { href?: string; name?: string };
23
      clone?: { href?: string; name?: string }[];
24
      hooks?: { href?: string; name?: string };
25
    };
26
    uuid?: string;
27
    full_name?: string;
28
    is_private?: boolean;
29
    parent?: unknown;
30
    scm?: "git";
31
    owner?: { type: string; [k: string]: unknown } & {
32
      links?: {
33
        avatar?: { href?: string; name?: string };
34
        [k: string]: unknown;
35
      };
36
      created_on?: string;
37
      display_name?: string;
38
      username?: string;
39
      uuid?: string;
40
      [k: string]: unknown;
41
    };
42
    name?: string;
43
    description?: string;
44
    created_on?: string;
45
    updated_on?: string;
46
    size?: number;
47
    language?: string;
48
    has_issues?: boolean;
49
    has_wiki?: boolean;
50
    fork_policy?: "allow_forks" | "no_public_forks" | "no_forks";
51
    project?: { type: string; [k: string]: unknown } & {
52
      links?: {
53
        html?: { href?: string; name?: string };
54
        avatar?: { href?: string; name?: string };
55
      };
56
      uuid?: string;
57
      key?: string;
58
      owner?: ({ type: string; [k: string]: unknown } & {
59
        links?: {
60
          avatar?: { href?: string; name?: string };
61
          [k: string]: unknown;
62
        };
63
        created_on?: string;
64
        display_name?: string;
65
        username?: string;
66
        uuid?: string;
67
        [k: string]: unknown;
68
      }) & {
69
        links?: {
70
          avatar?: { href?: string; name?: string };
71
          [k: string]: unknown;
72
        } & {
73
          self?: { href?: string; name?: string };
74
          html?: { href?: string; name?: string };
75
          members?: { href?: string; name?: string };
76
          projects?: { href?: string; name?: string };
77
          repositories?: { href?: string; name?: string };
78
          [k: string]: unknown;
79
        };
80
        [k: string]: unknown;
81
      };
82
      name?: string;
83
      description?: string;
84
      is_private?: boolean;
85
      created_on?: string;
86
      updated_on?: string;
87
      has_publicly_visible_repos?: boolean;
88
      [k: string]: unknown;
89
    };
90
    mainbranch?: {
91
      type: string;
92
      links?: {
93
        self?: { href?: string; name?: string };
94
        commits?: { href?: string; name?: string };
95
        html?: { href?: string; name?: string };
96
      };
97
      name?: string;
98
      target?: ({ type: string; [k: string]: unknown } & {
99
        hash?: string;
100
        date?: string;
101
        author?: { type: string; [k: string]: unknown } & {
102
          raw?: string;
103
          user?: { type: string; [k: string]: unknown } & {
104
            links?: {
105
              avatar?: { href?: string; name?: string };
106
              [k: string]: unknown;
107
            };
108
            created_on?: string;
109
            display_name?: string;
110
            username?: string;
111
            uuid?: string;
112
            [k: string]: unknown;
113
          };
114
          [k: string]: unknown;
115
        };
116
        message?: string;
117
        summary?: {
118
          raw?: string;
119
          markup?: "markdown" | "creole" | "plaintext";
120
          html?: string;
121
        };
122
        parents?: unknown[];
123
        [k: string]: unknown;
124
      }) & {
125
        repository?: unknown;
126
        participants?: ({ type: string; [k: string]: unknown } & {
127
          user?: { type: string; [k: string]: unknown } & {
128
            links?: {
129
              avatar?: { href?: string; name?: string };
130
              [k: string]: unknown;
131
            };
132
            created_on?: string;
133
            display_name?: string;
134
            username?: string;
135
            uuid?: string;
136
            [k: string]: unknown;
137
          };
138
          role?: "PARTICIPANT" | "REVIEWER";
139
          approved?: boolean;
140
          state?: "approved" | "changes_requested" | null;
141
          participated_on?: string;
142
          [k: string]: unknown;
143
        })[];
144
        [k: string]: unknown;
145
      };
146
      [k: string]: unknown;
147
    } & {
148
      merge_strategies?: ("merge_commit" | "squash" | "fast_forward")[];
149
      default_merge_strategy?: string;
150
      [k: string]: unknown;
151
    };
152
    [k: string]: unknown;
153
  }
154
) {
155
  const url = new URL(
156
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/forks`
157
  );
158

159
  const response = await fetch(url, {
160
    method: "POST",
161
    headers: {
162
      "Content-Type": "application/json",
163
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
164
    },
165
    body: JSON.stringify(body),
166
  });
167
  if (!response.ok) {
168
    const text = await response.text();
169
    throw new Error(`${response.status} ${text}`);
170
  }
171
  return await response.json();
172
}
173