1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Validates a multi-source commit selector |
5
|
|
|
* |
6
|
|
|
* @package deploynaut |
7
|
|
|
* @subpackage control |
8
|
|
|
*/ |
9
|
|
|
class DeployForm_CommitValidator extends DeployForm_ValidatorBase { |
|
|
|
|
10
|
|
|
|
11
|
|
|
public function php($data) { |
12
|
|
|
// Check release method |
13
|
|
|
if(empty($data['SelectRelease']) |
14
|
|
|
|| !in_array($data['SelectRelease'], array('Tag', 'Branch', 'Redeploy', 'SHA', 'FilteredCommits')) |
15
|
|
|
) { |
16
|
|
|
$method = empty($data['SelectRelease']) ? '(blank)' : $data['SelectRelease']; |
17
|
|
|
$this->validationError( |
18
|
|
|
'SelectRelease', |
19
|
|
|
"Bad release selection method: $method", |
20
|
|
|
"error" |
21
|
|
|
); |
22
|
|
|
return false; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Check sha |
26
|
|
|
return $this->validateCommit( |
27
|
|
|
$this->form->getSelectedBuild($data), |
28
|
|
|
'SelectRelease' |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Form for generating deployments from a specified commit |
36
|
|
|
* |
37
|
|
|
* @package deploynaut |
38
|
|
|
* @subpackage control |
39
|
|
|
*/ |
40
|
|
|
class DeployForm extends Form { |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param DNRoot $controller |
44
|
|
|
* @param string $name |
45
|
|
|
* @param DNEnvironment $environment |
46
|
|
|
* @param DNProject $project |
47
|
|
|
*/ |
48
|
|
|
public function __construct($controller, $name, DNEnvironment $environment, DNProject $project) { |
49
|
|
|
$field = $this->buildCommitSelector($project); |
50
|
|
|
$validator = new DeployForm_CommitValidator(); |
51
|
|
|
$actions = new FieldList( |
52
|
|
|
new FormAction('showDeploySummary', 'Plan deployment', 'Show deployment plan'), |
|
|
|
|
53
|
|
|
new FormAction('doDeploy', 'Do deploy', 'Do deploy') |
|
|
|
|
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
parent::__construct($controller, $name, new FieldList($field), $actions, $validator); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Construct fields to select any commit |
61
|
|
|
* |
62
|
|
|
* @param DNProject $project |
63
|
|
|
* @return FormField |
64
|
|
|
*/ |
65
|
|
|
protected function buildCommitSelector($project) { |
66
|
|
|
// Branches |
67
|
|
|
$branches = array(); |
68
|
|
View Code Duplication |
foreach($project->DNBranchList() as $branch) { |
|
|
|
|
69
|
|
|
$sha = $branch->SHA(); |
70
|
|
|
$name = $branch->Name(); |
71
|
|
|
$branchValue = sprintf("%s (%s, %s old)", |
72
|
|
|
$name, |
73
|
|
|
substr($sha, 0, 8), |
74
|
|
|
$branch->LastUpdated()->TimeDiff() |
75
|
|
|
); |
76
|
|
|
$branches[$sha . '-' . $name] = $branchValue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Tags |
80
|
|
|
$tags = array(); |
81
|
|
View Code Duplication |
foreach($project->DNTagList()->setLimit(null) as $tag) { |
|
|
|
|
82
|
|
|
$sha = $tag->SHA(); |
83
|
|
|
$name = $tag->Name(); |
84
|
|
|
$tagValue = sprintf("%s (%s, %s old)", |
85
|
|
|
$name, |
86
|
|
|
substr($sha, 0, 8), |
87
|
|
|
$branch->LastUpdated()->TimeDiff() |
|
|
|
|
88
|
|
|
); |
89
|
|
|
$tags[$sha . '-' . $tag] = $tagValue; |
90
|
|
|
} |
91
|
|
|
$tags = array_reverse($tags); |
92
|
|
|
|
93
|
|
|
// Past deployments |
94
|
|
|
$redeploy = array(); |
95
|
|
|
foreach($project->DNEnvironmentList() as $dnEnvironment) { |
96
|
|
|
$envName = $dnEnvironment->Name; |
97
|
|
|
foreach($dnEnvironment->DeployHistory() as $deploy) { |
98
|
|
|
$sha = $deploy->SHA; |
99
|
|
|
if(!isset($redeploy[$envName])) { |
100
|
|
|
$redeploy[$envName] = array(); |
101
|
|
|
} |
102
|
|
|
if(!isset($redeploy[$envName][$sha])) { |
103
|
|
|
$pastValue = sprintf("%s (deployed %s)", |
104
|
|
|
substr($sha, 0, 8), |
105
|
|
|
$deploy->obj('LastEdited')->Ago() |
106
|
|
|
); |
107
|
|
|
$redeploy[$envName][$sha] = $pastValue; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Merge fields |
113
|
|
|
$releaseMethods = array(); |
114
|
|
|
if(!empty($branches)) { |
115
|
|
|
$releaseMethods[] = new SelectionGroup_Item( |
116
|
|
|
'Branch', |
117
|
|
|
new DropdownField('Branch', 'Select a branch', $branches), |
118
|
|
|
'Deploy the latest version of a branch' |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
if($tags) { |
122
|
|
|
$releaseMethods[] = new SelectionGroup_Item( |
123
|
|
|
'Tag', |
124
|
|
|
new DropdownField('Tag', 'Select a tag', $tags), |
125
|
|
|
'Deploy a tagged release' |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
if($redeploy) { |
129
|
|
|
$releaseMethods[] = new SelectionGroup_Item( |
130
|
|
|
'Redeploy', |
131
|
|
|
new GroupedDropdownField('Redeploy', 'Redeploy', $redeploy), |
132
|
|
|
'Redeploy a release that was previously deployed (to any environment)' |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$releaseMethods[] = new SelectionGroup_Item( |
137
|
|
|
'SHA', |
138
|
|
|
new Textfield('SHA', 'Please specify the full SHA'), |
139
|
|
|
'Deploy a specific SHA' |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$field = new TabbedSelectionGroup('SelectRelease', $releaseMethods); |
143
|
|
|
$field->setValue(reset($releaseMethods)->getValue()); |
144
|
|
|
return $field; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the build selected from the given data |
149
|
|
|
* |
150
|
|
|
* @param array $data |
151
|
|
|
* @return string SHA of selected build |
152
|
|
|
*/ |
153
|
|
|
public function getSelectedBuild($data) { |
154
|
|
|
if(isset($data['SelectRelease']) && !empty($data[$data['SelectRelease']])) { |
155
|
|
|
// Filter out the tag/branch name if required |
156
|
|
|
$array = explode('-', $data[$data['SelectRelease']]); |
157
|
|
|
return reset($array); |
158
|
|
|
} |
159
|
|
|
if(isset($data['FilteredCommits']) && !empty($data['FilteredCommits'])) { |
160
|
|
|
return $data['FilteredCommits']; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.