use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.
the class GitContentRepository method getRepoFirstCommitId.
@Override
public String getRepoFirstCommitId(final String site) {
String toReturn = EMPTY;
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repository = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repository != null) {
synchronized (repository) {
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repo != null) {
try (RevWalk rw = new RevWalk(repo)) {
ObjectId head = repo.resolve(HEAD);
if (head != null) {
RevCommit root = rw.parseCommit(head);
rw.sort(REVERSE);
rw.markStart(root);
ObjectId first = rw.next();
toReturn = first.getName();
logger.debug("getRepoFirstCommitId for site: " + site + " First commit ID: " + toReturn);
}
}
}
}
}
} catch (IOException | CryptoException e) {
logger.error("Error getting first commit ID for site " + site, e);
}
return toReturn;
}
use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.
the class GitContentRepository method getContentVersion.
@Override
public InputStream getContentVersion(String site, String path, String version) throws ContentNotFoundException {
InputStream toReturn = null;
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
RevTree tree = helper.getTreeForCommit(repo, version);
if (tree != null) {
try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
if (tw != null) {
ObjectId id = tw.getObjectId(0);
ObjectLoader objectLoader = repo.open(id);
toReturn = objectLoader.openStream();
tw.close();
}
} catch (IOException e) {
logger.error("Error while getting content for file at site: " + site + " path: " + path + " version: " + version, e);
}
}
} catch (IOException | CryptoException e) {
logger.error("Failed to create RevTree for site: " + site + " path: " + path + " version: " + version, e);
}
return toReturn;
}
use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.
the class BlueprintsUpgradeOperation method execute.
@Override
public void execute(final String site) throws UpgradeException {
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Path globalConfigPath = helper.buildRepoPath(GitRepositories.GLOBAL);
Path blueprintsPath = Paths.get(globalConfigPath.toAbsolutePath().toString(), studioConfiguration.getProperty(BLUE_PRINTS_PATH));
String studioManifestLocation = servletContext.getRealPath(STUDIO_MANIFEST_LOCATION);
String blueprintsManifestLocation = Paths.get(blueprintsPath.toAbsolutePath().toString(), "BLUEPRINTS.MF").toAbsolutePath().toString();
boolean blueprintManifestExists = Files.exists(Paths.get(blueprintsManifestLocation));
InputStream studioManifestStream = FileUtils.openInputStream(new File(studioManifestLocation));
Manifest studioManifest = new Manifest(studioManifestStream);
VersionInfo studioVersion = VersionInfo.getVersion(studioManifest);
InputStream blueprintsManifestStream = null;
Manifest blueprintsManifest = null;
VersionInfo blueprintsVersion = null;
if (blueprintManifestExists) {
blueprintsManifestStream = FileUtils.openInputStream(new File(blueprintsManifestLocation));
blueprintsManifest = new Manifest(blueprintsManifestStream);
blueprintsVersion = VersionInfo.getVersion(blueprintsManifest);
}
if (!blueprintManifestExists || !StringUtils.equals(studioVersion.getPackageBuild(), blueprintsVersion.getPackageBuild()) || (StringUtils.equals(studioVersion.getPackageBuild(), blueprintsVersion.getPackageBuild()) && !StringUtils.equals(studioVersion.getPackageBuildDate(), blueprintsVersion.getPackageBuildDate()))) {
String bootstrapBlueprintsFolderPath = servletContext.getRealPath(FILE_SEPARATOR + BOOTSTRAP_REPO_PATH + FILE_SEPARATOR + BOOTSTRAP_REPO_GLOBAL_PATH + FILE_SEPARATOR + studioConfiguration.getProperty(BLUE_PRINTS_PATH));
File bootstrapBlueprintsFolder = new File(bootstrapBlueprintsFolderPath);
File[] blueprintFolders = bootstrapBlueprintsFolder.listFiles(File::isDirectory);
for (File blueprintFolder : blueprintFolders) {
String blueprintName = blueprintFolder.getName();
FileUtils.deleteDirectory(Paths.get(blueprintsPath.toAbsolutePath().toString(), blueprintName).toFile());
TreeCopier tc = new TreeCopier(Paths.get(blueprintFolder.getAbsolutePath()), Paths.get(blueprintsPath.toAbsolutePath().toString(), blueprintName));
EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
Files.walkFileTree(Paths.get(blueprintFolder.getAbsolutePath()), opts, Integer.MAX_VALUE, tc);
}
FileUtils.copyFile(Paths.get(studioManifestLocation).toFile(), Paths.get(globalConfigPath.toAbsolutePath().toString(), studioConfiguration.getProperty(BLUE_PRINTS_PATH), "BLUEPRINTS.MF").toFile());
}
Repository globalRepo = helper.getRepository(site, GitRepositories.GLOBAL);
try (Git git = new Git(globalRepo)) {
StatusCommand statusCommand = git.status();
Status status = retryingRepositoryOperationFacade.call(statusCommand);
if (status.hasUncommittedChanges() || !status.isClean()) {
// Commit everything
// TODO: Consider what to do with the commitId in the future
AddCommand addCommand = git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS);
retryingRepositoryOperationFacade.call(addCommand);
CommitCommand commitCommand = git.commit().setAll(true).setMessage(studioConfiguration.getProperty(REPO_BLUEPRINTS_UPDATED_COMMIT_MESSAGE));
retryingRepositoryOperationFacade.call(commitCommand);
}
} catch (GitAPIException err) {
logger.error("error creating initial commit for global configuration", err);
}
} catch (Exception e) {
throw new UpgradeException("Error upgrading blueprints in the global repo", e);
} finally {
generalLockService.unlock(gitLockKey);
}
}
use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.
the class GitContentRepository method contentExists.
@Override
public boolean contentExists(String site, String path) {
boolean toReturn = false;
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repo != null) {
RevTree tree = helper.getTreeForLastCommit(repo);
try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
// pick the first item in the list
if (tw != null && tw.getObjectId(0) != null) {
toReturn = true;
tw.close();
} else if (tw == null) {
String gitPath = helper.getGitPath(path);
if (StringUtils.isEmpty(gitPath) || gitPath.equals(".")) {
toReturn = true;
}
}
} catch (IOException e) {
logger.info("Content not found for site: " + site + " path: " + path, e);
}
}
} catch (Exception e) {
logger.error("Failed to create RevTree for site: " + site + " path: " + path, e);
}
return toReturn;
}
use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.
the class GitContentRepository method writeContent.
@Override
public String writeContent(String site, String path, InputStream content) {
// Write content to git and commit it
String commitId = null;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repo != null) {
if (helper.writeFile(repo, site, path, content)) {
PersonIdent user = helper.getCurrentUserIdent();
String username = securityService.getCurrentUser();
String comment = helper.getCommitMessage(REPO_SANDBOX_WRITE_COMMIT_MESSAGE).replace(REPO_COMMIT_MESSAGE_USERNAME_VAR, username).replace(REPO_COMMIT_MESSAGE_PATH_VAR, path);
commitId = helper.commitFile(repo, site, path, comment, user);
} else {
logger.error("Failed to write content site: " + site + " path: " + path);
}
} else {
logger.error("Missing repository during write for site: " + site + " path: " + path);
}
}
} catch (ServiceLayerException | UserNotFoundException | CryptoException e) {
logger.error("Unknown service error during write for site: " + site + " path: " + path, e);
} finally {
generalLockService.unlock(gitLockKey);
}
return commitId;
}
Aggregations