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

Creates a new pull request comment.

Returns the newly created pull request comment.

Created by hugo697 456 days ago Viewed 12013 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 456 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create a comment on a pull request
7
 * Creates a new pull request comment.
8

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

466
  const response = await fetch(url, {
467
    method: "POST",
468
    headers: {
469
      "Content-Type": "application/json",
470
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
471
    },
472
    body: JSON.stringify(body),
473
  });
474
  if (!response.ok) {
475
    const text = await response.text();
476
    throw new Error(`${response.status} ${text}`);
477
  }
478
  return await response.json();
479
}
480