1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This dispatchers takes care of the viewing and changing the deploy plan. This includes |
5
|
|
|
* the guessed changes for a deployment, the deployment notes written by the deployer etc. |
6
|
|
|
*/ |
7
|
|
|
class PlanDispatcher extends Dispatcher { |
8
|
|
|
|
9
|
|
|
const ACTION_PLAN = 'plan'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private static $action_types = [ |
15
|
|
|
self::ACTION_PLAN |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
public static $allowed_actions = [ |
22
|
|
|
'deploysummary' |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \DNProject |
27
|
|
|
*/ |
28
|
|
|
protected $project = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \DNEnvironment |
32
|
|
|
*/ |
33
|
|
|
protected $environment = null; |
34
|
|
|
|
35
|
|
View Code Duplication |
public function init() { |
|
|
|
|
36
|
|
|
parent::init(); |
37
|
|
|
|
38
|
|
|
$this->project = $this->getCurrentProject(); |
39
|
|
|
|
40
|
|
|
if(!$this->project) { |
41
|
|
|
return $this->project404Response(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Performs canView permission check by limiting visible projects |
45
|
|
|
$this->environment = $this->getCurrentEnvironment($this->project); |
46
|
|
|
if(!$this->environment) { |
47
|
|
|
return $this->environment404Response(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
* @param \SS_HTTPRequest $request |
54
|
|
|
* |
55
|
|
|
* @return \HTMLText|\SS_HTTPResponse |
56
|
|
|
*/ |
57
|
|
|
public function index(\SS_HTTPRequest $request) { |
58
|
|
|
$this->setCurrentActionType(self::ACTION_PLAN); |
59
|
|
|
$this->httpError(404); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param SS_HTTPRequest $request |
64
|
|
|
* |
65
|
|
|
* @return SS_HTTPResponse |
66
|
|
|
*/ |
67
|
|
|
public function deploysummary(SS_HTTPRequest $request) { |
68
|
|
|
|
69
|
|
|
if(strtolower($request->httpMethod()) !== 'post') { |
70
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405); |
71
|
|
|
} |
72
|
|
|
$this->checkSecurityToken(); |
73
|
|
|
|
74
|
|
|
// @todo further permission checks? |
75
|
|
|
// @todo add more options, like force full deploy etc |
76
|
|
|
$options = [ |
77
|
|
|
'sha' => $request->requestVar('sha') |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
$strategy = $this->environment->Backend()->planDeploy($this->environment, $options); |
81
|
|
|
$data = $strategy->toArray(); |
82
|
|
|
|
83
|
|
|
$interface = $this->project->getRepositoryInterface(); |
84
|
|
|
if($this->canCompareCodeVersions($interface, $data['changes'])) { |
|
|
|
|
85
|
|
|
$compareurl = sprintf( |
86
|
|
|
'%s/compare/%s...%s', |
87
|
|
|
$interface->URL, |
88
|
|
|
$data['changes']['Code version']['from'], |
89
|
|
|
$data['changes']['Code version']['to'] |
90
|
|
|
); |
91
|
|
|
$data['changes']['Code version']['compareUrl'] = $compareurl; |
92
|
|
|
} |
93
|
|
|
$this->extend('updateDeploySummary', $data); |
94
|
|
|
return $this->getAPIResponse($data, 201); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function Link() { |
101
|
|
|
return \Controller::join_links($this->environment->Link(), self::ACTION_PLAN); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $name |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getModel($name = '') { |
110
|
|
|
return []; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param ArrayData $interface |
115
|
|
|
* @param $changes |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
* |
119
|
|
|
*/ |
120
|
|
|
protected function canCompareCodeVersions(\ArrayData $interface, $changes) { |
121
|
|
|
if(empty($changes['Code version'])) { |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
$codeVersion = ['Code version']; |
125
|
|
|
if(empty($interface)) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
if(empty($interface->URL)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
if(empty($codeVersion['from']) || empty($codeVersion['to'])) { |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
if(strlen($codeVersion['from']) !== 40 || strlen($codeVersion['to']) !== 40) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.