summaryrefslogtreecommitdiffstats
path: root/extensions/Push/lib/Connector/Aha.pm
blob: b64610e039021e84058b84e3e554e2ed8648f00d (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
170
171
172
173
174
175
176
177
178
179
180
181
182
# 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::Push::Connector::Aha;

use strict;
use warnings;

use base 'Bugzilla::Extension::Push::Connector::Base';

use Bugzilla::Constants;
use Bugzilla::Extension::Push::Constants;
use Bugzilla::Extension::Push::Util;
use Bugzilla::Bug;
use Bugzilla::Attachment;
use Bugzilla::BugUrl::Aha;

use DateTime;
use JSON 'decode_json', 'encode_json';
use LWP::UserAgent;

BEGIN {
    unless (LWP::UserAgent->can("put")) {
        *LWP::UserAgent::put = sub {
            require HTTP::Request::Common;
            my($self, @parameters) = @_;
            my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1));
            return $self->request( HTTP::Request::Common::PUT( @parameters ), @suff );
        };
    }
}

sub options {
    return (
        {
            name     => 'account_domain',
            label    => 'Account domain for Aha',
            type     => 'string',
            default  => 'bugzilla.aha.io',
            required => 1,
        },
        {
            name     => 'account_realm',
            label    => 'Aha! auth realm',
            type     => 'string',
            default  => 'Aha! API',
            required => 1,
        },
        {
            name     => 'username',
            label    => 'Username',
            type     => 'string',
            default  => 'guest',
            required => 1,
        },
        {
            name     => 'password',
            label    => 'Password',
            type     => 'password',
            default  => 'guest',
            required => 1,
        },
    );
}

sub stop {
    my ($self) = @_;
}

sub should_send {
    my ($self, $message) = @_;

    if ($message->routing_key =~ /^bug\.modify:.*\bbug_status\b/) {
        my $payload = $message->payload_decoded();
        my $target  = $payload->{event}->{target};
        my $bug     = $payload->{$target};

        return $bug->{status}{name} eq 'RESOLVED' && $bug->{resolution} eq 'FIXED';
    }
    else {
        # We're not interested in the message.
        return 0;
    }
}

sub _user_agent {
    my ($self) = @_;

    my $ua = LWP::UserAgent->new(agent => 'Bugzilla');
    $ua->timeout(10);
    $ua->protocols_allowed(['http', 'https']);
    if (my $proxy_url = Bugzilla->params->{proxy_url}) {
        $ua->proxy(['http', 'https'], $proxy_url);
    }
    else {
        $ua->env_proxy();
    }

    $ua->credentials($self->config->{account_domain} . ":443",
                     $self->config->{account_realm},
                     $self->config->{username},
                     $self->config->{password});
    return $ua;
}

sub _aha_uri {
    my ($self, $path) = @_;

    return URI->new("https://" . $self->config->{account_domain} . "/" . $path);
}

sub _aha_feature_uri {
    my ($self, $feature_id) = @_;

    return $self->_aha_uri("api/v1/features/$feature_id");
}

sub _aha_update_feature {
    my ($self, $feature_id, $workflow_status) = @_;
    my $feature_uri = $self->_aha_feature_uri($feature_id);
    my $ua          = $self->_user_agent;
    my $content     = encode_json({ workflow_status => $workflow_status });
    my $resp        = $ua->put($feature_uri, 'Content-Type' => 'application/json', Content => $content);

    if ($resp->code != 200) {
        die "Expected HTTP 200 resposne, got " . $resp->code;
    }
}

sub _aha_get_feature {
    my ($self, $feature_id) = @_;
    my $feature_uri = $self->_aha_feature_uri($feature_id);
    my $resp        = $self->_user_agent->get($feature_uri);

    if ($resp->code == 200) {
        my $result = eval { decode_json($resp->content) };
        if ($@) {
            die "Unable to parse JSON";
        }
        return $result;
    }
    else {
        die "Expected HTTP 200 resposne, got " . $resp->code;
    }
}

sub send {
    my ($self, $message) = @_;
    my $logger = Bugzilla->push_ext->logger;
    my $config = $self->config;

    eval {
        my $payload = $message->payload_decoded();
        my $target  = $payload->{event}->{target};
        my $bug     = Bugzilla::Bug->check($payload->{$target}->{id});
        foreach my $see_also (@{ $bug->see_also }) {
            if ($see_also->isa('Bugzilla::BugUrl::Aha')) {
                my $feature_id = $see_also->get_feature_id;
                my $feature = $self->_aha_get_feature($feature_id);
                if ($feature->{error}) {
                    next;
                }

                unless (lc($feature->{feature}{workflow_status}{name}) eq 'shipped') {
                    $self->_aha_update_feature($feature_id, "Ready to ship");
                }
            }
        }
    };
    if ($@) {
        return (PUSH_RESULT_TRANSIENT, clean_error($@));
    }

    return PUSH_RESULT_OK;
}


1;