<commit_before>#
# Copyright 2015 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

package apps::github::mode::issues;

use base qw(centreon::plugins::mode);

use strict;
use warnings;
use centreon::plugins::http;
use JSON;

sub new {
    my ($class, %options) = @_;
    my $self = $class->SUPER::new(package => __PACKAGE__, %options);
    bless $self, $class;

    $self->{version} = '1.0';
    $options{options}->add_options(arguments =>
        {
            "hostname:s"        => { name => 'hostname', default => 'api.github.com' },
            "port:s"            => { name => 'port', default => '443'},
            "proto:s"           => { name => 'proto', default => 'https' },
            "credentials"       => { name => 'credentials' },
            "username:s"        => { name => 'username' },
            "password:s"        => { name => 'password' },
            "warning:s"         => { name => 'warning' },
            "critical:s"        => { name => 'critical' },
            "owner:s"           => { name => 'owner' },
            "repository:s"      => { name => 'repository' },
            "label:s"           => { name => 'label', default => '' },
            "timeout:s"         => { name => 'timeout' },
        });

    $self->{http} = centreon::plugins::http->new(output => $self->{output});
    return $self;
}

sub check_options {
    my ($self, %options) = @_;
    $self->SUPER::init(%options);

    if (!defined($self->{option_results}->{repository})) {
        $self->{output}->add_option_msg(short_msg => "Please set the repository option");
        $self->{output}->option_exit();
    }
    if (!defined($self->{option_results}->{owner})) {
        $self->{output}->add_option_msg(short_msg => "Please set the owner option");
        $self->{output}->option_exit();
    }
    if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
        $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
        $self->{output}->option_exit();
    }
    if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
        $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
        $self->{output}->option_exit();
    }

    $self->{option_results}->{url_path} = "/repos/" . $self->{option_results}->{owner} . "/" . $self->{option_results}->{repository} . "/issues";
    if (defined($self->{option_results}->{label}) && $self->{option_results}->{label} ne '') {
        $self->{option_results}->{get_param} = ['state=open', 'labels=' . $self->{option_results}->{label}, 'per_page=100'];
    } else {
        $self->{option_results}->{get_param} = ['state=open', 'per_page=100'];
    }
    $self->{http}->set_options(%{$self->{option_results}});
}

sub run {
    my ($self, %options) = @_;

    my $pagination = 1;

    my $nb_issues = 0;
    my $header;
    my $attemps = 3;
    my $next_url = "";

    while ($pagination && $attemps > 0) {
        $attemps -= 1;

        my $jsoncontent;
        if ($next_url eq "") {
            print 'Next url null.\n';
            $jsoncontent = $self->{http}->request();
        }
        else {
            print 'New next url: ' . $next_url . '\n';
            #$next_url = "/repositories/9074774/issues?per_page=100&state=open&page=3";
            $self->{option_results}->{url_path} = $next_url;
            $self->{http}->set_options(%{$self->{option_results}});
            $jsoncontent = $self->{http}->request(url_path => $next_url, get_param => $self->{option_results}->{get_param});
            #$jsoncontent = $self->{http}->request(url_path => $next_url);
        }

        my $json = JSON->new;
        my $webcontent;
        eval {
            $webcontent = $json->decode($jsoncontent);
        };

        if ($@) {
            $self->{output}->add_option_msg(short_msg => "Cannot decode json response");
            $self->{output}->option_exit();
        }

        # Number of issues is array length
        $nb_issues += @{$webcontent};

        $header = $self->{http}->get_header();
        #use Data::Dumper;
        #print Dumper($header);
        if (defined($header->header('link'))) {
            print "Link found\n";
            if ($header->header('link') =~ /<https:\/\/api.github.com(\/.*)>; rel="next"/) {
                print "Found regex\n";
                $next_url = $1;
                print $next_url . "\n";
            } else {
                $pagination = 0;
            }
        } else {
            $pagination = 0;
            print "No link found\n";
        }
    }<commit_msg>+ Replace issues api by search api to get number of tickets<commit_after>#
# Copyright 2015 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

package apps::github::mode::issues;

use base qw(centreon::plugins::mode);

use strict;
use warnings;
use centreon::plugins::http;
use JSON;

sub new {
    my ($class, %options) = @_;
    my $self = $class->SUPER::new(package => __PACKAGE__, %options);
    bless $self, $class;

    $self->{version} = '1.0';
    $options{options}->add_options(arguments =>
        {
            "hostname:s"        => { name => 'hostname', default => 'api.github.com' },
            "port:s"            => { name => 'port', default => '443'},
            "proto:s"           => { name => 'proto', default => 'https' },
            "credentials"       => { name => 'credentials' },
            "username:s"        => { name => 'username' },
            "password:s"        => { name => 'password' },
            "warning:s"         => { name => 'warning' },
            "critical:s"        => { name => 'critical' },
            "owner:s"           => { name => 'owner' },
            "repository:s"      => { name => 'repository' },
            "label:s"           => { name => 'label', default => '' },
            "timeout:s"         => { name => 'timeout' },
        });

    $self->{http} = centreon::plugins::http->new(output => $self->{output});
    return $self;
}

sub check_options {
    my ($self, %options) = @_;
    $self->SUPER::init(%options);

    if (!defined($self->{option_results}->{repository})) {
        $self->{output}->add_option_msg(short_msg => "Please set the repository option");
        $self->{output}->option_exit();
    }
    if (!defined($self->{option_results}->{owner})) {
        $self->{output}->add_option_msg(short_msg => "Please set the owner option");
        $self->{output}->option_exit();
    }
    if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
        $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
        $self->{output}->option_exit();
    }
    if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
        $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
        $self->{output}->option_exit();
    }

    $self->{option_results}->{url_path} = "/search/issues?q=state:open+repo:" . $self->{option_results}->{owner} . "/" . $self->{option_results}->{repository};
    if (defined($self->{option_results}->{label}) && $self->{option_results}->{label} ne '') {
        $self->{option_results}->{url_path} .= "+label:" . $self->{option_results}->{label};
    }
    $self->{http}->set_options(%{$self->{option_results}});
}

sub run {
    my ($self, %options) = @_;


    my $jsoncontent;
    $jsoncontent = $self->{http}->request();

    my $json = JSON->new;
    my $webcontent;
    eval {
        $webcontent = $json->decode($jsoncontent);
    };

    if ($@) {
        $self->{output}->add_option_msg(short_msg => "Cannot decode json response");
        $self->{output}->option_exit();
    }
    
    my $nb_issues = $webcontent->{total_count};
{'fraction_non_alphanumeric': 0.18983985576413193, 'fraction_numerical': 0.005833068193869976, 'mean_word_length': 2.626923076923077, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 234, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4861429', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2900, 'n_words': 636}