use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.
the class Schema_139 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
ListMultimap<Account.Id, ProjectWatch> imports = MultimapBuilder.hashKeys().arrayListValues().build();
try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
ResultSet rs = stmt.executeQuery("SELECT " + "account_id, " + "project_name, " + "filter, " + "notify_abandoned_changes, " + "notify_all_comments, " + "notify_new_changes, " + "notify_new_patch_sets, " + "notify_submitted_changes " + "FROM account_project_watches")) {
while (rs.next()) {
Account.Id accountId = new Account.Id(rs.getInt(1));
ProjectWatch.Builder b = ProjectWatch.builder().project(new Project.NameKey(rs.getString(2))).filter(rs.getString(3)).notifyAbandonedChanges(rs.getBoolean(4)).notifyAllComments(rs.getBoolean(5)).notifyNewChanges(rs.getBoolean(6)).notifyNewPatchSets(rs.getBoolean(7)).notifySubmittedChanges(rs.getBoolean(8));
imports.put(accountId, b.build());
}
}
if (imports.isEmpty()) {
return;
}
try (Repository git = repoManager.openRepository(allUsersName);
RevWalk rw = new RevWalk(git)) {
BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
bru.setRefLogIdent(serverUser);
bru.setRefLogMessage(MSG, false);
for (Map.Entry<Account.Id, Collection<ProjectWatch>> e : imports.asMap().entrySet()) {
Map<ProjectWatchKey, Set<NotifyType>> projectWatches = new HashMap<>();
for (ProjectWatch projectWatch : e.getValue()) {
ProjectWatchKey key = ProjectWatchKey.create(projectWatch.project(), projectWatch.filter());
if (projectWatches.containsKey(key)) {
throw new OrmDuplicateKeyException("Duplicate key for watched project: " + key.toString());
}
Set<NotifyType> notifyValues = EnumSet.noneOf(NotifyType.class);
if (projectWatch.notifyAbandonedChanges()) {
notifyValues.add(NotifyType.ABANDONED_CHANGES);
}
if (projectWatch.notifyAllComments()) {
notifyValues.add(NotifyType.ALL_COMMENTS);
}
if (projectWatch.notifyNewChanges()) {
notifyValues.add(NotifyType.NEW_CHANGES);
}
if (projectWatch.notifyNewPatchSets()) {
notifyValues.add(NotifyType.NEW_PATCHSETS);
}
if (projectWatch.notifySubmittedChanges()) {
notifyValues.add(NotifyType.SUBMITTED_CHANGES);
}
projectWatches.put(key, notifyValues);
}
try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
md.getCommitBuilder().setAuthor(serverUser);
md.getCommitBuilder().setCommitter(serverUser);
md.setMessage(MSG);
WatchConfig watchConfig = new WatchConfig(e.getKey());
watchConfig.load(md);
watchConfig.setProjectWatches(projectWatches);
watchConfig.commit(md);
}
}
bru.execute(rw, NullProgressMonitor.INSTANCE);
} catch (IOException | ConfigInvalidException ex) {
throw new OrmException(ex);
}
}
use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.
the class Schema_106 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException {
if (!(repoManager instanceof LocalDiskRepositoryManager)) {
return;
}
ui.message("listing all repositories ...");
SortedSet<Project.NameKey> repoList = repoManager.list();
ui.message("done");
ui.message(String.format("creating reflog files for %s branches ...", RefNames.REFS_CONFIG));
ExecutorService executorPool = createExecutor(ui, repoList.size());
List<Future<Void>> futures = new ArrayList<>();
for (Project.NameKey project : repoList) {
Callable<Void> callable = new ReflogCreator(project);
futures.add(executorPool.submit(callable));
}
executorPool.shutdown();
try {
for (Future<Void> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
ui.message(e.getCause().getMessage());
}
}
ui.message("done");
} catch (InterruptedException ex) {
String msg = String.format("Migration step 106 was interrupted. " + "Reflog created in %d of %d repositories only.", countDone(futures), repoList.size());
ui.message(msg);
}
}
use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.
the class Schema_130 method migrateData.
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException {
SortedSet<Project.NameKey> repoList = repoManager.list();
SortedSet<Project.NameKey> repoUpgraded = new TreeSet<>();
ui.message("\tMigrating " + repoList.size() + " repositories ...");
for (Project.NameKey projectName : repoList) {
try (Repository git = repoManager.openRepository(projectName);
MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, projectName, git)) {
ProjectConfigSchemaUpdate cfg = ProjectConfigSchemaUpdate.read(md);
cfg.removeForceFromPermission("pushTag");
if (cfg.isUpdated()) {
repoUpgraded.add(projectName);
}
cfg.save(serverUser, COMMIT_MSG);
} catch (ConfigInvalidException | IOException ex) {
throw new OrmException("Cannot migrate project " + projectName, ex);
}
}
ui.message("\tMigration completed: " + repoUpgraded.size() + " repositories updated:");
ui.message("\t" + repoUpgraded.stream().map(n -> n.get()).collect(joining(" ")));
}
use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.
the class SubmoduleSectionParser method parse.
private SubmoduleSubscription parse(final String id) {
final String url = bbc.getString("submodule", id, "url");
final String path = bbc.getString("submodule", id, "path");
String branch = bbc.getString("submodule", id, "branch");
try {
if (url != null && url.length() > 0 && path != null && path.length() > 0 && branch != null && branch.length() > 0) {
// All required fields filled.
String project;
if (branch.equals(".")) {
branch = superProjectBranch.get();
}
// relative URL
if (url.startsWith("../")) {
// prefix with a slash for easier relative path walks
project = '/' + superProjectBranch.getParentKey().get();
String hostPart = url;
while (hostPart.startsWith("../")) {
int lastSlash = project.lastIndexOf('/');
if (lastSlash < 0) {
// too many levels up, ignore for now
return null;
}
project = project.substring(0, lastSlash);
hostPart = hostPart.substring(3);
}
project = project + "/" + hostPart;
// remove leading '/'
project = project.substring(1);
} else {
// It is actually an URI. It could be ssh://localhost/project-a.
URI targetServerURI = new URI(url);
URI thisServerURI = new URI(canonicalWebUrl);
String thisHost = thisServerURI.getHost();
String targetHost = targetServerURI.getHost();
if (thisHost == null || targetHost == null || !targetHost.equalsIgnoreCase(thisHost)) {
return null;
}
String p1 = targetServerURI.getPath();
String p2 = thisServerURI.getPath();
if (!p1.startsWith(p2)) {
// http://server/other-teams-gerrit/
return null;
}
// skip common part
project = p1.substring(p2.length());
}
while (project.startsWith("/")) {
project = project.substring(1);
}
if (project.endsWith(Constants.DOT_GIT_EXT)) {
project = project.substring(//
0, project.length() - Constants.DOT_GIT_EXT.length());
}
Project.NameKey projectKey = new Project.NameKey(project);
return new SubmoduleSubscription(superProjectBranch, new Branch.NameKey(projectKey, branch), path);
}
} catch (URISyntaxException e) {
// Error in url syntax (in fact it is uri syntax)
}
return null;
}
use of com.google.gerrit.reviewdb.client.Project in project gerrit by GerritCodeReview.
the class ChangeIT method addReviewerThatCannotSeeChange.
@Test
public void addReviewerThatCannotSeeChange() throws Exception {
// create hidden project that is only visible to administrators
Project.NameKey p = createProject("p");
ProjectConfig cfg = projectCache.checkedGet(p).getConfig();
Util.allow(cfg, Permission.READ, groupCache.get(new AccountGroup.NameKey("Administrators")).getGroupUUID(), "refs/*");
Util.block(cfg, Permission.READ, REGISTERED_USERS, "refs/*");
saveProjectConfig(p, cfg);
// create change
TestRepository<InMemoryRepository> repo = cloneProject(p, admin);
PushOneCommit push = pushFactory.create(db, admin.getIdent(), repo);
PushOneCommit.Result result = push.to("refs/for/master");
result.assertOkStatus();
// check the user cannot see the change
setApiUser(user);
try {
gApi.changes().id(result.getChangeId()).get();
fail("Expected ResourceNotFoundException");
} catch (ResourceNotFoundException e) {
// Expected.
}
// try to add user as reviewer
setApiUser(admin);
AddReviewerInput in = new AddReviewerInput();
in.reviewer = user.email;
AddReviewerResult r = gApi.changes().id(result.getChangeId()).addReviewer(in);
assertThat(r.input).isEqualTo(user.email);
assertThat(r.error).contains("does not have permission to see this change");
assertThat(r.reviewers).isNull();
}
Aggregations