1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-19 13:22:42 +01:00

Write separate comments for every action in Audit

Summary:
Ref T4896. Depends on D10023. Prepares the code for the final migration.

The transaction table stores one row per distinct effect (e.g., add CCs) rather than one row per user action (e.g., "add CCs + comment"). We can double-read that table as long as the code doesn't expect transactions/comments to have multiple different effects, and doesn't try to write any such rows.

Everywhere that we were writing a big "X + Y" comment, write two separate "X" and "Y" comments instead. Like D10023, this disrupts the UI a little (you get more boxes), but that will be resolved once the rendering code swaps over. Otherwise, this retains the existing behavior.

Test Plan:
- Used `diffusion.createcomment` to add comments, raise concern, and accept.
- Previewed commenting, adding auditors/ccs, accepting, raising concern.
- Actually performed commenting, adding auditors/ccs, accepting, raising concern.
- Added a user with mentions.
- Added an explicit CC and a mention user.

Reviewers: btrahan, joshuaspence

Reviewed By: joshuaspence

Subscribers: epriestley

Maniphest Tasks: T4896

Differential Revision: https://secure.phabricator.com/D10052
This commit is contained in:
epriestley 2014-07-28 15:00:18 -07:00
parent bf39748011
commit 608e1d20b4
5 changed files with 155 additions and 113 deletions

View file

@ -15,41 +15,55 @@ final class PhabricatorAuditAddCommentController
$commit = id(new PhabricatorRepositoryCommit())->loadOneWhere( $commit = id(new PhabricatorRepositoryCommit())->loadOneWhere(
'phid = %s', 'phid = %s',
$commit_phid); $commit_phid);
if (!$commit) { if (!$commit) {
return new Aphront404Response(); return new Aphront404Response();
} }
$phids = array($commit_phid); $phids = array($commit_phid);
$action = $request->getStr('action'); $comments = array();
$comment = id(new PhabricatorAuditComment())
->setAction($action)
->setContent($request->getStr('content'));
// make sure we only add auditors or ccs if the action matches // make sure we only add auditors or ccs if the action matches
$action = $request->getStr('action');
switch ($action) { switch ($action) {
case 'add_auditors': case PhabricatorAuditActionConstants::ADD_AUDITORS:
$auditors = $request->getArr('auditors'); $auditors = $request->getArr('auditors');
$ccs = array(); $comments[] = id(new PhabricatorAuditComment())
->setAction(PhabricatorAuditActionConstants::ADD_AUDITORS)
->setMetadata(
array(
PhabricatorAuditComment::METADATA_ADDED_AUDITORS => $auditors,
));
break; break;
case 'add_ccs': case PhabricatorAuditActionConstants::ADD_CCS:
$auditors = array();
$ccs = $request->getArr('ccs'); $ccs = $request->getArr('ccs');
$comments[] = id(new PhabricatorAuditComment())
->setAction(PhabricatorAuditActionConstants::ADD_CCS)
->setMetadata(
array(
PhabricatorAuditComment::METADATA_ADDED_CCS => $ccs,
));
break;
case PhabricatorAuditActionConstants::COMMENT:
// We'll deal with this below.
break; break;
default: default:
$auditors = array(); $comments[] = id(new PhabricatorAuditComment())
$ccs = array(); ->setAction($action);
break; break;
} }
$content = $request->getStr('content');
if (strlen($content)) {
$comments[] = id(new PhabricatorAuditComment())
->setAction(PhabricatorAuditActionConstants::COMMENT)
->setContent($content);
}
id(new PhabricatorAuditCommentEditor($commit)) id(new PhabricatorAuditCommentEditor($commit))
->setActor($user) ->setActor($user)
->setAttachInlineComments(true) ->setAttachInlineComments(true)
->addAuditors($auditors) ->addComments($comments);
->addCCs($ccs)
->addComment($comment);
$handles = $this->loadViewerHandles($phids); $handles = $this->loadViewerHandles($phids);
$uri = $handles[$commit_phid]->getURI(); $uri = $handles[$commit_phid]->getURI();

View file

@ -20,38 +20,58 @@ final class PhabricatorAuditPreviewController
$action = $request->getStr('action'); $action = $request->getStr('action');
$comment = id(new PhabricatorAuditComment())
->setActorPHID($user->getPHID())
->setTargetPHID($commit->getPHID())
->setAction($action)
->setContent($request->getStr('content'));
$phids = array( $phids = array(
$user->getPHID(), $user->getPHID(),
$commit->getPHID(), $commit->getPHID(),
); );
$comments = array();
if ($action != PhabricatorAuditActionConstants::COMMENT) {
$action_comment = id(new PhabricatorAuditComment())
->setActorPHID($user->getPHID())
->setTargetPHID($commit->getPHID())
->setAction($action);
$auditors = $request->getStrList('auditors'); $auditors = $request->getStrList('auditors');
if ($action == PhabricatorAuditActionConstants::ADD_AUDITORS && $auditors) { if ($action == PhabricatorAuditActionConstants::ADD_AUDITORS &&
$comment->setMetadata(array( $auditors) {
$action_comment->setMetadata(array(
PhabricatorAuditComment::METADATA_ADDED_AUDITORS => $auditors)); PhabricatorAuditComment::METADATA_ADDED_AUDITORS => $auditors));
$phids = array_merge($phids, $auditors); $phids = array_merge($phids, $auditors);
} }
$ccs = $request->getStrList('ccs'); $ccs = $request->getStrList('ccs');
if ($action == PhabricatorAuditActionConstants::ADD_CCS && $ccs) { if ($action == PhabricatorAuditActionConstants::ADD_CCS && $ccs) {
$comment->setMetadata(array( $action_comment->setMetadata(array(
PhabricatorAuditComment::METADATA_ADDED_CCS => $ccs)); PhabricatorAuditComment::METADATA_ADDED_CCS => $ccs));
$phids = array_merge($phids, $ccs); $phids = array_merge($phids, $ccs);
} }
$comments[] = $action_comment;
}
$content = $request->getStr('content');
if (strlen($content)) {
$comments[] = id(new PhabricatorAuditComment())
->setActorPHID($user->getPHID())
->setTargetPHID($commit->getPHID())
->setAction(PhabricatorAuditActionConstants::COMMENT)
->setContent($content);
}
$engine = new PhabricatorMarkupEngine(); $engine = new PhabricatorMarkupEngine();
$engine->setViewer($user); $engine->setViewer($user);
foreach ($comments as $comment) {
$engine->addObject( $engine->addObject(
$comment, $comment,
PhabricatorAuditComment::MARKUP_FIELD_BODY); PhabricatorAuditComment::MARKUP_FIELD_BODY);
}
$engine->process(); $engine->process();
$views = array();
foreach ($comments as $comment) {
$view = id(new DiffusionCommentView()) $view = id(new DiffusionCommentView())
->setMarkupEngine($engine) ->setMarkupEngine($engine)
->setUser($user) ->setUser($user)
@ -59,18 +79,22 @@ final class PhabricatorAuditPreviewController
->setIsPreview(true); ->setIsPreview(true);
$phids = array_merge($phids, $view->getRequiredHandlePHIDs()); $phids = array_merge($phids, $view->getRequiredHandlePHIDs());
$views[] = $view;
}
$handles = $this->loadViewerHandles($phids); $handles = $this->loadViewerHandles($phids);
foreach ($views as $view) {
$view->setHandles($handles); $view->setHandles($handles);
}
id(new PhabricatorDraft()) id(new PhabricatorDraft())
->setAuthorPHID($comment->getActorPHID()) ->setAuthorPHID($user->getPHID())
->setDraftKey('diffusion-audit-'.$this->id) ->setDraftKey('diffusion-audit-'.$this->id)
->setDraft($comment->getContent()) ->setDraft($content)
->replaceOrDelete(); ->replaceOrDelete();
return id(new AphrontAjaxResponse()) return id(new AphrontAjaxResponse())->setContent(hsprintf('%s', $views));
->setContent($view->render());
} }
} }

View file

@ -3,11 +3,7 @@
final class PhabricatorAuditCommentEditor extends PhabricatorEditor { final class PhabricatorAuditCommentEditor extends PhabricatorEditor {
private $commit; private $commit;
private $attachInlineComments; private $attachInlineComments;
private $auditors = array();
private $ccs = array();
private $noEmail; private $noEmail;
public function __construct(PhabricatorRepositoryCommit $commit) { public function __construct(PhabricatorRepositoryCommit $commit) {
@ -15,16 +11,6 @@ final class PhabricatorAuditCommentEditor extends PhabricatorEditor {
return $this; return $this;
} }
public function addAuditors(array $auditor_phids) {
$this->auditors = array_merge($this->auditors, $auditor_phids);
return $this;
}
public function addCCs(array $cc_phids) {
$this->ccs = array_merge($this->ccs, $cc_phids);
return $this;
}
public function setAttachInlineComments($attach_inline_comments) { public function setAttachInlineComments($attach_inline_comments) {
$this->attachInlineComments = $attach_inline_comments; $this->attachInlineComments = $attach_inline_comments;
return $this; return $this;
@ -35,7 +21,8 @@ final class PhabricatorAuditCommentEditor extends PhabricatorEditor {
return $this; return $this;
} }
public function addComment(PhabricatorAuditComment $comment) { public function addComments(array $comments) {
assert_instances_of($comments, 'PhabricatorAuditComment');
$commit = $this->commit; $commit = $this->commit;
$actor = $this->getActor(); $actor = $this->getActor();
@ -51,38 +38,26 @@ final class PhabricatorAuditCommentEditor extends PhabricatorEditor {
$commit->getPHID()); $commit->getPHID());
} }
$comment $content_blocks = array();
->setActorPHID($actor->getPHID()) foreach ($comments as $comment) {
->setTargetPHID($commit->getPHID()) $content_blocks[] = $comment->getContent();
->save(); }
$content_blocks = array($comment->getContent());
foreach ($inline_comments as $inline) { foreach ($inline_comments as $inline) {
$content_blocks[] = $inline->getContent(); $content_blocks[] = $inline->getContent();
} }
$ccs = $this->ccs;
$auditors = $this->auditors;
$metadata = $comment->getMetadata();
$metacc = array();
// Find any "@mentions" in the content blocks. // Find any "@mentions" in the content blocks.
$mention_ccs = PhabricatorMarkupEngine::extractPHIDsFromMentions( $mention_ccs = PhabricatorMarkupEngine::extractPHIDsFromMentions(
$this->getActor(), $this->getActor(),
$content_blocks); $content_blocks);
if ($mention_ccs) { if ($mention_ccs) {
$metacc = idx( $comments[] = id(new PhabricatorAuditComment())
$metadata, ->setAction(PhabricatorAuditActionConstants::ADD_CCS)
PhabricatorAuditComment::METADATA_ADDED_CCS, ->setMetadata(
array()); array(
foreach ($mention_ccs as $cc_phid) { PhabricatorAuditComment::METADATA_ADDED_CCS => $mention_ccs,
$metacc[] = $cc_phid; ));
}
}
if ($metacc) {
$ccs = array_merge($ccs, $metacc);
} }
// When an actor submits an audit comment, we update all the audit requests // When an actor submits an audit comment, we update all the audit requests
@ -101,14 +76,25 @@ final class PhabricatorAuditCommentEditor extends PhabricatorEditor {
$action = $comment->getAction(); $action = $comment->getAction();
// TODO: We should validate the action, currently we allow anyone to, e.g., // TODO: We should validate the action, currently we allow anyone to, e.g.,
// close an audit if they muck with form parameters. I'll followup with this // close an audit if they muck with form parameters. I'll followup with this
// and handle the no-effect cases (e.g., closing and already-closed audit). // and handle the no-effect cases (e.g., closing and already-closed audit).
$actor_is_author = ($actor->getPHID() == $commit->getAuthorPHID()); $actor_is_author = ($actor->getPHID() == $commit->getAuthorPHID());
// Pick a meaningful action, if we have one.
$action = PhabricatorAuditActionConstants::COMMENT;
foreach ($comments as $comment) {
switch ($comment->getAction()) {
case PhabricatorAuditActionConstants::CLOSE:
case PhabricatorAuditActionConstants::RESIGN:
case PhabricatorAuditActionConstants::ACCEPT:
case PhabricatorAuditActionConstants::CONCERN:
$action = $comment->getAction();
break;
}
}
if ($action == PhabricatorAuditActionConstants::CLOSE) { if ($action == PhabricatorAuditActionConstants::CLOSE) {
if (!PhabricatorEnv::getEnvConfig('audit.can-author-close-audit')) { if (!PhabricatorEnv::getEnvConfig('audit.can-author-close-audit')) {
throw new Exception('Cannot Close Audit without enabling'. throw new Exception('Cannot Close Audit without enabling'.
@ -219,33 +205,34 @@ final class PhabricatorAuditCommentEditor extends PhabricatorEditor {
} }
} }
$auditors = array();
$ccs = array();
foreach ($comments as $comment) {
$meta = $comment->getMetadata();
$auditor_phids = idx(
$meta,
PhabricatorAuditComment::METADATA_ADDED_AUDITORS,
array());
foreach ($auditor_phids as $phid) {
$auditors[] = $phid;
}
$cc_phids = idx(
$meta,
PhabricatorAuditComment::METADATA_ADDED_CCS,
array());
foreach ($cc_phids as $phid) {
$ccs[] = $phid;
}
}
$requests_by_auditor = mpull($requests, null, 'getAuditorPHID'); $requests_by_auditor = mpull($requests, null, 'getAuditorPHID');
$requests_phids = array_keys($requests_by_auditor); $requests_phids = array_keys($requests_by_auditor);
$ccs = array_diff($ccs, $requests_phids); $ccs = array_diff($ccs, $requests_phids);
$auditors = array_diff($auditors, $requests_phids); $auditors = array_diff($auditors, $requests_phids);
if ($action == PhabricatorAuditActionConstants::ADD_CCS) {
if ($ccs) {
$metadata[PhabricatorAuditComment::METADATA_ADDED_CCS] = $ccs;
$comment->setMetaData($metadata);
} else {
$comment->setAction(PhabricatorAuditActionConstants::COMMENT);
}
}
if ($action == PhabricatorAuditActionConstants::ADD_AUDITORS) {
if ($auditors) {
$metadata[PhabricatorAuditComment::METADATA_ADDED_AUDITORS]
= $auditors;
$comment->setMetaData($metadata);
} else {
$comment->setAction(PhabricatorAuditActionConstants::COMMENT);
}
}
$comment->save();
if ($auditors) { if ($auditors) {
foreach ($auditors as $auditor_phid) { foreach ($auditors as $auditor_phid) {
$audit_requested = PhabricatorAuditStatusConstants::AUDIT_REQUESTED; $audit_requested = PhabricatorAuditStatusConstants::AUDIT_REQUESTED;
@ -275,7 +262,13 @@ final class PhabricatorAuditCommentEditor extends PhabricatorEditor {
$commit->updateAuditStatus($requests); $commit->updateAuditStatus($requests);
$commit->save(); $commit->save();
$comments = array($comment); foreach ($comments as $comment) {
$comment
->setActorPHID($actor->getPHID())
->setTargetPHID($commit->getPHID())
->save();
}
foreach ($inline_comments as $inline) { foreach ($inline_comments as $inline) {
$xaction = id(new PhabricatorAuditComment()) $xaction = id(new PhabricatorAuditComment())
->setAction(PhabricatorAuditActionConstants::INLINE) ->setAction(PhabricatorAuditActionConstants::INLINE)
@ -307,7 +300,9 @@ final class PhabricatorAuditCommentEditor extends PhabricatorEditor {
$feed_dont_publish_phids = array_keys($feed_dont_publish_phids); $feed_dont_publish_phids = array_keys($feed_dont_publish_phids);
$feed_phids = array_diff($requests_phids, $feed_dont_publish_phids); $feed_phids = array_diff($requests_phids, $feed_dont_publish_phids);
foreach ($comments as $comment) {
$this->publishFeedStory($comment, $feed_phids); $this->publishFeedStory($comment, $feed_phids);
}
id(new PhabricatorSearchIndexer()) id(new PhabricatorSearchIndexer())
->queueDocumentForIndexing($commit->getPHID()); ->queueDocumentForIndexing($commit->getPHID());

View file

@ -45,7 +45,7 @@ final class PhabricatorAuditReplyHandler extends PhabricatorMailReplyHandler {
$editor->setActor($actor); $editor->setActor($actor);
$editor->setExcludeMailRecipientPHIDs( $editor->setExcludeMailRecipientPHIDs(
$this->getExcludeMailRecipientPHIDs()); $this->getExcludeMailRecipientPHIDs());
$editor->addComment($comment); $editor->addComments(array($comment));
} }
} }

View file

@ -69,14 +69,23 @@ final class DiffusionCreateCommentConduitAPIMethod
throw new ConduitException('ERR_BAD_ACTION'); throw new ConduitException('ERR_BAD_ACTION');
} }
$comment = id(new PhabricatorAuditComment()) $comments = array();
->setAction($action)
if ($action != PhabricatorAuditActionConstants::COMMENT) {
$comments[] = id(new PhabricatorAuditComment())
->setAction($action);
}
if (strlen($message)) {
$comments[] = id(new PhabricatorAuditComment())
->setAction(PhabricatorAuditActionConstants::COMMENT)
->setContent($message); ->setContent($message);
}
id(new PhabricatorAuditCommentEditor($commit)) id(new PhabricatorAuditCommentEditor($commit))
->setActor($request->getUser()) ->setActor($request->getUser())
->setNoEmail($request->getValue('silent')) ->setNoEmail($request->getValue('silent'))
->addComment($comment); ->addComments($comments);
return true; return true;
// get the full uri of the comment? // get the full uri of the comment?