Search in sources :

Example 1 with Gits

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);
    }
}
Also used : Gits(com.orion.vcs.git.Gits) ApplicationVcsDO(com.orion.ops.entity.domain.ApplicationVcsDO) File(java.io.File)

Example 2 with Gits

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);
    }
}
Also used : Gits(com.orion.vcs.git.Gits) BranchInfo(com.orion.vcs.git.info.BranchInfo)

Example 3 with Gits

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;
    }
}
Also used : Gits(com.orion.vcs.git.Gits) LogInfo(com.orion.vcs.git.info.LogInfo)

Example 4 with Gits

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);
    }
}
Also used : Gits(com.orion.vcs.git.Gits) ApplicationVcsDO(com.orion.ops.entity.domain.ApplicationVcsDO) File(java.io.File)

Example 5 with Gits

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;
    }
}
Also used : Gits(com.orion.vcs.git.Gits) EventKeys(com.orion.ops.consts.event.EventKeys) LogInfo(com.orion.vcs.git.info.LogInfo) Exceptions(com.orion.utils.Exceptions) java.util(java.util) ApplicationVcsCommitVO(com.orion.ops.entity.vo.ApplicationVcsCommitVO) ValueMix(com.orion.ops.utils.ValueMix) ApplicationVcsRequest(com.orion.ops.entity.request.ApplicationVcsRequest) ApplicationInfoDAO(com.orion.ops.dao.ApplicationInfoDAO) MessageConst(com.orion.ops.consts.MessageConst) ApplicationVcsBranchVO(com.orion.ops.entity.vo.ApplicationVcsBranchVO) EventParamsHolder(com.orion.ops.consts.event.EventParamsHolder) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) ApplicationVcsDAO(com.orion.ops.dao.ApplicationVcsDAO) VcsStatus(com.orion.ops.consts.app.VcsStatus) ApplicationBuildDAO(com.orion.ops.dao.ApplicationBuildDAO) ApplicationVcsInfoVO(com.orion.ops.entity.vo.ApplicationVcsInfoVO) Streams(com.orion.utils.io.Streams) Service(org.springframework.stereotype.Service) Arrays1(com.orion.utils.Arrays1) VcsAuthType(com.orion.ops.consts.app.VcsAuthType) Utils(com.orion.ops.utils.Utils) Const(com.orion.ops.consts.Const) DataGrid(com.orion.lang.wrapper.DataGrid) ApplicationVcsDO(com.orion.ops.entity.domain.ApplicationVcsDO) ApplicationVcsVO(com.orion.ops.entity.vo.ApplicationVcsVO) Resource(javax.annotation.Resource) BranchInfo(com.orion.vcs.git.info.BranchInfo) Valid(com.orion.ops.utils.Valid) File(java.io.File) Files1(com.orion.utils.io.Files1) DataQuery(com.orion.ops.utils.DataQuery) Converts(com.orion.utils.convert.Converts) Slf4j(lombok.extern.slf4j.Slf4j) VcsTokenType(com.orion.ops.consts.app.VcsTokenType) SystemEnvAttr(com.orion.ops.consts.system.SystemEnvAttr) ApplicationVcsService(com.orion.ops.service.api.ApplicationVcsService) VcsType(com.orion.ops.consts.app.VcsType) Strings(com.orion.utils.Strings) Gits(com.orion.vcs.git.Gits) Transactional(org.springframework.transaction.annotation.Transactional) ApplicationVcsBranchVO(com.orion.ops.entity.vo.ApplicationVcsBranchVO) LogInfo(com.orion.vcs.git.info.LogInfo) ApplicationVcsInfoVO(com.orion.ops.entity.vo.ApplicationVcsInfoVO) ApplicationVcsCommitVO(com.orion.ops.entity.vo.ApplicationVcsCommitVO)

Aggregations

Gits (com.orion.vcs.git.Gits)5 ApplicationVcsDO (com.orion.ops.entity.domain.ApplicationVcsDO)3 File (java.io.File)3 BranchInfo (com.orion.vcs.git.info.BranchInfo)2 LogInfo (com.orion.vcs.git.info.LogInfo)2 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 DataGrid (com.orion.lang.wrapper.DataGrid)1 Const (com.orion.ops.consts.Const)1 MessageConst (com.orion.ops.consts.MessageConst)1 VcsAuthType (com.orion.ops.consts.app.VcsAuthType)1 VcsStatus (com.orion.ops.consts.app.VcsStatus)1 VcsTokenType (com.orion.ops.consts.app.VcsTokenType)1 VcsType (com.orion.ops.consts.app.VcsType)1 EventKeys (com.orion.ops.consts.event.EventKeys)1 EventParamsHolder (com.orion.ops.consts.event.EventParamsHolder)1 SystemEnvAttr (com.orion.ops.consts.system.SystemEnvAttr)1 ApplicationBuildDAO (com.orion.ops.dao.ApplicationBuildDAO)1 ApplicationInfoDAO (com.orion.ops.dao.ApplicationInfoDAO)1 ApplicationVcsDAO (com.orion.ops.dao.ApplicationVcsDAO)1 ApplicationVcsRequest (com.orion.ops.entity.request.ApplicationVcsRequest)1