diff --git a/src/applications/conpherence/editor/ConpherenceEditor.php b/src/applications/conpherence/editor/ConpherenceEditor.php --- a/src/applications/conpherence/editor/ConpherenceEditor.php +++ b/src/applications/conpherence/editor/ConpherenceEditor.php @@ -397,7 +397,7 @@ return $body; } - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return PhabricatorEnv::getEnvConfig('metamta.conpherence.subject-prefix'); } diff --git a/src/applications/differential/config/PhabricatorDifferentialConfigOptions.php b/src/applications/differential/config/PhabricatorDifferentialConfigOptions.php --- a/src/applications/differential/config/PhabricatorDifferentialConfigOptions.php +++ b/src/applications/differential/config/PhabricatorDifferentialConfigOptions.php @@ -243,7 +243,11 @@ 'metamta.differential.subject-prefix', 'string', '[Differential]') - ->setDescription(pht('Subject prefix for Differential mail.')), + ->setSummary(pht('Subject prefix template for Differential mail.')) + ->setDescription(pht('Available keys: id, title, repository, branch')) + ->addExample( + '[${repository}/${branch}] [Differential]', + pht('Use repository and branch')), $this->newOption( 'metamta.differential.attach-patches', 'bool', diff --git a/src/applications/differential/editor/DifferentialTransactionEditor.php b/src/applications/differential/editor/DifferentialTransactionEditor.php --- a/src/applications/differential/editor/DifferentialTransactionEditor.php +++ b/src/applications/differential/editor/DifferentialTransactionEditor.php @@ -1080,8 +1080,17 @@ return $action; } - protected function getMailSubjectPrefix() { - return PhabricatorEnv::getEnvConfig('metamta.differential.subject-prefix'); + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { + $branch = nonempty($object->getActiveDiff()->getBranch(), '-'); + $repository = $object->getRepository() ? + $object->getRepository()->getName() : '-'; + + return $this->mergeVariables( + PhabricatorEnv::getEnvConfig('metamta.differential.subject-prefix'), + array( + 'branch' => $branch, + 'repository' => $repository, + )); } protected function getMailThreadID(PhabricatorLiskDAO $object) { @@ -1109,6 +1118,42 @@ ->addHeader('Thread-Topic', $thread_topic); } + /** + * Convert a user-provided string with variables in it, like: + * + * ${project}/${branch} + * + * ...into a string with variables merged into it safely: + * + * myproject/mybranch + * + * Maybe move to libphutil? + * @param string User-provided pattern string containing `${variables}`. + * @param dict List of available replacement variables. + * @return string String with variables replaced safely into it. + * + */ + protected function mergeVariables($pattern, array $variables) { + $regexp = '/\\$\\{(?P[a-z\\.]+)\\}/'; + + $matches = null; + preg_match_all($regexp, $pattern, $matches); + + $argv = array(); + foreach ($matches['name'] as $name) { + if (!array_key_exists($name, $variables)) { + throw new Exception(pht("No such variable '%s'!", $name)); + } + $argv[] = $variables[$name]; + } + + $pattern = str_replace('%', '%%', $pattern); + $pattern = preg_replace($regexp, '%s', $pattern); + + return vsprintf($pattern, $argv); + } + + protected function buildMailBody( PhabricatorLiskDAO $object, array $xactions) { diff --git a/src/applications/files/editor/PhabricatorFileEditor.php b/src/applications/files/editor/PhabricatorFileEditor.php --- a/src/applications/files/editor/PhabricatorFileEditor.php +++ b/src/applications/files/editor/PhabricatorFileEditor.php @@ -42,7 +42,7 @@ return true; } - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return PhabricatorEnv::getEnvConfig('metamta.files.subject-prefix'); } diff --git a/src/applications/legalpad/editor/LegalpadDocumentEditor.php b/src/applications/legalpad/editor/LegalpadDocumentEditor.php --- a/src/applications/legalpad/editor/LegalpadDocumentEditor.php +++ b/src/applications/legalpad/editor/LegalpadDocumentEditor.php @@ -178,7 +178,7 @@ return $body; } - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return PhabricatorEnv::getEnvConfig('metamta.legalpad.subject-prefix'); } diff --git a/src/applications/macro/editor/PhabricatorMacroEditor.php b/src/applications/macro/editor/PhabricatorMacroEditor.php --- a/src/applications/macro/editor/PhabricatorMacroEditor.php +++ b/src/applications/macro/editor/PhabricatorMacroEditor.php @@ -152,7 +152,7 @@ return $body; } - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return PhabricatorEnv::getEnvConfig('metamta.macro.subject-prefix'); } diff --git a/src/applications/maniphest/editor/ManiphestTransactionEditor.php b/src/applications/maniphest/editor/ManiphestTransactionEditor.php --- a/src/applications/maniphest/editor/ManiphestTransactionEditor.php +++ b/src/applications/maniphest/editor/ManiphestTransactionEditor.php @@ -311,7 +311,7 @@ return $xactions; } - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return PhabricatorEnv::getEnvConfig('metamta.maniphest.subject-prefix'); } diff --git a/src/applications/paste/editor/PhabricatorPasteEditor.php b/src/applications/paste/editor/PhabricatorPasteEditor.php --- a/src/applications/paste/editor/PhabricatorPasteEditor.php +++ b/src/applications/paste/editor/PhabricatorPasteEditor.php @@ -126,7 +126,7 @@ return true; } - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return PhabricatorEnv::getEnvConfig('metamta.paste.subject-prefix'); } diff --git a/src/applications/pholio/editor/PholioMockEditor.php b/src/applications/pholio/editor/PholioMockEditor.php --- a/src/applications/pholio/editor/PholioMockEditor.php +++ b/src/applications/pholio/editor/PholioMockEditor.php @@ -406,7 +406,7 @@ return $body; } - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return PhabricatorEnv::getEnvConfig('metamta.pholio.subject-prefix'); } diff --git a/src/applications/phriction/editor/PhrictionDocumentEditor.php b/src/applications/phriction/editor/PhrictionDocumentEditor.php --- a/src/applications/phriction/editor/PhrictionDocumentEditor.php +++ b/src/applications/phriction/editor/PhrictionDocumentEditor.php @@ -352,7 +352,7 @@ $body = implode("\n\n", $body); - $subject_prefix = $this->getMailSubjectPrefix(); + $subject_prefix = $this->getMailSubjectPrefix(null); $mail = new PhabricatorMetaMTAMail(); $mail->setSubject($name) @@ -370,7 +370,7 @@ /* --( For less copy-pasting when switching to ApplicationTransactions )--- */ - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return PhabricatorEnv::getEnvConfig('metamta.phriction.subject-prefix'); } diff --git a/src/applications/ponder/editor/PonderEditor.php b/src/applications/ponder/editor/PonderEditor.php --- a/src/applications/ponder/editor/PonderEditor.php +++ b/src/applications/ponder/editor/PonderEditor.php @@ -27,7 +27,7 @@ ); } - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return '[Ponder]'; } diff --git a/src/applications/releeph/editor/ReleephRequestTransactionalEditor.php b/src/applications/releeph/editor/ReleephRequestTransactionalEditor.php --- a/src/applications/releeph/editor/ReleephRequestTransactionalEditor.php +++ b/src/applications/releeph/editor/ReleephRequestTransactionalEditor.php @@ -188,7 +188,7 @@ ->setMailReceiver($object); } - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { return '[Releeph]'; } diff --git a/src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php b/src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php --- a/src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php +++ b/src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php @@ -1768,7 +1768,7 @@ $template ->setFrom($this->requireActor()->getPHID()) - ->setSubjectPrefix($this->getMailSubjectPrefix()) + ->setSubjectPrefix($this->getMailSubjectPrefix($object)) ->setVarySubjectPrefix('['.$action.']') ->setThreadID($this->getMailThreadID($object), $this->getIsNewObject()) ->setRelatedPHID($object->getPHID()) @@ -1841,7 +1841,7 @@ /** * @task mail */ - protected function getMailSubjectPrefix() { + protected function getMailSubjectPrefix(PhabricatorLiskDAO $object) { throw new Exception("Capability not supported."); }