| 1 | package Net::Lighthouse::Project; | ||||||||||
| 2 | 11 | ▶ | use Any::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 11 | ▶ | use Net::Lighthouse::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 11 | ▶ | use Params::Validate ':all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 11 | ▶ | use Net::Lighthouse::Project::Ticket; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 11 | ▶ | use Net::Lighthouse::Project::TicketBin; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 11 | ▶ | use Net::Lighthouse::Project::Milestone; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 11 | ▶ | use Net::Lighthouse::Project::Message; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 11 | ▶ | use Net::Lighthouse::Project::Changeset; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 11 | extends 'Net::Lighthouse::Base'; | |||||||||
| 12 | # read only attr | ||||||||||
| 13 | |||||||||||
| 14 | 11 | has [qw/created_at updated_at/] => ( | |||||||||
| 15 | isa => 'DateTime', | ||||||||||
| 16 | is => 'ro', | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 11 | has [qw/ open_states_list closed_states_list open_states closed_states /] => ( | |||||||||
| 20 | isa => 'ArrayRef', | ||||||||||
| 21 | is => 'ro', | ||||||||||
| 22 | auto_deref => 1, | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | 11 | has [ | |||||||||
| 26 | qw/default_assigned_user_id default_milestone_id id open_tickets_count /] => | ||||||||||
| 27 | ( | ||||||||||
| 28 | isa => 'Maybe[Int]', | ||||||||||
| 29 | is => 'ro', | ||||||||||
| 30 | ); | ||||||||||
| 31 | 11 | has [ qw/hidden send_changesets_to_events/ ] => | |||||||||
| 32 | ( | ||||||||||
| 33 | isa => 'Bool', | ||||||||||
| 34 | is => 'ro', | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 11 | has [qw/description description_html permalink access license/] => ( | |||||||||
| 38 | isa => 'Maybe[Str]', | ||||||||||
| 39 | is => 'ro', | ||||||||||
| 40 | ); | ||||||||||
| 41 | |||||||||||
| 42 | # read&write attr | ||||||||||
| 43 | 11 | has [qw/archived public/] => ( | |||||||||
| 44 | isa => 'Bool', | ||||||||||
| 45 | is => 'rw', | ||||||||||
| 46 | ); | ||||||||||
| 47 | |||||||||||
| 48 | 11 | has [qw/name/] => ( | |||||||||
| 49 | isa => 'Str', | ||||||||||
| 50 | is => 'rw', | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | 11 | ▶ | no Any::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 11 | __PACKAGE__->meta->make_immutable; | |||||||||
| 55 | |||||||||||
| 56 | 1 | ▶ | sub load { | ||||||||
|
Subroutine
load: called CC=4 SCAR=17.5
Pod
documented
| |||||||||||
| 57 | 1 | my $self = shift; | |||||||||
| 58 | 1 | validate_pos( @_, { type => SCALAR, regex => qr/^\d+|\w+$/ } ); | |||||||||
| 59 | 1 | my $id = shift; | |||||||||
| 60 | |||||||||||
| 61 | 1 | ▶ | if ( $id !~ /^\d+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | # so we got a project name, let's find it | ||||||||||
| 64 | 0 | my ( $project ) = grep { $_->name eq $id } $self->list; | |||||||||
| 65 | 0 | ▶ | if ($project) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | $id = $project->id; | |||||||||
| 67 | } | ||||||||||
| 68 | else { | ||||||||||
| 69 | 0 | die "can't find project $id in account " . $self->account; | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 1 | my $ua = $self->ua; | |||||||||
| 74 | 1 | my $url = $self->base_url . '/projects/' . $id . '.xml'; | |||||||||
| 75 | 1 | my $res = $ua->get( $url ); | |||||||||
| 76 | 1 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 1 | $self->load_from_xml( $res->content ); | |||||||||
| 78 | } | ||||||||||
| 79 | else { | ||||||||||
| 80 | 0 | die "try to get $url failed: " | |||||||||
| 81 | . $res->status_line . "\n" | ||||||||||
| 82 | . $res->content; | ||||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 7 | ▶ | sub load_from_xml { | ||||||||
|
Subroutine
load_from_xml: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 87 | 7 | my $self = shift; | |||||||||
| 88 | 7 | my $ref = $self->_translate_from_xml( shift ); | |||||||||
| 89 | |||||||||||
| 90 | # dirty hack: some attrs are read-only, and Mouse doesn't support | ||||||||||
| 91 | # writer => '...' | ||||||||||
| 92 | 7 | for my $k ( keys %$ref ) { | |||||||||
| 93 | 133 | $self->{$k} = $ref->{$k}; | |||||||||
| 94 | } | ||||||||||
| 95 | 7 | return $self; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub create { | ||||||||
|
Subroutine
create: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 99 | 0 | my $self = shift; | |||||||||
| 100 | 0 | validate( | |||||||||
| 101 | @_, | ||||||||||
| 102 | { | ||||||||||
| 103 | name => { type => SCALAR }, | ||||||||||
| 104 | archived => { optional => 1, type => BOOLEAN }, | ||||||||||
| 105 | public => { optional => 1, type => BOOLEAN }, | ||||||||||
| 106 | } | ||||||||||
| 107 | ); | ||||||||||
| 108 | 0 | my %args = @_; | |||||||||
| 109 | |||||||||||
| 110 | 0 | my $xml = Net::Lighthouse::Util->translate_to_xml( | |||||||||
| 111 | \%args, | ||||||||||
| 112 | root => 'project', | ||||||||||
| 113 | boolean => [qw/archived public/], | ||||||||||
| 114 | ); | ||||||||||
| 115 | |||||||||||
| 116 | 0 | my $ua = $self->ua; | |||||||||
| 117 | |||||||||||
| 118 | 0 | my $url = $self->base_url . '/projects.xml'; | |||||||||
| 119 | |||||||||||
| 120 | 0 | my $request = HTTP::Request->new( 'POST', $url, undef, $xml ); | |||||||||
| 121 | 0 | my $res = $ua->request( $request ); | |||||||||
| 122 | 0 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 0 | $self->load_from_xml( $res->content ); | |||||||||
| 124 | 0 | return 1; | |||||||||
| 125 | } | ||||||||||
| 126 | else { | ||||||||||
| 127 | 0 | die "try to POST $url failed: " | |||||||||
| 128 | . $res->status_line . "\n" | ||||||||||
| 129 | . $res->content; | ||||||||||
| 130 | } | ||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 0 | ▶ | sub update { | ||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 134 | 0 | my $self = shift; | |||||||||
| 135 | 0 | validate( | |||||||||
| 136 | @_, | ||||||||||
| 137 | { | ||||||||||
| 138 | archived => { optional => 1, type => BOOLEAN }, | ||||||||||
| 139 | name => { optional => 1, type => SCALAR }, | ||||||||||
| 140 | public => { optional => 1, type => BOOLEAN }, | ||||||||||
| 141 | } | ||||||||||
| 142 | ); | ||||||||||
| 143 | 0 | my %args = ( ( map { $_ => $self->$_ } qw/archived name public/ ), @_ ); | |||||||||
| 144 | |||||||||||
| 145 | 0 | my $xml = Net::Lighthouse::Util->translate_to_xml( | |||||||||
| 146 | \%args, | ||||||||||
| 147 | root => 'project', | ||||||||||
| 148 | boolean => [qw/archived public/], | ||||||||||
| 149 | ); | ||||||||||
| 150 | |||||||||||
| 151 | 0 | my $ua = $self->ua; | |||||||||
| 152 | 0 | my $url = $self->base_url . '/projects/' . $self->id . '.xml'; | |||||||||
| 153 | |||||||||||
| 154 | 0 | my $request = HTTP::Request->new( 'PUT', $url, undef, $xml ); | |||||||||
| 155 | 0 | my $res = $ua->request( $request ); | |||||||||
| 156 | 0 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | $self->load( $self->id ); # let's reload | |||||||||
| 158 | 0 | return 1; | |||||||||
| 159 | } | ||||||||||
| 160 | else { | ||||||||||
| 161 | 0 | die "try to PUT $url failed: " | |||||||||
| 162 | . $res->status_line . "\n" | ||||||||||
| 163 | . $res->content; | ||||||||||
| 164 | } | ||||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 0 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 168 | 0 | my $self = shift; | |||||||||
| 169 | 0 | my $ua = $self->ua; | |||||||||
| 170 | 0 | my $url = $self->base_url . '/projects/' . $self->id . '.xml'; | |||||||||
| 171 | |||||||||||
| 172 | 0 | my $request = HTTP::Request->new( 'DELETE', $url ); | |||||||||
| 173 | 0 | my $res = $ua->request( $request ); | |||||||||
| 174 | 0 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 175 | 0 | return 1; | |||||||||
| 176 | } | ||||||||||
| 177 | else { | ||||||||||
| 178 | 0 | die "try to DELETE $url failed: " | |||||||||
| 179 | . $res->status_line . "\n" | ||||||||||
| 180 | . $res->content; | ||||||||||
| 181 | } | ||||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | 3 | ▶ | sub list { | ||||||||
|
Subroutine
list: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 185 | 3 | my $self = shift; | |||||||||
| 186 | 3 | my $ua = $self->ua; | |||||||||
| 187 | 3 | my $url = $self->base_url . '/projects.xml'; | |||||||||
| 188 | 3 | my $res = $ua->get( $url ); | |||||||||
| 189 | 3 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 3 | my $ps = Net::Lighthouse::Util->read_xml( $res->content )->{projects}{project}; | |||||||||
| 191 | my @list = map { | ||||||||||
| 192 | 3 | my $p = Net::Lighthouse::Project->new( | |||||||||
| 193 | 12 | map { $_ => $self->$_ } | |||||||||
| 194 | 6 | grep { $self->$_ } qw/account auth/ | |||||||||
| 195 | ); | ||||||||||
| 196 | 6 | ▶ | $p->load_from_xml($_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | } ref $ps eq 'ARRAY' ? @$ps : $ps; | ||||||||||
| 198 | 3 | ▶ | return wantarray ? @list : \@list; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 199 | } | ||||||||||
| 200 | else { | ||||||||||
| 201 | 0 | die "try to get $url failed: " | |||||||||
| 202 | . $res->status_line . "\n" | ||||||||||
| 203 | . $res->content; | ||||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | } | ||||||||||
| 207 | |||||||||||
| 208 | 1 | ▶ | sub initial_state { | ||||||||
|
Subroutine
initial_state: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 209 | 1 | my $self = shift; | |||||||||
| 210 | 1 | my $ua = $self->ua; | |||||||||
| 211 | 1 | my $url = $self->base_url . '/projects/new.xml'; | |||||||||
| 212 | 1 | my $res = $ua->get( $url ); | |||||||||
| 213 | 1 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 214 | 1 | return $self->_translate_from_xml( $res->content ); | |||||||||
| 215 | } | ||||||||||
| 216 | else { | ||||||||||
| 217 | 0 | die "try to get $url failed: " | |||||||||
| 218 | . $res->status_line . "\n" | ||||||||||
| 219 | . $res->content; | ||||||||||
| 220 | } | ||||||||||
| 221 | } | ||||||||||
| 222 | |||||||||||
| 223 | 1 | ▶ | sub tickets { return shift->_list( 'Ticket', @_ ) } | ||||||||
|
Subroutine
tickets: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 224 | 1 | ▶ | sub ticket_bins { return shift->_list( 'TicketBin', @_ ) } | ||||||||
|
Subroutine
ticket_bins: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 225 | 1 | ▶ | sub messages { return shift->_list( 'Message', @_ ) } | ||||||||
|
Subroutine
messages: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 226 | 1 | ▶ | sub milestones { return shift->_list( 'Milestone', @_ ) } | ||||||||
|
Subroutine
milestones: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 227 | 1 | ▶ | sub changesets { return shift->_list( 'Changeset', @_ ) } | ||||||||
|
Subroutine
changesets: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 228 | |||||||||||
| 229 | 1 | ▶ | sub ticket { return shift->_new( 'Ticket' ) } | ||||||||
|
Subroutine
ticket: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 230 | 1 | ▶ | sub ticket_bin { return shift->_new( 'TicketBin' ) } | ||||||||
|
Subroutine
ticket_bin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 231 | 1 | ▶ | sub message { return shift->_new( 'Message' ) } | ||||||||
|
Subroutine
message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 232 | 1 | ▶ | sub milestone { return shift->_new( 'Milestone' ) } | ||||||||
|
Subroutine
milestone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 233 | 1 | ▶ | sub changeset { return shift->_new( 'Changeset' ) } | ||||||||
|
Subroutine
changeset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 234 | |||||||||||
| 235 | 5 | ▶ | sub _new { | ||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||
| 236 | 5 | my $self = shift; | |||||||||
| 237 | 5 | validate_pos( | |||||||||
| 238 | @_, | ||||||||||
| 239 | { | ||||||||||
| 240 | type => SCALAR, | ||||||||||
| 241 | regex => qr/^(TicketBin|Ticket|Message|Changeset|Milestone)$/, | ||||||||||
| 242 | } | ||||||||||
| 243 | ); | ||||||||||
| 244 | 5 | my $class = 'Net::Lighthouse::Project::' . shift; | |||||||||
| 245 | my $object = $class->new( | ||||||||||
| 246 | project_id => $self->id, | ||||||||||
| 247 | 10 | map { $_ => $self->$_ } | |||||||||
| 248 | 5 | grep { $self->$_ } qw/account auth/ | |||||||||
| 249 | ); | ||||||||||
| 250 | 5 | return $object; | |||||||||
| 251 | } | ||||||||||
| 252 | |||||||||||
| 253 | 5 | ▶ | sub _list { | ||||||||
|
Subroutine
_list: called CC=1 SCAR=0.0
| |||||||||||
| 254 | 5 | my $self = shift; | |||||||||
| 255 | 5 | validate_pos( | |||||||||
| 256 | @_, | ||||||||||
| 257 | { | ||||||||||
| 258 | type => SCALAR, | ||||||||||
| 259 | regex => qr/^(TicketBin|Ticket|Message|Changeset|Milestone)$/, | ||||||||||
| 260 | }, | ||||||||||
| 261 | (0)x(@_-1) | ||||||||||
| 262 | ); | ||||||||||
| 263 | 5 | my $class = 'Net::Lighthouse::Project::' . shift; | |||||||||
| 264 | my $object = $class->new( | ||||||||||
| 265 | project_id => $self->id, | ||||||||||
| 266 | 10 | map { $_ => $self->$_ } | |||||||||
| 267 | 5 | grep { $self->$_ } qw/account auth/ | |||||||||
| 268 | ); | ||||||||||
| 269 | 5 | return $object->list(@_); | |||||||||
| 270 | } | ||||||||||
| 271 | |||||||||||
| 272 | 8 | ▶ | sub _translate_from_xml { | ||||||||
|
Subroutine
_translate_from_xml: called CC=5 SCAR=16.2
| |||||||||||
| 273 | 8 | my $self = shift; | |||||||||
| 274 | 8 | my $ref = Net::Lighthouse::Util->translate_from_xml(shift); | |||||||||
| 275 | 8 | for (qw/open_states_list closed_states_list/) { | |||||||||
| 276 | 16 | $ref->{$_} = [ split /,/, $ref->{$_} ]; | |||||||||
| 277 | } | ||||||||||
| 278 | |||||||||||
| 279 | 8 | for my $states (qw/ open_states closed_states /) { | |||||||||
| 280 | 16 | my @values = split /\n/, $ref->{$states}; | |||||||||
| 281 | 16 | my @new_values; | |||||||||
| 282 | 16 | for my $value (@values) { | |||||||||
| 283 | # e.g. new/f17 # You can add comments here | ||||||||||
| 284 | 40 | ▶ | if ( $value =~ m{(\w+)(?:/(\w+))?\s+#?\s*(.*)?} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 285 | 40 | push @new_values, { name => $1, color => $2, comment => $3 }; | |||||||||
| 286 | } | ||||||||||
| 287 | else { | ||||||||||
| 288 | 0 | warn "parse $value failed"; | |||||||||
| 289 | } | ||||||||||
| 290 | } | ||||||||||
| 291 | 16 | $ref->{$states} = [@new_values]; | |||||||||
| 292 | } | ||||||||||
| 293 | 8 | return $ref; | |||||||||
| 294 | } | ||||||||||
| 295 | |||||||||||
| 296 | 11 | 1; | |||||||||
| 297 | |||||||||||
| 298 | __END__ | ||||||||||