use of com.atlassian.stash.util.PageRequestImpl in project stashbot by palantir.
the class RepoConfigurationServlet method doPost.
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Repository rep = getRepository(req);
if (rep == null) {
log.error("Failed to get repo for request" + req.toString());
res.sendError(404);
return;
}
try {
permissionValidationService.validateForRepository(rep, Permission.REPO_ADMIN);
} catch (AuthorisationException notRepoAdmin) {
// Skip form processing
doGet(req, res);
return;
}
try {
// This is the new jenkins server name
String jenkinsServerName = req.getParameter("jenkinsServerName");
// If either the old or the new Jenkins Server Configuration is "locked", and we are trying to change it, then enforce SYS_ADMIN instead of REPO_ADMIN
try {
RepositoryConfiguration rc = configurationPersistanceManager.getRepositoryConfigurationForRepository(rep);
JenkinsServerConfiguration oldConfig = configurationPersistanceManager.getJenkinsServerConfiguration(rc.getJenkinsServerName());
JenkinsServerConfiguration newConfig = configurationPersistanceManager.getJenkinsServerConfiguration(jenkinsServerName);
if (!jenkinsServerName.equals(oldConfig.getName())) {
if (oldConfig.getLocked()) {
permissionValidationService.validateForGlobal(Permission.SYS_ADMIN);
}
if (newConfig.getLocked()) {
permissionValidationService.validateForGlobal(Permission.SYS_ADMIN);
}
}
} catch (AuthorisationException notSysAdmin) {
// only thrown when oldconfig is locked and newconfig's name is different from oldconfig's name.
log.warn("User {} tried to change the jenkins configuration which was locked for repo {}", req.getRemoteUser(), rep.getSlug());
res.sendError(HttpServletResponse.SC_UNAUTHORIZED, "You do not have permission to change the jenkins server configuration");
return;
}
configurationPersistanceManager.setRepositoryConfigurationForRepositoryFromRequest(rep, req);
RepositoryConfiguration rc = configurationPersistanceManager.getRepositoryConfigurationForRepository(rep);
if (rc.getCiEnabled()) {
// ensure all pull request metadata exists
PullRequestSearchRequest prsr = new PullRequestSearchRequest.Builder().toRepositoryId(rep.getId()).build();
PageRequest pageReq = new PageRequestImpl(0, 500);
Page<PullRequest> page = prs.search(prsr, pageReq);
while (true) {
for (PullRequest pr : page.getValues()) {
// this auto-vivifies if it doesn't already exist
configurationPersistanceManager.getPullRequestMetadata(pr);
}
if (page.getIsLastPage()) {
break;
}
pageReq = page.getNextPageRequest();
page = prs.search(prsr, pageReq);
}
// add permission to the requisite user
JenkinsServerConfiguration jsc = configurationPersistanceManager.getJenkinsServerConfiguration(jenkinsServerName);
pluginUserManager.addUserToRepoForReading(jsc.getStashUsername(), rep);
// ensure hook is enabled, jobs exist
jenkinsManager.updateRepo(rep);
}
} catch (SQLException e) {
log.error("Unable to get repository confguration", e);
}
doGet(req, res);
}
use of com.atlassian.stash.util.PageRequestImpl in project stashbot by palantir.
the class JenkinsManager method createMissingJobs.
public void createMissingJobs() {
ExecutorService es = Executors.newCachedThreadPool();
List<Future<Void>> futures = new LinkedList<Future<Void>>();
PageRequest pageReq = new PageRequestImpl(0, 500);
Page<? extends Repository> p = repositoryService.findAll(pageReq);
while (true) {
for (Repository r : p.getValues()) {
Future<Void> f = es.submit(new CreateMissingRepositoryVisitor(jenkinsClientManager, jtm, cpm, r, lf));
futures.add(f);
}
if (p.getIsLastPage())
break;
pageReq = p.getNextPageRequest();
p = repositoryService.findAll(pageReq);
}
for (Future<Void> f : futures) {
try {
// don't care about return, just catch exceptions
f.get();
} catch (ExecutionException e) {
log.error("Exception while attempting to create missing jobs for a repo: ", e);
} catch (InterruptedException e) {
log.error("Interrupted: this shouldn't happen", e);
}
}
}
use of com.atlassian.stash.util.PageRequestImpl in project stashbot by palantir.
the class JenkinsManager method updateAllJobs.
public void updateAllJobs() {
ExecutorService es = Executors.newCachedThreadPool();
List<Future<Void>> futures = new LinkedList<Future<Void>>();
PageRequest pageReq = new PageRequestImpl(0, 500);
Page<? extends Repository> p = repositoryService.findAll(pageReq);
while (true) {
for (Repository r : p.getValues()) {
Future<Void> f = es.submit(new UpdateAllRepositoryVisitor(jenkinsClientManager, jtm, cpm, r, lf));
futures.add(f);
}
if (p.getIsLastPage())
break;
pageReq = p.getNextPageRequest();
p = repositoryService.findAll(pageReq);
}
for (Future<Void> f : futures) {
try {
// don't care about return, just catch exceptions
f.get();
} catch (ExecutionException e) {
log.error("Exception while attempting to create missing jobs for a repo: ", e);
} catch (InterruptedException e) {
log.error("Interrupted: this shouldn't happen", e);
}
}
}
use of com.atlassian.stash.util.PageRequestImpl in project stash-codesearch-plugin by palantir.
the class RepositoryServiceManagerImpl method getBranchMap.
@Override
public ImmutableMap<String, Branch> getBranchMap(Repository repository) {
PageRequest req = new PageRequestImpl(0, PAGE_SIZE);
Map<String, Branch> branchMap = new HashMap<String, Branch>();
RepositoryBranchesRequest rbr = new RepositoryBranchesRequest.Builder().repository(repository).order(RefOrder.ALPHABETICAL).build();
while (true) {
Page<? extends Branch> branchPage = repositoryMetadataService.getBranches(rbr, req);
for (Branch b : branchPage.getValues()) {
if (branchMap.containsKey(b)) {
log.error("Tryting to insert existing key '" + b.getId() + "' into branchMap with value '" + b + "'");
continue;
}
branchMap.put(b.getId(), b);
}
if (branchPage.getIsLastPage()) {
break;
}
req = branchPage.getNextPageRequest();
}
return ImmutableMap.copyOf(branchMap);
}
use of com.atlassian.stash.util.PageRequestImpl in project stash-codesearch-plugin by palantir.
the class RepositoryServiceManagerImpl method getRepositoryMap.
@Override
public ImmutableMap<String, Repository> getRepositoryMap(PermissionValidationService validationService) {
PageRequest req = new PageRequestImpl(0, PAGE_SIZE);
Map<String, Repository> repoMap = new HashMap<String, Repository>();
while (true) {
Page<? extends Repository> repoPage = repositoryService.findAll(req);
for (Repository r : repoPage.getValues()) {
try {
if (validationService != null) {
validationService.validateForRepository(r, Permission.REPO_READ);
}
final String key = r.getProject().getKey() + "^" + r.getSlug();
if (repoMap.containsKey(key)) {
// ITOOLS-13350
log.error("Trying to insert existing key '" + key + "' intp repoMap with value " + r.toString());
continue;
}
repoMap.put(key, r);
} catch (AuthorisationException e) {
// User doesn't have permission to access the repo
}
}
if (repoPage.getIsLastPage()) {
break;
}
req = repoPage.getNextPageRequest();
}
return ImmutableMap.copyOf(repoMap);
}
Aggregations