private function buildResults()
 {
     $results = array();
     $status_map = HarbormasterBuildStatus::getBuildStatusMap();
     foreach ($status_map as $value => $name) {
         $result = id(new PhabricatorTypeaheadResult())->setIcon(HarbormasterBuildStatus::getBuildStatusIcon($value))->setColor(HarbormasterBuildStatus::getBuildStatusColor($value))->setPHID($value)->setName($name)->addAttribute(pht('Status'));
         $results[$value] = $result;
     }
     return $results;
 }
 public function execute(PhutilArgumentParser $args)
 {
     $viewer = $this->getViewer();
     $ids = $args->getArg('id');
     $active = $args->getArg('active');
     if (!$ids && !$active) {
         throw new PhutilArgumentUsageException(pht('Use --id or --active to select builds.'));
     }
     if ($ids && $active) {
         throw new PhutilArgumentUsageException(pht('Use one of --id or --active to select builds, but not both.'));
     }
     $query = id(new HarbormasterBuildQuery())->setViewer($viewer);
     if ($ids) {
         $query->withIDs($ids);
     } else {
         $query->withBuildStatuses(HarbormasterBuildStatus::getActiveStatusConstants());
     }
     $builds = $query->execute();
     $console = PhutilConsole::getConsole();
     $count = count($builds);
     if (!$count) {
         $console->writeOut("%s\n", pht('No builds to restart.'));
         return 0;
     }
     $prompt = pht('Restart %s build(s)?', new PhutilNumber($count));
     if (!phutil_console_confirm($prompt)) {
         $console->writeOut("%s\n", pht('Cancelled.'));
         return 1;
     }
     $app_phid = id(new PhabricatorHarbormasterApplication())->getPHID();
     $editor = id(new HarbormasterBuildTransactionEditor())->setActor($viewer)->setActingAsPHID($app_phid)->setContentSource($this->newContentSource());
     foreach ($builds as $build) {
         $console->writeOut("<bg:blue> %s </bg> %s\n", pht('RESTARTING'), pht('Build %d: %s', $build->getID(), $build->getName()));
         if (!$build->canRestartBuild()) {
             $console->writeOut("<bg:yellow> %s </bg> %s\n", pht('INVALID'), pht('Cannot be restarted.'));
             continue;
         }
         $xactions = array();
         $xactions[] = id(new HarbormasterBuildTransaction())->setTransactionType(HarbormasterBuildTransaction::TYPE_COMMAND)->setNewValue(HarbormasterBuildCommand::COMMAND_RESTART);
         try {
             $editor->applyTransactions($build, $xactions);
         } catch (Exception $e) {
             $message = phutil_console_wrap($e->getMessage(), 2);
             $console->writeOut("<bg:red> %s </bg>\n%s\n", pht('FAILED'), $message);
             continue;
         }
         $console->writeOut("<bg:green> %s </bg>\n", pht('SUCCESS'));
     }
     return 0;
 }
 public function getAttachmentForObject($object, $data, $spec)
 {
     $status_name = HarbormasterBuildStatus::getBuildStatusName($object->getBuildStatus());
     return array('uri' => PhabricatorEnv::getProductionURI($object->getURI()), 'name' => $object->getName(), 'buildStatusName' => $status_name);
 }
 protected function renderResultList(array $builds, PhabricatorSavedQuery $query, array $handles)
 {
     assert_instances_of($builds, 'HarbormasterBuild');
     $viewer = $this->requireViewer();
     $buildables = mpull($builds, 'getBuildable');
     $object_phids = mpull($buildables, 'getBuildablePHID');
     $initiator_phids = mpull($builds, 'getInitiatorPHID');
     $phids = array_mergev(array($initiator_phids, $object_phids));
     $phids = array_unique(array_filter($phids));
     $handles = $viewer->loadHandles($phids);
     $list = new PHUIObjectItemListView();
     foreach ($builds as $build) {
         $id = $build->getID();
         $initiator = $handles[$build->getInitiatorPHID()];
         $buildable_object = $handles[$build->getBuildable()->getBuildablePHID()];
         $item = id(new PHUIObjectItemView())->setViewer($viewer)->setObject($build)->setObjectName(pht('Build %d', $build->getID()))->setHeader($build->getName())->setHref($build->getURI())->setEpoch($build->getDateCreated())->addAttribute($buildable_object->getName());
         if ($initiator) {
             $item->addHandleIcon($initiator, $initiator->getName());
         }
         $status = $build->getBuildStatus();
         $status_icon = HarbormasterBuildStatus::getBuildStatusIcon($status);
         $status_color = HarbormasterBuildStatus::getBuildStatusColor($status);
         $status_label = HarbormasterBuildStatus::getBuildStatusName($status);
         $item->setStatusIcon("{$status_icon} {$status_color}", $status_label);
         $list->addItem($item);
     }
     $result = new PhabricatorApplicationSearchResultView();
     $result->setObjectList($list);
     $result->setNoDataString(pht('No builds found.'));
     return $result;
 }
 private function buildBuildList(HarbormasterBuildable $buildable)
 {
     $viewer = $this->getRequest()->getUser();
     $build_list = id(new PHUIObjectItemListView())->setUser($viewer);
     foreach ($buildable->getBuilds() as $build) {
         $view_uri = $this->getApplicationURI('/build/' . $build->getID() . '/');
         $item = id(new PHUIObjectItemView())->setObjectName(pht('Build %d', $build->getID()))->setHeader($build->getName())->setHref($view_uri);
         $status = $build->getBuildStatus();
         $status_color = HarbormasterBuildStatus::getBuildStatusColor($status);
         $status_name = HarbormasterBuildStatus::getBuildStatusName($status);
         $item->setStatusIcon('fa-dot-circle-o ' . $status_color, $status_name);
         $item->addAttribute($status_name);
         if ($build->isRestarting()) {
             $item->addIcon('fa-repeat', pht('Restarting'));
         } else {
             if ($build->isPausing()) {
                 $item->addIcon('fa-pause', pht('Pausing'));
             } else {
                 if ($build->isResuming()) {
                     $item->addIcon('fa-play', pht('Resuming'));
                 }
             }
         }
         $build_id = $build->getID();
         $restart_uri = "build/restart/{$build_id}/buildable/";
         $resume_uri = "build/resume/{$build_id}/buildable/";
         $pause_uri = "build/pause/{$build_id}/buildable/";
         $abort_uri = "build/abort/{$build_id}/buildable/";
         $item->addAction(id(new PHUIListItemView())->setIcon('fa-repeat')->setName(pht('Restart'))->setHref($this->getApplicationURI($restart_uri))->setWorkflow(true)->setDisabled(!$build->canRestartBuild()));
         if ($build->canResumeBuild()) {
             $item->addAction(id(new PHUIListItemView())->setIcon('fa-play')->setName(pht('Resume'))->setHref($this->getApplicationURI($resume_uri))->setWorkflow(true));
         } else {
             $item->addAction(id(new PHUIListItemView())->setIcon('fa-pause')->setName(pht('Pause'))->setHref($this->getApplicationURI($pause_uri))->setWorkflow(true)->setDisabled(!$build->canPauseBuild()));
         }
         $targets = $build->getBuildTargets();
         if ($targets) {
             $target_list = id(new PHUIStatusListView());
             foreach ($targets as $target) {
                 $status = $target->getTargetStatus();
                 $icon = HarbormasterBuildTarget::getBuildTargetStatusIcon($status);
                 $color = HarbormasterBuildTarget::getBuildTargetStatusColor($status);
                 $status_name = HarbormasterBuildTarget::getBuildTargetStatusName($status);
                 $name = $target->getName();
                 $target_list->addItem(id(new PHUIStatusItemView())->setIcon($icon, $color, $status_name)->setTarget(pht('Target %d', $target->getID()))->setNote($name));
             }
             $target_box = id(new PHUIBoxView())->addPadding(PHUI::PADDING_SMALL)->appendChild($target_list);
             $item->appendChild($target_box);
         }
         $build_list->addItem($item);
     }
     $build_list->setFlush(true);
     $box = id(new PHUIObjectBoxView())->setHeaderText(pht('Builds'))->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->appendChild($build_list);
     return $box;
 }
 private function getStatus(HarbormasterBuild $build)
 {
     $status_view = new PHUIStatusListView();
     $item = new PHUIStatusItemView();
     if ($build->isPausing()) {
         $status_name = pht('Pausing');
         $icon = PHUIStatusItemView::ICON_RIGHT;
         $color = 'dark';
     } else {
         $status = $build->getBuildStatus();
         $status_name = HarbormasterBuildStatus::getBuildStatusName($status);
         $icon = HarbormasterBuildStatus::getBuildStatusIcon($status);
         $color = HarbormasterBuildStatus::getBuildStatusColor($status);
     }
     $item->setTarget($status_name);
     $item->setIcon($icon, $color);
     $status_view->addItem($item);
     return $status_view;
 }
 public function getFieldValuesForConduit()
 {
     $status = $this->getBuildStatus();
     return array('buildablePHID' => $this->getBuildablePHID(), 'buildPlanPHID' => $this->getBuildPlanPHID(), 'buildStatus' => array('value' => $status, 'name' => HarbormasterBuildStatus::getBuildStatusName($status)), 'initiatorPHID' => nonempty($this->getInitiatorPHID(), null), 'name' => $this->getName());
 }
 private function handlePropertyEvent($ui_event)
 {
     $viewer = $ui_event->getUser();
     $object = $ui_event->getValue('object');
     if (!$object || !$object->getPHID()) {
         // No object, or the object has no PHID yet..
         return;
     }
     if ($object instanceof HarbormasterBuildable) {
         // Although HarbormasterBuildable implements the correct interface, it
         // does not make sense to show a build's build status. In the best case
         // it is meaningless, and in the worst case it's confusing.
         return;
     }
     if ($object instanceof DifferentialRevision) {
         // TODO: This is a bit hacky and we could probably find a cleaner fix
         // eventually, but we show build status on each diff, immediately below
         // this property list, so it's redundant to show it on the revision view.
         return;
     }
     if (!$object instanceof HarbormasterBuildableInterface) {
         return;
     }
     $buildable_phid = $object->getHarbormasterBuildablePHID();
     if (!$buildable_phid) {
         return;
     }
     if (!$this->canUseApplication($ui_event->getUser())) {
         return;
     }
     $buildable = id(new HarbormasterBuildableQuery())->setViewer($viewer)->withManualBuildables(false)->withBuildablePHIDs(array($buildable_phid))->needBuilds(true)->needTargets(true)->executeOne();
     if (!$buildable) {
         return;
     }
     $builds = $buildable->getBuilds();
     $targets = array();
     foreach ($builds as $build) {
         foreach ($build->getBuildTargets() as $target) {
             $targets[] = $target;
         }
     }
     if ($targets) {
         $artifacts = id(new HarbormasterBuildArtifactQuery())->setViewer($viewer)->withBuildTargetPHIDs(mpull($targets, 'getPHID'))->withArtifactTypes(array(HarbormasterURIArtifact::ARTIFACTCONST))->execute();
         $artifacts = mgroup($artifacts, 'getBuildTargetPHID');
     } else {
         $artifacts = array();
     }
     $status_view = new PHUIStatusListView();
     $buildable_status = $buildable->getBuildableStatus();
     $buildable_icon = HarbormasterBuildable::getBuildableStatusIcon($buildable_status);
     $buildable_color = HarbormasterBuildable::getBuildableStatusColor($buildable_status);
     $buildable_name = HarbormasterBuildable::getBuildableStatusName($buildable_status);
     $target = phutil_tag('a', array('href' => '/' . $buildable->getMonogram()), pht('Buildable %d', $buildable->getID()));
     $target = phutil_tag('strong', array(), $target);
     $status_view->addItem(id(new PHUIStatusItemView())->setIcon($buildable_icon, $buildable_color, $buildable_name)->setTarget($target));
     foreach ($builds as $build) {
         $item = new PHUIStatusItemView();
         $item->setTarget($viewer->renderHandle($build->getPHID()));
         $links = array();
         foreach ($build->getBuildTargets() as $build_target) {
             $uris = idx($artifacts, $build_target->getPHID(), array());
             foreach ($uris as $uri) {
                 $impl = $uri->getArtifactImplementation();
                 if ($impl->isExternalLink()) {
                     $links[] = $impl->renderLink();
                 }
             }
         }
         if ($links) {
             $links = phutil_implode_html(" · ", $links);
             $item->setNote($links);
         }
         $status = $build->getBuildStatus();
         $status_name = HarbormasterBuildStatus::getBuildStatusName($status);
         $icon = HarbormasterBuildStatus::getBuildStatusIcon($status);
         $color = HarbormasterBuildStatus::getBuildStatusColor($status);
         $item->setIcon($icon, $color, $status_name);
         $status_view->addItem($item);
     }
     $view = $ui_event->getValue('view');
     $view->addProperty(pht('Build Status'), $status_view);
 }