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

Mutates the specified pull request.

This can be used to change the pull request's branches or description.

Only open pull requests can be mutated.

Created by hugo697 377 days ago Viewed 8955 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 377 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Update a pull request
7
 * Mutates the specified pull request.
8

9
This can be used to change the pull request's branches or description.
10

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

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