-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmerge_requests.c
1261 lines (1020 loc) · 32.6 KB
/
merge_requests.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2021-2025 Nico Sonack <nsonack@herrhotzenplotz.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <gcli/curl.h>
#include <gcli/gitlab/api.h>
#include <gcli/gitlab/comments.h>
#include <gcli/gitlab/config.h>
#include <gcli/gitlab/merge_requests.h>
#include <gcli/gitlab/repos.h>
#include <gcli/json_gen.h>
#include <gcli/json_gen.h>
#include <gcli/json_util.h>
#include <templates/gitlab/merge_requests.h>
#include <pdjson/pdjson.h>
#include <time.h> /* for nanosleep */
#include <openssl/evp.h>
/* Workaround because gitlab doesn't give us an explicit field for
* this. */
static void
gitlab_mrs_fixup(struct gcli_pull_list *const list)
{
for (size_t i = 0; i < list->pulls_size; ++i) {
list->pulls[i].merged = !strcmp(list->pulls[i].state, "merged");
}
}
int
gitlab_mr_make_url(struct gcli_ctx *ctx, struct gcli_path const *const path,
char **url, char const *const suffix_fmt, ...)
{
int rc = 0;
char *suffix = NULL;
va_list vp;
va_start(vp, suffix_fmt);
suffix = sn_vasprintf(suffix_fmt, vp);
va_end(vp);
switch (path->kind) {
case GCLI_PATH_DEFAULT: {
char *e_owner, *e_repo;
e_owner = gcli_urlencode(path->as_default.owner);
e_repo = gcli_urlencode(path->as_default.repo);
*url = sn_asprintf("%s/projects/%s%%2F%s/merge_requests/%"PRIid"%s",
gcli_get_apibase(ctx),
e_owner, e_repo, path->as_default.id,
suffix);
gcli_clear_ptr(&e_owner);
gcli_clear_ptr(&e_repo);
} break;
case GCLI_PATH_PID_ID: {
*url = sn_asprintf("%s/projects/%"PRIid"/merge_requests/%"PRIid"%s",
gcli_get_apibase(ctx),
path->as_pid_id.project_id,
path->as_pid_id.id,
suffix);
} break;
case GCLI_PATH_URL: {
*url = sn_asprintf("%s%s", path->as_url, suffix);
} break;
default: {
rc = gcli_error(ctx, "unsupported path type for gitlab merge request");
} break;
}
gcli_clear_ptr(&suffix);
return rc;
}
int
gitlab_fetch_mrs(struct gcli_ctx *ctx, char *url, int const max,
struct gcli_pull_list *const list)
{
int rc = 0;
struct gcli_fetch_list_ctx fl = {
.listp = &list->pulls,
.sizep = &list->pulls_size,
.max = max,
.parse = (parsefn)(parse_gitlab_mrs),
};
rc = gcli_fetch_list(ctx, url, &fl);
/* TODO: don't leak the list on error */
if (rc == 0)
gitlab_mrs_fixup(list);
return rc;
}
int
gitlab_get_mrs(struct gcli_ctx *ctx, struct gcli_path const *const path,
struct gcli_pull_fetch_details const *const details, int const max,
struct gcli_pull_list *const list)
{
int rc = 0;
char *url = NULL;
char *e_author = NULL;
char *e_label = NULL;
char *e_milestone = NULL;
char *e_search = NULL;
if (details->author) {
char *tmp = gcli_urlencode(details->author);
bool const need_qmark = details->all;
e_author = sn_asprintf("%cauthor_username=%s", need_qmark ? '?' : '&', tmp);
gcli_clear_ptr(&tmp);
}
if (details->label) {
char *tmp = gcli_urlencode(details->label);
bool const need_qmark = details->all && !details->author;
e_label = sn_asprintf("%clabels=%s", need_qmark ? '?' : '&', tmp);
gcli_clear_ptr(&tmp);
}
if (details->milestone) {
char *tmp = gcli_urlencode(details->milestone);
bool const need_qmark = details->all && !details->author && !details->label;
e_milestone = sn_asprintf("%cmilestone=%s", need_qmark ? '?' : '&', tmp);
gcli_clear_ptr(&tmp);
}
if (details->search_term) {
char *tmp = gcli_urlencode(details->search_term);
bool const need_qmark = details->all && !details->author &&
!details->label && !details->milestone;
e_search = sn_asprintf("%csearch=%s", need_qmark ? '?' : '&', tmp);
gcli_clear_ptr(&tmp);
}
rc = gitlab_repo_make_url(ctx, path, &url, "/merge_requests%s%s%s%s%s",
details->all ? "" : "?state=opened",
e_author ? e_author : "",
e_label ? e_label : "",
e_milestone ? e_milestone : "",
e_search ? e_search : "");
gcli_clear_ptr(&e_search);
gcli_clear_ptr(&e_milestone);
gcli_clear_ptr(&e_label);
gcli_clear_ptr(&e_author);
if (rc < 0)
return rc;
return gitlab_fetch_mrs(ctx, url, max, list);
}
static void
gitlab_free_diff(struct gitlab_diff *diff)
{
gcli_clear_ptr(&diff->diff);
gcli_clear_ptr(&diff->old_path);
gcli_clear_ptr(&diff->new_path);
gcli_clear_ptr(&diff->a_mode);
gcli_clear_ptr(&diff->b_mode);
memset(diff, 0, sizeof(*diff));
}
static void
gitlab_free_diffs(struct gitlab_diff_list *list)
{
for (size_t i = 0; i < list->diffs_size; ++i) {
gitlab_free_diff(&list->diffs[i]);
}
gcli_clear_ptr(&list->diffs);
list->diffs_size = 0;
}
static void
gitlab_make_commit_diff(struct gcli_commit const *const commit,
struct gitlab_diff const *const diff,
char const *const prev_commit_sha, FILE *const out)
{
fprintf(out, "diff --git a/%s b/%s\n", diff->old_path, diff->new_path);
if (diff->new_file) {
fprintf(out, "new file mode %s\n", diff->b_mode);
fprintf(out, "index 0000000..%s\n", commit->sha);
} else {
fprintf(out, "index %s..%s %s\n", prev_commit_sha, commit->sha,
diff->b_mode);
}
fprintf(out, "--- %s%s\n",
diff->new_file ? "" : "a/",
diff->new_file ? "/dev/null" : diff->old_path);
fprintf(out, "+++ %s%s\n",
diff->deleted_file ? "" : "b/",
diff->deleted_file ? "/dev/null" : diff->new_path);
fputs(diff->diff, out);
}
static int
gitlab_make_commit_patch(struct gcli_ctx *ctx, FILE *stream,
struct gcli_path const *const repo_path,
char const *const prev_commit_sha,
struct gcli_commit const *const commit)
{
char *url;
int rc;
struct gitlab_diff_list list = {0};
struct gcli_fetch_list_ctx fl = {
.listp = &list.diffs,
.sizep = &list.diffs_size,
.max = -1,
.parse = (parsefn)(parse_gitlab_diffs),
};
/* /projects/:id/repository/commits/:sha/diff */
rc = gitlab_repo_make_url(ctx, repo_path, &url,
"/repository/commits/%s/diff", commit->sha);
if (rc < 0)
return rc;
rc = gcli_fetch_list(ctx, url, &fl);
if (rc < 0) {
gcli_clear_ptr(&url);
return rc;
}
fprintf(stream, "From %s Mon Sep 17 00:00:00 2001\n", commit->long_sha);
fprintf(stream, "From: %s <%s>\n", commit->author, commit->email);
fprintf(stream, "Date: %s\n", commit->date);
fprintf(stream, "Subject: %s\n\n", commit->message);
for (size_t i = 0; i < list.diffs_size; ++i) {
gitlab_make_commit_diff(commit, &list.diffs[i],
prev_commit_sha, stream);
}
fprintf(stream, "--\n2.42.2\n\n\n");
gitlab_free_diffs(&list);
return rc;
}
int
gitlab_mr_get_patch(struct gcli_ctx *ctx, FILE *stream,
struct gcli_path const *const path)
{
int rc = 0;
struct gcli_pull pull = {0};
struct gcli_commit_list commits = {0};
char const *prev_commit_sha;
char *base_sha_short;
rc = gitlab_get_pull(ctx, path, &pull);
if (rc < 0)
goto err_get_pull;
rc = gitlab_get_pull_commits(ctx, path, &commits);
if (rc < 0)
goto err_get_commit_list;
base_sha_short = sn_strndup(pull.base_sha, 8);
prev_commit_sha = base_sha_short;
for (size_t i = commits.commits_size; i > 0; --i) {
rc = gitlab_make_commit_patch(ctx, stream, path,
prev_commit_sha,
&commits.commits[i - 1]);
if (rc < 0)
goto err_make_commit_patch;
prev_commit_sha = commits.commits[i - 1].sha;
}
err_make_commit_patch:
gcli_clear_ptr(&base_sha_short);
gcli_commits_free(&commits);
err_get_commit_list:
gcli_pull_free(&pull);
err_get_pull:
return rc;
}
static int
gitlab_mr_get_diff_versions(struct gcli_ctx *ctx,
struct gcli_path const *const path,
struct gitlab_mr_version_list *const out)
{
char *url = NULL;
int rc = 0;
struct gcli_fetch_list_ctx fl_ctx = {
.listp = &out->versions,
.sizep = &out->versions_size,
.max = -1,
.parse = (parsefn)parse_gitlab_mr_version_list,
};
rc = gitlab_mr_make_url(ctx, path, &url, "/versions");
if (rc < 0)
return rc;
return gcli_fetch_list(ctx, url, &fl_ctx);
}
static int
gitlab_mr_get_diff_version(struct gcli_ctx *ctx,
struct gcli_path const *const path,
gcli_id const version_id,
struct gitlab_diff_list *const out)
{
char *url = NULL;
int rc = 0;
struct gcli_fetch_buffer buffer = {0};
rc = gitlab_mr_make_url(ctx, path, &url, "/versions/%"PRIid, version_id);
if (rc < 0)
return rc;
rc = gcli_fetch(ctx, url, NULL, &buffer);
if (rc == 0) {
struct json_stream stream = {0};
json_open_buffer(&stream, buffer.data, buffer.length);
rc = parse_gitlab_mr_version_diffs(ctx, &stream, out);
json_close(&stream);
}
gcli_clear_ptr(&url);
gcli_clear_ptr(&buffer.data);
return rc;
}
int
gitlab_mr_get_diff(struct gcli_ctx *ctx, FILE *stream,
struct gcli_path const *const path)
{
int rc;
struct gitlab_diff_list diff_list = {0};
struct gitlab_mr_version const *version;
struct gitlab_mr_version_list version_list = {0};
/* Grab a list of diff versions available for this MR.
* The list is sorted numerically decending. Thus we need
* to just grab the very first version in the array and use
* it. */
rc = gitlab_mr_get_diff_versions(ctx, path, &version_list);
if (rc < 0)
goto err_get_mr_diff_version;
if (!version_list.versions_size) {
rc = gcli_error(ctx, "no diffs available for the merge request");
goto err_get_mr_diff_version;
}
version = &version_list.versions[0];
rc = gitlab_mr_get_diff_version(ctx, path, version->id, &diff_list);
if (rc < 0)
goto err_get_diffs;
fprintf(stream, "GCLI: Below is metadata for this diff. Do not remove or alter\n");
fprintf(stream, "GCLI: in case you're using this for a review.\n");
fprintf(stream, "GCLI: base_sha %s\n", version->base_commit);
fprintf(stream, "GCLI: start_sha %s\n", version->start_commit);
fprintf(stream, "GCLI: head_sha %s\n", version->head_commit);
for (size_t i = 0; i < diff_list.diffs_size; ++i) {
struct gitlab_diff const *const diff = &diff_list.diffs[i];
fprintf(stream, "diff --git a/%s b/%s\n", diff->old_path, diff->new_path);
if (diff->new_file) {
fprintf(stream, "new file mode %s\n", diff->b_mode);
fprintf(stream, "index 0000000..%s\n", version->head_commit);
} else {
fprintf(stream, "index %s..%s %s\n",
version->base_commit, version->head_commit,
diff->b_mode);
}
fprintf(stream, "--- %s%s\n",
diff->new_file ? "" : "a/",
diff->new_file ? "/dev/null" : diff->old_path);
fprintf(stream, "+++ %s%s\n",
diff->deleted_file ? "" : "b/",
diff->deleted_file ? "/dev/null" : diff->new_path);
fputs(diff->diff, stream);
}
gitlab_free_diffs(&diff_list);
err_get_diffs:
gitlab_mr_version_list_free(&version_list);
err_get_mr_diff_version:
return rc;
}
int
gitlab_mr_set_automerge(struct gcli_ctx *const ctx,
struct gcli_path const *const path)
{
char *url;
int rc;
rc = gitlab_mr_make_url(ctx, path, &url, "");
if (rc < 0)
return rc;
rc = gcli_fetch_with_method(ctx, "PUT", url, NULL, NULL, NULL);
gcli_clear_ptr(&url);
return rc;
}
int
gitlab_mr_merge(struct gcli_ctx *ctx, struct gcli_path const *const path,
enum gcli_merge_flags const flags)
{
bool const delete_source = flags & GCLI_PULL_MERGE_DELETEHEAD;
bool const squash = flags & GCLI_PULL_MERGE_SQUASH;
char *url = NULL;
char const *data = "{}";
int rc = 0;
struct gcli_fetch_buffer buffer = {0};
/* PUT /projects/:id/merge_requests/:merge_request_iid/merge */
rc = gitlab_mr_make_url(ctx, path, &url, "/merge?squash=%s"
"&should_remove_source_branch=%s",
squash ? "true" : "false",
delete_source ? "true" : "false");
if (rc < 0)
return rc;
rc = gcli_fetch_with_method(ctx, "PUT", url, data, NULL, &buffer);
gcli_fetch_buffer_free(&buffer);
gcli_clear_ptr(&url);
return rc;
}
int
gitlab_get_pull(struct gcli_ctx *ctx, struct gcli_path const *const path,
struct gcli_pull *const out)
{
struct gcli_fetch_buffer buffer = {0};
char *url = NULL;
int rc = 0;
/* GET /projects/:id/merge_requests/:merge_request_iid */
rc = gitlab_mr_make_url(ctx, path, &url, "");
if (rc < 0)
return rc;
rc = gcli_fetch(ctx, url, NULL, &buffer);
if (rc == 0) {
struct json_stream stream = {0};
json_open_buffer(&stream, buffer.data, buffer.length);
parse_gitlab_mr(ctx, &stream, out);
json_close(&stream);
}
gcli_clear_ptr(&url);
gcli_fetch_buffer_free(&buffer);
return rc;
}
int
gitlab_get_pull_commits(struct gcli_ctx *ctx,
struct gcli_path const *const path,
struct gcli_commit_list *const out)
{
char *url = NULL;
int rc = 0;
struct gcli_fetch_list_ctx fl = {
.listp = &out->commits,
.sizep = &out->commits_size,
.max = -1,
.parse = (parsefn)(parse_gitlab_commits),
};
/* GET /projects/:id/merge_requests/:merge_request_iid/commits */
rc = gitlab_mr_make_url(ctx, path, &url, "/commits");
if (rc < 0)
return rc;
return gcli_fetch_list(ctx, url, &fl);
}
static int
gitlab_mr_patch_state(struct gcli_ctx *const ctx,
struct gcli_path const *const path,
char const *const new_state)
{
char *url = NULL, *payload = NULL;
struct gcli_jsongen gen = {0};
int rc = 0;
/* Generate URL */
rc = gitlab_mr_make_url(ctx, path, &url, "");
if (rc < 0)
return rc;
/* Generate payload */
gcli_jsongen_init(&gen);
gcli_jsongen_begin_object(&gen);
{
gcli_jsongen_objmember(&gen, "state_event");
gcli_jsongen_string(&gen, new_state);
}
gcli_jsongen_end_object(&gen);
payload = gcli_jsongen_to_string(&gen);
gcli_jsongen_free(&gen);
rc = gcli_fetch_with_method(ctx, "PUT", url, payload, NULL, NULL);
gcli_clear_ptr(&url);
gcli_clear_ptr(&payload);
return rc;
}
int
gitlab_mr_close(struct gcli_ctx *ctx, struct gcli_path const *const path)
{
return gitlab_mr_patch_state(ctx, path, "close");
}
int
gitlab_mr_reopen(struct gcli_ctx *ctx, struct gcli_path const *const path)
{
return gitlab_mr_patch_state(ctx, path, "reopen");
}
/* This routine is a workaround for a Gitlab bug:
*
* https://gitlab.com/gitlab-org/gitlab/-/issues/353984
*
* This is a race condition because something in the creation of a merge request
* is being handled asynchronously. See the above link for more details.
*
* TL;DR: We need to wait until the »merge_status« field of the MR is set to
* »can_be_merged«. This is indicated by the mergable field becoming true. */
static int
gitlab_mr_wait_until_mergeable(struct gcli_ctx *ctx,
struct gcli_path const *const path)
{
char *url;
int rc = 0;
struct timespec const ts = { .tv_sec = 1, .tv_nsec = 0 };
rc = gitlab_mr_make_url(ctx, path, &url, "");
if (rc < 0)
return rc;
for (;;) {
bool is_mergeable;
struct gcli_fetch_buffer buffer = {0};
struct json_stream stream = {0};
struct gcli_pull pull = {0};
rc = gcli_fetch(ctx, url, NULL, &buffer);
if (rc < 0)
break;
json_open_buffer(&stream, buffer.data, buffer.length);
rc = parse_gitlab_mr(ctx, &stream, &pull);
json_close(&stream);
/* FIXME: this doesn't quite cut it when the PR has no commits in it.
* In that case this will turn into an infinite loop. */
is_mergeable = pull.mergeable;
gcli_pull_free(&pull);
gcli_fetch_buffer_free(&buffer);
if (is_mergeable)
break;
/* sort of a hack: wait for a second until the next request goes out */
nanosleep(&ts, NULL);
}
gcli_clear_ptr(&url);
return rc;
}
int
gitlab_perform_submit_mr(struct gcli_ctx *ctx, struct gcli_submit_pull_options *opts)
{
/* Note: this doesn't really allow merging into repos with
* different names. We need to figure out a way to make this
* better for both github and gitlab. */
char *source_branch = NULL, *source_owner = NULL, *payload = NULL, *url = NULL;
char const *target_branch = NULL;
int rc = 0;
struct gcli_fetch_buffer buffer = {0};
struct gcli_jsongen gen = {0};
struct gcli_repo target = {0};
/* generate url */
rc = gitlab_repo_make_url(ctx, &opts->target_repo, &url, "/merge_requests");
if (rc < 0)
return rc;
target_branch = opts->target_branch;
source_owner = strdup(opts->from);
source_branch = strchr(source_owner, ':');
if (source_branch == NULL)
return gcli_error(ctx, "bad merge request source: expected 'owner:branch'");
*source_branch++ = '\0';
/* Figure out the project id */
rc = gitlab_get_repo(ctx, &opts->target_repo, &target);
if (rc < 0)
return rc;
/* generate payload */
gcli_jsongen_init(&gen);
gcli_jsongen_begin_object(&gen);
{
gcli_jsongen_objmember(&gen, "source_branch");
gcli_jsongen_string(&gen, source_branch);
gcli_jsongen_objmember(&gen, "target_branch");
gcli_jsongen_string(&gen, target_branch);
gcli_jsongen_objmember(&gen, "title");
gcli_jsongen_string(&gen, opts->title);
/* description is optional and will be NULL if unset */
if (opts->body) {
gcli_jsongen_objmember(&gen, "description");
gcli_jsongen_string(&gen, opts->body);
}
gcli_jsongen_objmember(&gen, "target_project_id");
gcli_jsongen_number(&gen, target.id);
/* Labels if any */
if (opts->labels_size) {
gcli_jsongen_objmember(&gen, "labels");
gcli_jsongen_begin_array(&gen);
for (size_t i = 0; i < opts->labels_size; ++i)
gcli_jsongen_string(&gen, opts->labels[i]);
gcli_jsongen_end_array(&gen);
}
/* Reviewers if any */
if (opts->reviewers_size) {
gcli_jsongen_objmember(&gen, "reviewer_ids");
gcli_jsongen_begin_array(&gen);
for (size_t i = 0; i < opts->reviewers_size; ++i) {
int uid;
uid = gitlab_user_id(ctx, opts->reviewers[i]);
if (uid < 0)
return uid;
gcli_jsongen_number(&gen, uid);
}
gcli_jsongen_end_array(&gen);
}
}
gcli_jsongen_end_object(&gen);
payload = gcli_jsongen_to_string(&gen);
gcli_jsongen_free(&gen);
gcli_repo_free(&target);
/* perform request */
rc = gcli_fetch_with_method(ctx, "POST", url, payload, NULL, &buffer);
/* if that succeeded and the user wants automerge, parse the result and
* set the automerge flag. */
if (rc == 0 && opts->automerge && opts->target_repo.kind == GCLI_PATH_DEFAULT) {
struct json_stream stream = {0};
struct gcli_pull pull = {0};
struct gcli_path target_mr_path = opts->target_repo;
json_open_buffer(&stream, buffer.data, buffer.length);
rc = parse_gitlab_mr(ctx, &stream, &pull);
json_close(&stream);
target_mr_path.as_default.id = pull.id;
if (rc < 0)
goto out;
rc = gitlab_mr_wait_until_mergeable(ctx, &target_mr_path);
if (rc < 0)
goto out;
rc = gitlab_mr_set_automerge(ctx, &target_mr_path);
out:
gcli_pull_free(&pull);
}
/* cleanup */
gcli_fetch_buffer_free(&buffer);
gcli_clear_ptr(&source_owner);
gcli_clear_ptr(&payload);
gcli_clear_ptr(&url);
return rc;
}
static int
gitlab_mr_update_labels(struct gcli_ctx *ctx,
struct gcli_path const *const path,
char const *const labels[], size_t const labels_size,
char const *const update_action)
{
char *url = NULL, *payload = NULL, *list = NULL;
struct gcli_jsongen gen = {0};
int rc = 0;
/* Generate URL */
rc = gitlab_mr_make_url(ctx, path, &url, "");
if (rc < 0)
return rc;
/* Generate payload */
list = sn_join_with(labels, labels_size, ",");
gcli_jsongen_init(&gen);
gcli_jsongen_begin_object(&gen);
{
gcli_jsongen_objmember(&gen, update_action);
gcli_jsongen_string(&gen, list);
}
gcli_jsongen_end_object(&gen);
payload = gcli_jsongen_to_string(&gen);
gcli_jsongen_free(&gen);
gcli_clear_ptr(&list);
rc = gcli_fetch_with_method(ctx, "PUT", url, payload, NULL, NULL);
gcli_clear_ptr(&url);
gcli_clear_ptr(&payload);
return rc;
}
int
gitlab_mr_add_labels(struct gcli_ctx *ctx, struct gcli_path const *const path,
char const *const labels[], size_t const labels_size)
{
return gitlab_mr_update_labels(ctx, path, labels, labels_size, "add_labels");
}
int
gitlab_mr_remove_labels(struct gcli_ctx *ctx,
struct gcli_path const *const path,
char const *const labels[], size_t const labels_size)
{
return gitlab_mr_update_labels(ctx, path, labels, labels_size, "remove_labels");
}
int
gitlab_mr_set_milestone(struct gcli_ctx *ctx,
struct gcli_path const *const mr_path,
gcli_id milestone_id)
{
char *url = NULL, *payload = NULL;
struct gcli_jsongen gen = {0};
int rc = 0;
/* Generate URL */
rc = gitlab_mr_make_url(ctx, mr_path, &url, "");
if (rc < 0)
return rc;
/* Generate Payload */
gcli_jsongen_init(&gen);
gcli_jsongen_begin_object(&gen);
{
gcli_jsongen_objmember(&gen, "milestone_id");
gcli_jsongen_id(&gen, milestone_id);
}
gcli_jsongen_end_object(&gen);
payload = gcli_jsongen_to_string(&gen);
gcli_jsongen_free(&gen);
rc = gcli_fetch_with_method(ctx, "PUT", url, payload, NULL, NULL);
gcli_clear_ptr(&url);
gcli_clear_ptr(&payload);
return rc;
}
int
gitlab_mr_clear_milestone(struct gcli_ctx *ctx,
struct gcli_path const *const mr_path)
{
/* GitLab's REST API docs state:
*
* The global ID of a milestone to assign the merge request
* to. Set to 0 or provide an empty value to unassign a
* milestone. */
return gitlab_mr_set_milestone(ctx, mr_path, 0);
}
/* helper typedef to shorten down the type of this function pointer */
typedef int (*user_id_list_parser)(
struct gcli_ctx *,
struct json_stream *,
struct gitlab_user_id_list *out);
/* generic helper function for extracting user id lists from a merge request */
static int
gitlab_mr_get_user_id_list(
struct gcli_ctx *ctx,
struct gcli_path const *const path,
user_id_list_parser parser,
struct gitlab_user_id_list *const out)
{
char *url = NULL;
int rc = 0;
struct gcli_fetch_buffer buffer = {0};
rc = gitlab_mr_make_url(ctx, path, &url, "");
if (rc < 0)
return rc;
rc = gcli_fetch(ctx, url, NULL, &buffer);
if (rc == 0) {
struct json_stream stream = {0};
json_open_buffer(&stream, buffer.data, buffer.length);
/* use generic parser to extract data */
rc = parser(ctx, &stream, out);
json_close(&stream);
}
gcli_clear_ptr(&url);
gcli_fetch_buffer_free(&buffer);
return rc;
}
static void
gitlab_user_id_list_free(struct gitlab_user_id_list *const list)
{
gcli_clear_ptr(&list->users);
list->users_size = 0;
}
/* helper function for adding a user to a list of users (e.g.
* assignees or reviewers */
static int
gitlab_mr_add_user_id(
struct gcli_ctx *ctx,
struct gcli_path const *const path, /* path to the MR */
user_id_list_parser const parser, /* parser for extracting user ids */
char const *const field_name, /* field in the POST payload */
char const *username)
{
char *url, *payload;
int uid, rc = 0;
struct gitlab_user_id_list list = {0};
struct gcli_jsongen gen = {0};
/* Fetch list of already existing users */
rc = gitlab_mr_get_user_id_list(ctx, path, parser, &list);
if (rc < 0)
goto bail_get_reviewers;
/* Resolve user id from user name */
uid = gitlab_user_id(ctx, username);
if (uid < 0)
goto bail_resolve_user_id;
/* Start generating payload */
gcli_jsongen_init(&gen);
gcli_jsongen_begin_object(&gen);
{
gcli_jsongen_objmember(&gen, field_name);
gcli_jsongen_begin_array(&gen);
{
for (size_t i = 0; i < list.users_size; ++i)
gcli_jsongen_number(&gen, list.users[i]);
/* Push new user id into list of user ids */
gcli_jsongen_number(&gen, uid);
}
gcli_jsongen_end_array(&gen);
}
gcli_jsongen_end_object(&gen);
payload = gcli_jsongen_to_string(&gen);
gcli_jsongen_free(&gen);
/* generate URL */
rc = gitlab_mr_make_url(ctx, path, &url, "");
if (rc == 0) {
rc = gcli_fetch_with_method(ctx, "PUT", url, payload, NULL, NULL);
}
gcli_clear_ptr(&url);
gcli_clear_ptr(&payload);
bail_resolve_user_id:
gitlab_user_id_list_free(&list);
bail_get_reviewers:
return rc;
}
int
gitlab_mr_add_reviewer(struct gcli_ctx *ctx, struct gcli_path const *const path,
char const *const username)
{
return gitlab_mr_add_user_id(ctx, path, parse_gitlab_reviewer_ids,
"reviewer_ids", username);
}
int
gitlab_mr_add_assignee(struct gcli_ctx *ctx, struct gcli_path const *const path,
char const *const username)
{
return gitlab_mr_add_user_id(ctx, path, parse_gitlab_assignee_ids,
"assignee_ids", username);
}
int
gitlab_mr_set_title(struct gcli_ctx *ctx, struct gcli_path const *const path,
char const *const new_title)
{
char *url, *payload;
struct gcli_jsongen gen = {0};
int rc = 0;
/* Generate url
*
* PUT /projects/:id/merge_requests/:merge_request_iid */
rc = gitlab_mr_make_url(ctx, path, &url, "");
if (rc < 0)
return rc;
/* Generate payload */
gcli_jsongen_init(&gen);
gcli_jsongen_begin_object(&gen);
{
gcli_jsongen_objmember(&gen, "title");
gcli_jsongen_string(&gen, new_title);