|
8 | 8 | import java.nio.charset.StandardCharsets;
|
9 | 9 | import java.nio.file.Files;
|
10 | 10 | import java.nio.file.Path;
|
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Arrays; |
11 | 13 | import java.util.List;
|
12 | 14 | import java.util.Map;
|
13 | 15 | import java.util.stream.Stream;
|
|
33 | 35 | import de.tum.cit.aet.artemis.core.service.FileService;
|
34 | 36 | import de.tum.cit.aet.artemis.core.service.ProfileService;
|
35 | 37 | import de.tum.cit.aet.artemis.core.service.ZipFileService;
|
| 38 | +import de.tum.cit.aet.artemis.programming.domain.AuxiliaryRepository; |
36 | 39 | import de.tum.cit.aet.artemis.programming.domain.ProgrammingExercise;
|
37 | 40 | import de.tum.cit.aet.artemis.programming.domain.Repository;
|
38 | 41 | import de.tum.cit.aet.artemis.programming.domain.RepositoryType;
|
@@ -170,44 +173,72 @@ private void importRepositoriesFromFile(ProgrammingExercise newExercise, Path ba
|
170 | 173 | Repository templateRepo = gitService.getOrCheckoutRepository(new VcsRepositoryUri(newExercise.getTemplateRepositoryUri()), false);
|
171 | 174 | Repository solutionRepo = gitService.getOrCheckoutRepository(new VcsRepositoryUri(newExercise.getSolutionRepositoryUri()), false);
|
172 | 175 | Repository testRepo = gitService.getOrCheckoutRepository(new VcsRepositoryUri(newExercise.getTestRepositoryUri()), false);
|
| 176 | + List<Repository> auxiliaryRepositories = new ArrayList<>(); |
| 177 | + for (AuxiliaryRepository auxiliaryRepository : newExercise.getAuxiliaryRepositories()) { |
| 178 | + auxiliaryRepositories.add(gitService.getOrCheckoutRepository(auxiliaryRepository.getVcsRepositoryUri(), false)); |
| 179 | + } |
173 | 180 |
|
174 |
| - copyImportedExerciseContentToRepositories(templateRepo, solutionRepo, testRepo, basePath); |
175 |
| - replaceImportedExerciseShortName(Map.of(oldExerciseShortName, newExercise.getShortName()), templateRepo, solutionRepo, testRepo); |
| 181 | + copyImportedExerciseContentToRepositories(templateRepo, solutionRepo, testRepo, auxiliaryRepositories, basePath); |
| 182 | + replaceImportedExerciseShortName(Map.of(oldExerciseShortName, newExercise.getShortName()), List.of(solutionRepo, templateRepo, testRepo)); |
| 183 | + replaceImportedExerciseShortName(Map.of(oldExerciseShortName, newExercise.getShortName()), auxiliaryRepositories); |
176 | 184 |
|
177 | 185 | gitService.stageAllChanges(templateRepo);
|
178 | 186 | gitService.stageAllChanges(solutionRepo);
|
179 | 187 | gitService.stageAllChanges(testRepo);
|
| 188 | + for (Repository auxRepo : auxiliaryRepositories) { |
| 189 | + gitService.stageAllChanges(auxRepo); |
| 190 | + } |
180 | 191 |
|
181 | 192 | gitService.commitAndPush(templateRepo, "Import template from file", true, user);
|
182 | 193 | gitService.commitAndPush(solutionRepo, "Import solution from file", true, user);
|
183 | 194 | gitService.commitAndPush(testRepo, "Import tests from file", true, user);
|
| 195 | + for (Repository auxRepo : auxiliaryRepositories) { |
| 196 | + gitService.commitAndPush(auxRepo, "Import auxiliary repo from file", true, user); |
| 197 | + } |
| 198 | + |
184 | 199 | }
|
185 | 200 |
|
186 |
| - private void replaceImportedExerciseShortName(Map<String, String> replacements, Repository... repositories) { |
| 201 | + private void replaceImportedExerciseShortName(Map<String, String> replacements, List<Repository> repositories) { |
187 | 202 | for (Repository repository : repositories) {
|
188 | 203 | fileService.replaceVariablesInFileRecursive(repository.getLocalPath(), replacements, SHORT_NAME_REPLACEMENT_EXCLUSIONS);
|
189 | 204 | }
|
190 | 205 | }
|
191 | 206 |
|
192 |
| - private void copyImportedExerciseContentToRepositories(Repository templateRepo, Repository solutionRepo, Repository testRepo, Path basePath) throws IOException { |
| 207 | + private void copyImportedExerciseContentToRepositories(Repository templateRepo, Repository solutionRepo, Repository testRepo, List<Repository> auxiliaryRepositories, |
| 208 | + Path basePath) throws IOException { |
193 | 209 | repositoryService.deleteAllContentInRepository(templateRepo);
|
194 | 210 | repositoryService.deleteAllContentInRepository(solutionRepo);
|
195 | 211 | repositoryService.deleteAllContentInRepository(testRepo);
|
196 |
| - copyExerciseContentToRepository(templateRepo, RepositoryType.TEMPLATE, basePath); |
197 |
| - copyExerciseContentToRepository(solutionRepo, RepositoryType.SOLUTION, basePath); |
198 |
| - copyExerciseContentToRepository(testRepo, RepositoryType.TESTS, basePath); |
| 212 | + for (Repository auxRepo : auxiliaryRepositories) { |
| 213 | + repositoryService.deleteAllContentInRepository(auxRepo); |
| 214 | + } |
| 215 | + |
| 216 | + copyExerciseContentToRepository(templateRepo, RepositoryType.TEMPLATE.getName(), basePath); |
| 217 | + copyExerciseContentToRepository(solutionRepo, RepositoryType.SOLUTION.getName(), basePath); |
| 218 | + copyExerciseContentToRepository(testRepo, RepositoryType.TESTS.getName(), basePath); |
| 219 | + for (Repository auxRepo : auxiliaryRepositories) { |
| 220 | + String[] parts = auxRepo.getLocalPath().toString().split("-"); |
| 221 | + var auxRepoName = String.join("-", Arrays.copyOfRange(parts, 1, parts.length)); |
| 222 | + copyExerciseContentToRepository(auxRepo, auxRepoName, basePath); |
| 223 | + } |
199 | 224 | }
|
200 | 225 |
|
201 | 226 | /**
|
202 | 227 | * Copies everything from the extracted zip file to the repository, except the .git folder
|
203 | 228 | *
|
204 |
| - * @param repository the repository to which the content should be copied |
205 |
| - * @param repositoryType the type of the repository |
206 |
| - * @param basePath the path to the extracted zip file |
| 229 | + * @param repository the repository to which the content should be copied |
| 230 | + * @param repoName the name of the repository |
| 231 | + * @param basePath the path to the extracted zip file |
207 | 232 | **/
|
208 |
| - private void copyExerciseContentToRepository(Repository repository, RepositoryType repositoryType, Path basePath) throws IOException { |
209 |
| - FileUtils.copyDirectory(retrieveRepositoryDirectoryPath(basePath, repositoryType.getName()).toFile(), repository.getLocalPath().toFile(), |
210 |
| - new NotFileFilter(new NameFileFilter(".git"))); |
| 233 | + private void copyExerciseContentToRepository(Repository repository, String repoName, Path basePath) throws IOException { |
| 234 | + // @formatter:off |
| 235 | + FileUtils.copyDirectory( |
| 236 | + retrieveRepositoryDirectoryPath(basePath, repoName).toFile(), |
| 237 | + repository.getLocalPath().toFile(), |
| 238 | + new NotFileFilter(new NameFileFilter(".git")) |
| 239 | + ); |
| 240 | + // @formatter:on |
| 241 | + |
211 | 242 | try (var files = Files.walk(repository.getLocalPath())) {
|
212 | 243 | files.filter(file -> "gradlew".equals(file.getFileName().toString())).forEach(file -> file.toFile().setExecutable(true));
|
213 | 244 | }
|
@@ -242,17 +273,17 @@ private void checkRepositoryForTypeExists(Path path, RepositoryType repoType) th
|
242 | 273 | }
|
243 | 274 | }
|
244 | 275 |
|
245 |
| - private Path retrieveRepositoryDirectoryPath(Path dirPath, String repoType) { |
| 276 | + private Path retrieveRepositoryDirectoryPath(Path dirPath, String repoName) { |
246 | 277 | List<Path> result;
|
247 | 278 | try (Stream<Path> walk = Files.walk(dirPath)) {
|
248 |
| - result = walk.filter(Files::isDirectory).filter(file -> file.getFileName().toString().endsWith("-" + repoType)).toList(); |
| 279 | + result = walk.filter(Files::isDirectory).filter(file -> file.getFileName().toString().endsWith("-" + repoName)).toList(); |
249 | 280 | }
|
250 | 281 | catch (IOException e) {
|
251 | 282 | throw new BadRequestAlertException("Could not read the directory", "programmingExercise", "couldnotreaddirectory");
|
252 | 283 | }
|
253 | 284 | if (result.size() != 1) {
|
254 | 285 | throw new IllegalArgumentException(
|
255 |
| - "There are either no or more than one sub-directories containing " + repoType + " in their name. Please make sure that there is exactly one."); |
| 286 | + "There are either no or more than one sub-directories containing " + repoName + " in their name. Please make sure that there is exactly one."); |
256 | 287 | }
|
257 | 288 |
|
258 | 289 | return result.getFirst();
|
|
0 commit comments