lib/CrashTest/Plugin/BugLink/Redmine.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Sun, 04 Dec 2016 01:16:39 +0100
changeset 120 1a0fa98037fa
child 122 8692800ec9ba
permissions -rw-r--r--
Add Bug status table in the detailed report. Initial support for Bugzilla and Redmine

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package CrashTest::Plugin::BugLink::Redmine;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Util qw/dumper/;

use Mojo::UserAgent;

has [ qw/app config/ ];
has [ qw/_redmine_url _ua/ ];

sub register {
    my ($self, $app, $args) = @_;

    $self->app($app);
    $self->config($args->{config});

    $app->log->debug("Registered BugLink::Redmine");

}

sub get_statuses {
    my ($self, $bug_keys) = @_;

    my $statuses = {};

    # Seems there is not bulk API in redmine ...
    # /issues.json does not allow filters on issue #id
    for my $bug_key(@$bug_keys) {
        $statuses->{$bug_key} = $self->get_status($bug_key);
    }

    return $statuses;
}

sub get_status {
    my ($self, $bug_key) = @_;

    $self->_connect() if(!defined($self->_ua));

    # Redmine requires the .json extension. We also provide Accept header for consistency
    my $res = $self->_ua->get($self->_redmine_url->path('/issues/')->path($bug_key . ".json"), { Accept => 'application/json' })->res;
    my $j = $res->json;
    my $issue = $j->{issue};

    my $resolution;
    foreach my $cf(@{$issue->{custom_fields}}) {
        if($cf->{name} =~ /resolution/i) {
            $resolution = $cf->{value};
        }
    }
    my $status = $issue->{status}->{name};
    my $closed = ($status =~ /^(?:Resolved|Closed)$/i);

    return {
      closed => $closed,
      status => $status,
      resolution => $resolution,
      summary => $issue->{subject},
    };
}

sub _connect {
    my $self = shift;

    $self->_ua(Mojo::UserAgent->new);

    $self->_redmine_url(Mojo::URL->new($self->config->{API}->{URL}));
}

1;