todo.org: Added.
[Fumo.git] / lib / Fumo / Import.pm
blobd7af036816449e778f04943ca57dfae9932a7570
1 package Fumo::Import;
3 use strict;
4 use warnings;
6 use Data::Dumper;
7 use Fumo::Import::DataSchema qw(data_schema);
9 =head1 NAME
11 Fumo::Controller::Revision - Catalyst Controller
13 =head1 DESCRIPTION
15 Catalyst Controller representing a revision (changeset).
17 =head1 METHODS
19 =head2 new
21 Initialises a new Fumo::Import object.
23 =cut
25 sub new {
26 my ($class) = @_;
27 my $self = { };
29 bless($self, $class);
31 return $self;
34 =head2 do_import
36 =over 4
38 =item Arguments: $data, $schema, $project_name, $branch_name, $revision_name
40 =item Return Value: $schema
42 =back
44 This method will import $data into the context of the *_name arguments.
46 =cut
48 sub do_import {
49 my ($self, $data, $schema, $project_name, $branch_name,
50 $revision_name) = @_;
52 # is the data valid?
53 $self->_validate($data);
55 $schema->txn_do(
56 sub {
57 my $branch_rs = $schema->resultset('Project')->find_or_create(
58 name => $project_name
59 )->branches->find_or_create(
60 name => $branch_name,
61 project => $project_name
64 my $revision_rs = $branch_rs->revisions->find_or_create(
65 name => $revision_name,
66 project => $project_name,
67 branch => $branch_name
70 #$revision_rs->files->delete_all;
72 # these are stored for the revision pass_rate
73 my ($total_tests_passed, $total_test_count);
75 # import the files
76 for my $file (@{$data->{files}}) {
77 my ($file_tests_passed, $file_test_count);
79 # zero count to avoid DBIC error for not-null column
80 $file_test_count = 0;
82 my $file_rs = $revision_rs->files->find_or_create({
83 name => $file->{name},
84 project => $project_name,
85 branch => $branch_name,
86 test_count => $file_test_count,
87 });
89 # import the tests within a file
90 for my $test (@{$file->{tests}}) {
91 $file_test_count++;
93 if ($test->{directive} or $test->{status} eq 'ok') {
94 $file_tests_passed++;
97 my $test_rs = $file_rs->tests->find_or_create({
98 name => $test->{name},
99 number => $test->{number},
100 project => $project_name,
101 branch => $branch_name,
102 revision => $revision_name,
103 status => $test->{status},
104 directive => ($test->{directive} or undef)
108 $total_tests_passed += $file_tests_passed;
109 $total_test_count += $file_test_count;
111 # update the file level pass rate
112 if ($file_tests_passed) {
113 my $passed = ($file_tests_passed * 100) / $file_test_count;
114 $file_rs->pass_rate($passed);
116 $file_rs->update();
120 if ($total_tests_passed) {
121 my $passed = ($total_tests_passed * 100) / $total_test_count;
122 $revision_rs->pass_rate($passed);
124 $revision_rs->update();
129 return $schema;
132 =head2 _validate
134 =over 4
136 =item Arguments: $data
138 =item Return Value: none
140 =back
142 This method will validate the data passed in as $data and throw an
143 exception of the errors if any.
145 =cut
147 sub _validate {
148 my ($self, $data) = @_;
150 my @status = ('ok', 'not ok');
151 my @directive = ('SKIP', 'TODO', '');
153 my $domain = data_schema(\@status, \@directive);
155 my $errors = $domain->inspect($data);
156 die Dumper $errors if $errors;