<?php
final class CustomDifferentialRunPackageBuildsHeraldAction
extends HeraldAction {
const ACTIONCONST = 'custom.differential.run-package-builds';
const DO_BUILD = 'do.custom-package-build';
public function getHeraldActionName() {
return pht('Run builds for affected packages');
}
public function getActionGroupKey() {
return HeraldApplicationActionGroup::ACTIONGROUPKEY;
}
public function supportsObject($object) {
return ($object instanceof DifferentialRevision);
}
public function supportsRuleType($rule_type) {
return ($rule_type == HeraldRuleTypeConfig::RULE_TYPE_GLOBAL);
}
public function applyEffect($object, HeraldEffect $effect) {
$viewer = PhabricatorUser::getOmnipotentUser();
// Replace this with the full field key of the custom field you want to
// examine. Generally, "std:owners:" plus whatever you named the field.
$field_key = "std:owners:mycompany:lore";
// Replace this with the PHID of the build plan you want to execute.
$buildplan_phid = "PHID-HMCP-vadfbuqvdbx5gy65pfba";
$adapter = $this->getAdapter();
$packages = $adapter->loadAffectedPackages();
$package_phids = mpull($packages, 'getPHID');
$this->logEffect(self::DO_BUILD, $package_phids);
$all = array();
foreach ($packages as $package) {
$field_list = PhabricatorCustomField::getObjectFields(
$package,
PhabricatorCustomField::ROLE_VIEW);
$field_list
->setViewer($viewer)
->readFieldsFromStorage($package);
foreach ($field_list->getFields() as $field) {
$key = $field->getFieldKey();
if ($key == $field_key) {
$all[] = $field->getValueForStorage();
}
}
}
$all = implode("\n", $all);
$all = phutil_split_lines($all, false);
$all = array_filter($all);
if (!$all) {
return;
}
$plan = id(new HarbormasterBuildPlanQuery())
->setViewer($viewer)
->withPHIDs(array($buildplan_phid))
->executeOne();
if (!$plan) {
throw new Exception(
pht(
'Unable to load build plan "%s"!', $buildplan_phid));
}
$buildable = HarbormasterBuildable::createOrLoadExisting(
$viewer,
$adapter->getHarbormasterBuildablePHID(),
$adapter->getHarbormasterContainerPHID());
foreach ($all as $parameter) {
// TODO: We need to actually parameterize this build.
$build = HarbormasterBuild::initializeNewBuild($viewer)
->setBuildablePHID($buildable->getPHID())
->setBuildPlanPHID($plan->getPHID())
->setBuildStatus(HarbormasterBuild::STATUS_PENDING)
->save();
PhabricatorWorker::scheduleTask(
'HarbormasterBuildWorker',
array(
'buildID' => $build->getID(),
));
}
}
public function getHeraldActionStandardType() {
return self::STANDARD_NONE;
}
public function renderActionDescription($value) {
return pht('Run builds for affected pakages.');
}
protected function getActionEffectMap() {
return array(
self::DO_BUILD => array(
'icon' => 'fa-play',
'color' => 'green',
'name' => pht('Ran Builds'),
),
);
}
protected function renderActionEffectDescription($type, $data) {
switch ($type) {
case self::DO_BUILD:
if (!$data) {
return pht('No affected packages.');
} else {
return pht(
'Ran builds for affected packages: %s.',
$this->renderHandleList($data));
}
}
}
}