Skip to content

Commit a77f4a4

Browse files
author
Mark Robinson
committed
Add author information from Github commits to Research Objects
1 parent 770171e commit a77f4a4

File tree

3 files changed

+124
-31
lines changed

3 files changed

+124
-31
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.commonwl.viewer.domain;
2+
3+
import org.apache.taverna.robundle.manifest.Agent;
4+
5+
import java.net.URI;
6+
7+
/**
8+
* An implementation of Agent with added HashCode and Equals methods
9+
* for use in sets
10+
*/
11+
public class HashableAgent extends Agent {
12+
13+
private String name;
14+
private URI orcid;
15+
private URI uri;
16+
17+
@Override
18+
public String getName() {
19+
return name;
20+
}
21+
22+
@Override
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
@Override
28+
public URI getOrcid() {
29+
return orcid;
30+
}
31+
32+
@Override
33+
public void setOrcid(URI orcid) {
34+
this.orcid = orcid;
35+
}
36+
37+
@Override
38+
public URI getUri() {
39+
return uri;
40+
}
41+
42+
@Override
43+
public void setUri(URI uri) {
44+
this.uri = uri;
45+
}
46+
47+
@Override
48+
public boolean equals(Object o) {
49+
if (this == o) return true;
50+
if (o == null || getClass() != o.getClass() || super.getClass() != o.getClass()) return false;
51+
52+
HashableAgent that = (HashableAgent) o;
53+
54+
if (name != null ? !name.equals(that.name) : that.name != null) return false;
55+
if (orcid != null ? !orcid.equals(that.orcid) : that.orcid != null) return false;
56+
return uri != null ? uri.equals(that.uri) : that.uri == null;
57+
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
int result = name != null ? name.hashCode() : 0;
63+
result = 31 * result + (orcid != null ? orcid.hashCode() : 0);
64+
result = 31 * result + (uri != null ? uri.hashCode() : 0);
65+
return result;
66+
}
67+
68+
}

src/main/java/org/commonwl/viewer/domain/ROBundle.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.commonwl.viewer.domain;
2121

22-
import com.fasterxml.jackson.databind.JsonNode;
23-
import com.fasterxml.jackson.databind.ObjectMapper;
2422
import org.apache.commons.io.FilenameUtils;
2523
import org.apache.taverna.robundle.Bundle;
2624
import org.apache.taverna.robundle.Bundles;
@@ -29,18 +27,18 @@
2927
import org.apache.taverna.robundle.manifest.PathMetadata;
3028
import org.commonwl.viewer.services.GitHubService;
3129
import org.eclipse.egit.github.core.RepositoryContents;
32-
import org.eclipse.egit.github.core.User;
3330
import org.slf4j.Logger;
3431
import org.slf4j.LoggerFactory;
35-
import org.yaml.snakeyaml.Yaml;
3632

3733
import java.io.IOException;
3834
import java.net.URI;
3935
import java.net.URISyntaxException;
4036
import java.nio.file.Files;
4137
import java.nio.file.Path;
4238
import java.util.ArrayList;
39+
import java.util.HashSet;
4340
import java.util.List;
41+
import java.util.Set;
4442
import java.util.regex.Matcher;
4543
import java.util.regex.Pattern;
4644

@@ -56,6 +54,7 @@ public class ROBundle {
5654
private Bundle bundle;
5755
private GithubDetails githubInfo;
5856
private String commitSha;
57+
private Set<HashableAgent> authors = new HashSet<HashableAgent>();
5958

6059
// Pattern for extracting version from a cwl file
6160
private final String CWL_VERSION_REGEX = "cwlVersion:\\s*\"?(?:cwl:)?([^\\s\"]+)\"?";
@@ -85,25 +84,6 @@ public ROBundle(GitHubService githubService, GithubDetails githubInfo, String co
8584
cwlViewer.setUri(new URI(appURL));
8685
manifest.setCreatedBy(cwlViewer);
8786

88-
// Github author attribution
89-
// TODO: way to add all the contributors somehow
90-
// TODO: set the aggregates details according to the github information
91-
User authorDetails = githubService.getUser(githubInfo.getOwner());
92-
93-
List<Agent> authorList = new ArrayList<>(1);
94-
Agent author = new Agent(authorDetails.getName());
95-
author.setUri(new URI(authorDetails.getHtmlUrl()));
96-
97-
// This tool supports putting your ORCID in the blog field of github as a URL
98-
// eg http://orcid.org/0000-0000-0000-0000
99-
String authorBlog = authorDetails.getBlog();
100-
if (authorBlog != null && authorBlog.startsWith("http://orcid.org/")) {
101-
author.setOrcid(new URI(authorBlog));
102-
}
103-
104-
authorList.add(author);
105-
manifest.setAuthoredBy(authorList);
106-
10787
// Retrieval Info
10888
manifest.setRetrievedBy(cwlViewer);
10989
manifest.setRetrievedOn(manifest.getCreatedOn());
@@ -120,16 +100,18 @@ public ROBundle(GitHubService githubService, GithubDetails githubInfo, String co
120100

121101
// Add the files from the Github repo to this workflow
122102
List<RepositoryContents> repoContents = githubService.getContents(githubInfo);
123-
addFiles(repoContents, bundleFiles, manifest);
103+
addFiles(repoContents, bundleFiles);
104+
105+
// Add combined authors
106+
manifest.setAuthoredBy(new ArrayList<Agent>(authors));
124107
}
125108

126109
/**
127110
* Add files to this bundle from a list of Github repository contents
128111
* @param repoContents The contents of the Github repository
129112
* @param path The path in the Research Object to add the files
130113
*/
131-
private void addFiles(List<RepositoryContents> repoContents, Path path,
132-
Manifest manifest) throws IOException {
114+
private void addFiles(List<RepositoryContents> repoContents, Path path) throws IOException {
133115

134116
// Loop through repo contents and add them
135117
for (RepositoryContents repoContent : repoContents) {
@@ -147,7 +129,7 @@ private void addFiles(List<RepositoryContents> repoContents, Path path,
147129
Files.createDirectory(subdirPath);
148130

149131
// Add the files in the subdirectory to this new folder
150-
addFiles(subdirectory, subdirPath, manifest);
132+
addFiles(subdirectory, subdirPath);
151133

152134
// Otherwise this is a file so add to the bundle
153135
} else if (repoContent.getType().equals("file")) {
@@ -162,7 +144,7 @@ private void addFiles(List<RepositoryContents> repoContents, Path path,
162144
Bundles.setStringValue(newFilePort, fileContent);
163145

164146
// Manifest aggregation
165-
PathMetadata aggregation = manifest.getAggregation(newFilePort);
147+
PathMetadata aggregation = bundle.getManifest().getAggregation(newFilePort);
166148

167149
try {
168150
// Special handling for cwl files
@@ -178,6 +160,11 @@ private void addFiles(List<RepositoryContents> repoContents, Path path,
178160
}
179161
}
180162

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+
181168
// Set retrievedFrom information for this file in the manifest
182169
aggregation.setRetrievedFrom(new URI("https://github.com/" + githubFile.getOwner() + "/" +
183170
githubFile.getRepoName() + "/blob/" + commitSha + "/" + githubFile.getPath()));

src/main/java/org/commonwl/viewer/services/GitHubService.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@
2121

2222
import org.apache.commons.io.IOUtils;
2323
import org.commonwl.viewer.domain.GithubDetails;
24+
import org.commonwl.viewer.domain.HashableAgent;
2425
import org.eclipse.egit.github.core.*;
25-
import org.eclipse.egit.github.core.RepositoryContents;
26-
import org.eclipse.egit.github.core.RepositoryId;
27-
import org.eclipse.egit.github.core.User;
2826
import org.eclipse.egit.github.core.client.GitHubClient;
2927
import org.eclipse.egit.github.core.service.CommitService;
3028
import org.eclipse.egit.github.core.service.ContentsService;
29+
import org.eclipse.egit.github.core.service.RepositoryService;
3130
import org.eclipse.egit.github.core.service.UserService;
3231
import org.springframework.beans.factory.annotation.Autowired;
3332
import org.springframework.beans.factory.annotation.Value;
3433
import org.springframework.stereotype.Service;
3534

3635
import java.io.IOException;
3736
import java.io.InputStream;
37+
import java.net.URI;
38+
import java.net.URISyntaxException;
3839
import java.net.URL;
40+
import java.util.HashSet;
3941
import java.util.List;
42+
import java.util.Set;
4043
import java.util.regex.Matcher;
4144
import java.util.regex.Pattern;
4245

@@ -50,6 +53,7 @@ public class GitHubService {
5053
private final ContentsService contentsService;
5154
private final UserService userService;
5255
private final CommitService commitService;
56+
private final RepositoryService repoService;
5357

5458
// URL validation for directory links
5559
private final String GITHUB_DIR_REGEX = "^https:\\/\\/github\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:tree\\/([^/]+)\\/(.*))?$";
@@ -69,6 +73,7 @@ public GitHubService(@Value("${githubAPI.authentication}") String authSetting,
6973
this.contentsService = new ContentsService(client);
7074
this.userService = new UserService(client);
7175
this.commitService = new CommitService(client);
76+
this.repoService = new RepositoryService(client);
7277
}
7378

7479
/**
@@ -138,4 +143,37 @@ public String getCommitSha(GithubDetails githubInfo) throws IOException {
138143
RepositoryCommit currentCommit = commitService.getCommit(repo, githubInfo.getBranch());
139144
return currentCommit.getSha();
140145
}
146+
147+
/**
148+
*
149+
* Get the contributors to a specific file by their commits
150+
* @param githubInfo The information to access the repository
151+
* @return A list of unique contributors
152+
* @throws IOException Any API errors which may have occurred
153+
* @throws URISyntaxException Any error in the author's URI (should never occur)
154+
*/
155+
public Set<HashableAgent> getContributors(GithubDetails githubInfo, String sha)
156+
throws IOException, URISyntaxException {
157+
RepositoryId repo = new RepositoryId(githubInfo.getOwner(), githubInfo.getRepoName());
158+
Set<HashableAgent> authors = new HashSet<HashableAgent>();
159+
160+
for (RepositoryCommit commit : commitService.getCommits(repo, sha, githubInfo.getPath())) {
161+
User author = commit.getAuthor();
162+
CommitUser commitAuthor = commit.getCommit().getAuthor();
163+
164+
// If there is author information for this commit in some form
165+
if (author != null || commitAuthor != null) {
166+
// Create a new agent and add as much detail as possible
167+
HashableAgent newAgent = new HashableAgent();
168+
if (author != null) {
169+
newAgent.setUri(new URI(author.getHtmlUrl()));
170+
}
171+
if (commitAuthor != null) {
172+
newAgent.setName(commitAuthor.getName());
173+
}
174+
authors.add(newAgent);
175+
}
176+
}
177+
return authors;
178+
}
141179
}

0 commit comments

Comments
 (0)