forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReleephProjectController.php
161 lines (141 loc) · 4.46 KB
/
ReleephProjectController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
abstract class ReleephProjectController extends ReleephController {
private $releephProject;
private $releephBranch;
private $releephRequest;
/**
* ReleephController will take care of loading any Releeph* objects
* referenced in the URL.
*/
public function willProcessRequest(array $data) {
$viewer = $this->getRequest()->getUser();
// Project
$project = null;
$project_id = idx($data, 'projectID');
$project_name = idx($data, 'projectName');
if ($project_id) {
$project = id(new ReleephProjectQuery())
->setViewer($viewer)
->withIDs(array($project_id))
->executeOne();
if (!$project) {
throw new Exception(
"ReleephProject with id '{$project_id}' not found!");
}
} elseif ($project_name) {
$project = id(new ReleephProject())
->loadOneWhere('name = %s', $project_name);
if (!$project) {
throw new Exception(
"ReleephProject with name '{$project_name}' not found!");
}
}
// Branch
$branch = null;
$branch_id = idx($data, 'branchID');
$branch_name = idx($data, 'branchName');
if ($branch_id) {
$branch = id(new ReleephBranchQuery())
->setViewer($viewer)
->withIDs(array($branch_id))
->executeOne();
if (!$branch) {
throw new Exception("Branch with id '{$branch_id}' not found!");
}
} elseif ($branch_name) {
if (!$project) {
throw new Exception(
"You cannot refer to a branch by name without also referring ".
"to a ReleephProject (branch names are only unique in projects).");
}
$branch = id(new ReleephBranch())->loadOneWhere(
'basename = %s AND releephProjectID = %d',
$branch_name,
$project->getID());
if (!$branch) {
throw new Exception(
"ReleephBranch with basename '{$branch_name}' not found ".
"in project '{$project->getName()}'!");
}
// Do the branch query again, properly, to hit policies and load attached
// data.
// TODO: Clean this up with T3657.
$branch = id(new ReleephBranchQuery())
->setViewer($viewer)
->withIDs(array($branch->getID()))
->executeOne();
if (!$branch) {
throw new Exception('404!');
}
}
// Request
$request = null;
$request_id = idx($data, 'requestID');
if ($request_id) {
$request = id(new ReleephRequest())->load($request_id);
if (!$request) {
throw new Exception(
"ReleephRequest with id '{$request_id}' not found!");
}
}
// Fill in the gaps
if ($request && !$branch) {
$branch = $request->loadReleephBranch();
}
if ($branch && !$project) {
$project = $branch->loadReleephProject();
}
// Set!
$this->releephProject = $project;
$this->releephBranch = $branch;
$this->releephRequest = $request;
}
protected function getReleephProject() {
if (!$this->releephProject) {
throw new Exception(
'This controller did not load a ReleephProject from the URL $data.');
}
return $this->releephProject;
}
protected function getReleephBranch() {
if (!$this->releephBranch) {
throw new Exception(
'This controller did not load a ReleephBranch from the URL $data.');
}
return $this->releephBranch;
}
protected function getReleephRequest() {
if (!$this->releephRequest) {
throw new Exception(
'This controller did not load a ReleephRequest from the URL $data.');
}
return $this->releephRequest;
}
protected function buildApplicationCrumbs() {
$crumbs = parent::buildApplicationCrumbs();
// TODO: The massive amount of derps here should be fixed once URIs get
// sorted out; see T3657.
try {
$project = $this->getReleephProject();
$project_id = $project->getID();
$project_uri = $this->getApplicationURI("project/{$project_id}/");
$crumbs->addCrumb(
id(new PhabricatorCrumbView())
->setHref($project_uri)
->setName($project->getName()));
} catch (Exception $ex) {
// TODO: This is derps.
}
try {
$branch = $this->getReleephBranch();
$branch_uri = $branch->getURI();
$crumbs->addCrumb(
id(new PhabricatorCrumbView())
->setHref($branch_uri)
->setName($branch->getDisplayNameWithDetail()));
} catch (Exception $ex) {
// TODO: This is also derps.
}
return $crumbs;
}
}