gmerge and pick-to-branch: further tweak output
[tools.git] / review-tools / gitlabutil
1 #!/usr/bin/env perl
2
3 use HTTP::Tiny;
4 use JSON::PP;
5 use Text::Wrap;
6
7 # OpenSSL project number
8 my $proj = 2;
9 my $num;
10
11
12 my $desc;
13 my $user;
14
15 my $state = "opened";
16 my $text  = 0;
17 my $fetch = 0;
18 my @matches;
19
20 # Gitlab token
21 my $token;
22
23 foreach (@ARGV) {
24     $state = "$1" if (/^--state=(.*)$/);
25     $desc  = "$1" if (/^--desc=(.*)$/);
26     $token = "$1" if (/^--token=(.*)$/);
27     $user  = "$1" if (/^--user=(.*)$/);
28     $num   = $_   if (/^\d+$/);
29     $text  = 1    if (/^--text$/);
30     $fetch = 1    if (/^--fetch$/);
31     $fetch = 2    if (/^--checkout$/);
32 }
33
34 if ( !defined $token && -f "$ENV{HOME}/.gitlabtoken" ) {
35     open( IN, "$ENV{HOME}/.gitlabtoken" );
36     $token = <IN>;
37     chomp $token;
38     close IN;
39 }
40
41 if ( !defined $token ) {
42     print STDERR "Gitlab token not defined!\n";
43     exit 1;
44 }
45
46
47 # Page number for gitlab query
48 my $pnum = 1;
49
50 while (1) {
51     my $response = HTTP::Tiny->new->get(
52 "https://gitlab.openssl.org/api/v3/projects/$proj/merge_requests/?state=$state;per_page=100;page=$pnum;private_token=$token"
53     );
54     if ( !$response->{success} ) {
55         print "Failed: $response->{status}, $response->{reason}\n";
56         exit 1;
57     }
58     my $resp = decode_json $response->{content};
59     last if @$resp == 0;
60
61     foreach (@$resp) {
62         next if ( defined $num && $_->{iid} ne $num );
63         next if ( defined $desc && $_->{description} !~ /$desc/ );
64         next if ( defined $user && $_->{author}->{username} ne $user );
65         push @matches, $_;
66     }
67     $pnum++;
68 }
69
70 if ( @matches == 0 ) {
71     print "ERROR: no matching requests found\n";
72     exit 1;
73 }
74 if ( $fetch && @matches != 1 ) {
75     print "ERROR: multiple matching requests\n";
76     exit 1;
77 }
78
79 foreach (@matches) {
80     print_req( $_, $text );
81     if ($fetch) {
82         my $br   = $_->{source_branch};
83         my $user = $_->{author}->{username};
84         system(
85             "git fetch git\@gitlab.openssl.org:$user/openssl.git $br:$br"
86         );
87         system("git checkout $br") if $fetch == 2;
88     }
89 }
90
91 sub print_req {
92     my ( $req, $all ) = @_;
93
94     print "Merge request: $req->{iid}, author: $req->{author}->{username},";
95     print " state: $req->{state}\n";
96
97     #print " branch: $req->{source_branch}\n";
98     print "    Title: $req->{title}\n";
99     if ( $all != 0 ) {
100         print "Description:\n";
101
102         # Note: using tabs here causes a failure
103         print fill( "    ", "    ", $req->{description} ) . "\n";
104     }
105     print "\n";
106 }