Support to retrieve a branch via REST
GET on /projects/<project-name>/branches/<branch-id> retrieves a branch. Change-Id: I1c2c81dcc5292d644d8048436c142039a4274e32 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
@@ -531,6 +531,35 @@ returned.
|
||||
]
|
||||
----
|
||||
|
||||
[[get-branch]]
|
||||
Get Branch
|
||||
~~~~~~~~~~
|
||||
[verse]
|
||||
'GET /projects/link:#project-name[\{project-name\}]/branches/link:#branch-id[\{branch-id\}]'
|
||||
|
||||
Retrieves a branch of a project.
|
||||
|
||||
.Request
|
||||
----
|
||||
GET /projects/work%2Fmy-project/branches/master HTTP/1.0
|
||||
----
|
||||
|
||||
As response a link:#branch-info[BranchInfo] entity is returned that
|
||||
describes the branch.
|
||||
|
||||
.Response
|
||||
----
|
||||
HTTP/1.1 200 OK
|
||||
Content-Disposition: attachment
|
||||
Content-Type: application/json;charset=UTF-8
|
||||
|
||||
)]}'
|
||||
{
|
||||
"ref": "refs/heads/master",
|
||||
"revision": "67ebf73496383c6777035e374d2d664009e2aa5c"
|
||||
}
|
||||
----
|
||||
|
||||
[[child-project-endpoints]]
|
||||
Child Project Endpoints
|
||||
-----------------------
|
||||
@@ -901,6 +930,12 @@ requests.
|
||||
IDs
|
||||
---
|
||||
|
||||
[[branch-id]]
|
||||
\{branch-id\}
|
||||
~~~~~~~~~~~~~
|
||||
The name of a branch or `HEAD`. The prefix `refs/heads/` can be
|
||||
omitted.
|
||||
|
||||
[[dashboard-id]]
|
||||
\{dashboard-id\}
|
||||
~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -15,21 +15,21 @@
|
||||
package com.google.gerrit.server.project;
|
||||
|
||||
import com.google.gerrit.extensions.restapi.RestView;
|
||||
import com.google.gerrit.reviewdb.client.Branch;
|
||||
import com.google.gerrit.server.project.ListBranches.BranchInfo;
|
||||
import com.google.inject.TypeLiteral;
|
||||
|
||||
public class BranchResource extends ProjectResource {
|
||||
public static final TypeLiteral<RestView<BranchResource>> BRANCH_KIND =
|
||||
new TypeLiteral<RestView<BranchResource>>() {};
|
||||
|
||||
private final Branch.NameKey branch;
|
||||
private final BranchInfo branchInfo;
|
||||
|
||||
public BranchResource(ProjectControl control, Branch.NameKey branch) {
|
||||
public BranchResource(ProjectControl control, BranchInfo branchInfo) {
|
||||
super(control);
|
||||
this.branch = branch;
|
||||
this.branchInfo = branchInfo;
|
||||
}
|
||||
|
||||
public Branch.NameKey getBranch() {
|
||||
return branch;
|
||||
public BranchInfo getBranchInfo() {
|
||||
return branchInfo;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,15 @@ import com.google.gerrit.extensions.restapi.ChildCollection;
|
||||
import com.google.gerrit.extensions.restapi.IdString;
|
||||
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
|
||||
import com.google.gerrit.extensions.restapi.RestView;
|
||||
import com.google.gerrit.server.project.ListBranches.BranchInfo;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class BranchesCollection implements
|
||||
ChildCollection<ProjectResource, BranchResource> {
|
||||
private final DynamicMap<RestView<BranchResource>> views;
|
||||
@@ -41,8 +47,19 @@ public class BranchesCollection implements
|
||||
|
||||
@Override
|
||||
public BranchResource parse(ProjectResource parent, IdString id)
|
||||
throws ResourceNotFoundException {
|
||||
throw new ResourceNotFoundException(id);
|
||||
throws ResourceNotFoundException, IOException {
|
||||
String branchName = id.get();
|
||||
if (!branchName.startsWith(Constants.R_REFS)
|
||||
&& !branchName.equals(Constants.HEAD)) {
|
||||
branchName = Constants.R_HEADS + branchName;
|
||||
}
|
||||
List<BranchInfo> branches = list.get().apply(parent);
|
||||
for (BranchInfo b : branches) {
|
||||
if (branchName.equals(b.ref)) {
|
||||
return new BranchResource(parent.getControl(), b);
|
||||
}
|
||||
}
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.project;
|
||||
|
||||
import com.google.gerrit.extensions.restapi.RestReadView;
|
||||
import com.google.gerrit.server.project.ListBranches.BranchInfo;
|
||||
|
||||
public class GetBranch implements RestReadView<BranchResource> {
|
||||
|
||||
@Override
|
||||
public BranchInfo apply(BranchResource rsrc) {
|
||||
return rsrc.getBranchInfo();
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,7 @@ public class Module extends RestApiModule {
|
||||
post(PROJECT_KIND, "gc").to(GarbageCollect.class);
|
||||
|
||||
child(PROJECT_KIND, "branches").to(BranchesCollection.class);
|
||||
get(BRANCH_KIND).to(GetBranch.class);
|
||||
|
||||
child(PROJECT_KIND, "dashboards").to(DashboardsCollection.class);
|
||||
get(DASHBOARD_KIND).to(GetDashboard.class);
|
||||
|
||||
Reference in New Issue
Block a user