/**
* Create deployment. Can't use {@link create()} as it's taken by Object.
*
* @param \SS_HTTPRequest $request
* @return \SS_HTTPResponse
*/
public function createdeployment(\SS_HTTPRequest $request)
{
if ($request->httpMethod() !== 'POST') {
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);
}
$this->checkSecurityToken();
// @todo the strategy should have been saved when there has been a request for an
// approval or a bypass. This saved state needs to be checked if it's invalidated
// if another deploy happens before this one
$sha = $this->project->resolveRevision($request->postVar('ref'));
if (!$sha) {
return $this->getAPIResponse(['message' => 'The given reference could not be resolved. Does it exist in the repository?'], 400);
}
// if a ref name was given that is not the sha, then set that. It could be a branch,
// or tag name, or anything else that git uses as a named reference
$refName = null;
if ($request->postVar('ref_name') !== $request->postVar('ref')) {
$refName = $request->postVar('ref_name');
}
$options = ['sha' => $sha, 'ref_type' => $request->postVar('ref_type'), 'ref_name' => $refName, 'title' => $request->postVar('title'), 'summary' => $request->postVar('summary')];
if ($request->postVar('options')) {
foreach (explode(',', $request->postVar('options')) as $option) {
$options[$option] = true;
}
}
$strategy = $this->createStrategy($options);
$approver = Member::get()->byId($request->postVar('approver_id'));
if ($approver && $approver->exists()) {
if (!$this->project->allowed(ApprovalsDispatcher::ALLOW_APPROVAL, $approver)) {
return $this->getAPIResponse(['message' => 'The given approver does not have permissions to approve'], 403);
}
}
if ($request->postVar('id')) {
$deployment = $strategy->updateDeployment($request->postVar('id'));
$message = 'Deployment has been updated';
$statusCode = 200;
} else {
$deployment = $strategy->createDeployment();
$message = 'Deployment has been created';
$statusCode = 201;
}
if ($approver && $approver->exists()) {
$deployment->ApproverID = $approver->ID;
$deployment->write();
}
$deploymentLink = \Controller::join_links(Director::absoluteBaseURL(), $deployment->Link());
$response = $this->getAPIResponse(['message' => $message, 'location' => $deploymentLink, 'deployment' => $this->formatter->getDeploymentData($deployment)], $statusCode);
$response->addHeader('Location', $deploymentLink);
return $response;
}