Plugin API for project

Change-Id: I5ba62674ca4df6d704145e7a1cd8e55810a030b6
This commit is contained in:
David Ostrovsky
2013-11-19 21:43:37 +01:00
parent 23e1a2e824
commit 1e2073d3af
14 changed files with 372 additions and 5 deletions

View File

@@ -0,0 +1,7 @@
include_defs('//gerrit-acceptance-tests/tests.defs')
acceptance_tests(
srcs = glob(['*IT.java']),
deps = ['//gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/git:util'],
)

View File

@@ -0,0 +1,88 @@
// 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.acceptance.api.project;
import static com.google.gerrit.acceptance.git.GitUtil.createProject;
import static com.google.gerrit.acceptance.git.GitUtil.initSsh;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.AcceptanceTestRequestScope;
import com.google.gerrit.acceptance.AccountCreator;
import com.google.gerrit.acceptance.SshSession;
import com.google.gerrit.acceptance.TestAccount;
import com.google.gerrit.extensions.api.GerritApi;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.util.Providers;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class ProjectIT extends AbstractDaemonTest {
@Inject
private AccountCreator accounts;
@Inject
private SchemaFactory<ReviewDb> reviewDbProvider;
@Inject
private GerritApi gApi;
@Inject
private AcceptanceTestRequestScope atrScope;
@Inject
private IdentifiedUser.GenericFactory identifiedUserFactory;
private TestAccount admin;
private ReviewDb db;
Project.NameKey project;
@Before
public void setUp() throws Exception {
admin = accounts.admin();
initSsh(admin);
project = new Project.NameKey("p");
SshSession sshSession = new SshSession(server, admin);
createProject(sshSession, project.get());
db = reviewDbProvider.open();
atrScope.set(atrScope.newContext(reviewDbProvider, sshSession,
identifiedUserFactory.create(Providers.of(db), admin.getId())));
}
@After
public void cleanup() {
db.close();
}
@Test
public void createBranch() throws GitAPIException,
IOException, RestApiException {
gApi.projects()
.name(project.get())
.branch("foo")
.create(new BranchInput());
}
}

View File

@@ -15,7 +15,9 @@
package com.google.gerrit.extensions.api;
import com.google.gerrit.extensions.api.changes.Changes;
import com.google.gerrit.extensions.api.projects.Projects;
public interface GerritApi {
public Changes changes();
public Projects projects();
}

View File

@@ -0,0 +1,21 @@
// 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.extensions.api.projects;
import com.google.gerrit.extensions.restapi.RestApiException;
public interface BranchApi {
BranchApi create(BranchInput in) throws RestApiException;
}

View File

@@ -0,0 +1,22 @@
// 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.extensions.api.projects;
import com.google.gerrit.extensions.restapi.DefaultInput;
public class BranchInput {
@DefaultInput
public String revision;
}

View File

@@ -0,0 +1,19 @@
// 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.extensions.api.projects;
public interface ProjectApi {
BranchApi branch(String ref);
}

View File

@@ -0,0 +1,21 @@
// 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.extensions.api.projects;
import com.google.gerrit.extensions.restapi.RestApiException;
public interface Projects {
ProjectApi name(String name) throws RestApiException;
}

View File

@@ -16,19 +16,28 @@ package com.google.gerrit.server.api;
import com.google.gerrit.extensions.api.GerritApi;
import com.google.gerrit.extensions.api.changes.Changes;
import com.google.gerrit.extensions.api.projects.Projects;
import com.google.inject.Inject;
import com.google.inject.Provider;
class GerritApiImpl implements GerritApi {
private final Provider<Changes> changes;
private final Provider<Projects> projects;
@Inject
GerritApiImpl(Provider<Changes> changes) {
GerritApiImpl(Provider<Changes> changes,
Provider<Projects> projects) {
this.changes = changes;
this.projects = projects;
}
@Override
public Changes changes() {
return changes.get();
}
@Override
public Projects projects() {
return projects.get();
}
}

View File

@@ -23,5 +23,6 @@ public class Module extends AbstractModule {
bind(GerritApi.class).to(GerritApiImpl.class);
install(new com.google.gerrit.server.api.changes.Module());
install(new com.google.gerrit.server.api.projects.Module());
}
}

View File

@@ -0,0 +1,58 @@
// 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.api.projects;
import com.google.gerrit.extensions.api.projects.BranchApi;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.project.CreateBranch;
import com.google.gerrit.server.project.ProjectResource;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import java.io.IOException;
public class BranchApiImpl implements BranchApi {
interface Factory {
BranchApiImpl create(ProjectResource project, String ref);
}
private final CreateBranch.Factory createBranchFactory;
private final String ref;
private final ProjectResource project;
@Inject
BranchApiImpl(
CreateBranch.Factory createBranchFactory,
@Assisted ProjectResource project,
@Assisted String ref) {
this.createBranchFactory = createBranchFactory;
this.project = project;
this.ref = ref;
}
@Override
public BranchApi create(BranchInput in) throws RestApiException {
try {
CreateBranch.Input input = new CreateBranch.Input();
input.ref = ref;
input.revision = in.revision;
createBranchFactory.create(ref).apply(project, input);
return this;
} catch (IOException e) {
throw new RestApiException("Cannot create branch", e);
}
}
}

View File

@@ -0,0 +1,28 @@
// 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.api.projects;
import com.google.gerrit.extensions.api.projects.Projects;
import com.google.gerrit.server.config.FactoryModule;
public class Module extends FactoryModule {
@Override
protected void configure() {
bind(Projects.class).to(ProjectsImpl.class);
factory(BranchApiImpl.Factory.class);
factory(ProjectApiImpl.Factory.class);
}
}

View File

@@ -0,0 +1,45 @@
// 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.api.projects;
import com.google.gerrit.extensions.api.projects.BranchApi;
import com.google.gerrit.extensions.api.projects.ProjectApi;
import com.google.gerrit.server.project.CreateBranch;
import com.google.gerrit.server.project.ProjectResource;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
public class ProjectApiImpl implements ProjectApi {
interface Factory {
ProjectApiImpl create(ProjectResource change);
}
private final ProjectResource project;
private final BranchApiImpl.Factory branchApi;
@Inject
ProjectApiImpl(
CreateBranch.Factory createBranchFactory,
BranchApiImpl.Factory branchApiFactory,
@Assisted ProjectResource project) {
this.project = project;
this.branchApi = branchApiFactory;
}
@Override
public BranchApi branch(String ref) {
return branchApi.create(project, ref);
}
}

View File

@@ -0,0 +1,46 @@
// 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.api.projects;
import com.google.gerrit.extensions.api.projects.ProjectApi;
import com.google.gerrit.extensions.api.projects.Projects;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
import com.google.gerrit.server.project.ProjectsCollection;
import com.google.inject.Inject;
import java.io.IOException;
class ProjectsImpl implements Projects {
private final ProjectsCollection projects;
private final ProjectApiImpl.Factory api;
@Inject
ProjectsImpl(ProjectsCollection projects, ProjectApiImpl.Factory api) {
this.projects = projects;
this.api = api;
}
@Override
public ProjectApi name(String name) throws RestApiException {
try {
return api.create(projects.parse(name));
} catch (IOException e) {
throw new RestApiException("Cannot retrieve project");
} catch (UnprocessableEntityException e) {
throw new RestApiException("Cannot retrieve project");
}
}
}

View File

@@ -53,14 +53,14 @@ import java.io.IOException;
public class CreateBranch implements RestModifyView<ProjectResource, Input> {
private static final Logger log = LoggerFactory.getLogger(CreateBranch.class);
static class Input {
String ref;
public static class Input {
public String ref;
@DefaultInput
String revision;
public String revision;
}
static interface Factory {
public static interface Factory {
CreateBranch create(String ref);
}