19
19
20
20
package org .commonwl .view .researchobject ;
21
21
22
+ import org .apache .commons .io .FileUtils ;
23
+ import org .apache .commons .io .FilenameUtils ;
22
24
import org .apache .taverna .robundle .Bundle ;
23
25
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 .*;
28
27
import org .commonwl .view .cwl .CWLTool ;
29
28
import org .commonwl .view .github .GitDetails ;
30
29
import org .commonwl .view .github .GitService ;
31
30
import org .commonwl .view .graphviz .GraphVizService ;
32
31
import org .commonwl .view .workflow .Workflow ;
33
32
import org .eclipse .jgit .api .Git ;
33
+ import org .eclipse .jgit .lib .PersonIdent ;
34
+ import org .eclipse .jgit .revwalk .RevCommit ;
34
35
import org .slf4j .Logger ;
35
36
import org .slf4j .LoggerFactory ;
36
37
import org .springframework .beans .factory .annotation .Autowired ;
48
49
import java .util .HashSet ;
49
50
import java .util .List ;
50
51
import java .util .Set ;
52
+ import java .util .regex .Matcher ;
51
53
import java .util .regex .Pattern ;
52
54
55
+ import static org .apache .commons .io .FileUtils .readFileToString ;
56
+
53
57
/**
54
58
* Service handling Research Object Bundles
55
59
*/
@@ -122,13 +126,14 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
122
126
123
127
// Make a directory in the RO bundle to store the files
124
128
Path bundleRoot = bundle .getRoot ();
125
- Path bundleFiles = bundleRoot .resolve ("workflow" );
126
- Files .createDirectory (bundleFiles );
129
+ Path bundlePath = bundleRoot .resolve ("workflow" );
130
+ Files .createDirectory (bundlePath );
127
131
128
132
// Add the files from the Github repo to this workflow
129
133
Set <HashableAgent > authors = new HashSet <>();
130
134
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 );
132
137
133
138
// Add combined authors
134
139
manifest .setAuthoredBy (new ArrayList <>(authors ));
@@ -177,42 +182,32 @@ public Bundle createBundle(Workflow workflow, GitDetails gitInfo) throws IOExcep
177
182
/**
178
183
* Add files to this bundle from a list of Github repository contents
179
184
* @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
181
186
* @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
183
189
*/
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 ,
186
192
Set <HashableAgent > authors ) throws IOException {
187
- /* File[] files = gitRepo.getRepository().getDirectory().listFiles();
193
+ File [] files = gitRepo .getRepository ().getDirectory ().listFiles ();
188
194
for (File file : files ) {
189
195
if (file .isDirectory ()) {
190
196
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 );
199
200
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 );
202
204
203
205
} else {
204
206
try {
205
- // Raw URI of the bundle
206
- GitDetails githubFile = new GitDetails(gitInfo.getOwner(),
207
- gitInfo.getRepoName(), gitInfo.getBranch(), repoContent.getPath());
208
-
209
207
// 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 ());
215
209
210
+ // Get direct URL
216
211
URI rawURI = new URI ("https://raw.githubusercontent.com/" + githubFile .getOwner () + "/" +
217
212
githubFile .getRepoName () + "/" + commitSha + "/" + githubFile .getPath ());
218
213
@@ -221,11 +216,9 @@ private void addFilesToBundle(Bundle bundle, GitDetails gitInfo,
221
216
PathMetadata aggregation ;
222
217
223
218
// 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 ) {
228
220
// Save file to research object bundle
221
+ fileContent = readFileToString (file );
229
222
Bundles .setStringValue (bundleFilePath , fileContent );
230
223
231
224
// Set retrieved information for this file in the manifest
@@ -234,21 +227,21 @@ private void addFilesToBundle(Bundle bundle, GitDetails gitInfo,
234
227
aggregation .setRetrievedBy (appAgent );
235
228
aggregation .setRetrievedOn (aggregation .getCreatedOn ());
236
229
} 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 ()) + "/" +
239
232
FileUtils .byteCountToDisplaySize (singleFileSizeLimit ) +
240
233
", linking externally to RO bundle" );
241
234
242
235
// Set information for this file in the manifest
243
236
aggregation = bundle .getManifest ().getAggregation (rawURI );
244
237
Proxy bundledAs = new Proxy ();
245
238
bundledAs .setURI ();
246
- bundledAs.setFolder(path );
239
+ bundledAs .setFolder (repoPath );
247
240
aggregation .setBundledAs (bundledAs );
248
241
}
249
242
250
243
// Special handling for cwl files
251
- if (FilenameUtils.getExtension(repoContent .getName()).equals("cwl")) {
244
+ if (FilenameUtils .getExtension (file .getName ()).equals ("cwl" )) {
252
245
// Correct mime type (no official standard for yaml)
253
246
aggregation .setMediatype ("text/x-yaml" );
254
247
@@ -263,23 +256,26 @@ private void addFilesToBundle(Bundle bundle, GitDetails gitInfo,
263
256
264
257
// Add authors from github commits to the file
265
258
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 ) {
272
266
// Create a new agent and add as much detail as possible
273
267
HashableAgent newAgent = new HashableAgent ();
274
268
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 ()));
279
274
}
280
275
fileAuthors .add (newAgent );
281
276
}
282
277
}
278
+
283
279
authors .addAll (fileAuthors );
284
280
aggregation .setAuthoredBy (new ArrayList <>(fileAuthors ));
285
281
@@ -292,7 +288,7 @@ private void addFilesToBundle(Bundle bundle, GitDetails gitInfo,
292
288
logger .error ("Error creating URI for RO Bundle" , ex );
293
289
}
294
290
}
295
- }*/
291
+ }
296
292
}
297
293
298
294
/**
0 commit comments