summary refs log tree commit diff
path: root/lib/jitterbug/Hook.pm
blob: fe27921ae55d91f1cc3b2205dd01ea96f7fd53ef (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
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
package jitterbug::Hook;

use Dancer ':syntax';
use Dancer::Plugin::DBIC;

use Try::Tiny;

setting serializer => 'JSON';

post '/' => sub {
    my $payload = params->{payload};

    # don't confuse poster, and don't care about it
    if (!defined $payload) {
        error("no payload in input");
        status 200;
        return;
    }

    $payload = from_json($payload);
    my $repo = $payload->{repository}->{name};
    my $ref  = $payload->{ref};

    if ( !_authorized_branch( $repo, $ref ) ) {
        debug("this branch is not authorized");
        status 200;
        return;
    }

    my $project = schema->resultset('Project')->find( { name => $repo } );
    $project = _create_new_project( $repo, $payload ) if !$project;

    if ( !_slot_available_for_task( $project->id ) ) {
        debug("task already present for this project");
        status 200;
        return;
    }

    my $last_commit = pop @{ $payload->{commits} };
    $last_commit->{compare} = $payload->{compare};
    $last_commit->{pusher}  = $payload->{pushed};
    $last_commit->{ref}     = $payload->{ref};

    _insert_commit($last_commit, $project);
    _insert_new_task( $last_commit, $project );

    debug("hook accepted");

    { updated => $repo };
};

sub _authorized_branch {
    my ($repo, $ref) = @_;
    my $jtbg_conf     = setting 'jitterbug';
    my $branches_conf = $jtbg_conf->{branches};

    foreach my $name ($repo, 'jt_global') {
        if ( defined $branches_conf->{$name} ) {
            return 0 if _should_skip( $ref, $branches_conf->{$name} );
        }
    }
    return 1;
}

sub _should_skip {
    my ( $ref, $conf ) = @_;
    foreach my $br_name (@$conf) {
        return 1 if $ref =~ m!^refs/heads/$br_name!;
    }
    return 0;
}

sub _create_new_project {
    my ($repo, $payload) = @_;

    debug("need to create a new project");

    my $project;
    try {
        schema->txn_do(
            sub {
                $project = schema->resultset('Project')->create(
                    {
                        name        => $repo,
                        url         => $payload->{repository}->{url},
                        description => $payload->{repository}->{description},
                        owner => to_json( $payload->{repository}->{owner} ),
                    }
                );
            }
        );
    }
    catch {
        error($_);
    };
    return $project;
}

sub _slot_available_for_task {
    my $project_id = shift;

    # is there already a task for this project, and could we stack ?
    my $jtbg_settings = setting('jitterbug') || {};
    my $stack_option = $jtbg_settings->{options}->{stack_tasks};
    my $total_task =
      schema->resultset('Task')->search( { projectid => $project_id } )->count;

    ( $total_task && !$stack_option) ? return 0 : return 1;
}

sub _insert_commit {
    my ($commit, $project) = @_;

    try {
        schema->txn_do(
            sub {
                schema->resultset('Commit')->create(
                    {
                        sha256    => $commit->{id},
                        content   => to_json($commit),
                        projectid => $project->projectid,
                        timestamp => $commit->{timestamp},
                    }
                );
            }
        );
    }
    catch {
        error($_);
    };
}

sub _insert_new_task {
    my ( $commit, $project ) = @_;
    try {
        schema->txn_do(
            sub {
                schema->resultset('Task')->create(
                    {
                        sha256    => $commit->{id},
                        projectid => $project->projectid
                    }
                );
            }
        );
    }
    catch {
        error($_);
    };
}

1;