1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class DeployPlanDispatcher extends Dispatcher { |
5
|
|
|
|
6
|
|
|
const ACTION_PLAN = 'plan'; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var array |
10
|
|
|
*/ |
11
|
|
|
private static $action_types = [ |
12
|
|
|
self::ACTION_PLAN |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
public static $allowed_actions = [ |
19
|
|
|
'git_refs', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \DNProject |
24
|
|
|
*/ |
25
|
|
|
protected $project = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \DNEnvironment |
29
|
|
|
*/ |
30
|
|
|
protected $environment = null; |
31
|
|
|
|
32
|
|
|
public function init() { |
33
|
|
|
parent::init(); |
34
|
|
|
|
35
|
|
|
$this->project = $this->getCurrentProject(); |
36
|
|
|
|
37
|
|
|
if(!$this->project) { |
38
|
|
|
return $this->project404Response(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// if(!$this->project->allowed(Dispatcher::ALLOW_WHITELIST_READ)) { |
|
|
|
|
42
|
|
|
// return \Security::permissionFailure(); |
43
|
|
|
// } |
44
|
|
|
|
45
|
|
|
// Performs canView permission check by limiting visible projects |
46
|
|
|
$this->environment = $this->getCurrentEnvironment($this->project); |
47
|
|
|
if(!$this->environment) { |
48
|
|
|
return $this->environment404Response(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function Link() { |
56
|
|
|
return \Controller::join_links($this->environment->Link(), self::ACTION_PLAN); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Render configuration form. |
61
|
|
|
* |
62
|
|
|
* @param \SS_HTTPRequest $request |
63
|
|
|
* |
64
|
|
|
* @return \HTMLText|\SS_HTTPResponse |
65
|
|
|
*/ |
66
|
|
|
public function index(\SS_HTTPRequest $request) { |
67
|
|
|
$this->setCurrentActionType(self::ACTION_PLAN); |
68
|
|
|
return $this->customise([ |
69
|
|
|
'Environment' => $this->environment |
70
|
|
|
])->renderWith(['Plan', 'DNRoot']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function update_git(\SS_HTTPRequest $request) { |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param SS_HTTPRequest $request |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function git_refs(\SS_HTTPRequest $request) { |
83
|
|
|
|
84
|
|
|
$refs = []; |
85
|
|
|
$order = 0; |
86
|
|
|
$refs[] = [ |
87
|
|
|
'id' => ++$order, |
88
|
|
|
'label' => "Branch version", |
89
|
|
|
"description" => "Deploy the latest version of a branch", |
90
|
|
|
"list" => $this->getGitBranches($this->project) |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
$refs[] = [ |
94
|
|
|
'id' => ++$order, |
95
|
|
|
'label' => "Tag version", |
96
|
|
|
"description" => "Deploy a tagged release", |
97
|
|
|
"list" => $this->getGitTags($this->project) |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
// @todo: the original was a tree that was keyed by environment, the |
101
|
|
|
// front-end dropdown needs to be changed to support that. brrrr. |
102
|
|
|
$prevDeploys = []; |
103
|
|
|
foreach($this->getGitPrevDeploys($this->project) as $env) { |
104
|
|
|
foreach($env as $deploy) { |
105
|
|
|
$prevDeploys[] = $deploy; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
$refs[] = [ |
109
|
|
|
'id' => ++$order, |
110
|
|
|
'label' => "Redeploy a release that was previously deployed (to any environment", |
111
|
|
|
"description" => "Deploy a previous release", |
112
|
|
|
"list" => $prevDeploys |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
$body = json_encode($refs, JSON_PRETTY_PRINT); |
116
|
|
|
$this->getResponse()->addHeader('Content-Type', 'application/json'); |
117
|
|
|
$this->getResponse()->setBody($body); |
118
|
|
|
return $body; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Generate the data structure used by the frontend component. |
123
|
|
|
* |
124
|
|
|
* @param string $name of the component |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function getModel($name) { |
129
|
|
|
return [ |
130
|
|
|
'APIEndpoint' => Director::absoluteBaseURL().$this->Link() |
131
|
|
|
]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $project |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
protected function getGitBranches($project) { |
140
|
|
|
$branches = []; |
141
|
|
|
foreach($project->DNBranchList() as $branch) { |
142
|
|
|
$branches[] = [ |
143
|
|
|
'key' => $branch->SHA(), |
144
|
|
|
'value' => $branch->Name(), |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
return $branches; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $project |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
protected function getGitTags($project) { |
156
|
|
|
$tags = []; |
157
|
|
|
foreach($project->DNTagList()->setLimit(null) as $tag) { |
158
|
|
|
$tags[] = [ |
159
|
|
|
'key' => $tag->SHA(), |
160
|
|
|
'value' => $tag->Name(), |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
return $tags; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $project |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
protected function getGitPrevDeploys($project) { |
172
|
|
|
$redeploy = []; |
173
|
|
View Code Duplication |
foreach($project->DNEnvironmentList() as $dnEnvironment) { |
|
|
|
|
174
|
|
|
$envName = $dnEnvironment->Name; |
175
|
|
|
$perEnvDeploys = []; |
176
|
|
|
foreach($dnEnvironment->DeployHistory() as $deploy) { |
177
|
|
|
$sha = $deploy->SHA; |
178
|
|
|
|
179
|
|
|
// Check if exists to make sure the newest deployment date is used. |
180
|
|
|
if(!isset($perEnvDeploys[$sha])) { |
181
|
|
|
$pastValue = sprintf( |
182
|
|
|
"%s (deployed %s)", |
183
|
|
|
substr($sha, 0, 8), |
184
|
|
|
$deploy->obj('LastEdited')->Ago() |
185
|
|
|
); |
186
|
|
|
$perEnvDeploys[$sha] = [ |
187
|
|
|
'key' => $sha, |
188
|
|
|
'value' => $pastValue |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
if(!empty($perEnvDeploys)) { |
193
|
|
|
$redeploy[$envName] = array_values($perEnvDeploys); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
return $redeploy; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.