Jenkins CI Stuff

work in progress.. YMMV..

Command Line Jenkins Control with Auth

use strict;
use warnings;
use LWP::UserAgent;
use JSON::XS;

sub jenkinsAPI
{
    my $path = shift;
    my $params = shift || {};

    my $ua = LWP::UserAgent->new(timeout => 10, agent => 'blabla/123');

    # here's the trick:
    $ua->default_headers()->authorization_basic($jenkinsUser, $jenkinsKey);

    my $url = $jenkinsUrl . $path . '/api/json?pretty=true';
    $url .= "&$_=$params->{$_}" for (sort keys %{$params});
    DEBUG1("jenkinsAPI: %s", $url);

    my $resp = $ua->get($url);

    DEBUG1("jenkinsAPI: %s", $resp->status_line());
    if ($resp->is_success())
    {
        my $content = $resp->decoded_content(); # JSON data
        #DEBUG1("jenkins said:", split(/n/, $content));
        return JSON::XS->new()->utf8()->decode( $content );
    }
    else
    {
        ERROR("Ouch! %s", $resp->status_line(), 500);
    }
}

sub jenkinsPOST
{
    my $path = shift;
    my $params = shift;

    my $ua = LWP::UserAgent->new(timeout => 10, agent => 'blabla/123');

    # here's the tricks:
    $ua->requests_redirectable([ qw(GET HEAD POST) ]); # POSTs will redirect..
    $ua->default_headers()->authorization_basic($jenkinsUser, $jenkinsKey);

    my $url = $jenkinsUrl . $path;
    DEBUG1("jenkinsPOST: %s", $url);

    #$params->{json} = JSON::XS->new()->utf8()->canonical(1)->pretty(1)->encode($params);

    my $resp = $ua->post($url, $params);

    DEBUG1("jenkins: %s", $resp->status_line());

    #my $content = $resp->decoded_content();
    #DEBUG1("jenkins said:", split(/n/, $content));

    if ($resp->is_success())
    {
        return 1;
    }
    else
    {
        ERROR("Ouch! %s", $resp->status_line());
        return 0;
    }
}