summaryrefslogtreecommitdiffstats
path: root/git-hooks/gerrit-bot
blob: e5e8845743a0845edb16809d7939e62b01273c63 (plain)
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
#! /usr/bin/perl

# Copyright (C) 2015 The Qt Company Ltd.
# Contact: http://www.qt.io/licensing/
#
# You may use this file under the terms of the 3-clause BSD license.
# See the file LICENSE from this package for details.
#

use strict;
use warnings;
use POSIX;
use JSON;
use File::Path;
use Gerrit::REST;

# Usage: $0 [instance]
# - default instance is 'sanitybot'
# - configure ssh: Host, Port, User, IdentityFile
# - configure git: git config --global <instance>}.<option> <value>
# Valid options are:
#   gerrithost (mandatory)
#     Target host. The identification is done via SSH.
#   gerritactorhost
#     Target host for inviting reviewers. Falls back go gerrithost.
#   resthost (mandatory)
#     The REST base URL of the target host.
#   restuser
#     The user name for resthost. If omitted, credentials are expected in .netrc.
#   restpass
#     The password for resthost. If omitted, credentials are expected in .netrc.
#   useremail (mandatory)
#     Bot's email address. Used to identify invitations and own actions.
#   inviteonly (default 0)
#     If this flag is set, the bot will only become active if it is a
#     requested reviewer. DON'T USE (see TODO).
#   gitbasedir (mandatory)
#     Base dir for local GIT clones of the projects.
#   gitdofetch
#     Need to fetch the repos or are they local?
#   workers
#     A space-separated list of workers.
#     They are run in a local bare clone of the inspected repository.
#     There must be a workers.<worker> config entry for each worker
#     (note that these entries are NOT namespaced with the instance).
#     The magic string @SHA1@ is replaced by the commit to be checked;
#     @PROJ@ by the gerrit project the worker is run for.
#     It is expected to dump a Gerrit ReviewInput JSON entity to stdout.
#     The output from all workers is merged.
#   excluded (optional)
#     Space-separated list of exclusions of the form <project> or
#     <project>:<branch>.
#   maintainers (optional)
#     Space-separated list of reviewers to add on "special occasions".
#   verbose (default 0)
#     Print progress/result messages.

# TODO
# - Implement some retry mechanism to deal with network failures
# - Make inviteonly actually work beyond the initial startup.
#   See http://code.google.com/p/gerrit/issues/detail?id=1200

my $instance = 'sanitybot';
$instance = $ARGV[0] if ($#ARGV > -1);

# Doing this is less expensive than calling git repeatedly.
my %config = ();
for (`git config -l`) {
  /^([^=]+)=(.*$)/;
  $config{$1} = $2;
}

sub getcfg($;$)
{
  my ($key, $def) = @_;
  my $fkey = $instance.'.'.$key;
  if (defined $config{$fkey}) {
    return $config{$fkey};
  } elsif (@_ > 1) {
    return $def;
  } else {
    die $fkey." not set.\n";
  }
}

my $GERRIT_HOST = getcfg 'gerrithost';
my $GERRIT_ACTOR_HOST = getcfg 'gerritactorhost', $GERRIT_HOST;
my $USER_EMAIL = getcfg 'useremail';
my $INVITE_ONLY = getcfg 'inviteonly', 0;
my $REST_HOST = getcfg 'resthost';
my $REST_USER = getcfg 'restuser', undef;
my $REST_PASS = getcfg 'restpass', undef;
my $GIT_BASEDIR = getcfg 'gitbasedir';
my $GIT_DO_FETCH = getcfg 'gitdofetch';
my $WORKERS = getcfg 'workers';
my %EXCLUDED_PROJECTS = map { $_ => 1 } split(/\s+/, getcfg('excluded', ""));
my @MAINTAINERS = split(/\s+/, getcfg('maintainers', ""));
my $verbose = getcfg 'verbose', 0;

my @gerrit = ("ssh", $GERRIT_HOST, "gerrit");
my @gerrit_actor = ("ssh", $GERRIT_ACTOR_HOST, "gerrit");
my $gerrit_rest = Gerrit::REST->new($REST_HOST, $REST_USER, $REST_PASS);

my %processed = ();
my %skipfetch = ();

sub printerr($)
{
  my ($msg) = @_;
  die $msg.": execution failed: ".$!."\n" if ($? < 0);
  die $msg.": command crashed with signal ".$?."\n" if ($? & 127);
  print STDERR $msg.".\n";
}

sub merge_hashes($$);

sub merge_hashes($$)
{
  my ($base, $ext) = @_;

  for my $k (keys %$ext) {
    my $br = \$$base{$k};
    my $ev = $$ext{$k};
    if (!defined($$br)) {
      $$br = $ev;
    } else {
      my $bt = ref($$br);
      my $et = ref($ev);
      die "Values for '$k' have different types ($bt, $et).\n" if ($bt ne $et);
      if ($et eq "HASH") {
        merge_hashes($$br, $ev);
      } elsif ($et eq "ARRAY") {
        push @$$br, @$ev;
      } elsif ($et eq "") {
        $$br = $$br."\n\n".$ev;
      } else {
        die "Values for '$k' have unsupported type $bt.\n";
      }
    }
  }
}

sub process_commit($$$$$)
{
  my ($number, $project, $branch, $ref, $rev) = @_;

  if (defined $processed{$ref}) {
    return;
  }
  $processed{$ref} = 1;
  my $orig_project = $project;
  $project =~ s,/$,,; # XXX Workaround QTQAINFRA-381
  my $verdict = {};
  my @invite;
  if (defined($EXCLUDED_PROJECTS{$project}) || defined($EXCLUDED_PROJECTS{$project.":".$branch})) {
    $verbose and print "===== ".strftime("%c", localtime(time()))." ===== excluding commit ".$ref." in ".$project."\n";
    $$verdict{message} = "(skipped)";
    $$verdict{labels}{'Sanity-Review'} = 1;
  } else {
    $verbose and print "===== ".strftime("%c", localtime(time()))." ===== processing commit ".$ref." in ".$project."\n";
    my $GIT_DIR = $GIT_BASEDIR."/".$project.".git";
    if (!-d $GIT_DIR) {
      mkpath $GIT_DIR or die "cannot create ".$GIT_DIR.": ".$!;
    }
    chdir $GIT_DIR or die "cannot change to ".$GIT_DIR.": ".$!;
    if ($GIT_DO_FETCH) {
      if (!-d $GIT_DIR."/refs/remotes" and `git config remote.origin.url` eq "") {
        if (!-d $GIT_DIR."/refs") {
          if (system("git", "init", "--bare")) {
            printerr "Init of ".$project." failed";
            return;
          }
        }
        if (system("git", "remote", "add", "origin", 'ssh://'.$GERRIT_HOST.'/'.$project)) {
          printerr "Adding remote for ".$project." failed";
          return;
        }
      }
      my @mainlines;
      if (!defined $skipfetch{$project}) {
        # Update refs, otherwise the selective fetches start from scratch each time.
        chomp(@mainlines = `git config remote.origin.fetch`);
        $skipfetch{$project} = 1;
      }
      if (system("git", "fetch", "-f", "--prune", "origin", $ref.":refs/changes/".$number, @mainlines)) {
        printerr "GIT fetch of ".$ref." from ".$project." failed";
        return;
      }
      $verbose and print "===== ".strftime("%c", localtime(time()))." ===== fetched change\n";
    }

    for my $w (split(/ /, $WORKERS)) {
      my $worker = $config{'workers.'.$w};
      die "workers.$worker not set.\n" if (!defined($worker));
      $worker =~ s/\@PROJ\@/$project/g;
      $worker =~ s/\@SHA1\@/$rev/g;
      my $output;
      open VERDICT, $worker." 2>&1 |" or die "cannot run worker '$w': ".$!;
      {
        local $/;
        $output = <VERDICT>;
      }
      close VERDICT;
      while ($output =~ s/^([^\{][^\n]*\n)//s) {
        print STDERR "Non-JSON output from worker '$w': $1\n";
      }
      die "Worker '$w' for commit ".$ref." in ".$project." crashed with signal ".$?.".\n" if ($? & 127);
      die "Worker '$w' returned non-zero exit code for commit ".$ref." in ".$project.".\n" if ($?);
      my $vd = {};
      if (length($output) > 50000) {
        $$vd{message} = "**** Worker '$w' produced an unreasonable amount of output. You should ask the bot maintainers for advice.";
        push @invite, @MAINTAINERS;
      } else {
        $vd = decode_json($output);
        defined($vd) or die "cannot decode verdict from worker '$w' as JSON\n";
        if (defined($$vd{invite})) {
          push @invite, @{$$vd{invite}};
          delete $$vd{invite};
        }
      }
      merge_hashes($verdict, $vd);
    }
  }
  if (@invite) {
    if (system(@gerrit_actor, "set-reviewers", (map { ('-a', $_) } @invite), '--', $rev)) {
      print "===== ".strftime("%c", localtime(time()))." ===== invitation FAILED\n";
      printerr("Inviting reviewers to ".$rev." (".$project."/".$ref.") failed");
    } else {
      $verbose and print "Invited @invite to ".$rev." (".$project."/".$ref.")\n";
    }
  }
  eval {
    $gerrit_rest->POST("/changes/$number/revisions/$rev/review", $verdict);
  };
  if ($@) {
    print "===== ".strftime("%c", localtime(time()))." ===== verdict NOT submitted\n";
    print STDERR "Submission of REST verdict for ".$rev." (".$project."/".$ref.") failed: $@\n";
    return;
  }
  $verbose and print "Submitted verdict for ".$rev." (".$project."/".$ref.")\n";
}

sub do_move($$$)
{
  my ($chg, $author, $target) = @_;
  my $branch = $$chg{'branch'};
  if ($target eq $branch) {
    return "The change already targets $branch";
  }
  # FIXME: this doesn't work, as for some reason the event stream does not include the status.
  #my %allowed_status = ('NEW' => 1, 'DEFERRED' => 1, 'DRAFT' => 1, 'ABANDONED' => 1);
  #my $status = $$chg{'status'};
  #return "$status changes cannot be moved" if (!$allowed_status{$status});
  my $project = $$chg{'project'};
  my $id = $$chg{'id'};
  # First, check there's no such review on that branch already
  my $query = "project:$project change:$id branch:$target";
  open(my $qry, "-|", @gerrit, "query", "--format", "JSON", $query) or die "cannot run ssh: ".$!;
  while (<$qry>) {
    my $review = decode_json($_);
    defined($review) or die "cannot decode JSON string '".chomp($_)."'\n";
    if (my $url = $$review{'url'}) {
      return "Cannot move: Conflicting change $url - please ask a Gerrit admin for help.";
    }
  }
  close $qry;
  # Only allow the change owner to execute move
  if ($$author{'username'} ne $$chg{'owner'}{'username'}) {
    return "Only the owner is allowed to move a change";
  }
  my $number = $$chg{'number'};
  open(my $retarget, "gerrit_retarget_changes x $target $number 2>&1 |")
    or die "Cannot run gerrit_retarget_changes: ".$!;
  my @output = <$retarget>;
  return "Moved from $branch" if (close($retarget));
  return ($! ? "Failed to move ($!)\n" : "Move rejected ($?)\n") . join(' ', @output);
}

sub process_move($$$) {
  my ($chg, $author, $target) = @_;
  my $number = $$chg{'number'};
  $gerrit_rest->POST("/changes/$number/revisions/current/review",
                     { message => do_move($chg, $author, $target) });
}

$| = 1; # make STDOUT autoflush

open UPDATES, "-|", @gerrit, "stream-events" or die "cannot run ssh: ".$!;

# Try to ensure that the event streaming has started before we make the snapshot, to avoid a race.
# Of course, the first connect may be still delayed ...
sleep(1);

my @query = ("status:open");
push @query, "reviewer:".$USER_EMAIL if ($INVITE_ONLY);
open STATUS, "-|", @gerrit, "query", "--format", "JSON", "--current-patch-set", @query or die "cannot run ssh: ".$!;
REVIEW: while (<STATUS>) {
  my $review = decode_json($_);
  defined($review) or die "cannot decode JSON string '".chomp($_)."'\n";
  my $number = $$review{'number'};
  my $project = $$review{'project'};
  my $branch = $$review{'branch'};
  my $cps = $$review{'currentPatchSet'};
  if (defined $cps) {
    my $status = $$review{'status'};
    if ($status ne 'NEW' and $status ne 'DRAFT') {
       next REVIEW;
    }
    my $ref = $$cps{'ref'};
    my $revision = $$cps{'revision'};
    my $approvals = $$cps{'approvals'};
    if (defined $approvals) {
      foreach my $appr (@$approvals) {
        my $by = $$appr{'by'};
        defined $$by{'email'} or next;   # The reviewer may be gone and thus have no valid mail any more.
        if ($$by{'email'} eq $USER_EMAIL) {
          next REVIEW;
        }
      }
    }
    process_commit($number, $project, $branch, $ref, $revision);
  }
}
close STATUS;

while (<UPDATES>) {
  my $update = decode_json($_);
  defined($update) or die "cannot decode JSON string '".chomp($_)."'\n";
  my $type = $$update{'type'};
  if (defined($type)) {
    if ($type eq 'patchset-created') {
      my $chg = $$update{'change'};
      my $ps = $$update{'patchSet'};
      process_commit($$chg{'number'}, $$chg{'project'}, $$chg{'branch'}, $$ps{'ref'}, $$ps{'revision'});
    } elsif ($type eq 'ref-updated') {
      my $rup = $$update{'refUpdate'};
      delete $skipfetch{$$rup{'project'}};
    } elsif ($type eq 'comment-added') {
      my $cmd = $$update{'comment'};
      if ($cmd =~ /^(?:gerrit-)?bot:\h*(?:please\h+)?move\h+(?:back\h+)?to\h+(?:branch\h+)?([\w.\/-]*\w)\b/im) {
        process_move($$update{'change'}, $$update{'author'}, $1);
      }
    }
  }
}
close UPDATES;