blib/lib/Ukigumo/Server/API/Dispatcher.pm

filter: Statementstmt 42.6% 29 / 68 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 8 / 14 Podpod 0.0% 0 / 4 totaltotal 37.0% 37 / 100 CC 8 SCAR 24.6
CC
8
Cov
35%
dispatch
30.0
__ANON__
20.0
rule
2.0
CRAP 11.7
« blib/lib/Ukigumo/Server/Web.pm ↑ Summary blib/lib/Ukigumo/Server/Command/Report.pm »
1 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use 5.0100000;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Ukigumo::Server::API::Dispatcher;
7 11 ▶use Router::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Data::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 my $router = Router::Simple->new();
11  
12 0 ▶sub rule { Ukigumo::Server::Validator->new(@_) }
Subroutine rule: not called CC=1 SCAR=6.9
Pod undocumented
13 sub post {
14 22 ▶ my ($path, $rule_src, $code) = @_;
Subroutine post: called CC=1 SCAR=0.0
Pod undocumented
15 22 my $rule = Data::Validator->new(@$rule_src)->with('NoThrow');
16 22 $router->connect($path, +{ code => $code, rule => $rule }, {method => 'POST'})
17 }
18 sub get {
19 22 ▶ my ($path, $rule_src, $code) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
20 22 my $rule = Data::Validator->new(@$rule_src)->with('NoThrow');
21 22 $router->connect($path, +{ code => $code, rule => $rule }, {method => 'GET'})
22 }
23  
24 get '/api/v1/branch/list' => [
25 project => { isa => 'Str' },
26 ] => sub {
27 0 ▶ my ($c, $args) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
28  
29 return +{
30 0 branches => scalar(Ukigumo::Server::Command::Branch->list(
31 %$args
32 ))
33 };
34 11 };
35  
36 post '/api/v1/branch/delete' => [
37 project => { isa => 'Str' },
38 branch => { isa => 'Str' },
39 ] => sub {
40 0 ▶ my ($c, $args) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
41  
42 0 my $branch_id = Ukigumo::Server::Command::Branch->find(
43 %$args
44 );
45 0 Ukigumo::Server::Command::Branch->delete(
46 branch_id => $branch_id
47 );
48  
49 0 return { };
50 11 };
51  
52 get '/api/v1/report/search' => [
53 project => { isa => 'Str' },
54 branch => { isa => 'Str' },
55 revision => { isa => 'Str' },
56 limit => { isa => 'Int', default => 20 },
57 ] => sub {
58 0 ▶ my ($c, $args) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
59  
60 # and insert it
61 0 my $reports = Ukigumo::Server::Command::Report->search(
62 %$args
63 );
64  
65 return {
66 reports => +[
67 0 map { +{
68 report_id => $_->{report_id},
69 status => $_->{status},
70 ctime => $_->{ctime},
71 0 } } @$reports
72 ]
73 };
74 11 };
75  
76 post '/api/v1/report/add' => [
77 status => { isa => 'Int' },
78 project => { isa => 'Str' },
79 branch => { isa => 'Str' },
80 vc_log => { isa => 'Str', optional => 1 },
81 body => { isa => 'Str', optional => 1 },
82 revision => { isa => 'Str' },
83 repo => { isa => 'Str' },
84 compare_url => { isa => 'Str', optional => 1 },
85 elapsed_time_sec => { isa => 'Int', optional => 1 },
86 ] => sub {
87 0 ▶ my ($c, $args) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
88  
89 0 my $body;
90 0 ▶ if (my $upload = $c->req->upload('body')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $upload = $c->req->upload("body")) 0 0 0
91 0 my $fname = $upload->path;
92 0 ▶ open my $fh, '<:encoding(utf-8)', $fname or die "Cannot open file: $fname: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf-8)", $fname or die "Cannot open file: ${fname}: $!" 0 0 0
93 0 $body = do { local $/; <$fh> };
94 }
95  
96 # get last report status
97 my $last_status = Ukigumo::Server::Command::Report->get_last_status(
98 project => $args->{project},
99 branch => $args->{branch}
100 0 );
101  
102 # and insert it
103 0 ▶ my $report_id = Ukigumo::Server::Command::Report->insert(
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 0 0 0
104 (defined $body ? (body => $body) : () ),
105 %$args
106 );
107 0 my $url = Ukigumo::Server::Command::Report->get_url($report_id);
108  
109 return {
110 0 report => +{
111 report_id => $report_id,
112 url => $url,
113 last_status => $last_status,
114 }
115 };
116 11 };
117  
118 sub dispatch {
119 0 ▶ my ($class, $c) = @_;
Subroutine dispatch: not called CC=5 SCAR=34.0
Pod undocumented
120  
121 0 ▶ if (my $controller = $router->match($c->req->env)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $controller = $router->match($c->req->env)) { } 0 0 0
122 0 ▶ my $rule = $controller->{rule} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $rule = $controller->{"rule"} or die 0 0 0
123 0 my $args = $rule->validate($c->req->parameters->flatten);
124 0 ▶ if ($rule->has_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->has_errors) 0 0 0
125 0 my $errors = $rule->clear_errors;
126 0 my $message = join("\n", map { $_->{message} } @$errors);
127 0 my $res = $c->render_json( +{ error => +{ message => $message } } );
128 0 $res->code(400);
129 0 return $res;
130 }
131 0 my $res = $controller->{code}->($c, $args);
132 0 ▶ if (ref $res eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq 'HASH') { } 0 0 0
133 0 return $c->render_json($res); # succeeded
134 } else {
135 0 return $res; # succeeded
136 }
137 } else {
138 0 return $c->res_404(); # not found...
139 }
140 }
141  
142 11 1;
143  
« blib/lib/Ukigumo/Server/Web.pm ↑ Summary blib/lib/Ukigumo/Server/Command/Report.pm »