# Type: Input # Category: Issue # Description: Allows to add notes to issues # Author: Andriy Lesyuk new Orangutan::Context( response => sub { my ($context, $user, $formatted, $original) = @_; my $message = ($context->GetField('issue')) ? $original : $formatted; my @result = ( ); my $regexp = 'issue'; my $trackers = $main::config->Get('redmine', '_trackers_regexp'); if ($trackers) { $regexp = "(?:issue|$trackers)"; } my @regexps = ( '(?:(?:Add|insert) )?(?:(?:the|an?) )?(?:comment|answer|reply|response|notes|this) '. '(?:(?:in|on|for|to) )?(?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)!*\.*:?' ); foreach my $regexp (@regexps) { if ($message =~ /^$regexp$/i) { push(@result, 1); push(@result, $1); last; } } if ((scalar @result == 0) && $context->GetField('issue')) { push(@result, 2); push(@result, $message); } return @result; }, handler => sub { my ($context, $user, $item, $value) = @_; # Switch to comment entering mode if ($item == 1) { my $issue = $context->GetField('issue'); if ($issue) { my $format = Orangutan::Context::Random( 'You did not enter comment for #%d!..', 'First post comment for #%d!', 'Hey! What about #%d?!..' ); $user->SendMessage(sprintf($format, $issue->{'id'})); return 1; } $issue = $main::query->GetIssue($value); if ($issue) { if (!$main::query->Can('add_issue_notes', $user, $issue->{'project_id'})) { $user->SendMessage([ 'Sorry, no permissions to add notes to this issue!', "You can't add notes to this issue!", 'You may not comment this issue!', ]); return 1; } } else { $user->SendMessage([ "Can't comment non-existent issue...", "Can't find this issue...", 'There is no such issue!' ]); return 1; } $context->SetField('issue', $issue); my $message = Orangutan::Context::Random( 'Send your comment in a single message', 'Send me the notes in a single message', 'Go ahead! Put your comment here', 'Enter the notes' ); $message .= ':'; $user->SendMessage($message); $context->SetTimeout(time + 1800); # Accept comment text } else { my $issue = $context->UnsetField('issue'); my ($journal, @details) = $main::query->UpdateIssue($user, $issue->{'id'}, notes => $value); if (defined($journal)) { my %watchers = ( ); $watchers{$issue->{'author_name'}}++; if ($issue->{'assigned_to_name'}) { $watchers{$issue->{'assigned_to_name'}}++; } if ($main::query->GetWatchers($issue->{'id'})) { while (defined(my $watcher = $main::query->Next)) { $watchers{$watcher->{'login'}}++; } } foreach my $username (keys %watchers) { my $user = $main::users->GetUserLogin($username); if (defined($user)) { $user->FireEvent('issue', 'change', 'add', $issue, $journal, @details); } } $user->SendMessage([ 'Added.', 'Saved.', 'Submitted.' ]); $context->SetField('journal', $journal); $context->SetTimeout; } else { $user->SendMessage([ "Oops! Can't save comment... :(", 'Could not save comment... :S' ]); $context->SetTimeout; } } return 1; }, ontimeout => sub { my ($context, $user) = @_; $context->UnsetField('issue'); $context->SetTimeout; $user->SendMessage([ 'Ok... Forget it!', 'Never mind...', 'Too late...' ]); return 1; }, undo => sub { my ($context, $user) = @_; my $issue = $context->UnsetField('issue'); if ($issue) { $context->UnsetField('issue'); $user->SendMessage([ 'Ok...', 'No problem!', 'I see...' ]); } else { my $journal = $context->UnsetField('journal'); if ($journal) { my $deleted = $main::query->RemoveNotes($journal->{'id'}); $issue = $main::query->GetIssue($journal->{'issue_id'}); if ($issue) { my %watchers = ( ); $watchers{$issue->{'author_name'}}++; if ($issue->{'assigned_to_name'}) { $watchers{$issue->{'assigned_to_name'}}++; } if ($main::query->GetWatchers($issue->{'id'})) { while (defined(my $watcher = $main::query->Next)) { $watchers{$watcher->{'login'}}++; } } foreach my $username (keys %watchers) { my $user = $main::users->GetUserLogin($username); if (defined($user)) { $user->FireEvent('issue', 'change', $deleted ? 'remove' : 'change', $issue, $journal); } } } my $message = Orangutan::Context::Random('Got it!', 'Ok...', 'Oops!'); $message .= ' '; $message .= Orangutan::Context::Random('Removed.', 'Fixed.'); $user->SendMessage($message); } } }, help => { title => 'How can I comment on an issue?', question => '^(?:How )?(?:(?:can|do) I |to )(?:add (?:(?:the|an?) )?)?(?:comment|answer|reply|response|notes) '. '(?:(?:on|in|to|for) )?(?:(?:the|an?) )?issues?\?*!*\.*$', answer => "Orangutan allows you to add notes to an issue directly from the chat window.\n". "Check the following:\n". " o Add notes to #658\n". " o Comment on #859\n". " o Reply in #51\n". 'These queries will "switch" Orangutan to text entering mode...', weight => 1080 }, weight => -10, copy => 1 ); # kate: syntax perl