Compare commits

...

1 Commits
master ... ww

Author SHA1 Message Date
weishao 25b3f0d113 1 2022-12-24 20:19:39 +08:00
6 changed files with 50 additions and 8 deletions

View File

@ -28,6 +28,7 @@ public enum ErrorCodeEnum {
AFTERMATH_EXP("000013","评测线程出错,善后处理发生异常"),
GIT_FAIL("000014","获取git凭证失败: "),
GIT_CREDENTIAL_FAIL("000015","设置git凭证失败:"),
GIT_CREDENTIAL_INVALID("000016","git用户名或密码错误"),
;
String value;

View File

@ -14,23 +14,40 @@ import org.apache.commons.lang3.StringUtils;
public class GitUtil {
// /**
// * 克隆代码
// *
// * @param fullWorkspace
// * @return
// */
// public static boolean gitClone(String gitUrl, String branch, String username, String password, String fullWorkspace) {
// // 拉代码
// // TODO 暂时为了测试
// int index = gitUrl.replaceAll("localhost:43001", "118.31.13.117:64300").lastIndexOf("//");
//// int index = gitUrl.lastIndexOf("//");
// String command = StringUtils.join("cd ", fullWorkspace, " && git clone -b ", branch, " ", gitUrl.substring(0, index + 2), username, ":",
// password, "@", gitUrl.substring(index + 2));
//
// ShellResult shellResult = ShellUtil.executeAndGetExitStatus(command);
// log.info("command:{},git克隆代码返回:{}", command, JSONObject.toJSONString(shellResult));
// return shellResult.getExitStatus() == 0;
// }
/**
* 克隆代码
*
* @param fullWorkspace
* @return
*/
public static boolean gitClone(String gitUrl, String branch, String username, String password, String fullWorkspace) {
public static ShellResult gitClone(String gitUrl, String branch, String username, String password, String fullWorkspace) {
// 拉代码
// TODO 暂时为了测试
int index = gitUrl.replaceAll("localhost:43001", "118.31.13.117:64300").lastIndexOf("//");
// int index = gitUrl.lastIndexOf("//");
int index = gitUrl.lastIndexOf("//");
String command = StringUtils.join("cd ", fullWorkspace, " && git clone -b ", branch, " ", gitUrl.substring(0, index + 2), username, ":",
password, "@", gitUrl.substring(index + 2));
ShellResult shellResult = ShellUtil.executeAndGetExitStatus(command);
log.info("command:{},git克隆代码返回:{}", command, JSONObject.toJSONString(shellResult));
return shellResult.getExitStatus() == 0;
log.info("command:{}, git克隆代码返回:{}", command, JSONObject.toJSONString(shellResult));
return shellResult;
}
}

BIN
dump.rdb Normal file

Binary file not shown.

View File

@ -1,11 +1,16 @@
package net.educoder.quality.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.educoder.quality.common.bean.ShellResult;
import net.educoder.quality.common.config.PropertiesConfig;
import net.educoder.quality.common.enums.ErrorCodeEnum;
import net.educoder.quality.common.exception.BusinessException;
import net.educoder.quality.common.util.GitUtil;
import net.educoder.quality.dto.CloneDetectionResultFileCodeLevelDTO;
import net.educoder.quality.dto.CloneDetectionResultProjectLevelDTO;
import net.educoder.quality.dto.CloneDetectionStatisticsDTO;
@ -19,16 +24,21 @@ import net.educoder.quality.service.CloneDetectionService;
import net.educoder.quality.service.ProjectService;
import net.educoder.quality.vo.AddCloneDetectionVO;
import net.educoder.quality.vo.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class CloneDetectionServiceImpl implements CloneDetectionService {
@Autowired
private PropertiesConfig propertiesConfig;
@Autowired
private ProjectService projectService;
@ -45,6 +55,17 @@ public class CloneDetectionServiceImpl implements CloneDetectionService {
throw new BusinessException(ErrorCodeEnum.PROJECT_NOT_EXISTS);
}
String fullPath = propertiesConfig.getWorkspace() + "/" + DateUtil.formatDate(new Date()) + "/" + RandomUtil.randomString(10);
File file = new File(fullPath);
if (!file.exists()) {
file.mkdirs();
}
ShellResult cloneResult = GitUtil.gitClone(addCloneDetectionVO.getTargetRepoURL(), addCloneDetectionVO.getTargetRepoBranch(),
addCloneDetectionVO.getTargetRepoUsername(), addCloneDetectionVO.getTargetRepoPassword(), fullPath);
if (StringUtils.containsAnyIgnoreCase(cloneResult.getOut(), "Authentication failed")) {
log.info("projectId:{}克隆代码失败,终止执行", projectId);
throw new BusinessException(ErrorCodeEnum.GIT_CREDENTIAL_INVALID.getValue(), ErrorCodeEnum.GIT_CREDENTIAL_INVALID.getDescription());
}
}
@Override

View File

@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.tools.Shell;
import lombok.extern.slf4j.Slf4j;
import net.educoder.quality.common.bean.ShellResult;
import net.educoder.quality.common.config.PropertiesConfig;
@ -87,8 +88,8 @@ public class ComponentServiceImpl implements ComponentService {
if (!file.exists()) {
file.mkdirs();
}
boolean flag = GitUtil.gitClone(projects.getGitUrl(), projects.getBranch(), propertiesConfig.getGitUsername(), propertiesConfig.getGitPassword(), fullPath);
if (!flag) {
ShellResult cloneResult = GitUtil.gitClone(projects.getGitUrl(), projects.getBranch(), propertiesConfig.getGitUsername(), propertiesConfig.getGitPassword(), fullPath);
if (cloneResult.getExitStatus() != 0) {
log.info("projectId:{}克隆代码失败,终止执行", projectId);
return;
}

View File

@ -9,6 +9,8 @@ public class AddCloneDetectionVO {
@NotBlank(message = "目标仓库不能为空")
private String targetRepoURL;
private String targetRepoBranch;
private String targetRepoUsername;
private String targetRepoPassword;