summaryrefslogtreecommitdiffstats
path: root/extensions/TryAutoLand/lib/WebService.pm
blob: 3ccb246d6456c8bacd7d2a5a49aaffcaf325bca4 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::TryAutoLand::WebService;

use strict;
use warnings;

use base qw(Bugzilla::WebService);

use Bugzilla::Error;
use Bugzilla::Util qw(trick_taint);

use Bugzilla::Extension::TryAutoLand::Constants;

use constant READ_ONLY => qw(
    getBugs 
);

# TryAutoLand.getBugs
# Params: status - List of statuses to filter attachments (only 'waiting' is default)
# Returns: List of bugs, each being a hash of data needed by the AutoLand polling server
# Params
# [ { bug_id => $bug_id1, attachments => [ $attach_id1, $attach_id2 ] }, branches => $branchListFromTextField ... ]

sub getBugs { 
    my ($self, $params) = @_;
    my $user = Bugzilla->user;
    my $dbh  = Bugzilla->dbh;
    my %bugs;

    if ($user->login ne WEBSERVICE_USER) {
        ThrowUserError("auth_failure", { action => "access",
                                         object => "autoland_attachments" });
    }

    my $status_where  = "AND status = 'waiting'";
    my $status_values = [];
    if (exists $params->{'status'}) {
        my $statuses = ref $params->{'status'}
                       ? $params->{'status'}
                       : [ $params->{'status'} ];
        foreach my $status (@$statuses) {
            if (grep($_ eq $status, VALID_STATUSES)) {
                trick_taint($status);
                push(@$status_values, $status);
            }
        }
        if (@$status_values) {
            my @qmarks = ("?") x @$status_values;
            $status_where = "AND " . $dbh->sql_in('status', \@qmarks);
        }
        
    }

    my $attachments = $dbh->selectall_arrayref("
        SELECT attachments.bug_id, 
               attachments.attach_id, 
               autoland_attachments.who, 
               autoland_attachments.status,
               autoland_attachments.status_when 
          FROM attachments, autoland_attachments 
         WHERE attachments.attach_id = autoland_attachments.attach_id
               $status_where
      ORDER BY attachments.bug_id",
        undef, @$status_values);

    foreach my $row (@$attachments) {
        my ($bug_id, $attach_id, $al_who, $al_status, $al_status_when) = @$row;

        my $al_user = Bugzilla::User->new($al_who);

        # Silent Permission checks
        next if !$user->can_see_bug($bug_id);
        my $attachment = Bugzilla::Attachment->new($attach_id);
        next if !$attachment 
                || $attachment->isobsolete 
                || ($attachment->isprivate && !$user->is_insider);

        $bugs{$bug_id} = {} if !exists $bugs{$bug_id};

        if (!$bugs{$bug_id}{'branches'}) {
            my $bug_result = $dbh->selectrow_hashref("SELECT branches, try_syntax
                                                        FROM autoland_branches 
                                                       WHERE bug_id = ?", 
                                                     undef, $bug_id);
            $bugs{$bug_id}{'branches'}   = $bug_result->{'branches'};
            $bugs{$bug_id}{'try_syntax'} = $bug_result->{'try_syntax'};
        }
      
        $bugs{$bug_id}{'attachments'} = [] if !exists $bugs{$bug_id}{'attachments'};

        push(@{$bugs{$bug_id}{'attachments'}}, {
            id          => $self->type('int', $attach_id),  
            who         => $self->type('string', $al_user->login), 
            status      => $self->type('string', $al_status), 
            status_when => $self->type('dateTime', $al_status_when), 
        });
    }

    return [ 
        map 
        { { bug_id => $_, attachments => $bugs{$_}{'attachments'}, 
            branches => $bugs{$_}{'branches'}, try_syntax => $bugs{$_}{'try_syntax'} } }
        keys %bugs 
    ];
}

# TryAutoLand.updateStatus({ attach_id => $attach_id, status => $status })
# Let's BMO know if a patch has landed or not and BMO will update the auto_land table accordingly
# $status will be a predetermined set of pending/complete codes -- when pending, the UI for submitting 
# autoland will be locked and once complete status update occurs the UI can be unlocked and this entry 
# can be removed from tracking by WebService API 
# Allowed statuses: waiting, running, failed, or success

sub updateStatus { 
    my ($self, $params) = @_;
    my $user = Bugzilla->user;
    my $dbh  = Bugzilla->dbh;

    if ($user->login ne WEBSERVICE_USER) {
        ThrowUserError("auth_failure", { action => "modify",
                                         object => "autoland_attachments" });
    }

    foreach my $param ('attach_id', 'status') {
        defined $params->{$param}
            || ThrowUserError('param_required', 
                              { param => $param });
    }

    my $attach_id = delete $params->{'attach_id'};
    my $status    = delete $params->{'status'};
 
    my $attachment = Bugzilla::Attachment->new($attach_id);
    $attachment
        || ThrowUserError('autoland_invalid_attach_id',
                          { attach_id => $attach_id });
   
    # Loud Permission checks
    if (!$user->can_see_bug($attachment->bug_id)) {
        ThrowUserError("bug_access_denied", { bug_id => $attachment->bug_id });
    }
    if ($attachment->isprivate && !$user->is_insider) {
        ThrowUserError('auth_failure', { action    => 'access',
                                         object    => 'attachment',
                                         attach_id => $attachment->id });
    }

    $attachment->autoland_checked 
        || ThrowUserError('autoland_invalid_attach_id',
                          { attach_id => $attach_id });

    # Update the status
    $attachment->autoland_update_status($status);

    return { 
        id          => $self->type('int', $attachment->id),
        who         => $self->type('string', $attachment->autoland_who->login),
        status      => $self->type('string', $attachment->autoland_status),
        status_when => $self->type('dateTime', $attachment->autoland_status_when),
    };
}

1;