Path: blob/master/src/applications/harbormaster/conduit/HarbormasterCreateArtifactConduitAPIMethod.php
12256 views
<?php12final class HarbormasterCreateArtifactConduitAPIMethod3extends HarbormasterConduitAPIMethod {45public function getAPIMethodName() {6return 'harbormaster.createartifact';7}89public function getMethodSummary() {10return pht('Create a build artifact.');11}1213public function getMethodDescription() {14$types = HarbormasterArtifact::getAllArtifactTypes();15$types = msort($types, 'getArtifactTypeName');1617$head_key = pht('Key');18$head_type = pht('Type');19$head_desc = pht('Description');20$head_atype = pht('Artifact Type');21$head_name = pht('Name');22$head_summary = pht('Summary');2324$out = array();25$out[] = pht(26'Use this method to attach artifacts to build targets while running '.27'builds. Artifacts can be used to carry data through a complex build '.28'workflow, provide extra information to users, or store build results.');29$out[] = null;30$out[] = pht(31'When creating an artifact, you will choose an `artifactType` from '.32'this table. These types of artifacts are supported:');3334$out[] = "| {$head_atype} | {$head_name} | {$head_summary} |";35$out[] = '|-------------|--------------|--------------|';36foreach ($types as $type) {37$type_name = $type->getArtifactTypeName();38$type_const = $type->getArtifactConstant();39$type_summary = $type->getArtifactTypeSummary();40$out[] = "| `{$type_const}` | **{$type_name}** | {$type_summary} |";41}4243$out[] = null;44$out[] = pht(45'Each artifact also needs an `artifactKey`, which names the artifact. '.46'Finally, you will provide some `artifactData` to fill in the content '.47'of the artifact. The data you provide depends on what type of artifact '.48'you are creating.');4950foreach ($types as $type) {51$type_name = $type->getArtifactTypeName();52$type_const = $type->getArtifactConstant();5354$out[] = $type_name;55$out[] = '--------------------------';56$out[] = null;57$out[] = $type->getArtifactTypeDescription();58$out[] = null;59$out[] = pht(60'Create an artifact of this type by passing `%s` as the '.61'`artifactType`. When creating an artifact of this type, provide '.62'these parameters as a dictionary to `artifactData`:',63$type_const);6465$spec = $type->getArtifactParameterSpecification();66$desc = $type->getArtifactParameterDescriptions();67$out[] = "| {$head_key} | {$head_type} | {$head_desc} |";68$out[] = '|-------------|--------------|--------------|';69foreach ($spec as $key => $key_type) {70$key_desc = idx($desc, $key);71$out[] = "| `{$key}` | //{$key_type}// | {$key_desc} |";72}7374$example = $type->getArtifactDataExample();75if ($example !== null) {76$json = new PhutilJSON();77$rendered = $json->encodeFormatted($example);7879$out[] = pht('For example:');80$out[] = '```lang=json';81$out[] = $rendered;82$out[] = '```';83}84}8586return implode("\n", $out);87}8889protected function defineParamTypes() {90return array(91'buildTargetPHID' => 'phid',92'artifactKey' => 'string',93'artifactType' => 'string',94'artifactData' => 'map<string, wild>',95);96}9798protected function defineReturnType() {99return 'wild';100}101102protected function execute(ConduitAPIRequest $request) {103$viewer = $request->getUser();104105$build_target_phid = $request->getValue('buildTargetPHID');106$build_target = id(new HarbormasterBuildTargetQuery())107->setViewer($viewer)108->withPHIDs(array($build_target_phid))109->executeOne();110if (!$build_target) {111throw new Exception(112pht(113'No such build target "%s"!',114$build_target_phid));115}116117$artifact_type = $request->getValue('artifactType');118119// Cast "artifactData" parameters to acceptable types if this request120// is submitting raw HTTP parameters. This is not ideal. See T11887 for121// discussion.122$artifact_data = $request->getValue('artifactData');123if (!$request->getIsStrictlyTyped()) {124$impl = HarbormasterArtifact::getArtifactType($artifact_type);125if ($impl) {126foreach ($artifact_data as $key => $value) {127$artifact_data[$key] = $impl->readArtifactHTTPParameter(128$key,129$value);130}131}132}133134$artifact = $build_target->createArtifact(135$viewer,136$request->getValue('artifactKey'),137$artifact_type,138$artifact_data);139140return array(141'data' => $this->returnArtifactList(array($artifact)),142);143}144145}146147148