summary refs log tree commit diff
path: root/scripts/builder.pl
blob: e8fa87d062702d68a84c32c13148ace021b96442 (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
81
82
83
84
85
86
87
88
89
#!/usr/bin/env perl

use strict;
use warnings;

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

$|++;

my $conf  = LoadFile('config.yml');
my $redis = Redis->new( server => $conf->{redis} );
my $key   = join( ':', 'jitterbug', 'tasks' );

while (1) {
    my $task_key = $redis->spop($key);
    if ($task_key) {
        my $task    = $redis->get($task_key);
        my $desc    = JSON::decode_json($task);
        my $repo    = $desc->{repo} . '.git';
        my $commit  = delete $desc->{id};
        my $project = delete $desc->{project};

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

        my $build_dir =
          File::Spec->catdir( $conf->{jitterbug}->{build}->{dir}, $project );

        my $r = Git::Repository->create( clone => $repo => $build_dir );
        $r->run( 'checkout', $commit );

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

        rmtree($build_dir);

        $redis->del($task_key);

        my $build = {
            project => $project,
            repo    => $repo,
            commit  => $commit,
            time    => time(),
            %$desc,
        };

        my @versions = glob( $report_path . '/*' );
        foreach my $version (@versions) {
            open my $fh, '<', $version;
            my @lines  = <$fh>;
            my $result = pop @lines;
            while ( $result !~ /^Result/ ) {
                $result = pop @lines;
            }
            chomp $result;
            $result =~ s/Result:\s//;
            my ( $name, ) = basename($version);
            $name =~ s/\.txt//;
            if ( $result !~ /PASS/ ) {

                # mail author of the commit
                my $message  = $desc->{message};
                my $commiter = $desc->{author}->{email};
                my $output   = "Build failed";
                my $sha      = $desc->{commit};
                my $on_failure =
                  $conf->{jitterbug}->{build_process}->{on_failure};
                `$on_failure $commiter $message $output $sha`;
            }
            $build->{version}->{$name} = $result;
            close $fh;
        }

        my $build_key = join( ':', 'jitterbug', 'build', $commit );
        $redis->set( $build_key, JSON::encode_json($build) );

        my $project_build = join( ':', 'jitterbug', 'builds', $project );
        $redis->sadd( $project_build, $build_key );
        warn "done, next\n";
    }
    sleep 5;
}