use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class GitClient method getHeadCommit.
/**
* Return the head commit of a repository
*
* @param gitRepository {@link GitRepository} to retrieve head commit from
* @return the head commit
* @throws BusinessException if we cannot read the repositories branches
*/
public RevCommit getHeadCommit(GitRepository gitRepository) throws BusinessException {
MeveoUser user = currentUser.get();
if (!GitHelper.hasReadRole(user, gitRepository)) {
throw new UserNotAuthorizedException(user.getUserName());
}
final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
keyLock.lock(gitRepository.getCode());
try (Git git = Git.open(repositoryDir)) {
try (RevWalk rw = new RevWalk(git.getRepository())) {
ObjectId head = git.getRepository().resolve(Constants.HEAD);
return rw.parseCommit(head);
}
} catch (IOException e) {
throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
} finally {
keyLock.unlock(gitRepository.getCode());
}
}
use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class GitClient method reset.
/**
* Reset a repository to a given commit
*
* @param gitRepository {@link GitRepository} to reset
* @param commit Commit to reset onto
*/
public void reset(GitRepository gitRepository, RevCommit commit) throws BusinessException {
MeveoUser user = currentUser.get();
if (!GitHelper.hasWriteRole(user, gitRepository)) {
throw new UserNotAuthorizedException(user.getUserName());
}
final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
keyLock.lock(gitRepository.getCode());
try (Git git = Git.open(repositoryDir)) {
git.reset().setMode(ResetCommand.ResetType.HARD).setRef(commit.getId().getName()).call();
} catch (IOException e) {
throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
} catch (GitAPIException e) {
throw new BusinessException("Cannot reset repository to commit " + commit.getId().getName(), e);
} finally {
keyLock.unlock(gitRepository.getCode());
}
}
use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class GitClient method currentBranch.
/**
* Retrieve the name of the current branch of the repository
*
* @param gitRepository Repository to get branch name from
* @return current branch name
* @throws BusinessException if repository cannot be opened
* @throws UserNotAuthorizedException if user does not have read access to the repository
*/
public String currentBranch(GitRepository gitRepository) throws BusinessException {
MeveoUser user = currentUser.get();
if (!GitHelper.hasReadRole(user, gitRepository)) {
throw new UserNotAuthorizedException(user.getUserName());
}
final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
keyLock.lock(gitRepository.getCode());
try (Git git = Git.open(repositoryDir)) {
return git.getRepository().getBranch();
} catch (IOException e) {
throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
} finally {
keyLock.unlock(gitRepository.getCode());
}
}
use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class GitClient method merge.
/**
* Merge a branch into an other
*
* @param gitRepository GitRepository where to merge branch
* @param from Branch to get changes
* @param to Branch to be updated
* @return <code>true</code> if the merge has no conflict.
* @throws BusinessException if problem happens during merge
* @throws UserNotAuthorizedException if user does not have write access to the repository
*/
public boolean merge(GitRepository gitRepository, String from, String to) throws BusinessException {
MeveoUser user = currentUser.get();
if (!GitHelper.hasWriteRole(user, gitRepository)) {
throw new UserNotAuthorizedException(user.getUserName());
}
final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
keyLock.lock(gitRepository.getCode());
try (Git git = Git.open(repositoryDir)) {
String previousBranch = git.getRepository().getBranch();
git.checkout().setCreateBranch(false).setName(to).call();
try {
boolean successful = git.rebase().setUpstream(from).call().getStatus().isSuccessful();
if (!successful) {
git.rebase().setOperation(RebaseCommand.Operation.ABORT).call();
}
return successful;
} catch (JGitInternalException e) {
log.warn("Cannot merge {} into {}", from, to, e);
git.rebase().setOperation(RebaseCommand.Operation.ABORT).call();
return false;
} catch (GitAPIException e) {
throw new BusinessException("Cannot merge " + from + " into " + to, e);
} finally {
git.checkout().setCreateBranch(false).setName(previousBranch).call();
}
} catch (IOException e) {
throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
} catch (GitAPIException e) {
throw new BusinessException("Checkout problem for repository " + gitRepository.getCode(), e);
} finally {
keyLock.unlock(gitRepository.getCode());
}
}
use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.
the class EntityCustomActionService method addFilesToModule.
@Override
public void addFilesToModule(EntityCustomAction entity, MeveoModule module) throws BusinessException {
BaseEntityDto businessEntityDto = businessEntitySerializer.serialize(entity);
String businessEntityDtoSerialize = JacksonUtil.toString(businessEntityDto);
File gitDirectory = GitHelper.getRepositoryDir(currentUser, module.getCode());
String cetCode = CustomEntityTemplate.getCodeFromAppliesTo(entity.getAppliesTo());
if (cetCode == null) {
cetCode = CustomRelationshipTemplate.getCodeFromAppliesTo(entity.getAppliesTo());
}
String path = entity.getClass().getAnnotation(ModuleItem.class).path() + "/" + cetCode;
File newDir = new File(gitDirectory, path);
newDir.mkdirs();
File newJsonFile = new File(newDir, entity.getCode() + ".json");
try {
MeveoFileUtils.writeAndPreserveCharset(businessEntityDtoSerialize, newJsonFile);
} catch (IOException e) {
throw new BusinessException("File cannot be updated or created", e);
}
GitRepository gitRepository = gitRepositoryService.findByCode(module.getCode());
gitClient.commitFiles(gitRepository, Collections.singletonList(newDir), "Add JSON file for custom action " + cetCode + "." + entity.getCode());
}
Aggregations