line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Ukigumo::Agent::Dispatcher; |
2
|
11
|
|
|
11
|
|
54
|
use strict; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
341
|
|
3
|
11
|
|
|
11
|
|
52
|
use warnings; |
|
11
|
|
|
|
|
20
|
|
|
11
|
|
|
|
|
325
|
|
4
|
11
|
|
|
11
|
|
103
|
use utf8; |
|
11
|
|
|
|
|
16
|
|
|
11
|
|
|
|
|
159
|
|
5
|
|
|
|
|
|
|
|
6
|
11
|
|
|
11
|
|
8891
|
use Amon2::Web::Dispatcher::RouterBoom; |
|
11
|
|
|
|
|
83324
|
|
|
11
|
|
|
|
|
123
|
|
7
|
11
|
|
|
11
|
|
7463
|
use Ukigumo::Agent::Manager; |
|
11
|
|
|
|
|
33
|
|
|
11
|
|
|
|
|
370
|
|
8
|
11
|
|
|
11
|
|
11433
|
use Data::Validator; |
|
11
|
|
|
|
|
26974
|
|
|
11
|
|
|
|
|
343
|
|
9
|
11
|
|
|
11
|
|
74
|
use JSON; |
|
11
|
|
|
|
|
18
|
|
|
11
|
|
|
|
|
92
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
get '/' => sub { |
12
|
|
|
|
|
|
|
my $c = shift; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$c->render( |
15
|
|
|
|
|
|
|
'index.tt' => { |
16
|
|
|
|
|
|
|
children => $c->manager->children, |
17
|
|
|
|
|
|
|
job_queue => $c->manager->job_queue, |
18
|
|
|
|
|
|
|
server_url => $c->manager->server_url, |
19
|
|
|
|
|
|
|
work_dir => $c->manager->work_dir, |
20
|
|
|
|
|
|
|
max_children => $c->manager->max_children, |
21
|
|
|
|
|
|
|
timeout => $c->manager->timeout, |
22
|
|
|
|
|
|
|
ignore_github_tags => $c->manager->ignore_github_tags, |
23
|
|
|
|
|
|
|
force_git_url => $c->manager->force_git_url, |
24
|
|
|
|
|
|
|
cleanup_cycle => $c->manager->cleanup_cycle, |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $rule = Data::Validator->new( |
30
|
|
|
|
|
|
|
repository => { isa => 'Str' }, |
31
|
|
|
|
|
|
|
branch => { isa => 'Str' }, |
32
|
|
|
|
|
|
|
)->with('NoThrow'); |
33
|
|
|
|
|
|
|
post '/api/v0/enqueue' => sub { |
34
|
|
|
|
|
|
|
my $c = shift; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $args = $rule->validate(+{%{$c->req->parameters}}); |
37
|
|
|
|
|
|
|
if ($rule->has_errors) { |
38
|
|
|
|
|
|
|
my $errors = $rule->clear_errors(); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $res = $c->render_json({errors => $errors}); |
41
|
|
|
|
|
|
|
$res->code(400); |
42
|
|
|
|
|
|
|
return $res; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$c->manager->register_job($args); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
return $c->render_json(+{}); |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
post '/api/github_hook' => sub { |
51
|
|
|
|
|
|
|
my $c = shift; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$c->logger->infof("payload: %s", $c->req->param('payload')); |
54
|
|
|
|
|
|
|
my $payload = from_json $c->req->param('payload'); |
55
|
|
|
|
|
|
|
my $args; |
56
|
|
|
|
|
|
|
if (!$payload->{deleted} and my $ref = $payload->{ref}) { |
57
|
|
|
|
|
|
|
eval { |
58
|
|
|
|
|
|
|
# TODO How to pass commit id? |
59
|
|
|
|
|
|
|
# my @commits = @{$payload->{commits}}; |
60
|
|
|
|
|
|
|
# ... |
61
|
|
|
|
|
|
|
# commit => $commits[$#commits]->{id}, |
62
|
|
|
|
|
|
|
my $repo_url = $payload->{repository}->{url}; |
63
|
|
|
|
|
|
|
if ($c->manager->force_git_url) { |
64
|
|
|
|
|
|
|
# From: https://github.com/tokuhirom/plenv.git |
65
|
|
|
|
|
|
|
# To: git@github.com:tokuhirom/plenv.git |
66
|
|
|
|
|
|
|
$repo_url =~ s!\Ahttps?://([^/]+)/!git\@$1:!; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $tag; |
70
|
|
|
|
|
|
|
my $branch; |
71
|
|
|
|
|
|
|
if ($ref =~ s!\Arefs/heads/!!) { |
72
|
|
|
|
|
|
|
$branch = $ref; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
elsif ($ref =~ s!\Arefs/tags/!! && !$c->manager->ignore_github_tags) { |
75
|
|
|
|
|
|
|
$tag = $ref; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
if ($branch || $tag) { |
79
|
|
|
|
|
|
|
$args = +{ |
80
|
|
|
|
|
|
|
repository => $repo_url, |
81
|
|
|
|
|
|
|
branch => $branch || $tag, |
82
|
|
|
|
|
|
|
compare_url => $payload->{compare} || '', |
83
|
|
|
|
|
|
|
repository_owner => $payload->{repository}->{owner}->{name} || '', |
84
|
|
|
|
|
|
|
repository_name => $payload->{repository}->{name} || '', |
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
}; |
88
|
|
|
|
|
|
|
if (my $e = $@) { |
89
|
|
|
|
|
|
|
$c->logger->warnf("An error occured: %s", $e); |
90
|
|
|
|
|
|
|
my $res = $c->render_json({errors => $e}); |
91
|
|
|
|
|
|
|
$res->code(400); |
92
|
|
|
|
|
|
|
return $res; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
if ($args) { |
96
|
|
|
|
|
|
|
$c->manager->register_job($args); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
return $c->render_json($args || +{}); |
100
|
|
|
|
|
|
|
}; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |