diff --git a/src/main/java/de/hftstuttgart/dtabackend/rest/v1/task/TaskUpload.java b/src/main/java/de/hftstuttgart/dtabackend/rest/v1/task/TaskUpload.java index 344eb0bd7226ab33cd05f41ad38fd5d5b42f30f4..39119f32a8a71f3508cc95aebf224743b053a547 100644 --- a/src/main/java/de/hftstuttgart/dtabackend/rest/v1/task/TaskUpload.java +++ b/src/main/java/de/hftstuttgart/dtabackend/rest/v1/task/TaskUpload.java @@ -18,6 +18,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.regex.Matcher; +import java.util.regex.Pattern; + import jakarta.servlet.annotation.MultipartConfig; /** @@ -63,10 +65,20 @@ public class TaskUpload { case "text/plain": LOG.debug("textfile uploaded, searching for dta config"); // find URI in config file - Matcher config = RegexUtil.findStudentConfig(taskFileRef.getInputStream()); - + String subDir=""; + Matcher config = RegexUtil.extractConfig(taskFileRef.getInputStream(), Pattern.compile(RegexUtil.DTA_SUBMISSIONCONFIGREGEX)); + if(config==null) { + config = RegexUtil.extractConfig(taskFileRef.getInputStream(), Pattern.compile(RegexUtil.SUBMISSIONCONFIGREGEX)); + if(config==null) + { + throw new RuntimeException("couldn't find repo config for student submission clone"); + } + } + else { + subDir=config.group(4); + } LOG.debug("calling repo clone"); - jGitUtil.cloneRepository(config, srcPath.toAbsolutePath().toString()); + jGitUtil.cloneRepository(config, srcPath.toAbsolutePath().toString(), subDir); break; case "application/zip": diff --git a/src/main/java/de/hftstuttgart/dtabackend/rest/v1/unittest/UnitTestUpload.java b/src/main/java/de/hftstuttgart/dtabackend/rest/v1/unittest/UnitTestUpload.java index cda616077fd25654e80694876f59dc3e03b968e4..a894655ae1ec798c9b00feea764bb25b58562ce3 100644 --- a/src/main/java/de/hftstuttgart/dtabackend/rest/v1/unittest/UnitTestUpload.java +++ b/src/main/java/de/hftstuttgart/dtabackend/rest/v1/unittest/UnitTestUpload.java @@ -1,6 +1,8 @@ package de.hftstuttgart.dtabackend.rest.v1.unittest; import de.hftstuttgart.dtabackend.utils.JGitUtil; +import de.hftstuttgart.dtabackend.utils.RegexUtil; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.core.env.Environment; @@ -28,9 +30,6 @@ import java.util.regex.Pattern; public class UnitTestUpload { private static final Logger LOG = LogManager.getLogger(UnitTestUpload.class); - public final static String TESTCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)::(.*)$"; - public final static String SUBMISSIONCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)$"; - private final JGitUtil jGitUtil; private final String assignmentBasePath; @@ -55,48 +54,31 @@ public class UnitTestUpload { ) throws IOException { LOG.info("received new assignment"); - File file = Paths.get( - this.assignmentBasePath, - assignmentId + ".txt") - .toFile(); + File file = Paths.get(this.assignmentBasePath, assignmentId + ".txt").toFile(); file.mkdirs(); // save assignment config unitTestFileRef.transferTo(file); LOG.debug(String.format("saved config file to: %s", file.getAbsolutePath())); - Pattern pattern = Pattern.compile(TESTCONFIGREGEX); - Matcher config = null; - - LOG.debug("reading test configuration file"); - // open saved config in a try-with - try (BufferedReader br = new BufferedReader( - new InputStreamReader( - new FileInputStream(file)))) { - String line; - - // search for a URI while none is found and there are lines left - while (config == null && (line = br.readLine()) != null) { - Matcher matcher = pattern.matcher(line); - if (matcher.matches()) { - LOG.debug(String.format("found dta test line: %s", line)); - config = matcher; - } - } - } catch (IOException e) { - LOG.error("Error while reading repo config", e); + String subDir=""; + Pattern pattern = Pattern.compile(RegexUtil.DTA_TESTCONFIGREGEX); + + Matcher config = RegexUtil.extractConfig(new FileInputStream(file), pattern); + if (config == null) { + pattern=Pattern.compile(RegexUtil.TESTCONFIGREGEX); + config = RegexUtil.extractConfig(new FileInputStream(file), pattern); + if(config==null) + { + throw new RuntimeException("couldn't find repo config for unittest clone"); + } } - finally { - if (config == null) { - throw new RuntimeException("couldn't find repo config for unittest clone"); - } + else { + subDir=config.group(4); } - LOG.debug("calling test repo clone"); // cloning assignment repo to persistent space - jGitUtil.cloneRepository( - config, - Paths.get(this.assignmentBasePath, assignmentId).toAbsolutePath().toString()); + jGitUtil.cloneRepository(config, Paths.get(this.assignmentBasePath, assignmentId).toAbsolutePath().toString(), subDir); LOG.info(String.format("stored new assignment: %s", file.getAbsolutePath())); } diff --git a/src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java b/src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java index 8318f734d3ff2662588c1ecf059396a48fcd3494..17fd1b138e32b64fe1ba2fdc5d0b6ff619283efa 100644 --- a/src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java +++ b/src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java @@ -20,6 +20,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.regex.Matcher; +import java.util.regex.Pattern; @Component public class ExecuteTestUtil { @@ -60,31 +61,34 @@ public class ExecuteTestUtil { // clone stored test to tmpdir LOG.debug("copying pre-downloaded unitttest repo"); FileUtil.copyFolder( - Paths.get( - assignmentBasePath, - assignmentId - ), - testPath - ); + Paths.get(assignmentBasePath, assignmentId), + testPath); LOG.debug("copy test config"); Files.copy( - Paths.get( - assignmentBasePath, - assignmentId + ".txt" - ), - Paths.get( - workDirectory.toAbsolutePath().toString(), - "config.txt" - ) - ); + Paths.get(assignmentBasePath, assignmentId + ".txt"), + Paths.get(workDirectory.toAbsolutePath().toString(), "config.txt")); Files.createDirectory(resultPath); LOG.info("reading test config"); - Matcher config = RegexUtil.findProfessorConfig( - new FileInputStream(Paths.get(workDirectory.toAbsolutePath().toString(), "config.txt").toFile())); - + Matcher config = RegexUtil.extractConfig( + new FileInputStream(Paths.get(workDirectory.toAbsolutePath().toString(), "config.txt").toFile()), Pattern.compile(RegexUtil.DTA_TESTCONFIGREGEX)); + String image=""; + if(config==null) + { + config = RegexUtil.extractConfig( + new FileInputStream(Paths.get(workDirectory.toAbsolutePath().toString(), "config.txt").toFile()), Pattern.compile(RegexUtil.TESTCONFIGREGEX)); + if(config==null) + { + throw new RuntimeException("couldn't find repo config for unittest image extraction"); + } + image=config.group(4); + } + else + { + image=config.group(5); + } // define the paths to mount as Binds from Host to the test-container Path testPathHost = Paths.get( testTmpPathHost.toAbsolutePath().toString(), @@ -104,7 +108,7 @@ public class ExecuteTestUtil { // start test-container with professor given image and bind mounts for test, submission and result dockerUtil.runContainer( - config.group(4), + image, new Bind(testPathHost.toAbsolutePath().toString(), new Volume("/data/test")), new Bind(srcPathHost.toAbsolutePath().toString(), new Volume("/data/src")), new Bind(resultPathHost.toAbsolutePath().toString(), new Volume("/data/result")) diff --git a/src/main/java/de/hftstuttgart/dtabackend/utils/JGitUtil.java b/src/main/java/de/hftstuttgart/dtabackend/utils/JGitUtil.java index 140772185983a47e1a13fc90ae94c3822bb6905d..b229c303b7efe11ef647f44956080ef9fea3db06 100644 --- a/src/main/java/de/hftstuttgart/dtabackend/utils/JGitUtil.java +++ b/src/main/java/de/hftstuttgart/dtabackend/utils/JGitUtil.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Component; import org.springframework.util.FileSystemUtils; import java.io.File; +import java.io.IOException; import java.util.regex.Matcher; @Component @@ -19,7 +20,7 @@ public class JGitUtil { public JGitUtil() {} - public void cloneRepository(Matcher config, String targetPath) { + public void cloneRepository(Matcher config, String targetPath, String subDir) { LOG.debug(String.format("cloning repository: %s", config.group(1))); File targetDirectory = new File(targetPath); @@ -28,46 +29,44 @@ public class JGitUtil { FileSystemUtils.deleteRecursively(targetDirectory); } - //create companion checkout dir "targetPath"+"_checkout" - File checkoutDirectory = new File(targetPath+"_checkout"); - if (targetDirectory.exists()) { - LOG.debug("clone checkout directory existing yet, deleting now"); - FileSystemUtils.deleteRecursively(checkoutDirectory); + File checkoutDirectory = targetDirectory; + //if an optional directory parameter was given + if(subDir!="") + { + //create companion checkout dir "targetPath"+"_checkout" + checkoutDirectory = new File(targetPath+"_checkout"); + if (targetDirectory.exists()) { + LOG.debug("clone checkout directory existing yet, deleting now"); + FileSystemUtils.deleteRecursively(checkoutDirectory); + } } - + try { - //check group(1) for possible directory - //if(!config.group(1).endsWith(".git")) - //cut off the directory part - //pos=instr(".git/") - //cloneURI=config.group(1).substr(1, pos+3) - //else - //cloneURI=config.group(1) LOG.debug("preparing clone"); CloneCommand cloneCommand = Git.cloneRepository() .setDirectory(checkoutDirectory) - //.setURI(cloneURI) .setURI(config.group(1)); if (!config.group(2).equals("none") && !config.group(3).equals("none")) { LOG.debug("setting credentials"); - cloneCommand.setCredentialsProvider( - new UsernamePasswordCredentialsProvider(config.group(2), config.group(3))); + cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(config.group(2), config.group(3))); } LOG.debug("cloning..."); - cloneCommand.call() - .close(); - - //copy appropriate path from checkout directory to target directory - //if(!config.group(1).endsWith(".git")) - //copy checkout+config.group(1).substr(pos+4) to target directory - //else - //copy checkout directory to target directory directly + cloneCommand.call().close(); + //if an optional directory parameter was given + if(subDir!="") + { + //copy appropriate path from checkout directory to target directory + FileSystemUtils.copyRecursively(targetDirectory, new File(checkoutDirectory+subDir)); + } + } + catch (IOException e) { + LOG.error(String.format("Error while cloning from %s: could not copy to unit test dir", config.group(1)), e); } catch (GitAPIException e) { - LOG.error(String.format("Error while cloning from %s", config.group(1)), e); + LOG.error(String.format("Error while cloning from %s: could not read from Git", config.group(1)), e); } LOG.debug(String.format("cloned from %s to %s", config.group(1), targetDirectory)); diff --git a/src/main/java/de/hftstuttgart/dtabackend/utils/RegexUtil.java b/src/main/java/de/hftstuttgart/dtabackend/utils/RegexUtil.java index 718442717f10235aa3139800f9f2540a9fb3e119..55969895275172c89f3e3d22a05099c5229007d9 100644 --- a/src/main/java/de/hftstuttgart/dtabackend/utils/RegexUtil.java +++ b/src/main/java/de/hftstuttgart/dtabackend/utils/RegexUtil.java @@ -3,9 +3,9 @@ package de.hftstuttgart.dtabackend.utils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import de.hftstuttgart.dtabackend.rest.v1.unittest.UnitTestUpload; - import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -14,6 +14,11 @@ import java.util.regex.Pattern; public class RegexUtil { + public final static String DTA_TESTCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)::(.*)::(.*)$"; + public final static String DTA_SUBMISSIONCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)::(.*)$"; + public final static String TESTCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)::(.*)$"; + public final static String SUBMISSIONCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)$"; + public enum ConfigType { TEACHER, STUDENT, @@ -33,11 +38,11 @@ public class RegexUtil { Pattern pattern; switch (configType) { case TEACHER: - pattern = Pattern.compile(UnitTestUpload.TESTCONFIGREGEX); + pattern = Pattern.compile(RegexUtil.TESTCONFIGREGEX); break; case STUDENT: - pattern = Pattern.compile(UnitTestUpload.SUBMISSIONCONFIGREGEX); + pattern = Pattern.compile(RegexUtil.SUBMISSIONCONFIGREGEX); break; default: @@ -46,33 +51,31 @@ public class RegexUtil { throw new RuntimeException(msg); } - Matcher config = null; + Matcher config = extractConfig(is, pattern); - LOG.debug("reading config file"); - // open received file in a try-with - try (BufferedReader br = new BufferedReader( - new InputStreamReader( - is))) { + return config; + } + + public static Matcher extractConfig(InputStream configFileStream, Pattern pattern) { + LOG.debug("reading configuration file"); + Matcher configItems=null; + // open saved config in a try-with + try (BufferedReader br = new BufferedReader(new InputStreamReader(configFileStream))) { String line; - // as long as we haven't found a configuration and have lines left, search - while (config == null && (line = br.readLine()) != null) { + // search for a URI while none is found and there are lines left + while (configItems == null && (line = br.readLine()) != null) { Matcher matcher = pattern.matcher(line); if (matcher.matches()) { - LOG.debug(String.format("found dta line: %s", line)); - config = matcher; + LOG.debug(String.format("found valid config line: %s", line)); + configItems = matcher; } } } catch (IOException e) { LOG.error("Error while reading repo config", e); } finally { - if (config == null) { - throw new RuntimeException("couldn't find repo config for clone"); - } } - - return config; - } - + return configItems; + } }