about summary refs log tree commit diff
path: root/scripts/builder.pl
blob: 646037ab180e5648bdef5d241bcc438e65a2b189 (plain) (blame)
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
#!/usr/bin/env perl

use strict;
use warnings;

use JSON;
use YAML qw/LoadFile Dump/;
use File::Spec;
use File::Path qw/rmtree/;
use File::Basename;
use Git::Repository;
use jitterbug::Schema;

$|++;

my $conf      = LoadFile('config.yml');
my $dbix_conf = $conf->{plugins}->{DBIC}->{schema};
my $schema =
  jitterbug::Schema->connect( $dbix_conf->{dsn}, $dbix_conf->{user},
    $dbix_conf->{pass} );

while (1) {
    my $task = $schema->resultset('Task')->search()->single();

    unless ($task) {
        sleep 5;
        next;
    }

    my $desc    = JSON::decode_json($task->commit->content);
    $desc->{build}->{start_time} = time();

    my $report_path = File::Spec->catdir( $conf->{jitterbug}->{reports}->{dir},
        $task->project->name, $task->commit->sha256 );
    my $build_dir = File::Spec->catdir( $conf->{jitterbug}->{build}->{dir},
        $task->project->name );

    my $repo    = $task->project->url . '.git';
    my $r = Git::Repository->create( clone => $repo => $build_dir );
    $r->run( 'checkout', $task->commit->sha256 );

    my $builder = $conf->{jitterbug}->{build_process}->{builder};
    my $res     = `$builder $build_dir $report_path`;

    rmtree($build_dir);

    $desc->{build}->{end_time} = time();

    my @versions = glob( $report_path . '/*' );
    foreach my $version (@versions) {
        open my $fh, '<', $version;
        my ($result, $lines);
        while (<$fh>){
            $lines .= $_;
        }
        ($result) = $lines =~ /^Result:\s(.*)$/;
        my ( $name, ) = basename($version);
        $name =~ s/\.txt//;
        if ( !$result || ($result && $result !~ /PASS/ )) {
            # mail author of the commit
            $result = "FAIL";
            my $message  = $desc->{message};
            my $commiter = $desc->{author}->{email};
            my $output   = "Build failed";
            my $sha      = $desc->{id};
            my $on_failure =
                $conf->{jitterbug}->{build_process}->{on_failure};
            `$on_failure $commiter $message $output $sha`;
        }
        $desc->{build}->{version}->{$name} = $result;
        close $fh;
    }

    $task->commit->update({
        content => JSON::encode_json($desc),
    });
    $task->delete();
    warn "done\n";
    sleep 5;
}