Skip to content

Instantly share code, notes, and snippets.

@mjgardner
Created November 29, 2012 15:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjgardner/4169704 to your computer and use it in GitHub Desktop.
Save mjgardner/4169704 to your computer and use it in GitHub Desktop.
Script that dumps plan data from an Atlassian Bamboo project using Bamboo's REST API
Example usage from command prompt:
$ perl bamboo_project_dump.pl --help
Mandatory parameter 'project' missing in call to (eval)
usage: bamboo_project_dump.pl [-?h] [long options...]
-h -? --usage --help Prints this usage information.
--user user name for HTTP basic authentication
--server Bamboo REST endpoint
--project Bamboo project ID to retrieve
--passwd password for HTTP basic authentication
--type MIME type for queries and output (default:
application/json)
$ perl bamboo_project_dump.pl --project MYPROJECT --user johndoe --passwd password --server http://bamboo.sample.com/bamboo/rest/api/latest
#!/usr/bin/env perl
use Modern::Perl;
{
package Bamboo::Client;
use HTTP::Exception;
use Moose;
use MooseX::Has::Options;
use Moose::Util::TypeConstraints;
with qw(MooseX::Getopt Role::REST::Client);
with 'Role::REST::Client::Auth::Basic';
# add command line option for --project that takes a Bamboo
# project ID with appropriate type restriction/coercion
subtype 'ProjectID', as 'Str',
where {/^ [[:upper:][:digit:]]+ $/xms},
message {"$_ is not a valid Bamboo project ID"};
coerce 'ProjectID', from 'Str', via {
my $old = $_;
$_ = uc;
s/ [^[:upper:][:digit:]] //gxms;
warn "coercing project ID $old to $_\n";
return $_;
};
has project_id => (
qw(:ro :required :coerce),
isa => 'ProjectID',
traits => ['Getopt'],
cmd_flag => 'project',
documentation => 'Bamboo project ID to retrieve',
);
# add project and plan methods that issue REST queries
for my $resource (qw(project plan)) {
__PACKAGE__->meta->add_method( $resource,
sub { $_[0]->get( "/$resource/$_[1]" => $_[2] )->data } );
}
# make HTTP GET queries throw an HTTP::Exception on non-OK responses
around get => sub {
my ( $orig, $self ) = splice @_, 0, 2;
my $response = $self->$orig(@_);
return $response if $response->code eq '200';
HTTP::Exception->throw( $response->code,
status_message => $response->failed
? $response->error
: $response->response->message );
};
# add documentation strings for --help flag
my %docs = (
server => 'Bamboo REST endpoint',
user => 'user name for HTTP basic authentication',
passwd => 'password for HTTP basic authentication',
type => 'MIME type for queries and output',
);
while ( my ( $attr, $doc ) = each %docs ) {
my $default = __PACKAGE__->meta->get_attribute($attr)->default;
$default = $default ? " (default: $default)" : q{};
has "+$attr" => ( documentation => "$doc$default" );
}
# don't expose certain Role::REST::Client attributes as command
# line options
has [
map {"+$_"}
qw(clientattrs
user_agent
serializer_class
httpheaders
persistent_headers
),
] => ( traits => ['NoGetopt'] );
__PACKAGE__->meta->make_immutable();
no Moose::Util::TypeConstraints;
no Moose;
}
my $bamboo = Bamboo::Client->new_with_options();
my %plans_ref # get list of Bamboo plans from server
= %{ $bamboo->project( $bamboo->project_id => { expand => 'plans' } )
->{plans}{plan} };
# insert actions, stages and variables from individual plans into the hashref
for ( values %plans_ref ) {
my $plan_key = $_->{key};
%{$_} = (
%{$_},
%{ $bamboo->plan($plan_key) },
map { $_ => $bamboo->plan( $plan_key => { expand => $_ } )->{$_} }
qw(actions stages variableContext),
);
}
# serialize to output using same serializer used for REST queries
say $bamboo->new_serializer( type => $bamboo->type )
->serialize( \%plans_ref );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment