ADD: removed code duplication

This commit is contained in:
Dominik Meyer 2018-07-04 18:33:10 +02:00
parent 37e09b68ff
commit 36c1c3d962
No known key found for this signature in database
GPG Key ID: B4C312B600606B64

View File

@ -1,6 +1,7 @@
package Git::IssueManager;
#ABSTRACT: Module for managing issues in a git branch within your repository
use Moose;
use MooseX::Privacy;
use Git::RepositoryHL;
use DateTime;
use DateTime::TimeZone;
@ -15,6 +16,40 @@ Git::Repository object on which to do the issue management
=cut
has 'repository' => (is =>'ro', isa => 'Git::RepositoryHL', required => 1);
=attr _open
B<private attribute>
=cut
has '_open' => (is => 'rw', isa => 'ArrayRef', traits => [qw/Private/]);
=attr _assigned
B<private attribute>
=cut
has '_assigned' => (is => 'rw', isa => 'ArrayRef', traits => [qw/Private/]);
=attr _inprogress
B<private attribute>
=cut
has '_inprogress' => (is => 'rw', isa => 'ArrayRef', traits => [qw/Private/]);
=attr _closed
B<private attribute>
=cut
has '_closed' => (is => 'rw', isa => 'ArrayRef', traits => [qw/Private/]);
=attr _root
B<private attribute>
=cut
has '_root' => (is => 'rw', isa => 'ArrayRef', traits => [qw/Private/]);
=method ready
@ -134,22 +169,190 @@ sub init
}
=method _recreate - internal method, do not call directly
=method _load
method used to recreate the tree structure received by getTree to something like createTree can use
B<private method>
called to load all the git trees
=cut
sub recreate
{
private_method _load => sub {
my $self = shift;
my $array = shift;
for my $a (@{$array})
my @root = $self->repository->getTree("issues");
$self->_root(\@root);
for my $a (@root)
{
$a->{path} = $a->{name};
}
my @open = $self->repository->getTree("issues","open/");
$self->_open(\@open);
for my $a (@open)
{
$a->{path} = $a->{name};
}
my @closed = $self->repository->getTree("issues","closed/");
$self->_closed(\@closed);
for my $a (@closed)
{
$a->{path} = $a->{name};
}
my @assigned = $self->repository->getTree("issues","assigned/");
$self->_assigned(\@assigned);
for my $a (@assigned)
{
$a->{path} = $a->{name};
}
my @inprogress = $self->repository->getTree("issues","inprogess/");
$self->_inprogress(\@inprogress);
for my $a (@inprogress)
{
$a->{path} = $a->{name};
}
};
=method _createTree
B<privat method>
creates the root tree for the issues branch and returns its hash
=cut
private_method _createTree => sub {
my $self = shift;
# now recreate tree structure
my $openhash = $self->repository->createTree($self->_open);
my $closedhash = $self->repository->createTree($self->_closed);
my $assignedhash = $self->repository->createTree($self->_assigned);
my $inprogresshash= $self->repository->createTree($self->_inprogress);
my $openfound=0;
my $closedfound=0;
my $assignedfound=0;
my $inprogressfound=0;
# now recreate the root tree
my @fordelete;
my $i=0;
for my $r (@{$self->_root})
{
if ($r->{name} eq "open")
{
if (defined($openhash) && length($openhash)==40)
{
$r->{ref}=$openhash;
$openfound=1;
}
else
{
push(@fordelete, $i);
}
}
if ($r->{name} eq "closed")
{
if (defined($closedhash) && length($closedhash)==40)
{
$r->{ref}=$closedhash;
$closedfound=1;
}
else
{
push(@fordelete, $i);
}
}
if ($r->{name} eq "assigned")
{
if (defined($assignedhash) && length($assignedhash)==40)
{
$r->{ref}=$assignedhash;
$assignedfound=1;
}
else
{
push(@fordelete, $i);
}
}
if ($r->{name} eq "inprogess")
{
if (defined($inprogresshash) && length($inprogresshash)==40)
{
$r->{ref}=$inprogresshash;
$inprogressfound=1;
}
else
{
push(@fordelete, $i);
}
}
$i++;
}
for my $d (@fordelete)
{
splice @{$self->_root}, $d, 1;
}
if (!$openfound && defined($openhash))
{
my $t = {
path => "open",
ref => $openhash,
type => "tree",
mode => "040000"
};
push(@{$self->_root}, $t);
}
if (!$closedfound && defined($closedhash))
{
my $t = {
path => "closed",
ref => $closedhash,
type => "tree",
mode => "040000"
};
push(@{$self->_root}, $t);
}
if (!$assignedfound && defined($assignedhash))
{
my $t = {
path => "assigned",
ref => $assignedhash,
type => "tree",
mode => "040000"
};
push(@{$self->_root}, $t);
}
if (!$inprogressfound && defined($inprogresshash))
{
my $t = {
path => "inprogress",
ref => $inprogresshash,
type => "tree",
mode => "040000"
};
push(@{$self->_root}, $t);
}
return $self->repository->createTree($self->_root);
};
=method add
add an issue to the repository
@ -162,33 +365,24 @@ sub add
die("IssueManager not initialized") unless $self->ready();
die("no Git::IssueManager::Issue object given") unless ref($issue) eq "Git::IssueManager::Issue";
my @root = $self->repository->getTree("issues");
my @open = $self->repository->getTree("issues","open/");
my @closed = $self->repository->getTree("issues","closed/");
my @assigned = $self->repository->getTree("issues","assigned/");
my @inprogress = $self->repository->getTree("issues","inprogress");
$self->recreate(\@open);
$self->recreate(\@closed);
$self->recreate(\@assigned);
$self->recreate(\@inprogress);
$self->_load();
my $issues;
if ($issue->status eq "open")
{
$issues=\@open;
$issues=$self->_open();
}
elsif ($issue->status eq "closed")
{
$issues=\@closed;
$issues=$self->_closed();
}
elsif ($issue->status eq "assigned")
{
$issues=\@assigned;
$issues=$self->_assigned;
}
elsif ($issue->status eq "inprogress")
{
$issues=\@inprogress;
$issues=$self->_inprogress;
}
# check if issues already exist, only checks the subject !!!
@ -209,90 +403,8 @@ sub add
mode => "040000"
});
# now recreate tree structure
my $openhash = $self->repository->createTree(\@open);
my $closedhash = $self->repository->createTree(\@closed);
my $assignedhash = $self->repository->createTree(\@assigned);
my $inprogresshash= $self->repository->createTree(\@inprogress);
my $openfound=0;
my $closedfound=0;
my $assignedfound=0;
my $inprogressfound=0;
# now recreate the root tree
for my $r (@root)
{
if ($r->{name} eq "open")
{
$r->{ref}=$openhash;
$openfound=1;
}
if ($r->{name} eq "closed")
{
$r->{ref}=$closedhash;
$closedfound=1;
}
if ($r->{name} eq "assigned")
{
$r->{ref}=$assignedhash;
$assignedfound=1;
}
if ($r->{name} eq "inprogess")
{
$r->{ref}=$inprogresshash;
$inprogressfound=1;
}
}
if (!$openfound && defined($openhash))
{
my $t = {
path => "open",
ref => $openhash,
type => "tree",
mode => "040000"
};
push(@root, $t);
}
if (!$closedfound && defined($closedhash))
{
my $t = {
path => "closed",
ref => $closedhash,
type => "tree",
mode => "040000"
};
push(@root, $t);
}
if (!$assignedfound && defined($assignedhash))
{
my $t = {
path => "assigned",
ref => $assignedhash,
type => "tree",
mode => "040000"
};
push(@root, $t);
}
if (!$inprogressfound && defined($inprogresshash))
{
my $t = {
path => "inprogress",
ref => $inprogresshash,
type => "tree",
mode => "040000"
};
push(@root, $t);
}
my $roothash = $self->repository->createTree(\@root);
my $roothash = $self->_createTree();
# commit the issue
my $commit=$self->repository->createTreeCommit($roothash,$self->repository->getBranchRef("heads/issues") || "start", "ADD: " . $issue->subject);
@ -455,13 +567,13 @@ sub delete
$id=~/^[A-Z]+-(.*)$/;
my $hash=$1;
$self->_load();
my $found=0;
my $index=0;
my @root = $self->repository->getTree("issues");
# first search for the id
my @open = $self->repository->getTree("issues","open/");
for my $i (@open)
for my $i (@{$self->_open})
{
if (substr($i->{ref},0,8) eq $hash)
{
@ -472,14 +584,14 @@ sub delete
}
if ($found)
{
splice @open,$index,1;
splice @{$self->_open},$index,1;
}
$index=0;
my @assigned = $self->repository->getTree("issues","assigned/");
if (!$found)
{
for my $i (@assigned)
for my $i (@{$self->_assigned})
{
if (substr($i->{ref},0,8) eq $hash)
{
@ -490,15 +602,15 @@ sub delete
}
if ($found)
{
splice @assigned,$index,1;
splice @{$self->_assigned},$index,1;
}
}
$index=0;
my @inprogress = $self->repository->getTree("issues","inprogress");
if (!$found)
{
for my $i (@inprogress)
for my $i (@{$self->_inprogress})
{
if (substr($i->{ref},0,8) eq $hash)
{
@ -509,15 +621,15 @@ sub delete
}
if ($found)
{
splice @inprogress,$index,1;
splice @{$self->_inprogress},$index,1;
}
}
$index=0;
my @closed = $self->repository->getTree("issues","closed/");
if (!$found)
{
for my $i (@closed)
for my $i (@{$self->_closed})
{
if (substr($i->{ref},0,8) eq $hash)
{
@ -528,137 +640,11 @@ sub delete
}
if ($found)
{
splice @closed,$index,1;
splice @{$self->_closed},$index,1;
}
}
$self->recreate(\@root);
$self->recreate(\@open);
$self->recreate(\@closed);
$self->recreate(\@assigned);
$self->recreate(\@inprogress);
# now recreate tree structure
my $openhash = $self->repository->createTree(\@open);
my $closedhash = $self->repository->createTree(\@closed);
my $assignedhash = $self->repository->createTree(\@assigned);
my $inprogresshash= $self->repository->createTree(\@inprogress);
my $openfound=0;
my $closedfound=0;
my $assignedfound=0;
my $inprogressfound=0;
# now recreate the root tree
my @fordelete;
my $i=0;
for my $r (@root)
{
if ($r->{name} eq "open")
{
if (defined($openhash) && length($openhash)==40)
{
$r->{ref}=$openhash;
$openfound=1;
}
else
{
push(@fordelete, $i);
}
}
if ($r->{name} eq "closed")
{
if (defined($closedhash) && length($closedhash)==40)
{
$r->{ref}=$closedhash;
$closedfound=1;
}
else
{
push(@fordelete, $i);
}
}
if ($r->{name} eq "assigned")
{
if (defined($assignedhash) && length($assignedhash)==40)
{
$r->{ref}=$assignedhash;
$assignedfound=1;
}
else
{
push(@fordelete, $i);
}
}
if ($r->{name} eq "inprogess")
{
if (defined($inprogresshash) && length($inprogresshash)==40)
{
$r->{ref}=$inprogresshash;
$inprogressfound=1;
}
else
{
push(@fordelete, $i);
}
}
$i++;
}
for my $d (@fordelete)
{
splice @root, $d, 1;
}
if (!$openfound && defined($openhash))
{
print "adding open " . $openhash . "\n";
my $t = {
path => "open",
ref => $openhash,
type => "tree",
mode => "040000"
};
push(@root, $t);
}
if (!$closedfound && defined($closedhash))
{
my $t = {
path => "closed",
ref => $closedhash,
type => "tree",
mode => "040000"
};
push(@root, $t);
}
if (!$assignedfound && defined($assignedhash))
{
my $t = {
path => "assigned",
ref => $assignedhash,
type => "tree",
mode => "040000"
};
push(@root, $t);
}
if (!$inprogressfound && defined($inprogresshash))
{
my $t = {
path => "inprogress",
ref => $inprogresshash,
type => "tree",
mode => "040000"
};
push(@root, $t);
}
my $roothash = $self->repository->createTree(\@root);
my $roothash = $self->_createTree();
# commit the issue
my $commit=$self->repository->createTreeCommit($roothash,$self->repository->getBranchRef("heads/issues") || "start", "RM: " . $id);