19
19
20
20
package org .commonwl .viewer .domain ;
21
21
22
+ import org .apache .commons .io .FilenameUtils ;
22
23
import org .apache .taverna .robundle .Bundle ;
23
24
import org .apache .taverna .robundle .Bundles ;
24
25
import org .apache .taverna .robundle .manifest .Agent ;
25
26
import org .apache .taverna .robundle .manifest .Manifest ;
27
+ import org .apache .taverna .robundle .manifest .PathMetadata ;
26
28
import org .commonwl .viewer .services .GitHubService ;
27
29
import org .eclipse .egit .github .core .RepositoryContents ;
28
- import org .eclipse .egit .github .core .User ;
29
30
import org .slf4j .Logger ;
30
31
import org .slf4j .LoggerFactory ;
31
32
35
36
import java .nio .file .Files ;
36
37
import java .nio .file .Path ;
37
38
import java .util .ArrayList ;
39
+ import java .util .HashSet ;
38
40
import java .util .List ;
41
+ import java .util .Set ;
42
+ import java .util .regex .Matcher ;
43
+ import java .util .regex .Pattern ;
39
44
40
45
/**
41
46
* Represents a Workflow Research Object Bundle
@@ -49,6 +54,11 @@ public class ROBundle {
49
54
private Bundle bundle ;
50
55
private GithubDetails githubInfo ;
51
56
private String commitSha ;
57
+ private Set <HashableAgent > authors = new HashSet <HashableAgent >();
58
+
59
+ // Pattern for extracting version from a cwl file
60
+ private final String CWL_VERSION_REGEX = "cwlVersion:\\ s*\" ?(?:cwl:)?([^\\ s\" ]+)\" ?" ;
61
+ private final Pattern cwlVersionPattern = Pattern .compile (CWL_VERSION_REGEX );
52
62
53
63
/**
54
64
* Creates a new research object bundle for a workflow from a Github repository
@@ -69,31 +79,19 @@ public ROBundle(GitHubService githubService, GithubDetails githubInfo, String co
69
79
70
80
// Simplified attribution for RO bundle
71
81
try {
82
+ // Tool attribution in createdBy
72
83
Agent cwlViewer = new Agent (appName );
73
84
cwlViewer .setUri (new URI (appURL ));
74
85
manifest .setCreatedBy (cwlViewer );
75
86
76
- // Github author attribution
77
- // TODO: way to add all the contributors somehow
78
- // TODO: set the aggregates details according to the github information
79
- User authorDetails = githubService .getUser (githubInfo .getOwner ());
80
-
81
- List <Agent > authorList = new ArrayList <>(1 );
82
- Agent author = new Agent (authorDetails .getName ());
83
- author .setUri (new URI (authorDetails .getHtmlUrl ()));
84
-
85
- // This tool supports putting your ORCID in the blog field of github as a URL
86
- // eg http://orcid.org/0000-0000-0000-0000
87
- String authorBlog = authorDetails .getBlog ();
88
- if (authorBlog != null && authorBlog .startsWith ("http://orcid.org/" )) {
89
- author .setOrcid (new URI (authorBlog ));
90
- }
91
-
92
- authorList .add (author );
93
- manifest .setAuthoredBy (authorList );
87
+ // Retrieval Info
88
+ manifest .setRetrievedBy (cwlViewer );
89
+ manifest .setRetrievedOn (manifest .getCreatedOn ());
90
+ manifest .setRetrievedFrom (new URI ("https://github.com/" + githubInfo .getOwner () + "/"
91
+ + githubInfo .getRepoName () + "/tree/" + commitSha + "/" + githubInfo .getPath ()));
94
92
95
93
} catch (URISyntaxException ex ) {
96
- logger .error (ex . getMessage () );
94
+ logger .error ("Error creating URI for RO Bundle" , ex );
97
95
}
98
96
99
97
// Make a directory in the RO bundle to store the files
@@ -103,6 +101,9 @@ public ROBundle(GitHubService githubService, GithubDetails githubInfo, String co
103
101
// Add the files from the Github repo to this workflow
104
102
List <RepositoryContents > repoContents = githubService .getContents (githubInfo );
105
103
addFiles (repoContents , bundleFiles );
104
+
105
+ // Add combined authors
106
+ manifest .setAuthoredBy (new ArrayList <Agent >(authors ));
106
107
}
107
108
108
109
/**
@@ -130,7 +131,7 @@ private void addFiles(List<RepositoryContents> repoContents, Path path) throws I
130
131
// Add the files in the subdirectory to this new folder
131
132
addFiles (subdirectory , subdirPath );
132
133
133
- // Otherwise this is a file so add to the bundle
134
+ // Otherwise this is a file so add to the bundle
134
135
} else if (repoContent .getType ().equals ("file" )) {
135
136
136
137
// Get the content of this file from Github
@@ -142,6 +143,35 @@ private void addFiles(List<RepositoryContents> repoContents, Path path) throws I
142
143
Path newFilePort = path .resolve (repoContent .getName ());
143
144
Bundles .setStringValue (newFilePort , fileContent );
144
145
146
+ // Manifest aggregation
147
+ PathMetadata aggregation = bundle .getManifest ().getAggregation (newFilePort );
148
+
149
+ try {
150
+ // Special handling for cwl files
151
+ if (FilenameUtils .getExtension (repoContent .getName ()).equals ("cwl" )) {
152
+ // Correct mime type (no official standard for yaml)
153
+ aggregation .setMediatype ("text/x-yaml" );
154
+
155
+ // Add conformsTo for version extracted from regex
156
+ // Lower overhead vs parsing entire file
157
+ Matcher m = cwlVersionPattern .matcher (fileContent );
158
+ if (m .find ()) {
159
+ aggregation .setConformsTo (new URI ("https://w3id.org/cwl/" + m .group (1 )));
160
+ }
161
+ }
162
+
163
+ // Add authors from github commits to the file
164
+ Set <HashableAgent > fileAuthors = githubService .getContributors (githubFile , commitSha );
165
+ authors .addAll (fileAuthors );
166
+ aggregation .setAuthoredBy (new ArrayList <Agent >(fileAuthors ));
167
+
168
+ // Set retrievedFrom information for this file in the manifest
169
+ aggregation .setRetrievedFrom (new URI ("https://github.com/" + githubFile .getOwner () + "/" +
170
+ githubFile .getRepoName () + "/blob/" + commitSha + "/" + githubFile .getPath ()));
171
+ } catch (URISyntaxException ex ) {
172
+ logger .error ("Error creating URI for RO Bundle" , ex );
173
+ }
174
+
145
175
}
146
176
}
147
177
}
0 commit comments