Skip to content

Commit 888269b

Browse files
committed
Delete git repository directory after RO Bundle is (re)created, success or not.
1 parent cf8b895 commit 888269b

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/main/java/org/commonwl/view/researchobject/ROBundleService.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ public class ROBundleService {
7373
private final Logger logger = LoggerFactory.getLogger(this.getClass());
7474

7575
// Services
76-
private GraphVizService graphVizService;
77-
private GitService gitService;
78-
private RDFService rdfService;
79-
private CWLTool cwlTool;
80-
private GitSemaphore gitSemaphore;
76+
private final GraphVizService graphVizService;
77+
private final GitService gitService;
78+
private final RDFService rdfService;
79+
private final CWLTool cwlTool;
80+
private final GitSemaphore gitSemaphore;
8181

8282
// Configuration variables
83-
private Agent appAgent;
84-
private int singleFileSizeLimit;
85-
private Path bundleStorage;
83+
private final Agent appAgent;
84+
private final int singleFileSizeLimit;
85+
private final Path bundleStorage;
8686

8787
// Pattern for extracting version from a cwl file
8888
private final String CWL_VERSION_REGEX = "cwlVersion:\\s*\"?(?:cwl:)?([^\\s\"]+)\"?";
@@ -129,6 +129,7 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
129129
Manifest manifest = bundle.getManifest();
130130

131131
// Simplified attribution for RO bundle
132+
Git gitRepo = null;
132133
try {
133134
manifest.setId(new URI(workflow.getPermalink()));
134135

@@ -150,7 +151,8 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
150151
Set<HashableAgent> authors = new HashSet<>();
151152

152153
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
153-
try (Git gitRepo = gitService.getRepository(workflow.getRetrievedFrom(), safeToAccess)) {
154+
try {
155+
gitRepo = gitService.getRepository(workflow.getRetrievedFrom(), safeToAccess);
154156
Path relativePath = Paths.get(FilenameUtils.getPath(gitInfo.getPath()));
155157
Path gitPath = gitRepo.getRepository().getWorkTree().toPath().resolve(relativePath);
156158
addFilesToBundle(gitInfo, bundle, bundlePath, gitRepo, gitPath, authors, workflow);
@@ -194,7 +196,7 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
194196
addAggregation(bundle, manifestAnnotations,
195197
"merged.cwl", cwlTool.getPackedVersion(rawUrl));
196198
} catch (CWLValidationException ex) {
197-
logger.error("Could not pack workflow when creating Research Object", ex.getMessage());
199+
logger.error("Could not pack workflow when creating Research Object", ex);
198200
}
199201
String rdfUrl = workflow.getIdentifier();
200202
if (rdfService.graphExists(rdfUrl)) {
@@ -216,6 +218,19 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
216218
logger.error("Error creating URI for RO Bundle", ex);
217219
} catch (GitAPIException ex) {
218220
logger.error("Error getting repository to create RO Bundle", ex);
221+
} finally {
222+
if (gitRepo != null) {
223+
gitRepo.close();
224+
File repoDir = gitRepo.getRepository().getDirectory();
225+
try {
226+
FileUtils.deleteDirectory(repoDir.getParentFile());
227+
} catch (IOException e) {
228+
logger.warn(
229+
String.format("Failed to delete Git repository directory: %s", repoDir.getAbsolutePath()),
230+
e
231+
);
232+
}
233+
}
219234
}
220235

221236
// Return the completed bundle

src/main/java/org/commonwl/view/workflow/WorkflowService.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -456,18 +456,11 @@ public Workflow findByCommitAndPath(String commitID, String path, Optional<Strin
456456
public PathResource getWorkflowGraph(String format, GitDetails gitDetails)
457457
throws WorkflowNotFoundException, IOException {
458458
// Determine file extension from format
459-
String extension;
460-
switch (format) {
461-
case "svg":
462-
case "png":
463-
extension = format;
464-
break;
465-
case "xdot":
466-
extension = "dot";
467-
break;
468-
default:
469-
throw new WorkflowNotFoundException();
470-
}
459+
String extension = switch (format) {
460+
case "svg", "png" -> format;
461+
case "xdot" -> "dot";
462+
default -> throw new WorkflowNotFoundException();
463+
};
471464

472465
// Get workflow
473466
Workflow workflow = getWorkflow(gitDetails);

0 commit comments

Comments
 (0)