Add --web and --tools to support other repo's.
[tools.git] / review-tools / gitaddrev
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use File::Basename;
7 use FindBin;
8
9 use OpenSSL::Query::REST;
10 use Module::Load::Conditional qw(can_load);
11
12 can_load(modules => { 'OpenSSL::Query::DB' => undef });
13
14 my $rmrev = 0;
15 my @commits;
16 my $skip = 0;
17 my $matchstr;
18 my $clatype;
19 my $found = 0;
20 my $num = 0;
21 my $refuse = 0;
22 my $prnum = 0;
23 my $verbose = 0;
24 my $WHAT = 'openssl';
25
26 my $query = OpenSSL::Query->new();
27
28 my @reviewers;
29 my @nocla_reviewers;
30 my @unknown_reviewers;
31 my $skip_reviewer;
32 my $omccount = 0;
33 sub try_add_reviewer {
34     my $id = lc(shift);
35     my $rc = undef;
36     my $id2 = $id =~ /^\@(.*)$/ ? { github => $1 } : $id;
37     my $rev = $query->find_person_tag($id2, 'rev');
38     if ($rev) {
39         my $cla = $query->has_cla($rev);
40         if ($cla) {
41             unless (grep {$_ eq $rev} @reviewers) {
42                 $omccount++ if $query->is_member_of($id2, 'omc');
43                 push @reviewers, $rev;
44             }
45             $rc = $rev;
46         } else {
47             push @nocla_reviewers, $id
48                 unless grep {$_ eq $id} @nocla_reviewers;
49         }
50     } else {
51         push @unknown_reviewers, $id
52             unless grep {$_ eq $id} @unknown_reviewers;
53         unless ($id =~ m|^.+\@.*$| && $query->has_cla($id)) {
54             push @nocla_reviewers, $id
55                 unless grep {$_ eq $id} @nocla_reviewers;
56         }
57     }
58     return $rc;
59 }
60
61 foreach (@ARGV) {
62     if (/^--list$/) {
63         my %list = ();
64         foreach ($query->list_people()) {
65             my $email_id = (grep { ref($_) eq "" && $_ =~ m|\@| } @$_)[0];
66             my $rev = $query->find_person_tag($email_id, 'rev');
67             my $omc = $query->is_member_of($email_id, 'omc');
68             next unless $query->has_cla($rev);
69             next unless $query->is_member_of($email_id, 'commit') || $omc;
70             my @ids =
71                 sort grep { $_ =~ /^[a-z]+$/ || $_ =~ /^\@(?:\w|\w-\w)+$/ }
72                 map {
73                     if (ref($_) eq "HASH") {
74                         my %h = %$_;
75                         map { $_ eq "github" ? '@'.$h{$_} : $h{$_} } keys %h;
76                     } else {
77                         $_;
78                     }
79                 } @$_;
80             foreach (@ids) {
81                 $list{$_} = { tag => $rev, omc => $omc };
82             }
83         }
84         foreach (sort { my $res = $list{$a}->{tag} cmp $list{$b}->{tag};
85                         $res != 0 ? $res : ($a cmp $b) } keys %list) {
86             printf "%-15s %-6s (%s)\n",
87                 $_, $list{$_}->{omc} ? "[OMC]" : "", $list{$_}->{tag};
88         }
89         exit 0;
90     } elsif (/^--reviewer=(.+)$/) {
91         try_add_reviewer($1);
92     } elsif (/^--prnum=(.+)$/) {
93         $prnum = $1;
94     } elsif (/^--commit=(.+)$/) {
95         push @commits, $1;
96         $skip = 1;
97     } elsif (/^--rmreviewers$/) {
98         $rmrev = 1;
99     } elsif (/^--myemail=(.+\@.+)$/) {
100         try_add_reviewer($1);
101     } elsif (/^--verbose$/) {
102         $verbose = 1;
103     } elsif (/^--web$/) {
104         $WHAT = 'web';
105     } elsif (/--tools$/) {
106         $WHAT = 'tools'
107     }
108 }
109
110 my @commit_message = map { (my $x = $_) =~ s|\R$||; $x } <STDIN>;
111 my $trivial = !! grep(/^CLA:\s*Trivial\s*$/i, @commit_message);
112
113 # If the author is a registered committer, that identity passes as a reviewer
114 # too.  There is a twist, though...  see next comment
115 if (my $rev = try_add_reviewer($ENV{GIT_AUTHOR_EMAIL})) {
116
117     # So here's the deal: We added the commit author because we need to keep
118     # count of the total amount of reviewers, which includes the commit author
119     # if it's a registered committer.  However, the author can "reviewed
120     # themselves", so no Reviewed-by: should be added for that identity.
121     # However, we still need to check the @reviewers count below, so the
122     # solution is to record this rev tag separately and remove it from
123     # @reviewers after the count check.
124     $skip_reviewer = $rev;
125
126 } else {
127
128     # In case the author is unknown to our databases or is lacking a CLA,
129     # we need to be extra careful to check if this is supposed to be a
130     # trivial commit.
131     my $author = lc($ENV{GIT_AUTHOR_EMAIL});
132
133     # Note: it really should be enough to check if $author is unknown, since
134     # the databases are supposed to be consistent with each other.  However,
135     # let's be cautious and check both, in case someone has been registered
136     # as a known identity without having a CLA in place.
137     die "Commit author ",$author," has no CLA, and this is a non-trivial commit\n"
138         if !$trivial && grep { $_ eq $author } (@nocla_reviewers);
139
140     # Now that that's cleared, remove the author from anything that could cause
141     # more unnecessary errors (false positives).
142     @nocla_reviewers = grep { $_ ne $author } @nocla_reviewers;
143     @unknown_reviewers = grep { $_ ne $author } @unknown_reviewers;
144 }
145
146 if (@unknown_reviewers) {
147     die "Unknown reviewers: ", join(", ", @unknown_reviewers), "\n";
148 }
149 if (@nocla_reviewers) {
150     die "Reviewers without CLA: ", join(", ", @nocla_reviewers), "\n";
151 }
152
153 print STDERR "Detected trivial marker\n" if $verbose && $trivial;
154
155 print STDERR "Going with these reviewers:\n  ", join("\n  ", @reviewers), "\n"
156     if $verbose;
157
158 if (scalar @reviewers < 2) {
159     die "Too few reviewers (total must be at least 2)\n";
160 }
161 if ($omccount < 1) {
162     die "At least one of the reviewers must be an OMC member\n";
163 }
164 if ($skip_reviewer) {
165     @reviewers = grep { $_ ne $skip_reviewer } @reviewers;
166     @nocla_reviewers = grep { $_ ne $skip_reviewer } @nocla_reviewers;
167     @unknown_reviewers = grep { $_ ne $skip_reviewer } @unknown_reviewers;
168 }
169
170 if ($skip == 1) {
171     my $commit_id = $ENV{GIT_COMMIT};
172     foreach(@commits) {
173         if ($commit_id =~ /^$_/) {
174             $skip = 0;
175             last;
176         }
177     }
178     if ($skip == 1) {
179         while(<STDIN>) {
180             print;
181         }
182     exit(0);
183     }
184 }
185
186 if (scalar @reviewers == 0 && $rmrev == 0) {
187     die "No reviewer set!\n";
188 }
189
190 # Remove blank lines from the end of commit message
191 pop @commit_message while $commit_message[$#commit_message] =~ m|^\s*$|;
192
193 my $last_is_rev = 0;
194 foreach (@commit_message) {
195     # Start each line with assuming it's not a reviewed-by line
196     $last_is_rev = 0;
197     if (/^\(Merged from https:\/\/github\.com\/openssl\/$WHAT\/pull\//) {
198         next if $rmrev == 1;
199         $last_is_rev = 1;
200         next;                   # Because we're rewriting it below
201                                 # (unless --nopr was given in addrev)
202     } elsif (/^Reviewed-by:\s*(\S.*\S)\s*$/) {
203         my $id = $1;
204         next if $rmrev == 1;
205         $last_is_rev = 1;
206         # Remove reviewers that are already in the message from our reviewer list
207         @reviewers = grep { $_ ne $id } @reviewers;
208     }
209     print $_,"\n";
210 }
211 if ($rmrev == 0) {
212     #Add a blank line unless the last one is a review line
213     print "\n" unless $last_is_rev;
214     foreach(@reviewers) {
215         print "Reviewed-by: $_\n";
216     }
217 }
218
219 print "(Merged from https://github.com/openssl/$WHAT/pull/$prnum)\n"
220     if $prnum;