| 1 | package Git::IssueManager; | ||
| 2 | #ABSTRACT: Module for managing issues in a git branch within your repository | ||
| 3 | use Moose; | ||
| 4 | use MooseX::Privacy; | ||
| 5 | use DateTime; | ||
| 6 | use DateTime::TimeZone; | ||
| 7 | use Data::Dumper; | ||
| 8 | use Git::LowLevel; | ||
| 9 | |||
| 10 | |||
| 11 | has 'gitcmd' => (is => 'ro', isa => 'Str', default=>"git"); | ||
| 12 | |||
| 13 | has 'repository' => (is =>'rw', isa => 'Git::LowLevel'); | ||
| 14 | |||
| 15 | has '_open' => (is => 'rw', isa => 'Git::LowLevel::Tree', traits => [qw/Private/]); | ||
| 16 | |||
| 17 | has '_assigned' => (is => 'rw', isa => 'Git::LowLevel::Tree', traits => [qw/Private/]); | ||
| 18 | |||
| 19 | |||
| 20 | has '_inprogress' => (is => 'rw', isa => 'Git::LowLevel::Tree', traits => [qw/Private/]); | ||
| 21 | |||
| 22 | has '_closed' => (is => 'rw', isa => 'Git::LowLevel::Tree', traits => [qw/Private/]); | ||
| 23 | |||
| 24 | has '_root' => (is => 'rw', isa => 'Git::LowLevel::Tree', traits => [qw/Private/]); | ||
| 25 | |||
| 26 | sub ready | ||
| 27 | { | ||
| 28 | my $self = shift; | ||
| 29 | my $ref = $self->repository->getReference('refs/heads/issues'); | ||
| 30 | return 0 unless $ref->exist; | ||
| 31 | |||
| 32 | my $version = $ref->find('.version'); | ||
| 33 | return 0 unless defined($version) && ref($version) eq "Git::LowLevel::Blob"; | ||
| 34 | |||
| 35 | my $tag = $ref->find('.tag'); | ||
| 36 | return 0 unless defined($tag) && ref($tag) eq "Git::LowLevel::Blob"; | ||
| 37 | |||
| 38 | return 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub version | ||
| 43 | { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | return unless $self->ready(); | ||
| 47 | |||
| 48 | my $ref = $self->repository->getReference('refs/heads/issues'); | ||
| 49 | my $version = $ref->find(".version"); | ||
| 50 | |||
| 51 | return $version->content; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub tag | ||
| 55 | { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | return unless $self->ready(); | ||
| 59 | |||
| 60 | my $ref = $self->repository->getReference('refs/heads/issues'); | ||
| 61 | my $tag = $ref->find(".tag"); | ||
| 62 | |||
| 63 | return $tag->content; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub init | ||
| 68 | { | ||
| 69 | my $self = shift; | ||
| 70 | my $issue_tag = shift; | ||
| 71 | |||
| 72 | return unless ! $self->ready(); | ||
| 73 | |||
| 74 | die("no issue tag given") unless defined($issue_tag) && length($issue_tag) > 0; | ||
| 75 | |||
| 76 | my $ref = $self->repository->getReference('refs/heads/issues'); | ||
| 77 | my $root = $ref->getTree(); | ||
| 78 | |||
| 79 | my $version = $root->newBlob(); | ||
| 80 | $version->path(".version"); | ||
| 81 | $version->_content("0.1"); | ||
| 82 | $root->add($version); | ||
| 83 | |||
| 84 | my $tag = $root->newBlob(); | ||
| 85 | $tag->path(".tag"); | ||
| 86 | $tag->_content($issue_tag); | ||
| 87 | $root->add($tag); | ||
| 88 | |||
| 89 | $ref->commit("initialized issue manager"); | ||
| 90 | |||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | sub add | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | my $issue = shift; | ||
| 98 | |||
| 99 | die("IssueManager not initialized") unless $self->ready(); | ||
| 100 | die("no issue given") unless defined($issue) && ref($issue) eq "Git::IssueManager::Issue"; | ||
| 101 | |||
| 102 | my $ref = $self->repository->getReference('refs/heads/issues'); | ||
| 103 | my $root = $ref->getTree(); | ||
| 104 | |||
| 105 | my $issueTree=$issue->createIssue($self->repository); | ||
| 106 | |||
| 107 | my $base=$root->find($issue->status); | ||
| 108 | if (!defined($base)) | ||
| 109 | { | ||
| 110 | $base = $root->newTree(); | ||
| 111 | $base->path($issue->status); | ||
| 112 | $root->add($base); | ||
| 113 | } | ||
| 114 | $base->add($issueTree); | ||
| 115 | $ref->commit("added issue " . $issue->subject); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub parseIssue | ||
| 119 | { | ||
| 120 | my $self = shift; | ||
| 121 | my $d = shift; | ||
| 122 | my $tag = shift; | ||
| 123 | my $status = shift; | ||
| 124 | my $subject = $d->find("subject"); | ||
| 125 | my $description = $d->find("description"); | ||
| 126 | my $priority = $d->find("priority"); | ||
| 127 | my $severity = $d->find("severity"); | ||
| 128 | my $type = $d->find("type"); | ||
| 129 | my $worker = $d->find("worker"); | ||
| 130 | my $substatus = $d->find("substatus"); | ||
| 131 | my $comment = $d->find("comment"); | ||
| 132 | my $estimated = $d->find("estimated"); | ||
| 133 | my $working = $d->find("working"); | ||
| 134 | my $tags = $d->find("tags"); | ||
| 135 | my $id = $tag . "-" . substr($subject->hash(),0,8); | ||
| 136 | my $cd = $d->timestamp_added(); | ||
| 137 | my $ld = $d->timestamp_last(); | ||
| 138 | my $author = $d->committer(); | ||
| 139 | |||
| 140 | # check for required attributes | ||
| 141 | die("description not available for issue " . $id) unless defined($description); | ||
| 142 | die("priority not available for issue " . $id) unless defined($priority); | ||
| 143 | die("severity not available for issue " . $id) unless defined($severity); | ||
| 144 | die("type not available for issue " . $id) unless defined($type); | ||
| 145 | |||
| 146 | |||
| 147 | my $tz=DateTime::TimeZone->new( name => 'local' ); | ||
| 148 | my $issue = Git::IssueManager::Issue->new(subject => $subject->content); | ||
| 149 | $issue->status($status); | ||
| 150 | $issue->description($description->content()); | ||
| 151 | $issue->priority($priority->content()); | ||
| 152 | $issue->severity($severity->content()); | ||
| 153 | $issue->type($type->content()); | ||
| 154 | $issue->id($id); | ||
| 155 | $issue->creation_date(DateTime->from_epoch( epoch =>$cd, time_zone=>$tz)); | ||
| 156 | $issue->last_change_date(DateTime->from_epoch( epoch =>$ld, time_zone=>$tz)); | ||
| 157 | |||
| 158 | if (defined($worker)) | ||
| 159 | { | ||
| 160 | $worker->content()=~/^(.*)\<(.*)\>$/; | ||
| 161 | $issue->worker($1); | ||
| 162 | $issue->worker_email($2); | ||
| 163 | } | ||
| 164 | |||
| 165 | if (defined($author)) | ||
| 166 | { | ||
| 167 | $author=~/^(.*)\<(.*)\>$/; | ||
| 168 | $issue->author($1); | ||
| 169 | $issue->author_email($2); | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | return $issue; | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | sub list | ||
| 178 | { | ||
| 179 | my $self = shift; | ||
| 180 | my @issues; | ||
| 181 | |||
| 182 | die("IssueManager not initialized") unless $self->ready(); | ||
| 183 | my $ref = $self->repository->getReference('refs/heads/issues'); | ||
| 184 | my $root = $ref->getTree(); | ||
| 185 | my $open = $ref->find("open"); | ||
| 186 | my $closed = $ref->find("closed"); | ||
| 187 | my $assigned = $ref->find("assigned"); | ||
| 188 | my $inprogress = $ref->find("inprogess"); | ||
| 189 | my $tag = $ref->find(".tag")->content(); | ||
| 190 | my @all; | ||
| 191 | |||
| 192 | my @statusse = ("open","closed","assigned","inprogress"); | ||
| 193 | for my $s (@statusse) | ||
| 194 | { | ||
| 195 | for my $status ($root->find($s)) | ||
| 196 | { | ||
| 197 | for my $i ($status->get()) | ||
| 198 | { | ||
| 199 | my $issue = $self->parseIssue($i,$tag,$s); | ||
| 200 | push(@issues,$issue); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | return @issues; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub delete | ||
| 209 | { | ||
| 210 | my $self = shift; | ||
| 211 | my $id = shift; | ||
| 212 | my @statusse = ("open","closed","assigned","inprogress"); | ||
| 213 | |||
| 214 | die("IssueManager not initialized") unless $self->ready(); | ||
| 215 | my $ref = $self->repository->getReference('refs/heads/issues'); | ||
| 216 | my $tag = $ref->find(".tag")->content(); | ||
| 217 | my $root = $ref->getTree(); | ||
| 218 | |||
| 219 | |||
| 220 | for my $s (@statusse) | ||
| 221 | { | ||
| 222 | for my $status ($root->find($s)) | ||
| 223 | { | ||
| 224 | for my $i ($status->get()) | ||
| 225 | { | ||
| 226 | if (ref($i) eq "Git::LowLevel::Tree") | ||
| 227 | { | ||
| 228 | my $subject = $i->find("subject"); | ||
| 229 | my $mytag = $tag . "-" . substr($subject->hash(),0,8); | ||
| 230 | if ($id eq $mytag) | ||
| 231 | { | ||
| 232 | $status->del($i); | ||
| 233 | $ref->commit("removed issue " . $i->mypath); | ||
| 234 | return; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | } | ||
| 240 | die("issue " . $id . " not found\n"); | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | sub changeStatus | ||
| 245 | { | ||
| 246 | my $self = shift; | ||
| 247 | my $id = shift; | ||
| 248 | my $status= shift; | ||
| 249 | |||
| 250 | die("unknown status") unless $status eq "open" || $status eq "closed" || $status eq "inprogress" || $status eq "assigned"; | ||
| 251 | |||
| 252 | my @statusse = ("open","assigned","inprogress","closed"); | ||
| 253 | |||
| 254 | die("IssueManager not initialized") unless $self->ready(); | ||
| 255 | my $ref = $self->repository->getReference('refs/heads/issues'); | ||
| 256 | my $tag = $ref->find(".tag")->content(); | ||
| 257 | my $root = $ref->getTree(); | ||
| 258 | |||
| 259 | |||
| 260 | for my $s (@statusse) | ||
| 261 | { | ||
| 262 | next unless $s ne $status; | ||
| 263 | for my $stat ($root->find($s)) | ||
| 264 | { | ||
| 265 | for my $i ($stat->get()) | ||
| 266 | { | ||
| 267 | if (ref($i) eq "Git::LowLevel::Tree") | ||
| 268 | { | ||
| 269 | my $subject = $i->find("subject"); | ||
| 270 | my $mytag = $tag . "-" . substr($subject->hash(),0,8); | ||
| 271 | if ($id eq $mytag) | ||
| 272 | { | ||
| 273 | my $base=$root->find($status); | ||
| 274 | $stat->del($i); | ||
| 275 | my $issue = $self->parseIssue($i, $tag, $s); | ||
| 276 | $issue->status($status); | ||
| 277 | my $issueTree=$issue->createIssue($self->repository); | ||
| 278 | |||
| 279 | if (!defined($base)) | ||
| 280 | { | ||
| 281 | $base = $root->newTree(); | ||
| 282 | $base->path($status); | ||
| 283 | $root->add($base); | ||
| 284 | } | ||
| 285 | $base->add($issueTree); | ||
| 286 | $ref->commit("closed issue " . $i->mypath); | ||
| 287 | return; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | die("issue " . $id . " not found\n"); | ||
| 294 | } | ||
| 295 | |||
| 296 | sub assign | ||
| 297 | { | ||
| 298 | my $self = shift; | ||
| 299 | my $id = shift; | ||
| 300 | my $worker_name = shift; | ||
| 301 | my $worker_email = shift; | ||
| 302 | my $status = "assigned"; | ||
| 303 | my @statusse = ("open","assigned","inprogress","closed"); | ||
| 304 | |||
| 305 | die("IssueManager not initialized") unless $self->ready(); | ||
| 306 | my $ref = $self->repository->getReference('refs/heads/issues'); | ||
| 307 | my $tag = $ref->find(".tag")->content(); | ||
| 308 | my $root = $ref->getTree(); | ||
| 309 | |||
| 310 | |||
| 311 | for my $s (@statusse) | ||
| 312 | { | ||
| 313 | next unless $s ne $status; | ||
| 314 | for my $stat ($root->find($s)) | ||
| 315 | { | ||
| 316 | for my $i ($stat->get()) | ||
| 317 | { | ||
| 318 | if (ref($i) eq "Git::LowLevel::Tree") | ||
| 319 | { | ||
| 320 | my $subject = $i->find("subject"); | ||
| 321 | my $mytag = $tag . "-" . substr($subject->hash(),0,8); | ||
| 322 | if ($id eq $mytag) | ||
| 323 | { | ||
| 324 | my $base=$root->find($status); | ||
| 325 | $stat->del($i); | ||
| 326 | my $issue = $self->parseIssue($i, $tag, $s); | ||
| 327 | $issue->status($status); | ||
| 328 | $issue->worker($worker_name); | ||
| 329 | $issue->worker_email($worker_email); | ||
| 330 | |||
| 331 | my $issueTree=$issue->createIssue($self->repository); | ||
| 332 | |||
| 333 | if (!defined($base)) | ||
| 334 | { | ||
| 335 | $base = $root->newTree(); | ||
| 336 | $base->path($status); | ||
| 337 | $root->add($base); | ||
| 338 | } | ||
| 339 | $base->add($issueTree); | ||
| 340 | $ref->commit("closed issue " . $i->mypath); | ||
| 341 | return; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | } | ||
| 345 | } | ||
| 346 | } | ||
| 347 | die("issue " . $id . " not found\n"); | ||
| 348 | |||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | |||
| 353 | sub close | ||
| 354 | { | ||
| 355 | my $self = shift; | ||
| 356 | my $id = shift; | ||
| 357 | |||
| 358 | $self->changeStatus($id, "closed"); | ||
| 359 | } | ||
| 360 | |||
| 361 | sub open | ||
| 362 | { | ||
| 363 | my $self = shift; | ||
| 364 | my $id = shift; | ||
| 365 | |||
| 366 | $self->changeStatus($id, "open"); | ||
| 367 | } | ||
| 368 | |||
| 369 | sub start | ||
| 370 | { | ||
| 371 | my $self = shift; | ||
| 372 | my $id = shift; | ||
| 373 | |||
| 374 | $self->changeStatus($id, "inprogress"); | ||
| 375 | } | ||
| 376 | |||
| 377 | |||
| 378 | 1; | ||
| 379 | |||
| 380 | __END__ |