| 1 | package Git::IssueManager::Issue; | ||
| 2 | #ABSTRACT: class representing an Issue | ||
| 3 | use Moose; | ||
| 4 | use File::Basename; | ||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | has 'subject' => (is => 'rw', isa => 'Str', required => 1, trigger => sub { | ||
| 9 | my ($self, $new, $old) = @_; | ||
| 10 | |||
| 11 | die("subject exceeds 50 chars") unless length($new) < 51; | ||
| 12 | |||
| 13 | } | ||
| 14 | ); | ||
| 15 | |||
| 16 | has 'priority' => (is => 'rw', isa => 'Str', default => 'low', trigger => sub { | ||
| 17 | my ($self, $new, $old) = @_; | ||
| 18 | |||
| 19 | die("unknown value (" . $new . ")") unless lc($new) eq "urgent" || lc($new) eq "high" || | ||
| 20 | lc($new) eq "medium" || lc($new) eq "low"; | ||
| 21 | }); | ||
| 22 | |||
| 23 | has 'severity' => (is => 'rw', isa => 'Str', default => 'low',trigger => sub { | ||
| 24 | my ($self, $new, $old) = @_; | ||
| 25 | |||
| 26 | die("unknown value (" . $new . ")") unless lc($new) eq "critical" || lc($new) eq "high" || | ||
| 27 | lc($new) eq "medium" || lc($new) eq "low"; | ||
| 28 | }); | ||
| 29 | |||
| 30 | has 'type' => (is => 'rw', isa => 'Str', default => 'bug', trigger => sub { | ||
| 31 | my ($self, $new, $old) = @_; | ||
| 32 | |||
| 33 | die("unknown value (" . $new . ")") unless lc($new) eq "bug" || lc($new) eq "security-bug" || | ||
| 34 | lc($new) eq "improvement" || lc($new) eq "feature" || | ||
| 35 | lc($new) eq "task"; | ||
| 36 | }); | ||
| 37 | |||
| 38 | has 'status' => (is => 'rw', isa => 'Str', default => 'open', trigger => sub { | ||
| 39 | my ($self, $new, $old) = @_; | ||
| 40 | |||
| 41 | die("unknown value (" . $new . ")") unless lc($new) eq "open" || lc($new) eq "assigned" || | ||
| 42 | lc($new) eq "inprogress" || lc($new) eq "closed"; | ||
| 43 | }); | ||
| 44 | |||
| 45 | has 'substatus' => (is => 'rw', isa => 'Str', default => 'none', trigger => sub { | ||
| 46 | my ($self, $new, $old) = @_; | ||
| 47 | |||
| 48 | die("unknown value (" . $new . ")") unless lc($new) eq "none" || lc($new) eq "fixed" || | ||
| 49 | lc($new) eq "wontfix"; | ||
| 50 | }); | ||
| 51 | |||
| 52 | has 'comment' => (is => 'rw', isa=>'Str', default => ""); | ||
| 53 | |||
| 54 | |||
| 55 | has 'description' => (is => 'rw', isa => 'Str', default => ""); | ||
| 56 | |||
| 57 | has 'tags' => (is => 'rw', isa => 'ArrayRef[Str]', default => sub {return[];}); | ||
| 58 | |||
| 59 | has 'attachements' => (is => 'rw', isa=> 'ArrayRef[Str]', default => sub{return [];}); | ||
| 60 | |||
| 61 | |||
| 62 | has 'author' => (is=> 'rw', isa => 'Str', default => ""); | ||
| 63 | |||
| 64 | has 'author_email' => (is => 'rw', isa => 'Str', default => ""); | ||
| 65 | |||
| 66 | has 'worker' => (is => 'rw', isa => 'Str', default => ""); | ||
| 67 | |||
| 68 | has 'worker_email' => ( is => 'rw', isa => 'Str', default =>""); | ||
| 69 | |||
| 70 | has 'creation_date' => (is => 'rw', isa=>'DateTime',default => sub{return DateTime->now();}); | ||
| 71 | |||
| 72 | has 'closed_date' => (is => 'rw', isa=>'DateTime',default => sub{return DateTime->now();}); | ||
| 73 | |||
| 74 | has 'last_change_date' => (is => 'rw', isa=>'DateTime',default => sub{return DateTime->now();}); | ||
| 75 | |||
| 76 | has 'id' => (is => 'rw', isa => 'Str', default => ""); | ||
| 77 | |||
| 78 | has 'estimated_time' => (is => 'rw', isa => 'Num', default => 0, trigger => sub { | ||
| 79 | my ($self, $new, $old) = @_; | ||
| 80 | |||
| 81 | die("unknown value (" . $new . ")") unless $new >=0 ; | ||
| 82 | }); | ||
| 83 | |||
| 84 | has 'working_time' => (is => 'rw', isa=>'Num', default => 0, trigger => sub { | ||
| 85 | my ($self, $new, $old) = @_; | ||
| 86 | |||
| 87 | die("unknown value (" . $new . ")") unless $new >= 0; | ||
| 88 | }); | ||
| 89 | |||
| 90 | |||
| 91 | sub addTag | ||
| 92 | { | ||
| 93 | my $self = shift; | ||
| 94 | my $tag = shift; | ||
| 95 | |||
| 96 | die("no tag given") unless defined($tag); | ||
| 97 | die("too many tags") unless @{$self->tags}<11; | ||
| 98 | die("tag exceeds 20 chars") unless length($tag) < 21; | ||
| 99 | |||
| 100 | push (@{$self->tags}, $tag); | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | sub delTag | ||
| 105 | { | ||
| 106 | my $self = shift; | ||
| 107 | my $tag = shift; | ||
| 108 | |||
| 109 | die("no tag given") unless defined($tag); | ||
| 110 | |||
| 111 | my $i = 0; | ||
| 112 | |||
| 113 | for my $t (@{$self->tags}) | ||
| 114 | { | ||
| 115 | if ($t eq $tag) | ||
| 116 | { | ||
| 117 | last; | ||
| 118 | } | ||
| 119 | $i++; | ||
| 120 | } | ||
| 121 | |||
| 122 | splice(@{$self->tags},$i,1); | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | sub addAttachement | ||
| 127 | { | ||
| 128 | my $self = shift; | ||
| 129 | my $attachment = shift; | ||
| 130 | |||
| 131 | die("no attachment given") unless defined($attachment); | ||
| 132 | die("file does not exist") unless -e $attachment; | ||
| 133 | |||
| 134 | |||
| 135 | push (@{$self->attachements}, $attachment); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub delAttachment | ||
| 139 | { | ||
| 140 | my $self = shift; | ||
| 141 | my $attachment = shift; | ||
| 142 | |||
| 143 | die("no attachment given") unless defined($attachment); | ||
| 144 | |||
| 145 | my $i = 0; | ||
| 146 | |||
| 147 | for my $a (@{$self->attachments}) | ||
| 148 | { | ||
| 149 | if ($a eq $attachment) | ||
| 150 | { | ||
| 151 | last; | ||
| 152 | } | ||
| 153 | $i++; | ||
| 154 | } | ||
| 155 | |||
| 156 | splice(@{$self->attachments},$i,1); | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | sub _createAttachmentTree | ||
| 161 | { | ||
| 162 | my $self = shift; | ||
| 163 | my $repository = shift; | ||
| 164 | my @tree; | ||
| 165 | |||
| 166 | for my $a (@{$self->attachments}) | ||
| 167 | { | ||
| 168 | my $hash = $repository->createFileObjectFromFile($a); | ||
| 169 | my $t = { | ||
| 170 | ref => $hash, | ||
| 171 | path => fileparse($a), | ||
| 172 | mode => "100644", | ||
| 173 | type => "blob" | ||
| 174 | }; | ||
| 175 | push(@tree, $t); | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | return $repository->createTree(\@tree); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub createIssue | ||
| 183 | { | ||
| 184 | my $self = shift; | ||
| 185 | my $repository = shift; | ||
| 186 | |||
| 187 | die("No Git::LowLevel object given") unless ref($repository) eq "Git::LowLevel"; | ||
| 188 | |||
| 189 | my $ref = $repository->getReference('refs/heads/issues'); | ||
| 190 | my $root = $ref->getTree(); | ||
| 191 | |||
| 192 | my $issueTree = $root->newTree(); | ||
| 193 | my $path = $self->subject; | ||
| 194 | $path=~s/\s/_/g; | ||
| 195 | $issueTree->path($self->subject); | ||
| 196 | |||
| 197 | my $subject = $issueTree->newBlob(); | ||
| 198 | $subject->path("subject"); | ||
| 199 | $subject->_content($self->subject); | ||
| 200 | $issueTree->add($subject); | ||
| 201 | |||
| 202 | my $priority = $issueTree->newBlob(); | ||
| 203 | $priority->path("priority"); | ||
| 204 | $priority->_content($self->priority); | ||
| 205 | $issueTree->add($priority); | ||
| 206 | |||
| 207 | my $severity = $issueTree->newBlob(); | ||
| 208 | $severity->path("severity"); | ||
| 209 | $severity->_content($self->severity); | ||
| 210 | $issueTree->add($severity); | ||
| 211 | |||
| 212 | my $type = $issueTree->newBlob(); | ||
| 213 | $type->path("type"); | ||
| 214 | $type->_content($self->type); | ||
| 215 | $issueTree->add($type); | ||
| 216 | |||
| 217 | if (defined($self->substatus) && length($self->substatus) > 0) | ||
| 218 | { | ||
| 219 | my $substatus = $issueTree->newBlob(); | ||
| 220 | $substatus->path("substatus"); | ||
| 221 | $substatus->_content($self->substatus); | ||
| 222 | $issueTree->add($substatus); | ||
| 223 | } | ||
| 224 | |||
| 225 | if (defined($self->comment) && length($self->comment)> 0) | ||
| 226 | { | ||
| 227 | my $comment = $issueTree->newBlob(); | ||
| 228 | $comment->path("comment"); | ||
| 229 | $comment->_content($self->comment); | ||
| 230 | $issueTree->add($comment); | ||
| 231 | } | ||
| 232 | |||
| 233 | if (defined($self->description) && length($self->description)> 0) | ||
| 234 | { | ||
| 235 | my $description = $issueTree->newBlob(); | ||
| 236 | $description->path("description"); | ||
| 237 | $description->_content($self->description); | ||
| 238 | $issueTree->add($description); | ||
| 239 | } | ||
| 240 | |||
| 241 | if (defined($self->worker) && length($self->worker)> 0) | ||
| 242 | { | ||
| 243 | my $worker = $issueTree->newBlob(); | ||
| 244 | $worker->path("worker"); | ||
| 245 | $worker->_content($self->worker . "<" . $self->worker_email . ">"); | ||
| 246 | $issueTree->add($worker); | ||
| 247 | } | ||
| 248 | |||
| 249 | my $estimated = $issueTree->newBlob(); | ||
| 250 | $estimated->path("estimated"); | ||
| 251 | $estimated->_content($self->estimated_time); | ||
| 252 | $issueTree->add($estimated); | ||
| 253 | |||
| 254 | my $working_time = $issueTree->newBlob(); | ||
| 255 | $working_time->path("working"); | ||
| 256 | $working_time->_content($self->working_time); | ||
| 257 | $issueTree->add($working_time); | ||
| 258 | |||
| 259 | if (defined($self->tags) && @{$self->tags}> 0) | ||
| 260 | { | ||
| 261 | my $tags = $issueTree->newBlob(); | ||
| 262 | $tags->path("tags"); | ||
| 263 | $tags->_content(join "\n", @{$self->tags}); | ||
| 264 | $issueTree->add($tags); | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | return $issueTree; | ||
| 269 | } | ||
| 270 | |||
| 271 | 1; | ||
| 272 | |||
| 273 | __END__ |