# Type: Input # Category: Issue # Description: Lets assign issue to some user # Author: Andriy Lesyuk new Orangutan::Context( response => sub { my ($context, $user, $message) = @_; my @result = ( ); my $regexp = 'issue'; my $trackers = $main::config->Get('redmine', '_trackers_regexp'); if ($trackers) { $regexp = "(?:issue|$trackers)"; } my @regexps = ( '(?:I|([^ ]+))(?:\'ll| will)? takes? (?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)!*\.*', # assign $user $issue '(?:Re)?assign (?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+) to (?:me|([^ ]+?))!*\.*', # assign $issue $user '(?:Un|de)assign (?:(?:the )?'.$regexp.' )?(?:#|No\.? ?)?([0-9]+)!*\.*' # deassign $issue ); for (my $i = 0; $i < scalar @regexps; $i++) { if ($message =~ /^$regexps[$i]$/i) { my ($username, $issueid) = (undef, undef); if ($i < 2) { if ($i == 0) { ($username, $issueid) = ($1, $2); } else { ($username, $issueid) = ($2, $1); } if (defined($username) && ($username =~ /^(?:no one|nobody)$/i)) { $username = undef; push(@result, 2); } else { push(@result, 1); } } else { $issueid = $1; push(@result, 2); } push(@result, $issueid); if (defined($username)) { my ($id, $login) = $main::query->UserExists($username); if ($id) { push(@result, $id); push(@result, $login); } else { push(@result, $username); push(@result, undef); } } else { push(@result, undef); push(@result, undef); } last; } } return @result; }, handler => sub { my ($context, $user, $item, $issueid, $userid, $username) = @_; my $issue = $main::query->GetIssue($issueid); if ($issue) { if (!$main::query->Can('edit_issues', $user, $issue->{'project_id'})) { $user->SendMessage([ 'You have no permissions to do this...', "You can't assign this issue!", "You can't do this!.." ]); return; } } else { $user->SendMessage([ 'Hm... But such issue does not exist... :S', "Can't find this issue...", 'No such issue!' ]); return; } # Assign if ($item == 1) { if (!defined($userid)) { $userid = $user->GetUserID; $username = $user->GetLogin; } elsif ($userid =~ /^[0-9]+$/) { if (!$main::query->Can('view_issues', $userid, $issue->{'project_id'})) { $user->SendMessage([ 'Even if I assigned this issue to this user the user would not be able to see the issue!..', 'This user is not a member of the project the issue belongs to...', 'This user cannot access this issue!' ]); return; } } else { $user->SendMessage([ 'There is no such user!', "Can't find such user!", 'No such user!' ]); return; } if ($userid == $issue->{'assigned_to_id'}) { $user->SendMessage([ 'The user is already assigned to the issue...', 'Already assigned...', 'Nothing to change!' ]); return; } my ($journal, @details) = $main::query->UpdateIssue($user, $issueid, assigned_to_id => $userid); if (defined($journal) && (scalar @details > 0)) { my %watchers = ( ); $watchers{$issue->{$username}}++; $watchers{$issue->{'author_name'}}++; if ($issue->{'assigned_to_name'}) { $watchers{$issue->{'assigned_to_name'}}++; } if ($main::query->GetWatchers($issueid)) { while (defined(my $watcher = $main::query->Next)) { $watchers{$watcher->{'login'}}++; } } $issue->{'assigned_to_id'} = $userid; $issue->{'assigned_to_name'} = $username; if ($details[0]->{'prop_key'} eq 'assigned_to_id') { $details[0]->{'value_name'} = $username; } foreach my $username (keys %watchers) { my $user = $main::users->GetUserLogin($username); if (defined($user)) { $user->FireEvent('issue', 'change', 'add', $issue, $journal, @details); } } my $message = Orangutan::Context::Random('Ok... ', 'Done! ', 'Well... ', 'Fine. '); if ($userid == $user->GetUserID) { $message .= Orangutan::Context::Random( 'Wish you fix it fast!', 'Fix it easy!', 'Good luck!' ); } else { $message .= Orangutan::Context::Random( 'Now issue belongs to this user...', 'Now you can congrat this user...', 'Good luck to user. :)' ); } $user->SendMessage($message); } else { $user->SendMessage([ 'Something wrong happened with me... :S', "God, help me! I'm in trouble... :S", 'Ouch! I need a doctor!..', ]); } # Deassign } else { if (!defined($issue->{'assigned_to_id'})) { $user->SendMessage([ "Can't unassign the issue without assignee...", 'The issue is not assigned to anyone!', 'Already unassigned.' ]); return; } my ($journal, @details) = $main::query->UpdateIssue($user, $issueid, assigned_to_id => undef); if (defined($journal) && (scalar @details > 0)) { my %watchers = ( ); $watchers{$issue->{'author_name'}}++; if ($issue->{'assigned_to_name'}) { $watchers{$issue->{'assigned_to_name'}}++; } if ($main::query->GetWatchers($issueid)) { while (defined(my $watcher = $main::query->Next)) { $watchers{$watcher->{'login'}}++; } } $issue->{'assigned_to_id'} = undef; $issue->{'assigned_to_name'} = undef; foreach my $username (keys %watchers) { my $user = $main::users->GetUserLogin($username); if (defined($user)) { $user->FireEvent('issue', 'change', 'add', $issue, $journal, @details); } } my $message = Orangutan::Context::Random('Ok... ', 'Done! ', 'Well... '); $message .= Orangutan::Context::Random( 'Hope this issue will find its assignee...', 'Now the issue is assigned to no one!' ); $user->SendMessage($message); } else { $user->SendMessage([ "Something's wrong with me... :(", 'Call a doctor!' ]); } } }, help => { title => 'How can I assign an issue?', question => '^(?:How )?(?:(?:can|do) I |to )(?:re|un|de)?assign (?:(?:the|an?) )?issues?(?: to (?:someone|anybody))?\?*!*\.*$', answer => "Orangutan lets you assign issues.\n". "Check out samples:\n". " o Assign #896 to iyarchuk\n". " o Assign #258 to me!\n". " o S-Andy takes #856\n". " o I'll take #236\n". "You can also unassign issues:\n". " o Assign #729 to nobody\n". " o Unassign #458", weight => 1010 } ); # kate: syntax perl