Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : package com.google.gerrit.server.api.projects;
16 :
17 : import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
18 : import static com.google.gerrit.server.restapi.project.DashboardsCollection.DEFAULT_DASHBOARD_NAME;
19 : import static java.util.stream.Collectors.toList;
20 :
21 : import com.google.gerrit.extensions.api.access.ProjectAccessInfo;
22 : import com.google.gerrit.extensions.api.access.ProjectAccessInput;
23 : import com.google.gerrit.extensions.api.config.AccessCheckInfo;
24 : import com.google.gerrit.extensions.api.config.AccessCheckInput;
25 : import com.google.gerrit.extensions.api.projects.BranchApi;
26 : import com.google.gerrit.extensions.api.projects.BranchInfo;
27 : import com.google.gerrit.extensions.api.projects.CheckProjectInput;
28 : import com.google.gerrit.extensions.api.projects.CheckProjectResultInfo;
29 : import com.google.gerrit.extensions.api.projects.ChildProjectApi;
30 : import com.google.gerrit.extensions.api.projects.CommitApi;
31 : import com.google.gerrit.extensions.api.projects.ConfigInfo;
32 : import com.google.gerrit.extensions.api.projects.ConfigInput;
33 : import com.google.gerrit.extensions.api.projects.DashboardApi;
34 : import com.google.gerrit.extensions.api.projects.DashboardInfo;
35 : import com.google.gerrit.extensions.api.projects.DeleteBranchesInput;
36 : import com.google.gerrit.extensions.api.projects.DeleteTagsInput;
37 : import com.google.gerrit.extensions.api.projects.DescriptionInput;
38 : import com.google.gerrit.extensions.api.projects.HeadInput;
39 : import com.google.gerrit.extensions.api.projects.IndexProjectInput;
40 : import com.google.gerrit.extensions.api.projects.LabelApi;
41 : import com.google.gerrit.extensions.api.projects.ParentInput;
42 : import com.google.gerrit.extensions.api.projects.ProjectApi;
43 : import com.google.gerrit.extensions.api.projects.ProjectInput;
44 : import com.google.gerrit.extensions.api.projects.SubmitRequirementApi;
45 : import com.google.gerrit.extensions.api.projects.TagApi;
46 : import com.google.gerrit.extensions.api.projects.TagInfo;
47 : import com.google.gerrit.extensions.common.BatchLabelInput;
48 : import com.google.gerrit.extensions.common.ChangeInfo;
49 : import com.google.gerrit.extensions.common.Input;
50 : import com.google.gerrit.extensions.common.LabelDefinitionInfo;
51 : import com.google.gerrit.extensions.common.ProjectInfo;
52 : import com.google.gerrit.extensions.common.SubmitRequirementInfo;
53 : import com.google.gerrit.extensions.restapi.BadRequestException;
54 : import com.google.gerrit.extensions.restapi.IdString;
55 : import com.google.gerrit.extensions.restapi.NotImplementedException;
56 : import com.google.gerrit.extensions.restapi.ResourceConflictException;
57 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
58 : import com.google.gerrit.extensions.restapi.RestApiException;
59 : import com.google.gerrit.extensions.restapi.TopLevelResource;
60 : import com.google.gerrit.server.permissions.GlobalPermission;
61 : import com.google.gerrit.server.permissions.PermissionBackend;
62 : import com.google.gerrit.server.project.ProjectJson;
63 : import com.google.gerrit.server.project.ProjectResource;
64 : import com.google.gerrit.server.restapi.project.Check;
65 : import com.google.gerrit.server.restapi.project.CheckAccess;
66 : import com.google.gerrit.server.restapi.project.ChildProjectsCollection;
67 : import com.google.gerrit.server.restapi.project.CommitsCollection;
68 : import com.google.gerrit.server.restapi.project.CommitsIncludedInRefs;
69 : import com.google.gerrit.server.restapi.project.CreateAccessChange;
70 : import com.google.gerrit.server.restapi.project.CreateProject;
71 : import com.google.gerrit.server.restapi.project.DeleteBranches;
72 : import com.google.gerrit.server.restapi.project.DeleteTags;
73 : import com.google.gerrit.server.restapi.project.GetAccess;
74 : import com.google.gerrit.server.restapi.project.GetConfig;
75 : import com.google.gerrit.server.restapi.project.GetDescription;
76 : import com.google.gerrit.server.restapi.project.GetHead;
77 : import com.google.gerrit.server.restapi.project.GetParent;
78 : import com.google.gerrit.server.restapi.project.Index;
79 : import com.google.gerrit.server.restapi.project.IndexChanges;
80 : import com.google.gerrit.server.restapi.project.ListBranches;
81 : import com.google.gerrit.server.restapi.project.ListDashboards;
82 : import com.google.gerrit.server.restapi.project.ListLabels;
83 : import com.google.gerrit.server.restapi.project.ListSubmitRequirements;
84 : import com.google.gerrit.server.restapi.project.ListTags;
85 : import com.google.gerrit.server.restapi.project.PostLabels;
86 : import com.google.gerrit.server.restapi.project.ProjectsCollection;
87 : import com.google.gerrit.server.restapi.project.PutConfig;
88 : import com.google.gerrit.server.restapi.project.PutDescription;
89 : import com.google.gerrit.server.restapi.project.SetAccess;
90 : import com.google.gerrit.server.restapi.project.SetHead;
91 : import com.google.gerrit.server.restapi.project.SetParent;
92 : import com.google.inject.Provider;
93 : import com.google.inject.assistedinject.Assisted;
94 : import com.google.inject.assistedinject.AssistedInject;
95 : import java.util.Collection;
96 : import java.util.Collections;
97 : import java.util.List;
98 : import java.util.Map;
99 : import java.util.Set;
100 :
101 : public class ProjectApiImpl implements ProjectApi {
102 : interface Factory {
103 : ProjectApiImpl create(ProjectResource project);
104 :
105 : ProjectApiImpl create(String name);
106 : }
107 :
108 : private final PermissionBackend permissionBackend;
109 : private final CreateProject createProject;
110 : private final ProjectApiImpl.Factory projectApi;
111 : private final ProjectsCollection projects;
112 : private final GetDescription getDescription;
113 : private final PutDescription putDescription;
114 : private final ChildProjectApiImpl.Factory childApi;
115 : private final ChildProjectsCollection children;
116 : private final ProjectResource project;
117 : private final ProjectJson projectJson;
118 : private final String name;
119 : private final BranchApiImpl.Factory branchApi;
120 : private final TagApiImpl.Factory tagApi;
121 : private final GetAccess getAccess;
122 : private final SetAccess setAccess;
123 : private final CreateAccessChange createAccessChange;
124 : private final GetConfig getConfig;
125 : private final PutConfig putConfig;
126 : private final CommitsIncludedInRefs commitsIncludedInRefs;
127 : private final Provider<ListBranches> listBranches;
128 : private final Provider<ListTags> listTags;
129 : private final DeleteBranches deleteBranches;
130 : private final DeleteTags deleteTags;
131 : private final CommitsCollection commitsCollection;
132 : private final CommitApiImpl.Factory commitApi;
133 : private final DashboardApiImpl.Factory dashboardApi;
134 : private final CheckAccess checkAccess;
135 : private final Check check;
136 : private final Provider<ListDashboards> listDashboards;
137 : private final GetHead getHead;
138 : private final SetHead setHead;
139 : private final GetParent getParent;
140 : private final SetParent setParent;
141 : private final Index index;
142 : private final IndexChanges indexChanges;
143 : private final Provider<ListLabels> listLabels;
144 : private final Provider<ListSubmitRequirements> listSubmitRequirements;
145 : private final PostLabels postLabels;
146 : private final LabelApiImpl.Factory labelApi;
147 : private final SubmitRequirementApiImpl.Factory submitRequirementApi;
148 :
149 : @AssistedInject
150 : ProjectApiImpl(
151 : PermissionBackend permissionBackend,
152 : CreateProject createProject,
153 : ProjectApiImpl.Factory projectApi,
154 : ProjectsCollection projects,
155 : GetDescription getDescription,
156 : PutDescription putDescription,
157 : ChildProjectApiImpl.Factory childApi,
158 : ChildProjectsCollection children,
159 : ProjectJson projectJson,
160 : BranchApiImpl.Factory branchApiFactory,
161 : TagApiImpl.Factory tagApiFactory,
162 : GetAccess getAccess,
163 : SetAccess setAccess,
164 : CreateAccessChange createAccessChange,
165 : GetConfig getConfig,
166 : PutConfig putConfig,
167 : CommitsIncludedInRefs commitsIncludedInRefs,
168 : Provider<ListBranches> listBranches,
169 : Provider<ListTags> listTags,
170 : DeleteBranches deleteBranches,
171 : DeleteTags deleteTags,
172 : CommitsCollection commitsCollection,
173 : CommitApiImpl.Factory commitApi,
174 : DashboardApiImpl.Factory dashboardApi,
175 : CheckAccess checkAccess,
176 : Check check,
177 : Provider<ListDashboards> listDashboards,
178 : GetHead getHead,
179 : SetHead setHead,
180 : GetParent getParent,
181 : SetParent setParent,
182 : Index index,
183 : IndexChanges indexChanges,
184 : Provider<ListLabels> listLabels,
185 : Provider<ListSubmitRequirements> listSubmitRequirements,
186 : PostLabels postLabels,
187 : LabelApiImpl.Factory labelApi,
188 : SubmitRequirementApiImpl.Factory submitRequirementApi,
189 : @Assisted ProjectResource project) {
190 143 : this(
191 : permissionBackend,
192 : createProject,
193 : projectApi,
194 : projects,
195 : getDescription,
196 : putDescription,
197 : childApi,
198 : children,
199 : projectJson,
200 : branchApiFactory,
201 : tagApiFactory,
202 : getAccess,
203 : setAccess,
204 : createAccessChange,
205 : getConfig,
206 : putConfig,
207 : commitsIncludedInRefs,
208 : listBranches,
209 : listTags,
210 : deleteBranches,
211 : deleteTags,
212 : project,
213 : commitsCollection,
214 : commitApi,
215 : dashboardApi,
216 : checkAccess,
217 : check,
218 : listDashboards,
219 : getHead,
220 : setHead,
221 : getParent,
222 : setParent,
223 : index,
224 : indexChanges,
225 : listLabels,
226 : listSubmitRequirements,
227 : postLabels,
228 : labelApi,
229 : submitRequirementApi,
230 : null);
231 143 : }
232 :
233 : @AssistedInject
234 : ProjectApiImpl(
235 : PermissionBackend permissionBackend,
236 : CreateProject createProject,
237 : ProjectApiImpl.Factory projectApi,
238 : ProjectsCollection projects,
239 : GetDescription getDescription,
240 : PutDescription putDescription,
241 : ChildProjectApiImpl.Factory childApi,
242 : ChildProjectsCollection children,
243 : ProjectJson projectJson,
244 : BranchApiImpl.Factory branchApiFactory,
245 : TagApiImpl.Factory tagApiFactory,
246 : GetAccess getAccess,
247 : SetAccess setAccess,
248 : CreateAccessChange createAccessChange,
249 : GetConfig getConfig,
250 : PutConfig putConfig,
251 : CommitsIncludedInRefs commitsIncludedInRefs,
252 : Provider<ListBranches> listBranches,
253 : Provider<ListTags> listTags,
254 : DeleteBranches deleteBranches,
255 : DeleteTags deleteTags,
256 : CommitsCollection commitsCollection,
257 : CommitApiImpl.Factory commitApi,
258 : DashboardApiImpl.Factory dashboardApi,
259 : CheckAccess checkAccess,
260 : Check check,
261 : Provider<ListDashboards> listDashboards,
262 : GetHead getHead,
263 : SetHead setHead,
264 : GetParent getParent,
265 : SetParent setParent,
266 : Index index,
267 : IndexChanges indexChanges,
268 : Provider<ListLabels> listLabels,
269 : Provider<ListSubmitRequirements> listSubmitRequirements,
270 : PostLabels postLabels,
271 : LabelApiImpl.Factory labelApi,
272 : SubmitRequirementApiImpl.Factory submitRequirementApi,
273 : @Assisted String name) {
274 143 : this(
275 : permissionBackend,
276 : createProject,
277 : projectApi,
278 : projects,
279 : getDescription,
280 : putDescription,
281 : childApi,
282 : children,
283 : projectJson,
284 : branchApiFactory,
285 : tagApiFactory,
286 : getAccess,
287 : setAccess,
288 : createAccessChange,
289 : getConfig,
290 : putConfig,
291 : commitsIncludedInRefs,
292 : listBranches,
293 : listTags,
294 : deleteBranches,
295 : deleteTags,
296 : null,
297 : commitsCollection,
298 : commitApi,
299 : dashboardApi,
300 : checkAccess,
301 : check,
302 : listDashboards,
303 : getHead,
304 : setHead,
305 : getParent,
306 : setParent,
307 : index,
308 : indexChanges,
309 : listLabels,
310 : listSubmitRequirements,
311 : postLabels,
312 : labelApi,
313 : submitRequirementApi,
314 : name);
315 143 : }
316 :
317 : private ProjectApiImpl(
318 : PermissionBackend permissionBackend,
319 : CreateProject createProject,
320 : ProjectApiImpl.Factory projectApi,
321 : ProjectsCollection projects,
322 : GetDescription getDescription,
323 : PutDescription putDescription,
324 : ChildProjectApiImpl.Factory childApi,
325 : ChildProjectsCollection children,
326 : ProjectJson projectJson,
327 : BranchApiImpl.Factory branchApiFactory,
328 : TagApiImpl.Factory tagApiFactory,
329 : GetAccess getAccess,
330 : SetAccess setAccess,
331 : CreateAccessChange createAccessChange,
332 : GetConfig getConfig,
333 : PutConfig putConfig,
334 : CommitsIncludedInRefs commitsIncludedInRefs,
335 : Provider<ListBranches> listBranches,
336 : Provider<ListTags> listTags,
337 : DeleteBranches deleteBranches,
338 : DeleteTags deleteTags,
339 : ProjectResource project,
340 : CommitsCollection commitsCollection,
341 : CommitApiImpl.Factory commitApi,
342 : DashboardApiImpl.Factory dashboardApi,
343 : CheckAccess checkAccess,
344 : Check check,
345 : Provider<ListDashboards> listDashboards,
346 : GetHead getHead,
347 : SetHead setHead,
348 : GetParent getParent,
349 : SetParent setParent,
350 : Index index,
351 : IndexChanges indexChanges,
352 : Provider<ListLabels> listLabels,
353 : Provider<ListSubmitRequirements> listSubmitRequirements,
354 : PostLabels postLabels,
355 : LabelApiImpl.Factory labelApi,
356 : SubmitRequirementApiImpl.Factory submitRequirementApi,
357 143 : String name) {
358 143 : this.permissionBackend = permissionBackend;
359 143 : this.createProject = createProject;
360 143 : this.projectApi = projectApi;
361 143 : this.projects = projects;
362 143 : this.getDescription = getDescription;
363 143 : this.putDescription = putDescription;
364 143 : this.childApi = childApi;
365 143 : this.children = children;
366 143 : this.projectJson = projectJson;
367 143 : this.project = project;
368 143 : this.branchApi = branchApiFactory;
369 143 : this.tagApi = tagApiFactory;
370 143 : this.getAccess = getAccess;
371 143 : this.setAccess = setAccess;
372 143 : this.getConfig = getConfig;
373 143 : this.putConfig = putConfig;
374 143 : this.commitsIncludedInRefs = commitsIncludedInRefs;
375 143 : this.listBranches = listBranches;
376 143 : this.listTags = listTags;
377 143 : this.deleteBranches = deleteBranches;
378 143 : this.deleteTags = deleteTags;
379 143 : this.commitsCollection = commitsCollection;
380 143 : this.commitApi = commitApi;
381 143 : this.createAccessChange = createAccessChange;
382 143 : this.dashboardApi = dashboardApi;
383 143 : this.checkAccess = checkAccess;
384 143 : this.check = check;
385 143 : this.listDashboards = listDashboards;
386 143 : this.getHead = getHead;
387 143 : this.setHead = setHead;
388 143 : this.getParent = getParent;
389 143 : this.setParent = setParent;
390 143 : this.name = name;
391 143 : this.index = index;
392 143 : this.indexChanges = indexChanges;
393 143 : this.listLabels = listLabels;
394 143 : this.listSubmitRequirements = listSubmitRequirements;
395 143 : this.postLabels = postLabels;
396 143 : this.labelApi = labelApi;
397 143 : this.submitRequirementApi = submitRequirementApi;
398 143 : }
399 :
400 : @Override
401 : public ProjectApi create() throws RestApiException {
402 0 : return create(new ProjectInput());
403 : }
404 :
405 : @Override
406 : public ProjectApi create(ProjectInput in) throws RestApiException {
407 : try {
408 143 : if (name == null) {
409 2 : throw new ResourceConflictException("Project already exists");
410 : }
411 143 : if (in.name != null && !name.equals(in.name)) {
412 1 : throw new BadRequestException("name must match input.name");
413 : }
414 143 : permissionBackend
415 143 : .currentUser()
416 143 : .checkAny(GlobalPermission.fromAnnotation(createProject.getClass()));
417 143 : createProject.apply(TopLevelResource.INSTANCE, IdString.fromDecoded(name), in);
418 143 : return projectApi.create(projects.parse(name));
419 3 : } catch (Exception e) {
420 2 : throw asRestApiException("Cannot create project: " + e.getMessage(), e);
421 : }
422 : }
423 :
424 : @Override
425 : public ProjectInfo get() throws RestApiException {
426 11 : if (project == null) {
427 1 : throw new ResourceNotFoundException(name);
428 : }
429 11 : return projectJson.format(project.getProjectState());
430 : }
431 :
432 : @Override
433 : public String description() throws RestApiException {
434 : try {
435 1 : return getDescription.apply(checkExists()).value();
436 0 : } catch (Exception e) {
437 0 : throw asRestApiException("Cannot get description", e);
438 : }
439 : }
440 :
441 : @Override
442 : public ProjectAccessInfo access() throws RestApiException {
443 : try {
444 1 : return getAccess.apply(checkExists()).value();
445 1 : } catch (Exception e) {
446 1 : throw asRestApiException("Cannot get access rights", e);
447 : }
448 : }
449 :
450 : @Override
451 : public ProjectAccessInfo access(ProjectAccessInput p) throws RestApiException {
452 : try {
453 7 : return setAccess.apply(checkExists(), p).value();
454 1 : } catch (Exception e) {
455 1 : throw asRestApiException("Cannot put access rights", e);
456 : }
457 : }
458 :
459 : @Override
460 : public ChangeInfo accessChange(ProjectAccessInput p) throws RestApiException {
461 : try {
462 1 : return createAccessChange.apply(checkExists(), p).value();
463 1 : } catch (Exception e) {
464 1 : throw asRestApiException("Cannot put access right change", e);
465 : }
466 : }
467 :
468 : @Override
469 : public AccessCheckInfo checkAccess(AccessCheckInput in) throws RestApiException {
470 : try {
471 1 : return checkAccess.apply(checkExists(), in).value();
472 1 : } catch (Exception e) {
473 1 : throw asRestApiException("Cannot check access rights", e);
474 : }
475 : }
476 :
477 : @Override
478 : public CheckProjectResultInfo check(CheckProjectInput in) throws RestApiException {
479 : try {
480 1 : return check.apply(checkExists(), in).value();
481 1 : } catch (Exception e) {
482 1 : throw asRestApiException("Cannot check project", e);
483 : }
484 : }
485 :
486 : @Override
487 : public void description(DescriptionInput in) throws RestApiException {
488 : try {
489 1 : putDescription.apply(checkExists(), in);
490 0 : } catch (Exception e) {
491 0 : throw asRestApiException("Cannot put project description", e);
492 1 : }
493 1 : }
494 :
495 : @Override
496 : public ConfigInfo config() throws RestApiException {
497 : try {
498 2 : return getConfig.apply(checkExists()).value();
499 0 : } catch (Exception e) {
500 0 : throw asRestApiException("Cannot get config", e);
501 : }
502 : }
503 :
504 : @Override
505 : public ConfigInfo config(ConfigInput in) throws RestApiException {
506 : try {
507 21 : return putConfig.apply(checkExists(), in).value();
508 1 : } catch (Exception e) {
509 1 : throw asRestApiException("Cannot list tags", e);
510 : }
511 : }
512 :
513 : @Override
514 : public Map<String, Set<String>> commitsIn(Collection<String> commits, Collection<String> refs)
515 : throws RestApiException {
516 : try {
517 1 : commitsIncludedInRefs.addCommits(commits);
518 1 : commitsIncludedInRefs.addRefs(refs);
519 1 : return commitsIncludedInRefs.apply(project).value();
520 1 : } catch (Exception e) {
521 1 : throw asRestApiException("Cannot list commits included in refs", e);
522 : }
523 : }
524 :
525 : @Override
526 : public ListRefsRequest<BranchInfo> branches() {
527 7 : return new ListRefsRequest<>() {
528 : @Override
529 : public List<BranchInfo> get() throws RestApiException {
530 : try {
531 7 : return listBranches.get().request(this).apply(checkExists()).value();
532 1 : } catch (Exception e) {
533 1 : throw asRestApiException("Cannot list branches", e);
534 : }
535 : }
536 : };
537 : }
538 :
539 : @Override
540 : public ListRefsRequest<TagInfo> tags() {
541 2 : return new ListRefsRequest<>() {
542 : @Override
543 : public List<TagInfo> get() throws RestApiException {
544 : try {
545 2 : return listTags.get().request(this).apply(checkExists()).value();
546 1 : } catch (Exception e) {
547 1 : throw asRestApiException("Cannot list tags", e);
548 : }
549 : }
550 : };
551 : }
552 :
553 : @Override
554 : public List<ProjectInfo> children() throws RestApiException {
555 1 : return children(false);
556 : }
557 :
558 : @Override
559 : public List<ProjectInfo> children(boolean recursive) throws RestApiException {
560 : try {
561 1 : return children.list().withRecursive(recursive).apply(checkExists()).value();
562 0 : } catch (Exception e) {
563 0 : throw asRestApiException("Cannot list children", e);
564 : }
565 : }
566 :
567 : @Override
568 : public List<ProjectInfo> children(int limit) throws RestApiException {
569 : try {
570 1 : return children.list().withLimit(limit).apply(checkExists()).value();
571 0 : } catch (Exception e) {
572 0 : throw asRestApiException("Cannot list children", e);
573 : }
574 : }
575 :
576 : @Override
577 : public ChildProjectApi child(String name) throws RestApiException {
578 : try {
579 1 : return childApi.create(children.parse(checkExists(), IdString.fromDecoded(name)));
580 2 : } catch (Exception e) {
581 2 : throw asRestApiException("Cannot parse child project", e);
582 : }
583 : }
584 :
585 : @Override
586 : public BranchApi branch(String ref) throws ResourceNotFoundException {
587 33 : return branchApi.create(checkExists(), ref);
588 : }
589 :
590 : @Override
591 : public TagApi tag(String ref) throws ResourceNotFoundException {
592 7 : return tagApi.create(checkExists(), ref);
593 : }
594 :
595 : @Override
596 : public void deleteBranches(DeleteBranchesInput in) throws RestApiException {
597 : try {
598 1 : deleteBranches.apply(checkExists(), in);
599 1 : } catch (Exception e) {
600 1 : throw asRestApiException("Cannot delete branches", e);
601 1 : }
602 1 : }
603 :
604 : @Override
605 : public void deleteTags(DeleteTagsInput in) throws RestApiException {
606 : try {
607 1 : deleteTags.apply(checkExists(), in);
608 1 : } catch (Exception e) {
609 1 : throw asRestApiException("Cannot delete tags", e);
610 1 : }
611 1 : }
612 :
613 : @Override
614 : public CommitApi commit(String commit) throws RestApiException {
615 : try {
616 3 : return commitApi.create(commitsCollection.parse(checkExists(), IdString.fromDecoded(commit)));
617 0 : } catch (Exception e) {
618 0 : throw asRestApiException("Cannot parse commit", e);
619 : }
620 : }
621 :
622 : @Override
623 : public DashboardApi dashboard(String name) throws RestApiException {
624 : try {
625 1 : return dashboardApi.create(checkExists(), name);
626 0 : } catch (Exception e) {
627 0 : throw asRestApiException("Cannot parse dashboard", e);
628 : }
629 : }
630 :
631 : @Override
632 : public DashboardApi defaultDashboard() throws RestApiException {
633 1 : return dashboard(DEFAULT_DASHBOARD_NAME);
634 : }
635 :
636 : @Override
637 : public void defaultDashboard(String name) throws RestApiException {
638 : try {
639 1 : dashboardApi.create(checkExists(), name).setDefault();
640 0 : } catch (Exception e) {
641 0 : throw asRestApiException("Cannot set default dashboard", e);
642 1 : }
643 1 : }
644 :
645 : @Override
646 : public void removeDefaultDashboard() throws RestApiException {
647 : try {
648 1 : dashboardApi.create(checkExists(), null).setDefault();
649 0 : } catch (Exception e) {
650 0 : throw asRestApiException("Cannot remove default dashboard", e);
651 1 : }
652 1 : }
653 :
654 : @Override
655 : public ListDashboardsRequest dashboards() throws RestApiException {
656 1 : return new ListDashboardsRequest() {
657 : @Override
658 : public List<DashboardInfo> get() throws RestApiException {
659 : try {
660 1 : List<?> r = listDashboards.get().apply(checkExists()).value();
661 1 : if (r.isEmpty()) {
662 1 : return Collections.emptyList();
663 : }
664 1 : if (r.get(0) instanceof DashboardInfo) {
665 1 : return r.stream().map(i -> (DashboardInfo) i).collect(toList());
666 : }
667 0 : throw new NotImplementedException("list with inheritance");
668 0 : } catch (Exception e) {
669 0 : throw asRestApiException("Cannot list dashboards", e);
670 : }
671 : }
672 : };
673 : }
674 :
675 : @Override
676 : public String head() throws RestApiException {
677 : try {
678 2 : return getHead.apply(checkExists()).value();
679 0 : } catch (Exception e) {
680 0 : throw asRestApiException("Cannot get HEAD", e);
681 : }
682 : }
683 :
684 : @Override
685 : public void head(String head) throws RestApiException {
686 1 : HeadInput input = new HeadInput();
687 1 : input.ref = head;
688 : try {
689 1 : setHead.apply(checkExists(), input);
690 1 : } catch (Exception e) {
691 1 : throw asRestApiException("Cannot set HEAD", e);
692 1 : }
693 1 : }
694 :
695 : @Override
696 : public String parent() throws RestApiException {
697 : try {
698 1 : return getParent.apply(checkExists()).value();
699 0 : } catch (Exception e) {
700 0 : throw asRestApiException("Cannot get parent", e);
701 : }
702 : }
703 :
704 : @Override
705 : public void parent(String parent) throws RestApiException {
706 : try {
707 1 : ParentInput input = new ParentInput();
708 1 : input.parent = parent;
709 1 : setParent.apply(checkExists(), input);
710 1 : } catch (Exception e) {
711 1 : throw asRestApiException("Cannot set parent", e);
712 1 : }
713 1 : }
714 :
715 : @Override
716 : public void index(boolean indexChildren) throws RestApiException {
717 : try {
718 1 : IndexProjectInput input = new IndexProjectInput();
719 1 : input.indexChildren = indexChildren;
720 1 : index.apply(checkExists(), input);
721 0 : } catch (Exception e) {
722 0 : throw asRestApiException("Cannot index project", e);
723 1 : }
724 1 : }
725 :
726 : @Override
727 : public void indexChanges() throws RestApiException {
728 : try {
729 1 : indexChanges.apply(checkExists(), new Input());
730 0 : } catch (Exception e) {
731 0 : throw asRestApiException("Cannot index changes", e);
732 1 : }
733 1 : }
734 :
735 : private ProjectResource checkExists() throws ResourceNotFoundException {
736 65 : if (project == null) {
737 4 : throw new ResourceNotFoundException(name);
738 : }
739 65 : return project;
740 : }
741 :
742 : @Override
743 : public ListLabelsRequest labels() {
744 2 : return new ListLabelsRequest() {
745 : @Override
746 : public List<LabelDefinitionInfo> get() throws RestApiException {
747 : try {
748 2 : return listLabels.get().withInherited(inherited).apply(checkExists()).value();
749 1 : } catch (Exception e) {
750 1 : throw asRestApiException("Cannot list labels", e);
751 : }
752 : }
753 : };
754 : }
755 :
756 : @Override
757 : public ListSubmitRequirementsRequest submitRequirements() {
758 1 : return new ListSubmitRequirementsRequest() {
759 : @Override
760 : public List<SubmitRequirementInfo> get() throws RestApiException {
761 : try {
762 1 : return listSubmitRequirements.get().withInherited(inherited).apply(checkExists()).value();
763 1 : } catch (Exception e) {
764 1 : throw asRestApiException("Cannot list submit requirements", e);
765 : }
766 : }
767 : };
768 : }
769 :
770 : @Override
771 : public LabelApi label(String labelName) throws RestApiException {
772 : try {
773 7 : return labelApi.create(checkExists(), labelName);
774 0 : } catch (Exception e) {
775 0 : throw asRestApiException("Cannot parse label", e);
776 : }
777 : }
778 :
779 : @Override
780 : public SubmitRequirementApi submitRequirement(String name) throws RestApiException {
781 : try {
782 2 : return submitRequirementApi.create(checkExists(), name);
783 0 : } catch (Exception e) {
784 0 : throw asRestApiException("Cannot parse submit requirement", e);
785 : }
786 : }
787 :
788 : @Override
789 : public void labels(BatchLabelInput input) throws RestApiException {
790 : try {
791 1 : postLabels.apply(checkExists(), input);
792 1 : } catch (Exception e) {
793 1 : throw asRestApiException("Cannot update labels", e);
794 1 : }
795 1 : }
796 : }
|