|
@@ -4,6 +4,7 @@ import nju.seec.SEECdemo.data.dao.assignment.CodeDAO;
|
|
|
import nju.seec.SEECdemo.data.dao.assignment.CodeResultDAO;
|
|
import nju.seec.SEECdemo.data.dao.assignment.CodeResultDAO;
|
|
|
import nju.seec.SEECdemo.data.entity.assignment.Code;
|
|
import nju.seec.SEECdemo.data.entity.assignment.Code;
|
|
|
import nju.seec.SEECdemo.data.entity.assignment.CodeResult;
|
|
import nju.seec.SEECdemo.data.entity.assignment.CodeResult;
|
|
|
|
|
+import nju.seec.SEECdemo.logic.service.ProjectService;
|
|
|
import nju.seec.SEECdemo.logic.service.impl.assignment.mock.MockGroup;
|
|
import nju.seec.SEECdemo.logic.service.impl.assignment.mock.MockGroup;
|
|
|
import nju.seec.SEECdemo.logic.api.GitlabApi;
|
|
import nju.seec.SEECdemo.logic.api.GitlabApi;
|
|
|
import nju.seec.SEECdemo.logic.api.JenkinsApi;
|
|
import nju.seec.SEECdemo.logic.api.JenkinsApi;
|
|
@@ -11,6 +12,8 @@ import nju.seec.SEECdemo.logic.service.impl.assignment.mock.MockQuestion;
|
|
|
import nju.seec.SEECdemo.logic.service.impl.assignment.stub.GroupServiceStub;
|
|
import nju.seec.SEECdemo.logic.service.impl.assignment.stub.GroupServiceStub;
|
|
|
import nju.seec.SEECdemo.logic.service.impl.assignment.stub.QuestionServiceStub;
|
|
import nju.seec.SEECdemo.logic.service.impl.assignment.stub.QuestionServiceStub;
|
|
|
import nju.seec.SEECdemo.logic.storage.StorageProvider;
|
|
import nju.seec.SEECdemo.logic.storage.StorageProvider;
|
|
|
|
|
+import nju.seec.SEECdemo.util.ApplicationProperties;
|
|
|
|
|
+import nju.seec.SEECdemo.util.ToolKit;
|
|
|
import nju.seec.SEECdemo.util.enums.AssignmentStatus;
|
|
import nju.seec.SEECdemo.util.enums.AssignmentStatus;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
|
@@ -24,6 +27,8 @@ import java.io.ByteArrayInputStream;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
|
|
|
+import java.time.ZoneId;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
|
|
|
@Component
|
|
@Component
|
|
|
public class AssignmentPaperBuilder {
|
|
public class AssignmentPaperBuilder {
|
|
@@ -38,10 +43,11 @@ public class AssignmentPaperBuilder {
|
|
|
|
|
|
|
|
private final QuestionServiceStub questionServiceStub;
|
|
private final QuestionServiceStub questionServiceStub;
|
|
|
private final GroupServiceStub groupServiceStub;
|
|
private final GroupServiceStub groupServiceStub;
|
|
|
|
|
+ private final ProjectService projectService;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
public AssignmentPaperBuilder(GitlabApi gitlabApi, GitHelper helper, JenkinsApi jenkinsApi, CodeDAO codeDAO, CodeResultDAO codeResultDAO, StorageProvider storageProvider,
|
|
public AssignmentPaperBuilder(GitlabApi gitlabApi, GitHelper helper, JenkinsApi jenkinsApi, CodeDAO codeDAO, CodeResultDAO codeResultDAO, StorageProvider storageProvider,
|
|
|
- QuestionServiceStub questionServiceStub, GroupServiceStub groupServiceStub){
|
|
|
|
|
|
|
+ QuestionServiceStub questionServiceStub, GroupServiceStub groupServiceStub, ProjectService projectService){
|
|
|
this.gitlabApi = gitlabApi;
|
|
this.gitlabApi = gitlabApi;
|
|
|
this.helper = helper;
|
|
this.helper = helper;
|
|
|
this.jenkinsApi = jenkinsApi;
|
|
this.jenkinsApi = jenkinsApi;
|
|
@@ -50,46 +56,67 @@ public class AssignmentPaperBuilder {
|
|
|
this.storageProvider = storageProvider;
|
|
this.storageProvider = storageProvider;
|
|
|
this.questionServiceStub = questionServiceStub;
|
|
this.questionServiceStub = questionServiceStub;
|
|
|
this.groupServiceStub = groupServiceStub;
|
|
this.groupServiceStub = groupServiceStub;
|
|
|
|
|
+ this.projectService = projectService;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Transactional
|
|
@Transactional
|
|
|
- public void buildCodePaper(int codeId) throws IOException, GitLabApiException, GitAPIException {
|
|
|
|
|
- Code code = codeDAO.findById(codeId);
|
|
|
|
|
- if (code.getStatus() != AssignmentStatus.PREPARING) return;
|
|
|
|
|
|
|
+ public void buildCodePaper(Code code) throws IOException, GitAPIException, GitLabApiException {
|
|
|
File tempDir = Files.createTempDirectory(TEMP_PREFIX).toFile();
|
|
File tempDir = Files.createTempDirectory(TEMP_PREFIX).toFile();
|
|
|
try {
|
|
try {
|
|
|
|
|
+ code.setId(gitlabApi.createProject(code.getUuid()));
|
|
|
releaseQuestion(code.getQuestionId(), tempDir);
|
|
releaseQuestion(code.getQuestionId(), tempDir);
|
|
|
code.setInitHash(helper.push(tempDir, code.getUuid()));
|
|
code.setInitHash(helper.push(tempDir, code.getUuid()));
|
|
|
- // 为群组创建项目,并将组员加入到项目中
|
|
|
|
|
- for(MockGroup group: groupServiceStub.getGroupListByCourse(code.getCourseId())){
|
|
|
|
|
- int projectId = gitlabApi.forkProject(group.getMemberSet().iterator().next(), code.getId(), group.getNamespace());
|
|
|
|
|
- CodeResult codeResult = new CodeResult();
|
|
|
|
|
- codeResult.setId(projectId);
|
|
|
|
|
- codeResult.setGroupId(group.getId());
|
|
|
|
|
- codeResult.setCode(code);
|
|
|
|
|
- // TODO
|
|
|
|
|
- // 创建jenkins上的构建job
|
|
|
|
|
- String buildJob = projectId + "-build";
|
|
|
|
|
- jenkinsApi.createBuildJob(buildJob);
|
|
|
|
|
- codeResult.setBuildJob(buildJob);
|
|
|
|
|
- // 调用kube Api创建该项目的应用空间,并记录其id
|
|
|
|
|
-
|
|
|
|
|
- // 创建jenkins上的测试job
|
|
|
|
|
- String testJob = projectId + "-test";
|
|
|
|
|
- jenkinsApi.createTestJob(testJob);
|
|
|
|
|
- codeResult.setTestJob(testJob);
|
|
|
|
|
- codeResultDAO.save(codeResult);
|
|
|
|
|
- }
|
|
|
|
|
- code.setStatus(AssignmentStatus.AVAILABLE);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
|
|
+ } catch (GitAPIException | GitLabApiException e) {
|
|
|
code.setStatus(AssignmentStatus.UNAVAILABLE);
|
|
code.setStatus(AssignmentStatus.UNAVAILABLE);
|
|
|
throw e;
|
|
throw e;
|
|
|
} finally {
|
|
} finally {
|
|
|
- codeDAO.save(code);
|
|
|
|
|
FileUtils.deleteDirectory(tempDir);
|
|
FileUtils.deleteDirectory(tempDir);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void addGroupsToPaper(int codeId) {
|
|
|
|
|
+ Code code = codeDAO.findById(codeId);
|
|
|
|
|
+ if (code.getStatus() != AssignmentStatus.PREPARING) return;
|
|
|
|
|
+ // 为群组创建项目,并将组员加入到项目中
|
|
|
|
|
+ try {
|
|
|
|
|
+ for(MockGroup group: groupServiceStub.getGroupListByCourse(code.getCourseId())){
|
|
|
|
|
+ addGroupToPaper(code, group);
|
|
|
|
|
+ }
|
|
|
|
|
+ code.setStatus(AssignmentStatus.AVAILABLE);
|
|
|
|
|
+ } catch (IOException | GitLabApiException e) {
|
|
|
|
|
+ System.out.println("将小组加入作业时发生异常" + ToolKit.format(e));
|
|
|
|
|
+ code.setStatus(AssignmentStatus.UNAVAILABLE);
|
|
|
|
|
+ }
|
|
|
|
|
+ codeDAO.save(code);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void addGroupToPaper(Code code, MockGroup group) throws IOException, GitLabApiException {
|
|
|
|
|
+ // fork项目的时候,将项目设置成private的,因为组员设置成了Master权限,而只有组的Owner才能够更项目可见性
|
|
|
|
|
+ // (private、internal、public),所以此处用root用户来fork
|
|
|
|
|
+ int projectId = gitlabApi.forkProject(gitlabApi.getRootUserId(), code.getId(), group.getNamespace());
|
|
|
|
|
+ CodeResult codeResult = new CodeResult();
|
|
|
|
|
+ codeResult.setId(projectId);
|
|
|
|
|
+ codeResult.setGroupId(group.getId());
|
|
|
|
|
+ codeResult.setCode(code);
|
|
|
|
|
+ // TODO
|
|
|
|
|
+ // 创建jenkins上的构建job
|
|
|
|
|
+ String buildJob = projectId + "-build";
|
|
|
|
|
+ jenkinsApi.createBuildJob(buildJob);
|
|
|
|
|
+ codeResult.setBuildJob(buildJob);
|
|
|
|
|
+ // 调用kube Api创建该项目的部署空间,并记录其id
|
|
|
|
|
+// Long projectDeployNameSpace = projectService.createSeecIIProject(group.getName() + code.getUuid(),
|
|
|
|
|
+// code.getDescription(), // 可以是中文吗?
|
|
|
|
|
+// Date.from(code.getStartAt().atZone(ZoneId.systemDefault()).toInstant()),
|
|
|
|
|
+// Date.from(code.getEndAt().atZone(ZoneId.systemDefault()).toInstant()));
|
|
|
|
|
+// codeResult.setDeployNamespace(projectDeployNameSpace);
|
|
|
|
|
+ // 创建jenkins上的测试job
|
|
|
|
|
+ String testJob = projectId + "-test";
|
|
|
|
|
+ jenkinsApi.createTestJob(testJob);
|
|
|
|
|
+ codeResult.setTestJob(testJob);
|
|
|
|
|
+ codeResultDAO.save(codeResult);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private void releaseQuestion(Long questionId, File codeDir) throws IOException {
|
|
private void releaseQuestion(Long questionId, File codeDir) throws IOException {
|
|
|
FileUtils.cleanDirectory(codeDir);
|
|
FileUtils.cleanDirectory(codeDir);
|
|
|
MockQuestion question = questionServiceStub.getQuestion(questionId);
|
|
MockQuestion question = questionServiceStub.getQuestion(questionId);
|