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 $environment->CurrentBuild(); |
13
|
|
|
* @var null|DNDeployment |
14
|
|
|
*/ |
15
|
|
|
private static $_cache_current_build = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Return data about a single deployment for use in API response. |
19
|
|
|
* @param DNDeployment $deployment |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public function getDeploymentData(DNDeployment $deployment) { |
23
|
|
|
if (empty(self::$_cache_current_build[$deployment->EnvironmentID])) { |
24
|
|
|
self::$_cache_current_build[$deployment->EnvironmentID] = $deployment->Environment()->CurrentBuild(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$environment = $deployment->Environment(); |
28
|
|
|
$project = $environment->Project(); |
29
|
|
|
|
30
|
|
|
$deployer = $deployment->Deployer(); |
31
|
|
|
$deployerData = null; |
32
|
|
|
if ($deployer && $deployer->exists()) { |
33
|
|
|
$deployerData = $this->getStackMemberData($project, $deployer); |
34
|
|
|
} |
35
|
|
|
$approver = $deployment->Approver(); |
|
|
|
|
36
|
|
|
$approverData = null; |
37
|
|
|
if ($approver && $approver->exists()) { |
38
|
|
|
$approverData = $this->getStackMemberData($project, $approver); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// failover for older deployments |
42
|
|
|
$started = $deployment->Created; |
43
|
|
|
$startedNice = $deployment->obj('Created')->Nice(); |
44
|
|
|
if($deployment->DeployStarted) { |
45
|
|
|
$started = $deployment->DeployStarted; |
46
|
|
|
$startedNice = $deployment->obj('DeployStarted')->Nice(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$requested = $deployment->Created; |
50
|
|
|
$requestedNice = $deployment->obj('Created')->Nice(); |
51
|
|
|
if($deployment->DeployRequested) { |
52
|
|
|
$requested = $deployment->DeployRequested; |
53
|
|
|
$requestedNice = $deployment->obj('DeployRequested')->Nice(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$isCurrentBuild = self::$_cache_current_build[$deployment->EnvironmentID] |
57
|
|
|
? ($deployment->ID === self::$_cache_current_build[$deployment->EnvironmentID]->ID) |
58
|
|
|
: false; |
59
|
|
|
|
60
|
|
|
return [ |
61
|
|
|
'id' => $deployment->ID, |
62
|
|
|
'date_created' => $deployment->Created, |
63
|
|
|
'date_created_nice' => $deployment->obj('Created')->Nice(), |
64
|
|
|
'date_started' => $started, |
65
|
|
|
'date_started_nice' => $startedNice, |
66
|
|
|
'date_requested' => $requested, |
67
|
|
|
'date_requested_nice' => $requestedNice, |
68
|
|
|
'date_updated' => $deployment->LastEdited, |
69
|
|
|
'date_updated_nice' => $deployment->obj('LastEdited')->Nice(), |
70
|
|
|
'summary' => $deployment->Summary, |
|
|
|
|
71
|
|
|
'branch' => $deployment->Branch, |
|
|
|
|
72
|
|
|
'tags' => $deployment->getTags()->toArray(), |
73
|
|
|
'changes' => $deployment->getDeploymentStrategy()->getChanges(), |
74
|
|
|
'sha' => $deployment->SHA, |
75
|
|
|
'short_sha' => substr($deployment->SHA, 0, 7), |
76
|
|
|
'ref_type' => $deployment->RefType, |
77
|
|
|
'commit_message' => $deployment->getCommitMessage(), |
78
|
|
|
'commit_url' => $deployment->getCommitURL(), |
79
|
|
|
'deployer' => $deployerData, |
80
|
|
|
'approver' => $approverData, |
81
|
|
|
'state' => $deployment->State, |
82
|
|
|
'is_current_build' => $isCurrentBuild |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return data about a particular {@link Member} of the stack for use in API response. |
88
|
|
|
* Note that role can be null in the response. This is the case of an admin, or an operations |
89
|
|
|
* user who can create the deployment but is not part of the stack roles. |
90
|
|
|
* |
91
|
|
|
* @param DNProject $project |
92
|
|
|
* @param Member $member |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getStackMemberData(DNProject $project, Member $member) { |
96
|
|
|
if (empty(self::$_cache_project_members[$project->ID])) { |
97
|
|
|
self::$_cache_project_members[$project->ID] = $project->listMembers(); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$role = null; |
101
|
|
|
foreach (self::$_cache_project_members[$project->ID] as $stackMember) { |
102
|
|
|
if ($stackMember['MemberID'] !== $member->ID) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
$role = $stackMember['RoleTitle']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return [ |
109
|
|
|
'id' => $member->ID, |
110
|
|
|
'email' => $member->Email, |
111
|
|
|
'role' => $role, |
112
|
|
|
'name' => $member->getName() |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
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
.