1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class DeploynautAPIFormatter { |
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This is a per request cache of $project()->listMembers() |
7
|
|
|
* @var null|array |
8
|
|
|
*/ |
9
|
|
|
private static $_cache_project_members = null; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This is a per request cache of Members |
13
|
|
|
* @var null|array |
14
|
|
|
*/ |
15
|
|
|
private static $_cache_members = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This is a per request cache of $environment->CurrentBuild(); |
19
|
|
|
* @var null|DNDeployment |
20
|
|
|
*/ |
21
|
|
|
private static $_cache_current_build = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return data about a single deployment for use in API response. |
25
|
|
|
* @param \DNDeployment $deployment |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function getDeploymentData(\DNDeployment $deployment) { |
29
|
|
|
if (empty(self::$_cache_current_build[$deployment->EnvironmentID])) { |
30
|
|
|
self::$_cache_current_build[$deployment->EnvironmentID] = $deployment->Environment()->CurrentBuild(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$environment = $deployment->Environment(); |
34
|
|
|
$project = $environment->Project(); |
35
|
|
|
|
36
|
|
|
$deployerData = $this->getStackMemberData($project, $deployment->DeployerID); |
37
|
|
|
$approverData = $this->getStackMemberData($project, $deployment->ApproverID); |
|
|
|
|
38
|
|
|
|
39
|
|
|
$started = null; |
|
|
|
|
40
|
|
|
$startedNice = null; |
|
|
|
|
41
|
|
|
$startedAgo = null; |
|
|
|
|
42
|
|
|
// we check first, before we do a expensive ->Nice() and ->Ago() |
43
|
|
|
if(!$deployment->DeployStarted) { |
44
|
|
|
$started = $deployment->Created; |
45
|
|
|
$startedNice = $deployment->obj('Created')->Nice(); |
46
|
|
|
$startedAgo = $deployment->obj('Created')->Ago(); |
47
|
|
|
} else { |
48
|
|
|
$started = $deployment->DeployStarted; |
49
|
|
|
$startedNice = $deployment->obj('DeployStarted')->Nice(); |
50
|
|
|
$startedAgo = $deployment->obj('DeployStarted')->Ago(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$isCurrentBuild = self::$_cache_current_build[$deployment->EnvironmentID] |
54
|
|
|
? ($deployment->ID === self::$_cache_current_build[$deployment->EnvironmentID]->ID) |
55
|
|
|
: false; |
56
|
|
|
|
57
|
|
|
$supportedOptions = $environment->getSupportedOptions(); |
58
|
|
|
$setOptions = $deployment->getDeploymentStrategy() ? $deployment->getDeploymentStrategy()->getOptions() : []; |
59
|
|
|
$options = []; |
60
|
|
|
|
61
|
|
|
foreach ($supportedOptions as $option) { |
62
|
|
|
if (!isset($setOptions[$option->getName()])) { |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
if ($setOptions[$option->getName()] === 'true' || $setOptions[$option->getName()] === true) { |
66
|
|
|
$options[$option->getName()] = true; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$tags = []; |
71
|
|
|
try { |
72
|
|
|
$tags = $deployment->getTags()->toArray(); |
73
|
|
|
} catch (\Exception $e) { |
74
|
|
|
// gitonomy exception |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$type = 'full'; |
78
|
|
|
if ($deployment->getDeploymentStrategy()->getActionCode() === 'fast') { |
79
|
|
|
$type = 'code-only'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return [ |
83
|
|
|
'id' => $deployment->ID, |
84
|
|
|
'date_created' => $deployment->Created, |
85
|
|
|
'date_created_nice' => $deployment->obj('Created')->Nice(), |
86
|
|
|
'date_created_ago' => $deployment->obj('Created')->Ago(), |
87
|
|
|
'date_started' => $started, |
88
|
|
|
'date_started_nice' => $startedNice, |
89
|
|
|
'date_started_ago' => $startedAgo, |
90
|
|
|
'date_requested' => $deployment->DeployRequested, |
91
|
|
|
'date_requested_nice' => $deployment->obj('DeployRequested')->Nice(), |
92
|
|
|
'date_requested_ago' => $deployment->obj('DeployRequested')->Ago(), |
93
|
|
|
'date_updated' => $deployment->LastEdited, |
94
|
|
|
'date_updated_nice' => $deployment->obj('LastEdited')->Nice(), |
95
|
|
|
'date_updated_ago' => $deployment->obj('LastEdited')->Ago(), |
96
|
|
|
'title' => $deployment->Title, |
|
|
|
|
97
|
|
|
'summary' => $deployment->Summary, |
|
|
|
|
98
|
|
|
'branch' => $deployment->Branch, |
|
|
|
|
99
|
|
|
'rejected_reason' => $deployment->RejectedReason ?: '', |
|
|
|
|
100
|
|
|
'tags' => $tags, |
101
|
|
|
'changes' => $deployment->getDeploymentStrategy()->getChanges(), |
102
|
|
|
'deployment_type' => $type, |
103
|
|
|
'deployment_estimate' => $deployment->getDeploymentStrategy()->getEstimatedTime(), |
104
|
|
|
'sha' => $deployment->SHA, |
105
|
|
|
'short_sha' => substr($deployment->SHA, 0, 7), |
106
|
|
|
'ref_type' => $deployment->RefType, |
107
|
|
|
'options' => $options, |
108
|
|
|
'commit_message' => $deployment->getCommitMessage(), |
109
|
|
|
'commit_url' => $deployment->getCommitURL(), |
110
|
|
|
'deployer' => $deployerData, |
111
|
|
|
'approver_id' => $deployment->ApproverID ?: '', |
|
|
|
|
112
|
|
|
'approver' => $approverData, |
113
|
|
|
'state' => $deployment->State, |
114
|
|
|
'is_current_build' => $isCurrentBuild |
115
|
|
|
]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Return data about a particular {@link Member} of the stack for use in API response. |
120
|
|
|
|
121
|
|
|
* Notes: |
122
|
|
|
* 1) This method returns null instead of an array if the member doesn't exists anymore |
123
|
|
|
* 2) 'role' can be null in the response. This is the case of an admin, or an operations |
124
|
|
|
* user who can create the deployment but is not part of the stack roles. |
125
|
|
|
* |
126
|
|
|
* @param \DNProject $project |
127
|
|
|
* @param int $memberID |
128
|
|
|
* @return null|array |
129
|
|
|
*/ |
130
|
|
|
public function getStackMemberData(\DNProject $project, $memberID) { |
131
|
|
|
if (empty(self::$_cache_project_members[$project->ID])) { |
132
|
|
|
self::$_cache_project_members[$project->ID] = $project->listMembers(); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// we cache all member lookup, even the false results |
136
|
|
View Code Duplication |
if (!isset(self::$_cache_members[$memberID])) { |
|
|
|
|
137
|
|
|
self::$_cache_members[$memberID] = \Member::get()->byId($memberID); |
138
|
|
|
} |
139
|
|
|
if(!self::$_cache_members[$memberID]) { |
140
|
|
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$role = null; |
144
|
|
|
foreach (self::$_cache_project_members[$project->ID] as $stackMember) { |
145
|
|
|
if ($stackMember['MemberID'] !== $memberID) { |
146
|
|
|
continue; |
147
|
|
|
} |
148
|
|
|
$role = $stackMember['RoleTitle']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// if an administator is approving, they should be shown as one |
152
|
|
|
if ($role === null && \Permission::checkMember(self::$_cache_members[$memberID], 'ADMIN')) { |
153
|
|
|
$role = 'Administrator'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return [ |
157
|
|
|
'id' => $memberID, |
158
|
|
|
'email' => self::$_cache_members[$memberID]->Email, |
159
|
|
|
'role' => $role, |
160
|
|
|
'name' => self::$_cache_members[$memberID]->getName() |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
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
.