[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/repository/worker/ -> PhabricatorRepositoryPushMailWorker.php (source)

   1  <?php
   2  
   3  final class PhabricatorRepositoryPushMailWorker
   4    extends PhabricatorWorker {
   5  
   6    public function doWork() {
   7      $viewer = PhabricatorUser::getOmnipotentUser();
   8  
   9      $task_data = $this->getTaskData();
  10  
  11      $email_phids = idx($task_data, 'emailPHIDs');
  12      if (!$email_phids) {
  13        // If we don't have any email targets, don't send any email.
  14        return;
  15      }
  16  
  17      $event_phid = idx($task_data, 'eventPHID');
  18      $event = id(new PhabricatorRepositoryPushEventQuery())
  19        ->setViewer($viewer)
  20        ->withPHIDs(array($event_phid))
  21        ->needLogs(true)
  22        ->executeOne();
  23  
  24      $repository = $event->getRepository();
  25      if ($repository->isImporting()) {
  26        // If the repository is still importing, don't send email.
  27        return;
  28      }
  29  
  30      if ($repository->getDetail('herald-disabled')) {
  31        // If publishing is disabled, don't send email.
  32        return;
  33      }
  34  
  35      $logs = $event->getLogs();
  36  
  37      list($ref_lines, $ref_list) = $this->renderRefs($logs);
  38      list($commit_lines, $subject_line) = $this->renderCommits(
  39        $repository,
  40        $logs,
  41        idx($task_data, 'info', array()));
  42  
  43      $ref_count = count($ref_lines);
  44      $commit_count = count($commit_lines);
  45  
  46      $handles = id(new PhabricatorHandleQuery())
  47        ->setViewer($viewer)
  48        ->withPHIDs(array($event->getPusherPHID()))
  49        ->execute();
  50  
  51      $pusher_name = $handles[$event->getPusherPHID()]->getName();
  52      $repo_name = $repository->getMonogram();
  53  
  54      if ($commit_count) {
  55        $overview = pht(
  56          '%s pushed %d commit(s) to %s.',
  57          $pusher_name,
  58          $commit_count,
  59          $repo_name);
  60      } else {
  61        $overview = pht(
  62          '%s pushed to %s.',
  63          $pusher_name,
  64          $repo_name);
  65      }
  66  
  67      $details_uri = PhabricatorEnv::getProductionURI(
  68        '/diffusion/pushlog/view/'.$event->getID().'/');
  69  
  70      $body = new PhabricatorMetaMTAMailBody();
  71      $body->addRawSection($overview);
  72  
  73      $body->addLinkSection(pht('DETAILS'), $details_uri);
  74  
  75      if ($commit_lines) {
  76        $body->addTextSection(pht('COMMITS'), implode("\n", $commit_lines));
  77      }
  78  
  79      if ($ref_lines) {
  80        $body->addTextSection(pht('REFERENCES'), implode("\n", $ref_lines));
  81      }
  82  
  83      $prefix = PhabricatorEnv::getEnvConfig('metamta.diffusion.subject-prefix');
  84  
  85      $parts = array();
  86      if ($commit_count) {
  87        $parts[] = pht('%s commit(s)', $commit_count);
  88      }
  89      if ($ref_count) {
  90        $parts[] = implode(', ', $ref_list);
  91      }
  92      $parts = implode(', ', $parts);
  93  
  94      if ($subject_line) {
  95        $subject = pht('(%s) %s', $parts, $subject_line);
  96      } else {
  97        $subject = pht('(%s)', $parts);
  98      }
  99  
 100      $mail = id(new PhabricatorMetaMTAMail())
 101        ->setRelatedPHID($event->getPHID())
 102        ->setSubjectPrefix($prefix)
 103        ->setVarySubjectPrefix(pht('[Push]'))
 104        ->setSubject($subject)
 105        ->setFrom($event->getPusherPHID())
 106        ->setBody($body->render())
 107        ->setThreadID($event->getPHID(), $is_new = true)
 108        ->addHeader('Thread-Topic', $subject)
 109        ->setIsBulk(true);
 110  
 111      $to_handles = id(new PhabricatorHandleQuery())
 112        ->setViewer($viewer)
 113        ->withPHIDs($email_phids)
 114        ->execute();
 115  
 116      $reply_handler = new PhabricatorRepositoryPushReplyHandler();
 117      $mails = $reply_handler->multiplexMail(
 118        $mail,
 119        $to_handles,
 120        array());
 121  
 122      foreach ($mails as $mail) {
 123        $mail->saveAndSend();
 124      }
 125    }
 126  
 127    public function renderForDisplay(PhabricatorUser $viewer) {
 128      // This data has some sensitive stuff, so don't show it.
 129      return null;
 130    }
 131  
 132    private function renderRefs(array $logs) {
 133      $ref_lines = array();
 134      $ref_list = array();
 135  
 136      foreach ($logs as $log) {
 137        $type_name = null;
 138        $type_prefix = null;
 139        switch ($log->getRefType()) {
 140          case PhabricatorRepositoryPushLog::REFTYPE_BRANCH:
 141            $type_name = pht('branch');
 142            break;
 143          case PhabricatorRepositoryPushLog::REFTYPE_TAG:
 144            $type_name = pht('tag');
 145            $type_prefix = pht('tag:');
 146            break;
 147          case PhabricatorRepositoryPushLog::REFTYPE_BOOKMARK:
 148            $type_name = pht('bookmark');
 149            $type_prefix = pht('bookmark:');
 150            break;
 151          case PhabricatorRepositoryPushLog::REFTYPE_COMMIT:
 152          default:
 153            break;
 154        }
 155  
 156        if ($type_name === null) {
 157          continue;
 158        }
 159  
 160        $flags = $log->getChangeFlags();
 161        if ($flags & PhabricatorRepositoryPushLog::CHANGEFLAG_DANGEROUS) {
 162          $action = '!';
 163        } else if ($flags & PhabricatorRepositoryPushLog::CHANGEFLAG_DELETE) {
 164          $action = '-';
 165        } else if ($flags & PhabricatorRepositoryPushLog::CHANGEFLAG_REWRITE) {
 166          $action = '~';
 167        } else if ($flags & PhabricatorRepositoryPushLog::CHANGEFLAG_APPEND) {
 168          $action = ' ';
 169        } else if ($flags & PhabricatorRepositoryPushLog::CHANGEFLAG_ADD) {
 170          $action = '+';
 171        } else {
 172          $action = '?';
 173        }
 174  
 175        $old = nonempty($log->getRefOldShort(), pht('<null>'));
 176        $new = nonempty($log->getRefNewShort(), pht('<null>'));
 177  
 178        $name = $log->getRefName();
 179  
 180        $ref_lines[] = "{$action} {$type_name} {$name} {$old} > {$new}";
 181        $ref_list[] = $type_prefix.$name;
 182      }
 183  
 184      return array(
 185        $ref_lines,
 186        array_unique($ref_list),
 187      );
 188    }
 189  
 190    private function renderCommits(
 191      PhabricatorRepository $repository,
 192      array $logs,
 193      array $info) {
 194  
 195      $commit_lines = array();
 196      $subject_line = null;
 197      foreach ($logs as $log) {
 198        if ($log->getRefType() != PhabricatorRepositoryPushLog::REFTYPE_COMMIT) {
 199          continue;
 200        }
 201  
 202        $commit_info = idx($info, $log->getRefNew(), array());
 203  
 204        $name = $repository->formatCommitName($log->getRefNew());
 205  
 206        $branches = null;
 207        if (idx($commit_info, 'branches')) {
 208          $branches = ' ('.implode(', ', $commit_info['branches']).')';
 209        }
 210  
 211        $summary = null;
 212        if (strlen(idx($commit_info, 'summary'))) {
 213          $summary = ' '.$commit_info['summary'];
 214        }
 215  
 216        $commit_lines[] = "{$name}{$branches}{$summary}";
 217        if ($subject_line === null) {
 218          $subject_line = "{$name}{$summary}";
 219        }
 220      }
 221  
 222      return array($commit_lines, $subject_line);
 223    }
 224  
 225  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1