Fix: REST API projects endpoints do not correctly handle '.git' suffix

The projects REST API documentation states that the `.git` suffix will
be stripped off the input project name, if present.

This was working for the "Create Project" endpoint, because the suffix
is stripped off by the repository manager. However it was not being
stripped off for other endpoints.  This caused the projects API to fail
when called with a project name with `.git` suffix.

Fix the name parsing in the projects collection to strip off the suffix.

Add a new acceptance test class for the "Get Project" endpoint, testing
the cases with and without the `.git` suffix, and with a non-existing
project.

Extend the "Create Project" REST API and projects API acceptance tests
to include the case with `.git` suffix.

Bug: Issue 3153
Change-Id: Id9c851972017e8ff833686c40623c3c4228cad30
This commit is contained in:
David Pursehouse 2015-02-02 17:20:00 +09:00
parent f0e50fce1e
commit 3695e7ebc9
4 changed files with 91 additions and 0 deletions

View File

@ -44,6 +44,17 @@ public class ProjectIT extends AbstractDaemonTest {
.name);
}
@Test
public void createProjectFooWithGitSuffix() throws Exception {
String name = "foo";
assertThat(name).isEqualTo(
gApi.projects()
.name(name + ".git")
.create()
.get()
.name);
}
@Test(expected = RestApiException.class)
public void createProjectFooBar() throws Exception {
ProjectInput in = new ProjectInput();

View File

@ -57,6 +57,17 @@ public class CreateProjectIT extends AbstractDaemonTest {
assertHead(newProjectName, "refs/heads/master");
}
@Test
public void testCreateProjectApiWithGitSuffix() throws Exception {
final String newProjectName = "newProject";
ProjectInfo p = gApi.projects().name(newProjectName + ".git").create().get();
assertThat(p.name).isEqualTo(newProjectName);
ProjectState projectState = projectCache.get(new Project.NameKey(newProjectName));
assertThat(projectState).isNotNull();
assertProjectInfo(projectState.getProject(), p);
assertHead(newProjectName, "refs/heads/master");
}
@Test
public void testCreateProject() throws Exception {
final String newProjectName = "newProject";
@ -70,6 +81,19 @@ public class CreateProjectIT extends AbstractDaemonTest {
assertHead(newProjectName, "refs/heads/master");
}
@Test
public void testCreateProjectWithGitSuffix() throws Exception {
final String newProjectName = "newProject";
RestResponse r = adminSession.put("/projects/" + newProjectName + ".git");
assertThat(r.getStatusCode()).isEqualTo(HttpStatus.SC_CREATED);
ProjectInfo p = newGson().fromJson(r.getReader(), ProjectInfo.class);
assertThat(p.name).isEqualTo(newProjectName);
ProjectState projectState = projectCache.get(new Project.NameKey(newProjectName));
assertThat(projectState).isNotNull();
assertProjectInfo(projectState.getProject(), p);
assertHead(newProjectName, "refs/heads/master");
}
@Test
public void testCreateProjectWithNameMismatch_BadRequest() throws Exception {
ProjectInput in = new ProjectInput();

View File

@ -0,0 +1,51 @@
// Copyright (C) 2015 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.acceptance.rest.project;
import static com.google.common.truth.Truth.assertThat;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.extensions.common.ProjectInfo;
import org.apache.http.HttpStatus;
import org.junit.Test;
public class GetProjectIT extends AbstractDaemonTest {
@Test
public void getProject() throws Exception {
String name = project.get();
RestResponse r = adminSession.get("/projects/" + name);
assertThat(r.getStatusCode()).isEqualTo(HttpStatus.SC_OK);
ProjectInfo p = newGson().fromJson(r.getReader(), ProjectInfo.class);
assertThat(p.name).isEqualTo(name);
}
@Test
public void getProjectWithGitSuffix() throws Exception {
String name = project.get();
RestResponse r = adminSession.get("/projects/" + name + ".git");
assertThat(r.getStatusCode()).isEqualTo(HttpStatus.SC_OK);
ProjectInfo p = newGson().fromJson(r.getReader(), ProjectInfo.class);
assertThat(p.name).isEqualTo(name);
}
@Test
public void getProjectNotExisting() throws Exception {
RestResponse r = adminSession.get("/projects/does-not-exist");
assertThat(r.getStatusCode()).isEqualTo(HttpStatus.SC_NOT_FOUND);
}
}

View File

@ -31,6 +31,8 @@ import com.google.inject.Singleton;
import java.io.IOException;
import org.eclipse.jgit.lib.Constants;
@Singleton
public class ProjectsCollection implements
RestCollection<TopLevelResource, ProjectResource>,
@ -88,6 +90,9 @@ public class ProjectsCollection implements
}
private ProjectResource _parse(String id) throws IOException {
if (id.endsWith(Constants.DOT_GIT_EXT)) {
id = id.substring(0, id.length() - Constants.DOT_GIT_EXT.length());
}
ProjectControl ctl;
try {
ctl = controlFactory.controlFor(