Skip to content

Commit efae9f8

Browse files
committed
Git API usage for RO bundles
1 parent 14c927a commit efae9f8

File tree

2 files changed

+46
-54
lines changed

2 files changed

+46
-54
lines changed

src/main/java/org/commonwl/view/cwl/CWLToolRunner.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.commonwl.view.cwl;
2121

22-
import org.commonwl.view.github.GitService;
2322
import org.commonwl.view.researchobject.ROBundleFactory;
2423
import org.commonwl.view.workflow.QueuedWorkflow;
2524
import org.commonwl.view.workflow.QueuedWorkflowRepository;
@@ -48,7 +47,6 @@ public class CWLToolRunner {
4847
private final WorkflowRepository workflowRepository;
4948
private final QueuedWorkflowRepository queuedWorkflowRepository;
5049
private final CWLService cwlService;
51-
private final GitService githubService;
5250
private final ROBundleFactory roBundleFactory;
5351
private final String cwlToolVersion;
5452

@@ -57,12 +55,10 @@ public CWLToolRunner(WorkflowRepository workflowRepository,
5755
QueuedWorkflowRepository queuedWorkflowRepository,
5856
CWLService cwlService,
5957
CWLTool cwlTool,
60-
GitService githubService,
6158
ROBundleFactory roBundleFactory) {
6259
this.workflowRepository = workflowRepository;
6360
this.queuedWorkflowRepository = queuedWorkflowRepository;
6461
this.cwlService = cwlService;
65-
this.githubService = githubService;
6662
this.cwlToolVersion = cwlTool.getVersion();
6763
this.roBundleFactory = roBundleFactory;
6864
}

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

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919

2020
package org.commonwl.view.researchobject;
2121

22+
import org.apache.commons.io.FileUtils;
23+
import org.apache.commons.io.FilenameUtils;
2224
import org.apache.taverna.robundle.Bundle;
2325
import org.apache.taverna.robundle.Bundles;
24-
import org.apache.taverna.robundle.manifest.Agent;
25-
import org.apache.taverna.robundle.manifest.Manifest;
26-
import org.apache.taverna.robundle.manifest.PathAnnotation;
27-
import org.apache.taverna.robundle.manifest.PathMetadata;
26+
import org.apache.taverna.robundle.manifest.*;
2827
import org.commonwl.view.cwl.CWLTool;
2928
import org.commonwl.view.github.GitDetails;
3029
import org.commonwl.view.github.GitService;
3130
import org.commonwl.view.graphviz.GraphVizService;
3231
import org.commonwl.view.workflow.Workflow;
3332
import org.eclipse.jgit.api.Git;
33+
import org.eclipse.jgit.lib.PersonIdent;
34+
import org.eclipse.jgit.revwalk.RevCommit;
3435
import org.slf4j.Logger;
3536
import org.slf4j.LoggerFactory;
3637
import org.springframework.beans.factory.annotation.Autowired;
@@ -48,8 +49,11 @@
4849
import java.util.HashSet;
4950
import java.util.List;
5051
import java.util.Set;
52+
import java.util.regex.Matcher;
5153
import java.util.regex.Pattern;
5254

55+
import static org.apache.commons.io.FileUtils.readFileToString;
56+
5357
/**
5458
* Service handling Research Object Bundles
5559
*/
@@ -122,13 +126,14 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
122126

123127
// Make a directory in the RO bundle to store the files
124128
Path bundleRoot = bundle.getRoot();
125-
Path bundleFiles = bundleRoot.resolve("workflow");
126-
Files.createDirectory(bundleFiles);
129+
Path bundlePath = bundleRoot.resolve("workflow");
130+
Files.createDirectory(bundlePath);
127131

128132
// Add the files from the Github repo to this workflow
129133
Set<HashableAgent> authors = new HashSet<>();
130134
Git gitRepo = Git.open(new File(workflow.getGitRepoPath()));
131-
addFilesToBundle(bundle, gitInfo, gitRepo, bundleFiles, authors);
135+
Path gitPath = gitRepo.getRepository().getDirectory().toPath();
136+
addFilesToBundle(bundle, bundlePath, gitRepo, gitPath, authors);
132137

133138
// Add combined authors
134139
manifest.setAuthoredBy(new ArrayList<>(authors));
@@ -177,42 +182,32 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
177182
/**
178183
* Add files to this bundle from a list of Github repository contents
179184
* @param bundle The RO bundle to add files/directories to
180-
* @param gitInfo The information used to access the repository
185+
* @param bundlePath The current path within the RO bundle
181186
* @param gitRepo The Git repository
182-
* @param path The path in the Research Object to add the files
187+
* @param repoPath The current path within the Git repository
188+
* @param authors The combined set of authors for al the files
183189
*/
184-
private void addFilesToBundle(Bundle bundle, GitDetails gitInfo,
185-
Git gitRepo, Path path,
190+
private void addFilesToBundle(Bundle bundle, Path bundlePath,
191+
Git gitRepo, Path repoPath,
186192
Set<HashableAgent> authors) throws IOException {
187-
/*File[] files = gitRepo.getRepository().getDirectory().listFiles();
193+
File[] files = gitRepo.getRepository().getDirectory().listFiles();
188194
for (File file : files) {
189195
if (file.isDirectory()) {
190196

191-
// Get the contents of the subdirectory
192-
GitDetails githubSubdir = new GitDetails(gitInfo.getOwner(),
193-
gitInfo.getRepoName(), gitInfo.getBranch(), repoContent.getPath());
194-
List<RepositoryContents> subdirectory = githubService.getContents(githubSubdir);
195-
196-
// Create a new folder in the RO for it
197-
Path subdirPath = path.resolve(repoContent.getName());
198-
Files.createDirectory(subdirPath);
197+
// Create a new folder in the RO for this directory
198+
Path newBundlePath = bundlePath.resolve(file.getName());
199+
Files.createDirectory(newBundlePath);
199200

200-
// Add the files in the subdirectory to this new folder
201-
addFilesToBundle(bundle, gitInfo, subdirectory, subdirPath, authors);
201+
// Add all files in the subdirectory to this new folder
202+
addFilesToBundle(bundle, newBundlePath, gitRepo,
203+
repoPath.resolve(file.getName()), authors);
202204

203205
} else {
204206
try {
205-
// Raw URI of the bundle
206-
GitDetails githubFile = new GitDetails(gitInfo.getOwner(),
207-
gitInfo.getRepoName(), gitInfo.getBranch(), repoContent.getPath());
208-
209207
// Where to store the new file in bundle
210-
Path bundleFilePath = path.resolve(repoContent.getName());
211-
212-
// Get commits
213-
List<RepositoryCommit> commitsOnFile = githubService.getCommits(githubFile);
214-
String commitSha = commitsOnFile.get(0).getSha();
208+
Path bundleFilePath = bundlePath.resolve(file.getName());
215209

210+
// Get direct URL
216211
URI rawURI = new URI("https://raw.githubusercontent.com/" + githubFile.getOwner() + "/" +
217212
githubFile.getRepoName() + "/" + commitSha + "/" + githubFile.getPath());
218213

@@ -221,11 +216,9 @@ private void addFilesToBundle(Bundle bundle, GitDetails gitInfo,
221216
PathMetadata aggregation;
222217

223218
// Download or externally link if oversized
224-
if (repoContent.getSize() <= singleFileSizeLimit) {
225-
// Get the content of this file from Github
226-
fileContent = githubService.downloadFile(githubFile, commitSha);
227-
219+
if (file.length() <= singleFileSizeLimit) {
228220
// Save file to research object bundle
221+
fileContent = readFileToString(file);
229222
Bundles.setStringValue(bundleFilePath, fileContent);
230223

231224
// Set retrieved information for this file in the manifest
@@ -234,21 +227,21 @@ private void addFilesToBundle(Bundle bundle, GitDetails gitInfo,
234227
aggregation.setRetrievedBy(appAgent);
235228
aggregation.setRetrievedOn(aggregation.getCreatedOn());
236229
} else {
237-
logger.info("File " + repoContent.getName() + " is too large to download - " +
238-
FileUtils.byteCountToDisplaySize(repoContent.getSize()) + "/" +
230+
logger.info("File " + file.getName() + " is too large to download - " +
231+
FileUtils.byteCountToDisplaySize(file.length()) + "/" +
239232
FileUtils.byteCountToDisplaySize(singleFileSizeLimit) +
240233
", linking externally to RO bundle");
241234

242235
// Set information for this file in the manifest
243236
aggregation = bundle.getManifest().getAggregation(rawURI);
244237
Proxy bundledAs = new Proxy();
245238
bundledAs.setURI();
246-
bundledAs.setFolder(path);
239+
bundledAs.setFolder(repoPath);
247240
aggregation.setBundledAs(bundledAs);
248241
}
249242

250243
// Special handling for cwl files
251-
if (FilenameUtils.getExtension(repoContent.getName()).equals("cwl")) {
244+
if (FilenameUtils.getExtension(file.getName()).equals("cwl")) {
252245
// Correct mime type (no official standard for yaml)
253246
aggregation.setMediatype("text/x-yaml");
254247

@@ -263,23 +256,26 @@ private void addFilesToBundle(Bundle bundle, GitDetails gitInfo,
263256

264257
// Add authors from github commits to the file
265258
Set<HashableAgent> fileAuthors = new HashSet<>();
266-
for (RepositoryCommit commit : commitsOnFile) {
267-
User author = commit.getAuthor();
268-
CommitUser commitAuthor = commit.getCommit().getAuthor();
269-
270-
// If there is author information for this commit in some form
271-
if (author != null || commitAuthor != null) {
259+
Iterable<RevCommit> logs = gitRepo.log()
260+
.addPath(bundlePath.toString())
261+
.call();
262+
for (RevCommit rev : logs) {
263+
PersonIdent author = rev.getAuthorIdent();
264+
PersonIdent committer = rev.getCommitterIdent();
265+
if (author != null || committer != null) {
272266
// Create a new agent and add as much detail as possible
273267
HashableAgent newAgent = new HashableAgent();
274268
if (author != null) {
275-
newAgent.setUri(new URI(author.getHtmlUrl()));
276-
}
277-
if (commitAuthor != null) {
278-
newAgent.setName(commitAuthor.getName());
269+
newAgent.setName(author.getName());
270+
newAgent.setUri(new URI("mailto:" + author.getEmailAddress()));
271+
} else {
272+
newAgent.setName(committer.getName());
273+
newAgent.setUri(new URI("mailto:" + committer.getEmailAddress()));
279274
}
280275
fileAuthors.add(newAgent);
281276
}
282277
}
278+
283279
authors.addAll(fileAuthors);
284280
aggregation.setAuthoredBy(new ArrayList<>(fileAuthors));
285281

@@ -292,7 +288,7 @@ private void addFilesToBundle(Bundle bundle, GitDetails gitInfo,
292288
logger.error("Error creating URI for RO Bundle", ex);
293289
}
294290
}
295-
}*/
291+
}
296292
}
297293

298294
/**

0 commit comments

Comments
 (0)