From 21ec987901a927793d7baa9db3fd37d0216aef06 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Mon, 8 Jan 2018 16:03:29 +0200 Subject: gerrit-bot: Validate change status on move Change-Id: Ideca76f81632a4d886193b8a8529aa9682b84066 Reviewed-by: Oswald Buddenhagen --- git-hooks/gerrit-bot | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/git-hooks/gerrit-bot b/git-hooks/gerrit-bot index 8a78b7b..a659c73 100755 --- a/git-hooks/gerrit-bot +++ b/git-hooks/gerrit-bot @@ -247,19 +247,22 @@ sub do_move($$$) if ($target eq $branch) { return "The change already targets $branch"; } + 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 $status, "-|", @gerrit, "query", "--format", "JSON", $query) or die "cannot run ssh: ".$!; - while (<$status>) { + 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 $status; + 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"; -- cgit v1.2.3