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

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

use File::Spec;

set serializer => 'JSON';

get '/build/:project/:commit/:version' => sub {
    my $project = params->{project};
    my $commit  = params->{commit};
    my $version = params->{version};

    my $conf = setting 'jitterbug';

    my $file = File::Spec->catfile( $conf->{reports}->{dir},
        $project, $commit, $version . '.txt' );

    if ( -f $file ) {
        open my $fh, '<', $file;
        my @content = <$fh>;
        close $fh;

        if ( request->accept =~ m!application/json! ) {
            return {
                commit  => $commit,
                version => $version,
                content => join( '', @content ),
            };
        }
        else {
            content_type 'text/plain';
            return join( '', @content );
        }
    }
};

del '/task/:id' => sub {
    my $id = params->{id};

    my $task = schema->resultset('Task')->find({sha256 => $id});

    if (!$task){
        send_error("Can't find task for $id", 404);
        return;
    }

    $task->delete;
    status(201);
    {status => "task $id deleted"};
};

get '/tasks' => sub {
    my $tasks = schema->resultset('Task')->search();

    my $content;

    # I think we should never use internal ID when there is a sha256 available
    while ( my $task = $tasks->next ) {
        push @$content,
          {
            id           => $task->sha256,
            running      => $task->running,
            started_when => $task->started_when,
            project      => {
                id   => $task->projectid,
                name => $task->project->name,
            },
            commit => from_json($task->commit->content),
          };
    }

    {tasks => $content};
};

1;