| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This dispatcher takes care of updating and returning information about this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * projects git repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class DeployDispatcher extends Dispatcher { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	const ACTION_DEPLOY = 'deploys'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	public static $allowed_actions = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		'history', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		'upcoming', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		'currentbuild', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		'show', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		'log', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		'create', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		'createdeployment' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * @var \DNProject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	protected $project = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @var \DNEnvironment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	protected $environment = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	private static $action_types = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		self::ACTION_DEPLOY | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * This is a per request cache of $this->project()->listMembers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @var null|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	private static $_cache_project_members = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * This is a per request cache of $this->environment->CurrentBuild(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @var null|DNDeployment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	private static $_cache_current_build = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 53 |  | View Code Duplication | 	public function init() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		parent::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->project = $this->getCurrentProject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		if (!$this->project) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			return $this->project404Response(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		// Performs canView permission check by limiting visible projects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$this->environment = $this->getCurrentEnvironment($this->project); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		if (!$this->environment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			return $this->environment404Response(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @param \SS_HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * @return \HTMLText|\SS_HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	public function index(\SS_HTTPRequest $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * @param \SS_HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * @return \SS_HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 81 |  | View Code Duplication | 	public function history(SS_HTTPRequest $request) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$list = $this->environment->DeployHistory('DeployStarted'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		foreach ($list as $deployment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			$data[] = $this->getDeploymentData($deployment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		return $this->getAPIResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			'list' => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		], 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * @param \SS_HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * @return \SS_HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 99 |  | View Code Duplication | 	public function upcoming(SS_HTTPRequest $request) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		$list = $this->environment->UpcomingDeployments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		foreach ($list as $deployment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			$data[] = $this->getDeploymentData($deployment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		return $this->getAPIResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			'list' => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		], 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @param \SS_HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @return \SS_HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	public function currentbuild(SS_HTTPRequest $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		$currentBuild = $this->environment->CurrentBuild(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		if (!$currentBuild) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			return $this->getAPIResponse(['deployment' => []], 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return $this->getAPIResponse(['deployment' => $this->getDeploymentData($currentBuild)], 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * @param \SS_HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * @return \SS_HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	public function show(SS_HTTPRequest $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		$deployment = DNDeployment::get()->byId($request->param('ID')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		$errorResponse = $this->validateDeployment($deployment); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		if ($errorResponse instanceof \SS_HTTPResponse) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			return $errorResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		return $this->getAPIResponse(['deployment' => $this->getDeploymentData($deployment)], 200); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 * @param \SS_HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 * @return \SS_HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	public function log(SS_HTTPRequest $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$deployment = DNDeployment::get()->byId($request->param('ID')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		$errorResponse = $this->validateDeployment($deployment); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		if ($errorResponse instanceof \SS_HTTPResponse) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			return $errorResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		$log = $deployment->log(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		$content = $log->exists() ? $log->content() : 'Waiting for action to start'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		$lines = explode(PHP_EOL, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		return $this->getAPIResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 			'message' => $lines, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 			'status' => $deployment->Status, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 			'deployment' => $this->getDeploymentData($deployment), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		], 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	/** | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  | 	 * Create deployment. Can't use {@link create()} as it's taken by Object. | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  | 	 * @param \SS_HTTPRequest $request | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  | 	 * @return \SS_HTTPResponse | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  | 	public function createdeployment(SS_HTTPRequest $request) { | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  | 		if ($request->httpMethod() !== 'POST') { | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  | 			return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  | 		$this->checkSecurityToken(); | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  | 		// @todo the strategy should have been saved when there has been a request for an | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  | 		// approval or a bypass. This saved state needs to be checked if it's invalidated | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  | 		// if another deploy happens before this one | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  | 		$isBranchDeploy = (int) $request->postVar('ref_type') === GitDispatcher::REF_TYPE_BRANCH; | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  | 		$options = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  | 			'sha' => $request->postVar('ref'), | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  | 			'ref_type' => $request->postVar('ref_type'), | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  | 			'branch' => $isBranchDeploy ? $request->postVar('ref_name') : null, | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  | 			'summary' => $request->postVar('summary') | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  | 		]; | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  | 		$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  | 		$strategy->fromArray($request->postVars()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  | 		$deployment = $strategy->createDeployment(); | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  | 		$deployment->getMachine()->apply(DNDeployment::TR_SUBMIT); | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  | 		return $this->getAPIResponse([ | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  | 			'message' => 'Deployment has been created', | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  | 			'id' => $deployment->ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 			'deployment' => $this->getDeploymentData($deployment), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		], 201); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	 * @param \SS_HTTPRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 	 * @return \SS_HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 	public function start(SS_HTTPRequest $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 		if ($request->httpMethod() !== 'POST') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 			return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 		$this->checkSecurityToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 		$deployment = DNDeployment::get()->byId($request->postVar('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 		if (!$deployment || !$deployment->exists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 			return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 		// The deployment cannot be started until it has been approved, or bypassed straight to approved state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 		if ($deployment->State != DNDeployment::STATE_APPROVED) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | 			return $this->getAPIResponse(['message' => 'This deployment has not been approved. Cannot deploy'], 403); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 		// until we have a system that can invalidate currently scheduled deployments due | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | 		// to emergency deploys etc, replan the deployment to check if it's still valid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | 		$options = $deployment->getDeploymentStrategy()->getOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 		$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 		$deployment->Strategy = $strategy->toJSON(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 		$deployment->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  | 		$deployment->getMachine()->apply(DNDeployment::TR_QUEUE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 		$location = \Controller::join_links(Director::absoluteBaseURL(), $this->Link('log'), $deployment->ID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  | 		$response = $this->getAPIResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 			'message' => 'Deployment has been queued', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 			'id' => $deployment->ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  | 			'location' => $location, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 			'deployment' => $this->getDeploymentData($deployment), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 		], 201); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  | 		$response->addHeader('Location', $location); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 		return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  | 	 * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  | 	public function Link($action = '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 		return \Controller::join_links($this->environment->Link(), self::ACTION_DEPLOY, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  | 	 * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 	public function getModel($name = '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  | 		return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  | 	 * Return data about a single deployment for use in API response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 	 * @param DNDeployment $deployment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  | 	protected function getDeploymentData(DNDeployment $deployment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 		if (self::$_cache_current_build === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 			self::$_cache_current_build = $this->environment->CurrentBuild(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  | 		$deployer = $deployment->Deployer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  | 		$deployerData = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 		if ($deployer && $deployer->exists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 			$deployerData = $this->getStackMemberData($deployer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  | 		$approver = $deployment->Approver(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 		$approverData = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 		if ($approver && $approver->exists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  | 			$approverData = $this->getStackMemberData($approver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  | 		// failover for older deployments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 		$started = $deployment->Created; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 		$startedNice = $deployment->obj('Created')->Nice(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  | 		if($deployment->DeployStarted) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  | 			$started = $deployment->DeployStarted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  | 			$startedNice = $deployment->obj('DeployStarted')->Nice(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  | 		$requested = $deployment->Created; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 		$requestedNice = $deployment->obj('Created')->Nice(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 		if($deployment->DeployRequested) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  | 			$requested = $deployment->DeployRequested; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  | 			$requestedNice = $deployment->obj('DeployRequested')->Nice(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  | 		$isCurrentBuild = self::$_cache_current_build ? ($deployment->ID === self::$_cache_current_build->ID) : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  | 			'id' => $deployment->ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  | 			'date_created' => $deployment->Created, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  | 			'date_created_nice' => $deployment->obj('Created')->Nice(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  | 			'date_started' => $started, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  | 			'date_started_nice' => $startedNice, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  | 			'date_requested' => $requested, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  | 			'date_requested_nice' => $requestedNice, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  | 			'date_updated' => $deployment->LastEdited, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  | 			'date_updated_nice' => $deployment->obj('LastEdited')->Nice(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  | 			'summary' => $deployment->Summary, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  | 			'branch' => $deployment->Branch, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  | 			'tags' => $deployment->getTags()->toArray(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  | 			'changes' => $deployment->getDeploymentStrategy()->getChanges(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  | 			'sha' => $deployment->SHA, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  | 			'short_sha' => substr($deployment->SHA, 0, 7), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  | 			'ref_type' => $deployment->RefType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  | 			'commit_message' => $deployment->getCommitMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  | 			'commit_url' => $deployment->getCommitURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  | 			'deployer' => $deployerData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  | 			'approver' => $approverData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  | 			'state' => $deployment->State, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  | 			'is_current_build' => $isCurrentBuild | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  | 	 * Return data about a particular {@link Member} of the stack for use in API response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  | 	 * Note that role can be null in the response. This is the case of an admin, or an operations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  | 	 * user who can create the deployment but is not part of the stack roles. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  | 	 * @param Member $member | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  | 	protected function getStackMemberData(Member $member) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  | 		if (self::$_cache_project_members === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  | 			self::$_cache_project_members = $this->project->listMembers(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  | 		$role = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  | 		foreach (self::$_cache_project_members as $stackMember) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  | 			if ($stackMember['MemberID'] !== $member->ID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  | 				continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  | 			$role = $stackMember['RoleTitle']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  | 			'id' => $member->ID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  | 			'email' => $member->Email, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  | 			'role' => $role, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  | 			'name' => $member->getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  | 	 * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  | 	 * an APIResponse with the error, otherwise null. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  | 	 * @param \DNDeployment $deployment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  | 	 * @return null|SS_HTTPResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 357 |  | View Code Duplication | 	protected function validateDeployment(\DNDeployment $deployment) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  | 		if (!$deployment || !$deployment->exists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  | 			return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  | 		if ($deployment->EnvironmentID != $this->environment->ID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  | 			return $this->getAPIResponse(['message' => 'This deployment does not belong to the environment'], 403); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  | 		if (!$deployment->canView()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  | 			return $this->getAPIResponse(['message' => 'You are not authorised to view this deployment'], 403); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  | 		return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 370 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 371 |  |  |  | 
            
                        
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.