|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This dispatcher takes care of updating and returning information about this |
|
5
|
|
|
* projects git repository |
|
6
|
|
|
*/ |
|
7
|
|
|
class GitDispatcher extends Dispatcher { |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
const ACTION_GIT = 'git'; |
|
10
|
|
|
|
|
11
|
|
|
const REF_TYPE_FROM_UAT = 0; |
|
12
|
|
|
const REF_TYPE_BRANCH = 1; |
|
13
|
|
|
const REF_TYPE_TAG = 2; |
|
14
|
|
|
const REF_TYPE_PREVIOUS = 3; |
|
15
|
|
|
const REF_TYPE_SHA = 4; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
public static $allowed_actions = [ |
|
21
|
|
|
'update', |
|
22
|
|
|
'show' |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \DNProject |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $project = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \DNEnvironment |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $environment = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $action_types = [ |
|
|
|
|
|
|
39
|
|
|
self::ACTION_GIT |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
public function init() { |
|
43
|
|
|
parent::init(); |
|
44
|
|
|
|
|
45
|
|
|
$this->project = $this->getCurrentProject(); |
|
46
|
|
|
|
|
47
|
|
|
if (!$this->project) { |
|
48
|
|
|
return $this->project404Response(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* |
|
54
|
|
|
* @param \SS_HTTPRequest $request |
|
55
|
|
|
* |
|
56
|
|
|
* @return \HTMLText|\SS_HTTPResponse |
|
57
|
|
|
*/ |
|
58
|
|
|
public function index(\SS_HTTPRequest $request) { |
|
59
|
|
|
return $this->redirect(\Controller::join_links($this->Link(), 'show'), 302); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param SS_HTTPRequest $request |
|
64
|
|
|
* @return SS_HTTPResponse |
|
65
|
|
|
*/ |
|
66
|
|
|
public function update(SS_HTTPRequest $request) { |
|
67
|
|
|
switch ($request->httpMethod()) { |
|
68
|
|
|
case 'POST': |
|
69
|
|
|
$this->checkSecurityToken(); |
|
70
|
|
|
return $this->createUpdate(); |
|
71
|
|
|
case 'GET': |
|
72
|
|
|
return $this->getUpdateStatus($this->getRequest()->param('ID')); |
|
73
|
|
|
default: |
|
74
|
|
|
return $this->getAPIResponse(['message' => 'Method not allowed, requires POST or GET/{id}'], 405); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param SS_HTTPRequest $request |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function show(\SS_HTTPRequest $request) { |
|
84
|
|
|
$targetEnvironment = null; |
|
85
|
|
|
$targetEnvironmentId = $request->getVar('environmentId'); |
|
86
|
|
|
if (!empty($targetEnvironmentId)) { |
|
87
|
|
|
$targetEnvironment = DNEnvironment::get()->byId((int) $targetEnvironmentId); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$refs = []; |
|
91
|
|
|
$prevDeploys = []; |
|
92
|
|
|
|
|
93
|
|
|
$uatEnvironment = $this->project->DNEnvironmentList()->filter('Usage', 'UAT')->first(); |
|
94
|
|
|
$uatBuild = $uatEnvironment ? $uatEnvironment->CurrentBuild() : null; |
|
95
|
|
|
if ($uatBuild && $uatBuild->exists() && $targetEnvironment && $targetEnvironment->Usage === 'Production') { |
|
96
|
|
|
$refs[self::REF_TYPE_FROM_UAT] = [ |
|
97
|
|
|
'id' => self::REF_TYPE_FROM_UAT, |
|
98
|
|
|
'label' => 'Promote the version currently on UAT', |
|
99
|
|
|
'description' => 'Promote the version currently on UAT', |
|
100
|
|
|
'promote_build' => [ |
|
101
|
|
|
'id' => $uatBuild->ID, |
|
102
|
|
|
'deployed' => DBField::create_field('SS_Datetime', $uatBuild->Created, 'Created')->Ago(), |
|
103
|
|
|
'branch' => $uatBuild->Branch, |
|
104
|
|
|
'tags' => $uatBuild->getTags()->toArray(), |
|
105
|
|
|
'sha' => $uatBuild->SHA, |
|
106
|
|
|
'commit_message' => $uatBuild->getCommitMessage(), |
|
107
|
|
|
'commit_url' => $uatBuild->getCommitURL(), |
|
108
|
|
|
] |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$refs[self::REF_TYPE_BRANCH] = [ |
|
113
|
|
|
'id' => self::REF_TYPE_BRANCH, |
|
114
|
|
|
'label' => 'Branch version', |
|
115
|
|
|
'description' => 'Deploy the latest version of a branch', |
|
116
|
|
|
'list' => $this->getGitBranches($this->project) |
|
117
|
|
|
]; |
|
118
|
|
|
|
|
119
|
|
|
$refs[self::REF_TYPE_TAG] = [ |
|
120
|
|
|
'id' => self::REF_TYPE_TAG, |
|
121
|
|
|
'label' => 'Tag version', |
|
122
|
|
|
'description' => 'Deploy a tagged release', |
|
123
|
|
|
'list' => $this->getGitTags($this->project) |
|
124
|
|
|
]; |
|
125
|
|
|
|
|
126
|
|
|
// @todo: the original was a tree that was keyed by environment, the |
|
127
|
|
|
// front-end dropdown needs to be changed to support that. brrrr. |
|
128
|
|
|
foreach ($this->getGitPrevDeploys($this->project) as $env) { |
|
129
|
|
|
foreach ($env as $deploy) { |
|
130
|
|
|
$prevDeploys[] = $deploy; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
$refs[self::REF_TYPE_PREVIOUS] = [ |
|
134
|
|
|
'id' => self::REF_TYPE_PREVIOUS, |
|
135
|
|
|
'label' => 'Redeploy a release that was previously deployed (to any environment)', |
|
136
|
|
|
'description' => 'Deploy a previous release', |
|
137
|
|
|
'list' => $prevDeploys |
|
138
|
|
|
]; |
|
139
|
|
|
$refs[self::REF_TYPE_SHA] = [ |
|
140
|
|
|
'id' => self::REF_TYPE_SHA, |
|
141
|
|
|
'label' => 'Deploy a specific SHA', |
|
142
|
|
|
'description' => 'Deploy a specific SHA' |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
return $this->getAPIResponse(['refs' => $refs], 200); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
public function Link() { |
|
152
|
|
|
return \Controller::join_links($this->project->Link(), self::ACTION_GIT); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $name |
|
157
|
|
|
* |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getModel($name = '') { |
|
161
|
|
|
return []; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param int $ID |
|
166
|
|
|
* @return SS_HTTPResponse |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function getUpdateStatus($ID) { |
|
169
|
|
|
$ping = DNGitFetch::get()->byID($ID); |
|
170
|
|
|
if (!$ping) { |
|
171
|
|
|
return $this->getAPIResponse(['message' => 'GIT update (' . $ID . ') not found'], 404); |
|
172
|
|
|
} |
|
173
|
|
|
$output = [ |
|
174
|
|
|
'id' => $ID, |
|
175
|
|
|
'status' => $ping->ResqueStatus(), |
|
176
|
|
|
'message' => array_filter(explode(PHP_EOL, $ping->LogContent())) |
|
177
|
|
|
]; |
|
178
|
|
|
|
|
179
|
|
|
return $this->getAPIResponse($output, 200); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @return SS_HTTPResponse |
|
184
|
|
|
*/ |
|
185
|
|
View Code Duplication |
protected function createUpdate() { |
|
|
|
|
|
|
186
|
|
|
/** @var DNGitFetch $fetch */ |
|
187
|
|
|
$fetch = DNGitFetch::create(); |
|
188
|
|
|
$fetch->ProjectID = $this->project->ID; |
|
189
|
|
|
$fetch->write(); |
|
190
|
|
|
$fetch->start(); |
|
191
|
|
|
|
|
192
|
|
|
$location = Director::absoluteBaseURL() . $this->Link() . '/update/' . $fetch->ID; |
|
193
|
|
|
$output = [ |
|
194
|
|
|
'message' => 'git fetch has been queued', |
|
195
|
|
|
'id' => $fetch->ID, |
|
196
|
|
|
'location' => $location, |
|
197
|
|
|
]; |
|
198
|
|
|
|
|
199
|
|
|
$response = $this->getAPIResponse($output, 201); |
|
200
|
|
|
$response->addHeader('Location', $location); |
|
201
|
|
|
return $response; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param $project |
|
206
|
|
|
* |
|
207
|
|
|
* @return array |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function getGitBranches($project) { |
|
210
|
|
|
$branches = []; |
|
211
|
|
|
foreach ($project->DNBranchList() as $branch) { |
|
212
|
|
|
$branches[] = [ |
|
213
|
|
|
'key' => $branch->SHA(), |
|
214
|
|
|
'value' => $branch->Name(), |
|
215
|
|
|
]; |
|
216
|
|
|
} |
|
217
|
|
|
return $branches; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param $project |
|
222
|
|
|
* |
|
223
|
|
|
* @return array |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function getGitTags($project) { |
|
226
|
|
|
$tags = []; |
|
227
|
|
|
foreach ($project->DNTagList()->setLimit(null) as $tag) { |
|
228
|
|
|
$tags[] = [ |
|
229
|
|
|
'key' => $tag->SHA(), |
|
230
|
|
|
'value' => $tag->Name(), |
|
231
|
|
|
]; |
|
232
|
|
|
} |
|
233
|
|
|
return $tags; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param $project |
|
238
|
|
|
* |
|
239
|
|
|
* @return array |
|
240
|
|
|
*/ |
|
241
|
|
|
protected function getGitPrevDeploys($project) { |
|
242
|
|
|
$redeploy = []; |
|
243
|
|
View Code Duplication |
foreach ($project->DNEnvironmentList() as $dnEnvironment) { |
|
|
|
|
|
|
244
|
|
|
$envName = $dnEnvironment->Name; |
|
245
|
|
|
$perEnvDeploys = []; |
|
246
|
|
|
foreach ($dnEnvironment->DeployHistory() as $deploy) { |
|
247
|
|
|
$sha = $deploy->SHA; |
|
248
|
|
|
|
|
249
|
|
|
// Check if exists to make sure the newest deployment date is used. |
|
250
|
|
|
if (!isset($perEnvDeploys[$sha])) { |
|
251
|
|
|
$pastValue = sprintf( |
|
252
|
|
|
"%s (deployed %s)", |
|
253
|
|
|
substr($sha, 0, 8), |
|
254
|
|
|
$deploy->obj('LastEdited')->Ago() |
|
255
|
|
|
); |
|
256
|
|
|
$perEnvDeploys[$sha] = [ |
|
257
|
|
|
'key' => $sha, |
|
258
|
|
|
'value' => $pastValue |
|
259
|
|
|
]; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
if (!empty($perEnvDeploys)) { |
|
263
|
|
|
$redeploy[$envName] = array_values($perEnvDeploys); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
return $redeploy; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
|
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.