0
Create a pull request
One script reply has been approved by the moderators Verified

Creates a new pull request where the destination repository is this repository and the author is the authenticated user.

Created by hugo697 126 days ago Viewed 20 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 126 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create a pull request
7
 * Creates a new pull request where the destination repository is
8
this repository and the author is the authenticated user.
9
 */
10
export async function main(
11
  auth: Bitbucket,
12
  repo_slug: string,
13
  workspace: string,
14
  body: { type: string; [k: string]: unknown } & {
15
    links?: {
16
      self?: { href?: string; name?: string };
17
      html?: { href?: string; name?: string };
18
      commits?: { href?: string; name?: string };
19
      approve?: { href?: string; name?: string };
20
      diff?: { href?: string; name?: string };
21
      diffstat?: { href?: string; name?: string };
22
      comments?: { href?: string; name?: string };
23
      activity?: { href?: string; name?: string };
24
      merge?: { href?: string; name?: string };
25
      decline?: { href?: string; name?: string };
26
    };
27
    id?: number;
28
    title?: string;
29
    rendered?: {
30
      title?: {
31
        raw?: string;
32
        markup?: "markdown" | "creole" | "plaintext";
33
        html?: string;
34
      };
35
      description?: {
36
        raw?: string;
37
        markup?: "markdown" | "creole" | "plaintext";
38
        html?: string;
39
      };
40
      reason?: {
41
        raw?: string;
42
        markup?: "markdown" | "creole" | "plaintext";
43
        html?: string;
44
      };
45
    };
46
    summary?: {
47
      raw?: string;
48
      markup?: "markdown" | "creole" | "plaintext";
49
      html?: string;
50
    };
51
    state?: "OPEN" | "MERGED" | "DECLINED" | "SUPERSEDED";
52
    author?: { type: string; [k: string]: unknown } & {
53
      links?: {
54
        avatar?: { href?: string; name?: string };
55
        [k: string]: unknown;
56
      };
57
      created_on?: string;
58
      display_name?: string;
59
      username?: string;
60
      uuid?: string;
61
      [k: string]: unknown;
62
    };
63
    source?: {
64
      repository?: { type: string; [k: string]: unknown } & {
65
        links?: {
66
          self?: { href?: string; name?: string };
67
          html?: { href?: string; name?: string };
68
          avatar?: { href?: string; name?: string };
69
          pullrequests?: { href?: string; name?: string };
70
          commits?: { href?: string; name?: string };
71
          forks?: { href?: string; name?: string };
72
          watchers?: { href?: string; name?: string };
73
          downloads?: { href?: string; name?: string };
74
          clone?: { href?: string; name?: string }[];
75
          hooks?: { href?: string; name?: string };
76
        };
77
        uuid?: string;
78
        full_name?: string;
79
        is_private?: boolean;
80
        parent?: unknown;
81
        scm?: "git";
82
        owner?: { type: string; [k: string]: unknown } & {
83
          links?: {
84
            avatar?: { href?: string; name?: string };
85
            [k: string]: unknown;
86
          };
87
          created_on?: string;
88
          display_name?: string;
89
          username?: string;
90
          uuid?: string;
91
          [k: string]: unknown;
92
        };
93
        name?: string;
94
        description?: string;
95
        created_on?: string;
96
        updated_on?: string;
97
        size?: number;
98
        language?: string;
99
        has_issues?: boolean;
100
        has_wiki?: boolean;
101
        fork_policy?: "allow_forks" | "no_public_forks" | "no_forks";
102
        project?: { type: string; [k: string]: unknown } & {
103
          links?: {
104
            html?: { href?: string; name?: string };
105
            avatar?: { href?: string; name?: string };
106
          };
107
          uuid?: string;
108
          key?: string;
109
          owner?: ({ type: string; [k: string]: unknown } & {
110
            links?: {
111
              avatar?: { href?: string; name?: string };
112
              [k: string]: unknown;
113
            };
114
            created_on?: string;
115
            display_name?: string;
116
            username?: string;
117
            uuid?: string;
118
            [k: string]: unknown;
119
          }) & {
120
            links?: {
121
              avatar?: { href?: string; name?: string };
122
              [k: string]: unknown;
123
            } & {
124
              self?: { href?: string; name?: string };
125
              html?: { href?: string; name?: string };
126
              members?: { href?: string; name?: string };
127
              projects?: { href?: string; name?: string };
128
              repositories?: { href?: string; name?: string };
129
              [k: string]: unknown;
130
            };
131
            [k: string]: unknown;
132
          };
133
          name?: string;
134
          description?: string;
135
          is_private?: boolean;
136
          created_on?: string;
137
          updated_on?: string;
138
          has_publicly_visible_repos?: boolean;
139
          [k: string]: unknown;
140
        };
141
        mainbranch?: {
142
          type: string;
143
          links?: {
144
            self?: { href?: string; name?: string };
145
            commits?: { href?: string; name?: string };
146
            html?: { href?: string; name?: string };
147
          };
148
          name?: string;
149
          target?: ({ type: string; [k: string]: unknown } & {
150
            hash?: string;
151
            date?: string;
152
            author?: { type: string; [k: string]: unknown } & {
153
              raw?: string;
154
              user?: { type: string; [k: string]: unknown } & {
155
                links?: {
156
                  avatar?: { href?: string; name?: string };
157
                  [k: string]: unknown;
158
                };
159
                created_on?: string;
160
                display_name?: string;
161
                username?: string;
162
                uuid?: string;
163
                [k: string]: unknown;
164
              };
165
              [k: string]: unknown;
166
            };
167
            message?: string;
168
            summary?: {
169
              raw?: string;
170
              markup?: "markdown" | "creole" | "plaintext";
171
              html?: string;
172
            };
173
            parents?: unknown[];
174
            [k: string]: unknown;
175
          }) & {
176
            repository?: unknown;
177
            participants?: ({ type: string; [k: string]: unknown } & {
178
              user?: { type: string; [k: string]: unknown } & {
179
                links?: {
180
                  avatar?: { href?: string; name?: string };
181
                  [k: string]: unknown;
182
                };
183
                created_on?: string;
184
                display_name?: string;
185
                username?: string;
186
                uuid?: string;
187
                [k: string]: unknown;
188
              };
189
              role?: "PARTICIPANT" | "REVIEWER";
190
              approved?: boolean;
191
              state?: "approved" | "changes_requested" | null;
192
              participated_on?: string;
193
              [k: string]: unknown;
194
            })[];
195
            [k: string]: unknown;
196
          };
197
          [k: string]: unknown;
198
        } & {
199
          merge_strategies?: ("merge_commit" | "squash" | "fast_forward")[];
200
          default_merge_strategy?: string;
201
          [k: string]: unknown;
202
        };
203
        [k: string]: unknown;
204
      };
205
      branch?: {
206
        name?: string;
207
        merge_strategies?: ("merge_commit" | "squash" | "fast_forward")[];
208
        default_merge_strategy?: string;
209
      };
210
      commit?: { hash?: string };
211
    };
212
    destination?: {
213
      repository?: { type: string; [k: string]: unknown } & {
214
        links?: {
215
          self?: { href?: string; name?: string };
216
          html?: { href?: string; name?: string };
217
          avatar?: { href?: string; name?: string };
218
          pullrequests?: { href?: string; name?: string };
219
          commits?: { href?: string; name?: string };
220
          forks?: { href?: string; name?: string };
221
          watchers?: { href?: string; name?: string };
222
          downloads?: { href?: string; name?: string };
223
          clone?: { href?: string; name?: string }[];
224
          hooks?: { href?: string; name?: string };
225
        };
226
        uuid?: string;
227
        full_name?: string;
228
        is_private?: boolean;
229
        parent?: unknown;
230
        scm?: "git";
231
        owner?: { type: string; [k: string]: unknown } & {
232
          links?: {
233
            avatar?: { href?: string; name?: string };
234
            [k: string]: unknown;
235
          };
236
          created_on?: string;
237
          display_name?: string;
238
          username?: string;
239
          uuid?: string;
240
          [k: string]: unknown;
241
        };
242
        name?: string;
243
        description?: string;
244
        created_on?: string;
245
        updated_on?: string;
246
        size?: number;
247
        language?: string;
248
        has_issues?: boolean;
249
        has_wiki?: boolean;
250
        fork_policy?: "allow_forks" | "no_public_forks" | "no_forks";
251
        project?: { type: string; [k: string]: unknown } & {
252
          links?: {
253
            html?: { href?: string; name?: string };
254
            avatar?: { href?: string; name?: string };
255
          };
256
          uuid?: string;
257
          key?: string;
258
          owner?: ({ type: string; [k: string]: unknown } & {
259
            links?: {
260
              avatar?: { href?: string; name?: string };
261
              [k: string]: unknown;
262
            };
263
            created_on?: string;
264
            display_name?: string;
265
            username?: string;
266
            uuid?: string;
267
            [k: string]: unknown;
268
          }) & {
269
            links?: {
270
              avatar?: { href?: string; name?: string };
271
              [k: string]: unknown;
272
            } & {
273
              self?: { href?: string; name?: string };
274
              html?: { href?: string; name?: string };
275
              members?: { href?: string; name?: string };
276
              projects?: { href?: string; name?: string };
277
              repositories?: { href?: string; name?: string };
278
              [k: string]: unknown;
279
            };
280
            [k: string]: unknown;
281
          };
282
          name?: string;
283
          description?: string;
284
          is_private?: boolean;
285
          created_on?: string;
286
          updated_on?: string;
287
          has_publicly_visible_repos?: boolean;
288
          [k: string]: unknown;
289
        };
290
        mainbranch?: {
291
          type: string;
292
          links?: {
293
            self?: { href?: string; name?: string };
294
            commits?: { href?: string; name?: string };
295
            html?: { href?: string; name?: string };
296
          };
297
          name?: string;
298
          target?: ({ type: string; [k: string]: unknown } & {
299
            hash?: string;
300
            date?: string;
301
            author?: { type: string; [k: string]: unknown } & {
302
              raw?: string;
303
              user?: { type: string; [k: string]: unknown } & {
304
                links?: {
305
                  avatar?: { href?: string; name?: string };
306
                  [k: string]: unknown;
307
                };
308
                created_on?: string;
309
                display_name?: string;
310
                username?: string;
311
                uuid?: string;
312
                [k: string]: unknown;
313
              };
314
              [k: string]: unknown;
315
            };
316
            message?: string;
317
            summary?: {
318
              raw?: string;
319
              markup?: "markdown" | "creole" | "plaintext";
320
              html?: string;
321
            };
322
            parents?: unknown[];
323
            [k: string]: unknown;
324
          }) & {
325
            repository?: unknown;
326
            participants?: ({ type: string; [k: string]: unknown } & {
327
              user?: { type: string; [k: string]: unknown } & {
328
                links?: {
329
                  avatar?: { href?: string; name?: string };
330
                  [k: string]: unknown;
331
                };
332
                created_on?: string;
333
                display_name?: string;
334
                username?: string;
335
                uuid?: string;
336
                [k: string]: unknown;
337
              };
338
              role?: "PARTICIPANT" | "REVIEWER";
339
              approved?: boolean;
340
              state?: "approved" | "changes_requested" | null;
341
              participated_on?: string;
342
              [k: string]: unknown;
343
            })[];
344
            [k: string]: unknown;
345
          };
346
          [k: string]: unknown;
347
        } & {
348
          merge_strategies?: ("merge_commit" | "squash" | "fast_forward")[];
349
          default_merge_strategy?: string;
350
          [k: string]: unknown;
351
        };
352
        [k: string]: unknown;
353
      };
354
      branch?: {
355
        name?: string;
356
        merge_strategies?: ("merge_commit" | "squash" | "fast_forward")[];
357
        default_merge_strategy?: string;
358
      };
359
      commit?: { hash?: string };
360
    };
361
    merge_commit?: { hash?: string };
362
    comment_count?: number;
363
    task_count?: number;
364
    close_source_branch?: boolean;
365
    closed_by?: { type: string; [k: string]: unknown } & {
366
      links?: {
367
        avatar?: { href?: string; name?: string };
368
        [k: string]: unknown;
369
      };
370
      created_on?: string;
371
      display_name?: string;
372
      username?: string;
373
      uuid?: string;
374
      [k: string]: unknown;
375
    };
376
    reason?: string;
377
    created_on?: string;
378
    updated_on?: string;
379
    reviewers?: ({ type: string; [k: string]: unknown } & {
380
      links?: {
381
        avatar?: { href?: string; name?: string };
382
        [k: string]: unknown;
383
      };
384
      created_on?: string;
385
      display_name?: string;
386
      username?: string;
387
      uuid?: string;
388
      [k: string]: unknown;
389
    })[];
390
    participants?: ({ type: string; [k: string]: unknown } & {
391
      user?: { type: string; [k: string]: unknown } & {
392
        links?: {
393
          avatar?: { href?: string; name?: string };
394
          [k: string]: unknown;
395
        };
396
        created_on?: string;
397
        display_name?: string;
398
        username?: string;
399
        uuid?: string;
400
        [k: string]: unknown;
401
      };
402
      role?: "PARTICIPANT" | "REVIEWER";
403
      approved?: boolean;
404
      state?: "approved" | "changes_requested" | null;
405
      participated_on?: string;
406
      [k: string]: unknown;
407
    })[];
408
    [k: string]: unknown;
409
  }
410
) {
411
  const url = new URL(
412
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pullrequests`
413
  );
414

415
  const response = await fetch(url, {
416
    method: "POST",
417
    headers: {
418
      "Content-Type": "application/json",
419
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
420
    },
421
    body: JSON.stringify(body),
422
  });
423
  if (!response.ok) {
424
    const text = await response.text();
425
    throw new Error(`${response.status} ${text}`);
426
  }
427
  return await response.json();
428
}
429