Path: blob/master/src/applications/harbormaster/step/HarbormasterAbortOlderBuildsBuildStepImplementation.php
12256 views
<?php12final class HarbormasterAbortOlderBuildsBuildStepImplementation3extends HarbormasterBuildStepImplementation {45public function getName() {6return pht('Abort Older Builds');7}89public function getGenericDescription() {10return pht(11'When building a revision, abort copies of this build plan which are '.12'currently running against older diffs.');13}1415public function getBuildStepGroupKey() {16return HarbormasterControlBuildStepGroup::GROUPKEY;17}1819public function getEditInstructions() {20return pht(<<<EOTEXT21When run against a revision, this build step will abort any older copies of22the same build plan which are currently running against older diffs.2324There are some nuances to the behavior:2526- if this build step is triggered manually, it won't abort anything;27- this build step won't abort manual builds;28- this build step won't abort anything if the diff it is building isn't29the active diff when it runs.3031Build results on outdated diffs often aren't very important, so this may32reduce build queue load without any substantial cost.33EOTEXT34);35}3637public function willStartBuild(38PhabricatorUser $viewer,39HarbormasterBuildable $buildable,40HarbormasterBuild $build,41HarbormasterBuildPlan $plan,42HarbormasterBuildStep $step) {4344if ($buildable->getIsManualBuildable()) {45// Don't abort anything if this is a manual buildable.46return;47}4849$object_phid = $buildable->getBuildablePHID();50if (phid_get_type($object_phid) !== DifferentialDiffPHIDType::TYPECONST) {51// If this buildable isn't building a diff, bail out. For example, we52// might be building a commit. In this case, this step has no effect.53return;54}5556$diff = id(new DifferentialDiffQuery())57->setViewer($viewer)58->withPHIDs(array($object_phid))59->executeOne();60if (!$diff) {61return;62}6364$revision_id = $diff->getRevisionID();6566$revision = id(new DifferentialRevisionQuery())67->setViewer($viewer)68->withIDs(array($revision_id))69->executeOne();70if (!$revision) {71return;72}7374$active_phid = $revision->getActiveDiffPHID();75if ($active_phid !== $object_phid) {76// If we aren't building the active diff, bail out.77return;78}7980$diffs = id(new DifferentialDiffQuery())81->setViewer($viewer)82->withRevisionIDs(array($revision_id))83->execute();84$abort_diff_phids = array();85foreach ($diffs as $diff) {86if ($diff->getPHID() !== $active_phid) {87$abort_diff_phids[] = $diff->getPHID();88}89}9091if (!$abort_diff_phids) {92return;93}9495// We're fetching buildables even if they have "passed" or "failed"96// because they may still have ongoing builds. At the time of writing97// only "failed" buildables may still be ongoing, but it seems likely that98// "passed" buildables may be ongoing in the future.99100$abort_buildables = id(new HarbormasterBuildableQuery())101->setViewer($viewer)102->withBuildablePHIDs($abort_diff_phids)103->withManualBuildables(false)104->execute();105if (!$abort_buildables) {106return;107}108109$statuses = HarbormasterBuildStatus::getIncompleteStatusConstants();110111$abort_builds = id(new HarbormasterBuildQuery())112->setViewer($viewer)113->withBuildablePHIDs(mpull($abort_buildables, 'getPHID'))114->withBuildPlanPHIDs(array($plan->getPHID()))115->withBuildStatuses($statuses)116->execute();117if (!$abort_builds) {118return;119}120121foreach ($abort_builds as $abort_build) {122$abort_build->sendMessage(123$viewer,124HarbormasterBuildMessageAbortTransaction::MESSAGETYPE);125}126}127128public function execute(129HarbormasterBuild $build,130HarbormasterBuildTarget $build_target) {131return;132}133134}135136137