use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class ReceiveCommits method validateConfigPush.
/**
* Validates a push to refs/meta/config, and reject the command if it fails.
*/
private void validateConfigPush(ReceiveCommand cmd) throws PermissionBackendException {
try (TraceTimer traceTimer = newTimer("validateConfigPush")) {
logger.atFine().log("Processing %s command", cmd.getRefName());
try {
permissions.check(ProjectPermission.WRITE_CONFIG);
} catch (AuthException e) {
reject(cmd, String.format("must be either project owner or have %s permission", ProjectPermission.WRITE_CONFIG.describeForException()));
return;
}
switch(cmd.getType()) {
case CREATE:
case UPDATE:
case UPDATE_NONFASTFORWARD:
try {
ProjectConfig cfg = projectConfigFactory.create(project.getNameKey());
cfg.load(project.getNameKey(), receivePack.getRevWalk(), cmd.getNewId());
if (!cfg.getValidationErrors().isEmpty()) {
addError("Invalid project configuration:");
for (ValidationError err : cfg.getValidationErrors()) {
addError(" " + err.getMessage());
}
reject(cmd, "invalid project configuration");
logger.atSevere().log("User %s tried to push invalid project configuration %s for %s", user.getLoggableName(), cmd.getNewId().name(), project.getName());
return;
}
Project.NameKey newParent = cfg.getProject().getParent(allProjectsName);
Project.NameKey oldParent = project.getParent(allProjectsName);
if (oldParent == null) {
// update of the 'All-Projects' project
if (newParent != null) {
reject(cmd, "invalid project configuration: root project cannot have parent");
return;
}
} else {
if (!oldParent.equals(newParent)) {
if (allowProjectOwnersToChangeParent) {
try {
permissionBackend.user(user).project(project.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
} catch (AuthException e) {
reject(cmd, "invalid project configuration: only project owners can set parent");
return;
}
} else {
try {
permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
} catch (AuthException e) {
reject(cmd, "invalid project configuration: only Gerrit admin can set parent");
return;
}
}
}
if (!projectCache.get(newParent).isPresent()) {
reject(cmd, "invalid project configuration: parent does not exist");
return;
}
}
validatePluginConfig(cmd, cfg);
} catch (Exception e) {
reject(cmd, "invalid project configuration");
logger.atSevere().withCause(e).log("User %s tried to push invalid project configuration %s for %s", user.getLoggableName(), cmd.getNewId().name(), project.getName());
return;
}
break;
case DELETE:
break;
default:
reject(cmd, "prohibited by Gerrit: don't know how to handle config update of type " + cmd.getType());
}
}
}
use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class SubmitStrategyOp method checkProjectConfig.
private void checkProjectConfig(RepoContext ctx, CodeReviewCommit commit) {
String refName = getDest().branch();
if (RefNames.REFS_CONFIG.equals(refName)) {
logger.atFine().log("Loading new configuration from %s", RefNames.REFS_CONFIG);
try {
ProjectConfig cfg = args.projectConfigFactory.create(getProject());
cfg.load(ctx.getRevWalk(), commit);
} catch (Exception e) {
throw new StorageException("Submit would store invalid" + " project configuration " + commit.name() + " for " + getProject(), e);
}
}
}
use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class AccessIT method grantRevertPermissionOnlyWorksOnce.
@Test
public void grantRevertPermissionOnlyWorksOnce() throws Exception {
grantRevertPermission.execute(newProjectName);
grantRevertPermission.execute(newProjectName);
try (Repository repo = repoManager.openRepository(newProjectName)) {
MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, newProjectName, repo);
ProjectConfig projectConfig = projectConfigFactory.read(md);
AccessSection all = projectConfig.getAccessSection(AccessSection.ALL);
Permission permission = all.getPermission(Permission.REVERT);
assertThat(permission.getRules()).hasSize(1);
}
}
use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class SubmitRequirementsValidationIT method readProjectConfig.
private Config readProjectConfig(RevCommit commit) throws Exception {
try (TreeWalk tw = TreeWalk.forPath(testRepo.getRevWalk().getObjectReader(), ProjectConfig.PROJECT_CONFIG, commit.getTree())) {
if (tw == null) {
throw new IllegalStateException(String.format("%s does not exist", ProjectConfig.PROJECT_CONFIG));
}
}
RevObject blob = testRepo.get(commit.getTree(), ProjectConfig.PROJECT_CONFIG);
byte[] data = testRepo.getRepository().open(blob).getCachedBytes(Integer.MAX_VALUE);
String content = RawParseUtils.decode(data);
Config projectConfig = new Config();
projectConfig.fromText(content);
return projectConfig;
}
use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.
the class RefControlTest method setUp.
@Before
public void setUp() throws Exception {
Injector injector = Guice.createInjector(new InMemoryModule());
injector.injectMembers(this);
// Tests previously used ProjectConfig.Factory to create ProjectConfigs without going through
// the ProjectCache, which was wrong. Manually call getInstance so we don't store it in a
// field that is accessible to test methods.
ProjectConfig.Factory projectConfigFactory = injector.getInstance(ProjectConfig.Factory.class);
singleVersionListener.start();
try {
schemaCreator.create();
} finally {
singleVersionListener.stop();
}
// changing defaults in SchemaCreator or ProjectCreator.
try (Repository allProjectsRepo = repoManager.createRepository(allProjectsName);
TestRepository<Repository> tr = new TestRepository<>(allProjectsRepo)) {
tr.delete(REFS_CONFIG);
try (MetaDataUpdate md = metaDataUpdateFactory.create(allProjectsName)) {
ProjectConfig allProjectsConfig = projectConfigFactory.create(allProjectsName);
allProjectsConfig.load(md);
LabelType cr = TestLabels.codeReview();
allProjectsConfig.upsertLabelType(cr);
allProjectsConfig.commit(md);
}
}
repoManager.createRepository(parentKey).close();
repoManager.createRepository(localKey).close();
try (MetaDataUpdate md = metaDataUpdateFactory.create(localKey)) {
ProjectConfig newLocal = projectConfigFactory.create(localKey);
newLocal.load(md);
newLocal.updateProject(p -> p.setParent(parentKey));
newLocal.commit(md);
}
requestContext.setContext(() -> null);
}
Aggregations