use of com.orion.vcs.git.Gits in project orion-ops by lijiahangmax.
the class ApplicationVcsServiceImpl method initEventVcs.
@Override
public void initEventVcs(Long id, boolean isReInit) {
// 查询数据
ApplicationVcsDO vcs = applicationVcsDAO.selectById(id);
Valid.notNull(vcs, MessageConst.UNKNOWN_DATA);
// 判断状态
if (VcsStatus.INITIALIZING.getStatus().equals(vcs.getVcsStatus())) {
throw Exceptions.runtime(MessageConst.VCS_INITIALIZING);
} else if (VcsStatus.OK.getStatus().equals(vcs.getVcsStatus()) && !isReInit) {
throw Exceptions.runtime(MessageConst.VCS_INITIALIZED);
} else if (!VcsStatus.OK.getStatus().equals(vcs.getVcsStatus()) && isReInit) {
throw Exceptions.runtime(MessageConst.VCS_UNINITIALIZED);
}
// 修改状态
ApplicationVcsDO update = new ApplicationVcsDO();
update.setId(id);
update.setVcsStatus(VcsStatus.INITIALIZING.getStatus());
applicationVcsDAO.updateById(update);
// 删除
File clonePath = new File(Utils.getVcsEventDir(id));
Files1.delete(clonePath);
// 初始化
Exception ex = null;
Gits gits = null;
try {
// clone
String[] pair = this.getVcsUsernamePassword(vcs);
String username = pair[0];
String password = pair[1];
if (username == null) {
gits = Gits.clone(vcs.getVscUrl(), clonePath);
} else {
gits = Gits.clone(vcs.getVscUrl(), clonePath, username, password);
}
} catch (Exception e) {
ex = e;
} finally {
Streams.close(gits);
}
// 修改状态
if (ex == null) {
update.setVcsStatus(VcsStatus.OK.getStatus());
} else {
Files1.delete(clonePath);
update.setVcsStatus(VcsStatus.ERROR.getStatus());
}
applicationVcsDAO.updateById(update);
// 设置日志参数
EventParamsHolder.addParam(EventKeys.ID, id);
EventParamsHolder.addParam(EventKeys.NAME, vcs.getVcsName());
if (ex != null) {
throw Exceptions.argument(MessageConst.VCS_INIT_ERROR, ex);
}
}
use of com.orion.vcs.git.Gits in project orion-ops by lijiahangmax.
the class ApplicationVcsServiceImpl method getVcsBranchList.
@Override
public List<ApplicationVcsBranchVO> getVcsBranchList(Long id) {
// 打开git
try (Gits gits = this.openEventGit(id)) {
gits.fetch();
// 查询分支信息
List<BranchInfo> branchList = gits.branchList();
return Converts.toList(branchList, ApplicationVcsBranchVO.class);
}
}
use of com.orion.vcs.git.Gits in project orion-ops by lijiahangmax.
the class ApplicationVcsServiceImpl method getVcsCommitList.
@Override
public List<ApplicationVcsCommitVO> getVcsCommitList(Long id, String branchName) {
// 打开git
try (Gits gits = this.openEventGit(id)) {
gits.fetch(branchName.split("/")[0]);
// 查询提交信息
List<LogInfo> logList = gits.logList(branchName, Const.VCS_COMMIT_LIMIT);
return Converts.toList(logList, ApplicationVcsCommitVO.class);
} catch (Exception e) {
log.error("获取vcs-commit列表失败: id: {}, branch: {}", id, branchName, e);
throw e;
}
}
use of com.orion.vcs.git.Gits in project orion-ops by lijiahangmax.
the class ApplicationVcsServiceImpl method openEventGit.
@Override
public Gits openEventGit(Long id) {
// 查询数据
ApplicationVcsDO vcs = applicationVcsDAO.selectById(id);
Valid.notNull(vcs, MessageConst.UNKNOWN_DATA);
Valid.isTrue(VcsStatus.OK.getStatus().equals(vcs.getVcsStatus()), MessageConst.VCS_UNINITIALIZED);
// 获取仓库位置
File vcsPath = new File(Utils.getVcsEventDir(id));
if (!vcsPath.isDirectory()) {
// 修改状态为未初始化
ApplicationVcsDO entity = new ApplicationVcsDO();
entity.setId(id);
entity.setVcsStatus(VcsStatus.UNINITIALIZED.getStatus());
applicationVcsDAO.updateById(entity);
throw Exceptions.argument(MessageConst.VCS_PATH_ABSENT, Exceptions.runtime(vcsPath.getAbsolutePath()));
}
// 打开git
try {
Gits gits = Gits.of(vcsPath);
String[] pair = this.getVcsUsernamePassword(vcs);
String username = pair[0];
String password = pair[1];
if (username != null) {
gits.auth(username, password);
}
return gits;
} catch (Exception e) {
throw Exceptions.runtime(MessageConst.VCS_UNABLE_CONNECT, e);
}
}
use of com.orion.vcs.git.Gits in project orion-ops by lijiahangmax.
the class ApplicationVcsServiceImpl method getVcsInfo.
@Override
public ApplicationVcsInfoVO getVcsInfo(ApplicationVcsRequest request) {
Long id = request.getId();
// 打开git
try (Gits gits = this.openEventGit(id)) {
gits.fetch();
ApplicationVcsInfoVO vcsInfo = new ApplicationVcsInfoVO();
ApplicationVcsBranchVO defaultBranch;
// 获取分支列表
List<ApplicationVcsBranchVO> branches = Converts.toList(gits.branchList(), ApplicationVcsBranchVO.class);
// 获取当前环境上次构建分支
String lastBranchName = applicationBuildDAO.selectLastBuildBranch(request.getAppId(), request.getProfileId(), id);
if (lastBranchName != null) {
defaultBranch = branches.stream().filter(s -> s.getName().equals(lastBranchName)).findFirst().orElseGet(() -> branches.get(branches.size() - 1));
} else {
defaultBranch = branches.get(branches.size() - 1);
}
defaultBranch.setIsDefault(Const.IS_DEFAULT);
vcsInfo.setBranches(branches);
// 获取commit
try {
List<LogInfo> logList = gits.logList(defaultBranch.getName(), Const.VCS_COMMIT_LIMIT);
vcsInfo.setCommits(Converts.toList(logList, ApplicationVcsCommitVO.class));
} catch (Exception e) {
log.error("获取vcs-commit列表失败: id: {}, branch: {}, e: {}", id, defaultBranch.getName(), e);
throw e;
}
return vcsInfo;
}
}
Aggregations