Skip to content

Commit

Permalink
fix issue #6394: capture git stderr/stdout
Browse files Browse the repository at this point in the history
Adds new dependency: perl(IPC::Run::Debug)
  • Loading branch information
asdil12 committed Aug 24, 2015
1 parent c7accde commit 5b8ae14
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
1 change: 1 addition & 0 deletions DEPENDENCIES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ FindBin
Getopt::Long
IO::Socket::INET6
IO::Socket::SSL
IPC::Run::Debug
JSON
LWP::UserAgent
List::MoreUtils
Expand Down
34 changes: 34 additions & 0 deletions lib/OpenQA/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use strict;
require 5.002;

use Carp;
use IPC::Run();

require Exporter;
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
Expand All @@ -20,6 +21,7 @@ $VERSION = sprintf "%d.%03d", q$Revision: 1.12 $ =~ /(\d+)/g;
&file_content
&log_debug
&save_base64_png
&run_cmd_with_log
);


Expand Down Expand Up @@ -114,6 +116,21 @@ sub log_debug {
$app->log->debug(shift) if $app && $app->log;
}

sub log_info {
# useful for models, but doesn't work in tests
$app->log->info(shift) if $app && $app->log;
}

sub log_warning {
# useful for models, but doesn't work in tests
$app->log->warning(shift) if $app && $app->log;
}

sub log_error {
# useful for models, but doesn't work in tests
$app->log->error(shift) if $app && $app->log;
}

sub save_base64_png($$$) {
my ($dir, $newfile, $png) = @_;
return unless $newfile;
Expand All @@ -135,5 +152,22 @@ sub image_md5_filename($) {
return ($imagesdir . "/$prefix/$md5.png", $imagesdir . "/$prefix/.thumbs/$md5.png");
}

sub run_cmd_with_log($) {
my ($cmd) = @_;
my ($stdin, $stdout_err, $ret);
log_info("Running cmd: " . join(' ', @$cmd));
$ret = IPC::Run::run($cmd, \$stdin, '>&', \$stdout_err);
chomp $stdout_err;
if ($ret) {
log_debug($stdout_err);
log_info("cmd returned 0");
}
else {
log_warning($stdout_err);
log_error("cmd returned non-zero value");
}
return $ret;
}

1;
# vim: set sw=4 et:
11 changes: 6 additions & 5 deletions lib/OpenQA/WebAPI/Controller/Step.pm
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,17 @@ sub _commit_git {
}
my @git = ('git', '--git-dir', "$dir/.git", '--work-tree', $dir);
my @files = ($dir . '/' . $name . '.json', $dir . '/' . $name . '.png');
if (system(@git, 'add', @files) != 0) {

unless (run_cmd_with_log([@git, 'add', @files])) {
die "failed to git add $name";
}
my @cmd = (@git, 'commit', '-q', '-m', sprintf("%s for %s", $name, $job->name), sprintf('--author=%s <%s>', $self->current_user->fullname, $self->current_user->email), @files);
$self->app->log->debug(join(' ', @cmd));
if (system(@cmd) != 0) {

unless (run_cmd_with_log([@git, 'commit', '-q', '-m', sprintf("%s for %s", $name, $job->name), sprintf('--author=%s <%s>', $self->current_user->fullname, $self->current_user->email), @files])) {
die "failed to git commit $name";
}

if (($self->app->config->{'scm git'}->{'do_push'} || '') eq 'yes') {
if (system(@git, 'push') != 0) {
unless (run_cmd_with_log([@git, 'push'])) {
die "failed to git push $name";
}
}
Expand Down
30 changes: 30 additions & 0 deletions t/16-utils-runcmd.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env perl -w

# Copyright (C) 2014 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

BEGIN {
unshift @INC, 'lib';
}

use strict;
use OpenQA::Utils;
use Test::More;

ok(run_cmd_with_log(['echo', 'Hallo', 'Welt']));
is(run_cmd_with_log(['false']), "");

done_testing();

0 comments on commit 5b8ae14

Please sign in to comment.