1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-25 16:22:43 +01:00
phorge-phorge/src/applications/harbormaster/step/HarbormasterHTTPRequestBuildStepImplementation.php

114 lines
2.8 KiB
PHP
Raw Normal View History

<?php
final class HarbormasterHTTPRequestBuildStepImplementation
extends HarbormasterBuildStepImplementation {
public function getName() {
return pht('Make HTTP Request');
}
public function getGenericDescription() {
return pht('Make an HTTP request.');
}
public function getBuildStepGroupKey() {
return HarbormasterExternalBuildStepGroup::GROUPKEY;
}
public function getDescription() {
$domain = null;
$uri = $this->getSetting('uri');
if ($uri) {
$domain = id(new PhutilURI($uri))->getDomain();
}
$method = $this->formatSettingForDescription('method', 'POST');
$domain = $this->formatValueForDescription($domain);
if ($this->getSetting('credential')) {
return pht(
'Make an authenticated HTTP %s request to %s.',
$method,
$domain);
} else {
return pht(
'Make an HTTP %s request to %s.',
$method,
$domain);
}
}
public function execute(
HarbormasterBuild $build,
HarbormasterBuildTarget $build_target) {
$viewer = PhabricatorUser::getOmnipotentUser();
$settings = $this->getSettings();
$variables = $build_target->getVariables();
$uri = $this->mergeVariables(
'vurisprintf',
$settings['uri'],
$variables);
$log_body = $build->createLog($build_target, $uri, 'http-body');
$start = $log_body->start();
$method = nonempty(idx($settings, 'method'), 'POST');
$future = id(new HTTPSFuture($uri))
->setMethod($method)
->setTimeout(60);
$credential_phid = $this->getSetting('credential');
if ($credential_phid) {
$key = PassphrasePasswordKey::loadFromPHID(
$credential_phid,
$viewer);
$future->setHTTPBasicAuthCredentials(
$key->getUsernameEnvelope()->openEnvelope(),
$key->getPasswordEnvelope());
}
list($status, $body, $headers) = $this->resolveFuture(
$build,
$build_target,
$future);
$log_body->append($body);
$log_body->finalize($start);
if ($status->getStatusCode() != 200) {
$build->setBuildStatus(HarbormasterBuild::STATUS_FAILED);
}
}
public function getFieldSpecifications() {
return array(
'uri' => array(
'name' => pht('URI'),
'type' => 'text',
'required' => true,
),
'method' => array(
'name' => pht('HTTP Method'),
'type' => 'select',
'options' => array_fuse(array('POST', 'GET', 'PUT', 'DELETE')),
),
'credential' => array(
'name' => pht('Credentials'),
'type' => 'credential',
'credential.type'
=> PassphrasePasswordCredentialType::CREDENTIAL_TYPE,
'credential.provides'
=> PassphrasePasswordCredentialType::PROVIDES_TYPE,
),
);
}
Allow Harbormaster build targets to wait for messages Summary: This hooks up all the pieces of the build pipeline so `harbormaster.sendmessage` actually works. Particularly: - Candidate build steps (i.e., those which interact with external systems) can now "Wait for Message". This pauses them indefinitely when they complete, until something calls `harbormaster.sendmessage`. - After processing a target, we check if we should move it to PASSED or WAITING. - Before updating a build, we move WAITING targets with pending messages to either PASSED or FAILED. - I added an explicit "Building" state, which doesn't affect workflows but communicates more information to human users. A big part of this is avoiding races. I believe we get the correct behavior no matter which order events occur in: - We update builds after targets complete and after we receive messages, so we're guaranteed to update once both these conditions are true. This means messages can't be lost (even if they arrive before a build completes). - The minor changes to the build engine logic mean that firing additional build updates is always safe, no matter what the current state of the build is. - The build itself is protected by a lock in the build engine. - The target is not covered by an explicit lock, but for all states only the engine (waiting) //or// the worker (all other states) can interact with it. All of the interactions also move the target state forward to the same destination and have no other side effects. - Messages are only consumed inside the engine lock, so they don't need an explicit lock. Test Plan: - Made an HTTP request wait after completion, then ran a pile of builds through it using `bin/harbormaster build` and the web UI. - Passed and failed message-awaiting builds with `harbormaster.sendmessage`. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley, zeeg Differential Revision: https://secure.phabricator.com/D8788
2014-04-16 22:01:46 +02:00
public function supportsWaitForMessage() {
return true;
}
}