use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method updateGitlog.
@Override
public void updateGitlog(String siteId, String lastProcessedCommitId, int batchSize) throws SiteNotFoundException {
RingBuffer<RevCommit> commitIds = new RingBuffer<RevCommit>(batchSize);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repository = helper.getRepository(siteId, StringUtils.isEmpty(siteId) ? GLOBAL : SANDBOX);
if (repository != null) {
try {
ObjectId objCommitIdFrom = repository.resolve(lastProcessedCommitId);
ObjectId objCommitIdTo = repository.resolve(HEAD);
logger.debug("Update git log from " + objCommitIdFrom.getName() + " to " + objCommitIdTo.getName());
try (Git git = new Git(repository)) {
// let's do it
if (!objCommitIdFrom.equals(objCommitIdTo)) {
// Get the log of all the commits between commitId and head
Iterable<RevCommit> commits = git.log().call();
ObjectId nextCommitId;
String commitId = EMPTY;
Iterator<RevCommit> iterator = commits.iterator();
while (iterator.hasNext()) {
RevCommit commit = iterator.next();
if (StringUtils.equals(commit.getId().getName(), lastProcessedCommitId)) {
break;
}
commitIds.write(commit);
}
List<String> batch = new ArrayList<String>();
RevCommit current = commitIds.read();
while (current != null) {
nextCommitId = current.getId();
commitId = nextCommitId.getName();
if (StringUtils.contains(current.getFullMessage(), studioConfiguration.getProperty(REPO_SYNC_DB_COMMIT_MESSAGE_NO_PROCESSING))) {
logger.debug("Skipping commitId: " + commitId + " for site " + siteId + " because it is marked not to be processed.");
} else {
batch.add(0, commitId);
}
current = commitIds.read();
}
if (batch.size() > 0) {
gitLogDao.insertIgnoreGitLogList(siteId, batch);
siteService.updateLastSyncedGitlogCommitId(siteId, batch.get(batch.size() - 1));
logger.debug("Inserted " + batch.size() + " git log commits for site " + siteId);
} else {
siteService.updateLastSyncedGitlogCommitId(siteId, objCommitIdTo.getName());
}
}
} catch (GitAPIException e) {
logger.error("Error getting commit ids for site " + siteId + " from commit ID: " + lastProcessedCommitId + " to HEAD", e);
}
} catch (IOException e) {
logger.error("Error getting commit ids for site " + siteId + " from commit ID: " + lastProcessedCommitId + " to HEAD", e);
}
}
} catch (CryptoException e) {
logger.error("Error getting commit ids for site " + siteId + " from commit ID: " + lastProcessedCommitId + " to HEAD", e);
}
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method createSiteCloneRemote.
@Override
public boolean createSiteCloneRemote(String siteId, String sandboxBranch, String remoteName, String remoteUrl, String remoteBranch, boolean singleBranch, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey, Map<String, String> params, boolean createAsOrphan) throws InvalidRemoteRepositoryException, InvalidRemoteRepositoryCredentialsException, RemoteRepositoryNotFoundException, ServiceLayerException {
boolean toReturn = false;
// clone remote git repository for site content
logger.debug("Creating site " + siteId + " as a clone of remote repository " + remoteName + " (" + remoteUrl + ").");
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
toReturn = helper.createSiteCloneRemoteGitRepo(siteId, sandboxBranch, remoteName, remoteUrl, remoteBranch, singleBranch, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey, createAsOrphan);
if (toReturn) {
try {
if (createAsOrphan) {
removeRemote(siteId, remoteName);
} else {
insertRemoteToDb(siteId, remoteName, remoteUrl, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey);
}
} catch (CryptoException e) {
throw new ServiceLayerException(e);
}
// update site name variable inside config files
logger.debug("Update site name configuration variables for site " + siteId);
toReturn = helper.updateSiteNameConfigVar(siteId);
if (toReturn) {
toReturn = helper.replaceParameters(siteId, params);
}
if (toReturn) {
// commit everything so it is visible
logger.debug("Perform initial commit for site " + siteId);
toReturn = helper.performInitialCommit(siteId, helper.getCommitMessage(REPO_INITIAL_COMMIT_COMMIT_MESSAGE), sandboxBranch);
}
} else {
logger.error("Error while creating site " + siteId + " by cloning remote repository " + remoteName + " (" + remoteUrl + ").");
}
} catch (CryptoException e) {
logger.error("Error while creating site " + siteId + " by cloning remote repository " + remoteName + " (" + remoteUrl + ").", e);
} finally {
generalLockService.unlock(gitLockKey);
}
return toReturn;
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method getRepoLastCommitId.
@Override
public String getRepoLastCommitId(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, SANDBOX);
ObjectId commitId = repo.resolve(HEAD);
if (commitId != null) {
toReturn = commitId.getName();
}
}
}
} catch (IOException | CryptoException e) {
logger.error("Error getting last commit ID for site " + site, e);
}
return toReturn;
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method getOperationsFromDelta.
@Override
public List<RepoOperation> getOperationsFromDelta(String site, String commitIdFrom, String commitIdTo) {
List<RepoOperation> operations = new ArrayList<>();
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repository = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repository != null) {
try {
// Get the sandbox repo, and then get a reference to the commitId we received and another for head
boolean fromEmptyRepo = StringUtils.isEmpty(commitIdFrom);
String firstCommitId = getRepoFirstCommitId(site);
if (fromEmptyRepo) {
commitIdFrom = firstCommitId;
}
Repository repo = helper.getRepository(site, SANDBOX);
ObjectId objCommitIdFrom = repo.resolve(commitIdFrom);
ObjectId objCommitIdTo = repo.resolve(commitIdTo);
if (Objects.nonNull(objCommitIdFrom) && Objects.nonNull(objCommitIdTo)) {
ObjectId objFirstCommitId = repo.resolve(firstCommitId);
try (Git git = new Git(repo)) {
if (fromEmptyRepo) {
try (RevWalk walk = new RevWalk(repo)) {
RevCommit firstCommit = walk.parseCommit(objFirstCommitId);
try (ObjectReader reader = repo.newObjectReader()) {
CanonicalTreeParser firstCommitTreeParser = new CanonicalTreeParser();
// reset(reader, firstCommitTree.getId());
firstCommitTreeParser.reset();
// Diff the two commit Ids
long startDiffMark1 = logger.isDebugEnabled() ? System.currentTimeMillis() : 0;
List<DiffEntry> diffEntries = git.diff().setOldTree(firstCommitTreeParser).setNewTree(null).call();
if (logger.isDebugEnabled()) {
logger.debug("Diff from " + objFirstCommitId.getName() + " to null " + "finished in " + ((System.currentTimeMillis() - startDiffMark1) / 1000) + " seconds");
logger.debug("Number of diff entries " + diffEntries.size());
}
// Now that we have a diff, let's itemize the file changes, pack them into a TO
// and add them to the list of RepoOperations to return to the caller
// also include date/time of commit by taking number of seconds and multiply by 1000 and
// convert to java date before sending over
operations.addAll(processDiffEntry(git, diffEntries, firstCommit.getId()));
}
}
}
// let's do it
if (!objCommitIdFrom.equals(objCommitIdTo)) {
// Compare HEAD with commitId we're given
// Get list of commits between commitId and HEAD in chronological order
RevTree fromTree = helper.getTreeForCommit(repo, objCommitIdFrom.getName());
RevTree toTree = helper.getTreeForCommit(repo, objCommitIdTo.getName());
if (fromTree != null && toTree != null) {
try (ObjectReader reader = repo.newObjectReader()) {
CanonicalTreeParser fromCommitTreeParser = new CanonicalTreeParser();
CanonicalTreeParser toCommitTreeParser = new CanonicalTreeParser();
fromCommitTreeParser.reset(reader, fromTree.getId());
toCommitTreeParser.reset(reader, toTree.getId());
// Diff the two commit Ids
long startDiffMark2 = logger.isDebugEnabled() ? System.currentTimeMillis() : 0;
List<DiffEntry> diffEntries = git.diff().setOldTree(fromCommitTreeParser).setNewTree(toCommitTreeParser).call();
if (logger.isDebugEnabled()) {
logger.debug("Diff from " + objCommitIdFrom.getName() + " to " + objCommitIdTo.getName() + " finished in " + ((System.currentTimeMillis() - startDiffMark2) / 1000) + " seconds");
logger.debug("Number of diff entries " + diffEntries.size());
}
// Now that we have a diff, let's itemize the file changes, pack them into a TO
// and add them to the list of RepoOperations to return to the caller
// also include date/time of commit by taking number of seconds and multiply by 1000 and
// convert to java date before sending over
operations.addAll(processDiffEntry(git, diffEntries, objCommitIdTo));
}
}
}
} catch (GitAPIException e) {
logger.error("Error getting operations for site " + site + " from commit ID: " + commitIdFrom + " to commit ID: " + commitIdTo, e);
}
}
} catch (IOException e) {
logger.error("Error getting operations for site " + site + " from commit ID: " + commitIdFrom + " to commit ID: " + commitIdTo, e);
}
}
} catch (CryptoException e) {
logger.error("Error getting operations for site " + site + " from commit ID: " + commitIdFrom + " to commit ID: " + commitIdTo, e);
}
return operations;
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method getPublishingHistory.
@Override
public List<PublishingHistoryItem> getPublishingHistory(String siteId, String environment, String pathRegex, String publisher, ZonedDateTime fromDate, ZonedDateTime toDate, int limit) {
List<PublishingHistoryItem> toRet = new ArrayList<PublishingHistoryItem>();
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository publishedRepo = helper.getRepository(siteId, PUBLISHED);
if (publishedRepo != null) {
int counter = 0;
try (Git git = new Git(publishedRepo)) {
// List all environments
List<Ref> environments = git.branchList().call();
for (int i = 0; i < environments.size() && counter < limit; i++) {
Ref env = environments.get(i);
String environmentGit = env.getName();
environmentGit = environmentGit.replace(R_HEADS, "");
if ((StringUtils.isBlank(environment) && !StringUtils.equals(MASTER, environmentGit)) || StringUtils.equals(environment, environmentGit)) {
List<RevFilter> filters = new ArrayList<RevFilter>();
if (fromDate != null) {
filters.add(CommitTimeRevFilter.after(fromDate.toInstant().toEpochMilli()));
}
if (toDate != null) {
filters.add(CommitTimeRevFilter.before(toDate.toInstant().toEpochMilli()));
} else {
filters.add(CommitTimeRevFilter.before(ZonedDateTime.now().toInstant().toEpochMilli()));
}
filters.add(NotRevFilter.create(MessageRevFilter.create("Initial commit.")));
if (StringUtils.isNotEmpty(publisher)) {
User user = userServiceInternal.getUserByIdOrUsername(-1, publisher);
filters.add(AuthorRevFilter.create(helper.getAuthorIdent(user).getName()));
}
Iterable<RevCommit> branchLog = git.log().add(env.getObjectId()).setRevFilter(AndRevFilter.create(filters)).call();
Iterator<RevCommit> iterator = branchLog.iterator();
while (iterator.hasNext() && counter < limit) {
RevCommit revCommit = iterator.next();
List<String> files = helper.getFilesInCommit(publishedRepo, revCommit);
for (int j = 0; j < files.size() && counter < limit; j++) {
String file = files.get(j);
Path path = Paths.get(file);
String fileName = path.getFileName().toString();
if (!ArrayUtils.contains(IGNORE_FILES, fileName)) {
boolean addFile = false;
if (StringUtils.isNotEmpty(pathRegex)) {
Pattern pattern = Pattern.compile(pathRegex);
Matcher matcher = pattern.matcher(file);
addFile = matcher.matches();
} else {
addFile = true;
}
if (addFile) {
PublishingHistoryItem phi = new PublishingHistoryItem();
phi.setSiteId(siteId);
phi.setPath(file);
phi.setPublishedDate(Instant.ofEpochSecond(revCommit.getCommitTime()).atZone(UTC));
phi.setPublisher(revCommit.getAuthorIdent().getName());
phi.setEnvironment(environmentGit.replace(R_HEADS, ""));
toRet.add(phi);
counter++;
}
}
}
}
}
}
git.close();
toRet.sort((o1, o2) -> o2.getPublishedDate().compareTo(o1.getPublishedDate()));
} catch (IOException | GitAPIException | UserNotFoundException | ServiceLayerException e1) {
logger.error("Error while getting deployment history for site " + siteId, e1);
}
}
} catch (CryptoException e) {
e.printStackTrace();
}
return toRet;
}
Aggregations