use of com.google.gerrit.server.git.MetaDataUpdate in project gerrit by GerritCodeReview.
the class PutConfig method apply.
public ConfigInfo apply(ProjectControl ctrl, ConfigInput input) throws ResourceNotFoundException, BadRequestException, ResourceConflictException {
Project.NameKey projectName = ctrl.getProject().getNameKey();
if (input == null) {
throw new BadRequestException("config is required");
}
try (MetaDataUpdate md = metaDataUpdateFactory.get().create(projectName)) {
ProjectConfig projectConfig = ProjectConfig.read(md);
Project p = projectConfig.getProject();
p.setDescription(Strings.emptyToNull(input.description));
if (input.useContributorAgreements != null) {
p.setUseContributorAgreements(input.useContributorAgreements);
}
if (input.useContentMerge != null) {
p.setUseContentMerge(input.useContentMerge);
}
if (input.useSignedOffBy != null) {
p.setUseSignedOffBy(input.useSignedOffBy);
}
if (input.createNewChangeForAllNotInTarget != null) {
p.setCreateNewChangeForAllNotInTarget(input.createNewChangeForAllNotInTarget);
}
if (input.requireChangeId != null) {
p.setRequireChangeID(input.requireChangeId);
}
if (serverEnableSignedPush) {
if (input.enableSignedPush != null) {
p.setEnableSignedPush(input.enableSignedPush);
}
if (input.requireSignedPush != null) {
p.setRequireSignedPush(input.requireSignedPush);
}
}
if (input.rejectImplicitMerges != null) {
p.setRejectImplicitMerges(input.rejectImplicitMerges);
}
if (input.maxObjectSizeLimit != null) {
p.setMaxObjectSizeLimit(input.maxObjectSizeLimit);
}
if (input.submitType != null) {
p.setSubmitType(input.submitType);
}
if (input.state != null) {
p.setState(input.state);
}
if (input.enableReviewerByEmail != null) {
p.setEnableReviewerByEmail(input.enableReviewerByEmail);
}
if (input.pluginConfigValues != null) {
setPluginConfigValues(ctrl.getProjectState(), projectConfig, input.pluginConfigValues);
}
md.setMessage("Modified project settings\n");
try {
projectConfig.commit(md);
projectCache.evict(projectConfig.getProject());
md.getRepository().setGitwebDescription(p.getDescription());
} catch (IOException e) {
if (e.getCause() instanceof ConfigInvalidException) {
throw new ResourceConflictException("Cannot update " + projectName + ": " + e.getCause().getMessage());
}
log.warn(String.format("Failed to update config of project %s.", projectName), e);
throw new ResourceConflictException("Cannot update " + projectName);
}
ProjectState state = projectStateFactory.create(projectConfig);
return new ConfigInfoImpl(serverEnableSignedPush, state.controlFor(user.get()), config, pluginConfigEntries, cfgFactory, allProjects, uiActions, views);
} catch (RepositoryNotFoundException notFound) {
throw new ResourceNotFoundException(projectName.get());
} catch (ConfigInvalidException err) {
throw new ResourceConflictException("Cannot read project " + projectName, err);
} catch (IOException err) {
throw new ResourceConflictException("Cannot update project " + projectName, err);
}
}
use of com.google.gerrit.server.git.MetaDataUpdate in project gerrit by GerritCodeReview.
the class SetDefaultDashboard method apply.
@Override
public Response<DashboardInfo> apply(DashboardResource resource, Input input) throws AuthException, BadRequestException, ResourceConflictException, ResourceNotFoundException, IOException {
if (input == null) {
// Delete would set input to null.
input = new Input();
}
input.id = Strings.emptyToNull(input.id);
ProjectControl ctl = resource.getControl();
if (!ctl.isOwner()) {
throw new AuthException("not project owner");
}
DashboardResource target = null;
if (input.id != null) {
try {
target = dashboards.parse(new ProjectResource(ctl), IdString.fromUrl(input.id));
} catch (ResourceNotFoundException e) {
throw new BadRequestException("dashboard " + input.id + " not found");
} catch (ConfigInvalidException e) {
throw new ResourceConflictException(e.getMessage());
}
}
try (MetaDataUpdate md = updateFactory.create(ctl.getProject().getNameKey())) {
ProjectConfig config = ProjectConfig.read(md);
Project project = config.getProject();
if (inherited) {
project.setDefaultDashboard(input.id);
} else {
project.setLocalDefaultDashboard(input.id);
}
String msg = MoreObjects.firstNonNull(Strings.emptyToNull(input.commitMessage), input.id == null ? "Removed default dashboard.\n" : String.format("Changed default dashboard to %s.\n", input.id));
if (!msg.endsWith("\n")) {
msg += "\n";
}
md.setAuthor(ctl.getUser().asIdentifiedUser());
md.setMessage(msg);
config.commit(md);
cache.evict(ctl.getProject());
if (target != null) {
DashboardInfo info = get.get().apply(target);
info.isDefault = true;
return Response.ok(info);
}
return Response.none();
} catch (RepositoryNotFoundException notFound) {
throw new ResourceNotFoundException(ctl.getProject().getName());
} catch (ConfigInvalidException e) {
throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
}
}
use of com.google.gerrit.server.git.MetaDataUpdate in project gerrit by GerritCodeReview.
the class SetParent method apply.
public String apply(ProjectResource rsrc, Input input, boolean checkIfAdmin) throws AuthException, ResourceConflictException, ResourceNotFoundException, UnprocessableEntityException, IOException, PermissionBackendException {
ProjectControl ctl = rsrc.getControl();
String parentName = MoreObjects.firstNonNull(Strings.emptyToNull(input.parent), allProjects.get());
validateParentUpdate(ctl, parentName, checkIfAdmin);
try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) {
ProjectConfig config = ProjectConfig.read(md);
Project project = config.getProject();
project.setParentName(parentName);
String msg = Strings.emptyToNull(input.commitMessage);
if (msg == null) {
msg = String.format("Changed parent to %s.\n", parentName);
} else if (!msg.endsWith("\n")) {
msg += "\n";
}
md.setAuthor(ctl.getUser().asIdentifiedUser());
md.setMessage(msg);
config.commit(md);
cache.evict(ctl.getProject());
Project.NameKey parent = project.getParent(allProjects);
checkNotNull(parent);
return parent.get();
} catch (RepositoryNotFoundException notFound) {
throw new ResourceNotFoundException(rsrc.getName());
} catch (ConfigInvalidException e) {
throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
}
}
use of com.google.gerrit.server.git.MetaDataUpdate in project gerrit by GerritCodeReview.
the class Schema_115 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
Map<Account.Id, DiffPreferencesInfo> imports = new HashMap<>();
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM account_diff_preferences")) {
Set<String> availableColumns = getColumns(rs);
while (rs.next()) {
Account.Id accountId = new Account.Id(rs.getInt("id"));
DiffPreferencesInfo prefs = new DiffPreferencesInfo();
if (availableColumns.contains("context")) {
prefs.context = (int) rs.getShort("context");
}
if (availableColumns.contains("expand_all_comments")) {
prefs.expandAllComments = toBoolean(rs.getString("expand_all_comments"));
}
if (availableColumns.contains("hide_line_numbers")) {
prefs.hideLineNumbers = toBoolean(rs.getString("hide_line_numbers"));
}
if (availableColumns.contains("hide_top_menu")) {
prefs.hideTopMenu = toBoolean(rs.getString("hide_top_menu"));
}
if (availableColumns.contains("ignore_whitespace")) {
// Enum with char as value
prefs.ignoreWhitespace = toWhitespace(rs.getString("ignore_whitespace"));
}
if (availableColumns.contains("intraline_difference")) {
prefs.intralineDifference = toBoolean(rs.getString("intraline_difference"));
}
if (availableColumns.contains("line_length")) {
prefs.lineLength = rs.getInt("line_length");
}
if (availableColumns.contains("manual_review")) {
prefs.manualReview = toBoolean(rs.getString("manual_review"));
}
if (availableColumns.contains("render_entire_file")) {
prefs.renderEntireFile = toBoolean(rs.getString("render_entire_file"));
}
if (availableColumns.contains("retain_header")) {
prefs.retainHeader = toBoolean(rs.getString("retain_header"));
}
if (availableColumns.contains("show_line_endings")) {
prefs.showLineEndings = toBoolean(rs.getString("show_line_endings"));
}
if (availableColumns.contains("show_tabs")) {
prefs.showTabs = toBoolean(rs.getString("show_tabs"));
}
if (availableColumns.contains("show_whitespace_errors")) {
prefs.showWhitespaceErrors = toBoolean(rs.getString("show_whitespace_errors"));
}
if (availableColumns.contains("skip_deleted")) {
prefs.skipDeleted = toBoolean(rs.getString("skip_deleted"));
}
if (availableColumns.contains("skip_uncommented")) {
prefs.skipUncommented = toBoolean(rs.getString("skip_uncommented"));
}
if (availableColumns.contains("syntax_highlighting")) {
prefs.syntaxHighlighting = toBoolean(rs.getString("syntax_highlighting"));
}
if (availableColumns.contains("tab_size")) {
prefs.tabSize = rs.getInt("tab_size");
}
if (availableColumns.contains("theme")) {
// Enum with name as values; can be null
prefs.theme = toTheme(rs.getString("theme"));
}
if (availableColumns.contains("hide_empty_pane")) {
prefs.hideEmptyPane = toBoolean(rs.getString("hide_empty_pane"));
}
if (availableColumns.contains("auto_hide_diff_table_header")) {
prefs.autoHideDiffTableHeader = toBoolean(rs.getString("auto_hide_diff_table_header"));
}
imports.put(accountId, prefs);
}
}
if (imports.isEmpty()) {
return;
}
try (Repository git = mgr.openRepository(allUsersName);
RevWalk rw = new RevWalk(git)) {
BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
for (Map.Entry<Account.Id, DiffPreferencesInfo> e : imports.entrySet()) {
try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser);
VersionedAccountPreferences p = VersionedAccountPreferences.forUser(e.getKey());
p.load(md);
storeSection(p.getConfig(), UserConfigSections.DIFF, null, e.getValue(), DiffPreferencesInfo.defaults());
p.commit(md);
}
}
bru.execute(rw, NullProgressMonitor.INSTANCE);
} catch (ConfigInvalidException | IOException ex) {
throw new OrmException(ex);
}
}
use of com.google.gerrit.server.git.MetaDataUpdate in project gerrit by GerritCodeReview.
the class Schema_120 method allowSubmoduleSubscription.
private void allowSubmoduleSubscription(Branch.NameKey subbranch, Branch.NameKey superBranch) throws OrmException {
try (Repository git = mgr.openRepository(subbranch.getParentKey());
RevWalk rw = new RevWalk(git)) {
BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, subbranch.getParentKey(), git, bru)) {
md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser);
md.setMessage("Added superproject subscription during upgrade");
ProjectConfig pc = ProjectConfig.read(md);
SubscribeSection s = null;
for (SubscribeSection s1 : pc.getSubscribeSections(subbranch)) {
if (s1.getProject().equals(superBranch.getParentKey())) {
s = s1;
}
}
if (s == null) {
s = new SubscribeSection(superBranch.getParentKey());
pc.addSubscribeSection(s);
}
RefSpec newRefSpec = new RefSpec(subbranch.get() + ":" + superBranch.get());
if (!s.getMatchingRefSpecs().contains(newRefSpec)) {
// For the migration we use only exact RefSpecs, we're not trying to
// generalize it.
s.addMatchingRefSpec(newRefSpec);
}
pc.commit(md);
}
bru.execute(rw, NullProgressMonitor.INSTANCE);
} catch (ConfigInvalidException | IOException e) {
throw new OrmException(e);
}
}
Aggregations