Skip to content

Commit fa28092

Browse files
author
Mark Robinson
committed
Improve caching scheme to check for changes to just the workflow
Rather than the whole repository
1 parent 52572cf commit fa28092

File tree

6 files changed

+46
-47
lines changed

6 files changed

+46
-47
lines changed

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

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
package org.commonwl.viewer.github;
2121

2222
import org.apache.commons.io.IOUtils;
23-
import org.commonwl.viewer.researchobject.HashableAgent;
24-
import org.eclipse.egit.github.core.*;
23+
import org.eclipse.egit.github.core.RepositoryCommit;
24+
import org.eclipse.egit.github.core.RepositoryContents;
25+
import org.eclipse.egit.github.core.RepositoryId;
2526
import org.eclipse.egit.github.core.client.GitHubClient;
2627
import org.eclipse.egit.github.core.service.CommitService;
2728
import org.eclipse.egit.github.core.service.ContentsService;
@@ -31,12 +32,8 @@
3132

3233
import java.io.IOException;
3334
import java.io.InputStream;
34-
import java.net.URI;
35-
import java.net.URISyntaxException;
3635
import java.net.URL;
37-
import java.util.HashSet;
3836
import java.util.List;
39-
import java.util.Set;
4037
import java.util.regex.Matcher;
4138
import java.util.regex.Pattern;
4239

@@ -142,34 +139,13 @@ public String getCommitSha(GithubDetails githubInfo) throws IOException {
142139
}
143140

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

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
import org.apache.taverna.robundle.manifest.PathMetadata;
2929
import org.commonwl.viewer.github.GitHubService;
3030
import org.commonwl.viewer.github.GithubDetails;
31+
import org.eclipse.egit.github.core.CommitUser;
32+
import org.eclipse.egit.github.core.RepositoryCommit;
3133
import org.eclipse.egit.github.core.RepositoryContents;
34+
import org.eclipse.egit.github.core.User;
3235
import org.slf4j.Logger;
3336
import org.slf4j.LoggerFactory;
3437

@@ -55,21 +58,23 @@ public class ROBundle {
5558

5659
private Bundle bundle;
5760
private GithubDetails githubInfo;
58-
private String commitSha;
5961
private Agent thisApp;
6062
private int singleFileSizeLimit;
61-
private Set<HashableAgent> authors = new HashSet<HashableAgent>();
63+
private Set<HashableAgent> authors = new HashSet<>();
6264

6365
// Pattern for extracting version from a cwl file
6466
private final String CWL_VERSION_REGEX = "cwlVersion:\\s*\"?(?:cwl:)?([^\\s\"]+)\"?";
6567
private final Pattern cwlVersionPattern = Pattern.compile(CWL_VERSION_REGEX);
6668

6769
/**
6870
* Creates a new research object bundle for a workflow from a Github repository
71+
* @param githubService The service for handling Github functionality
6972
* @param githubInfo The information necessary to access the Github directory associated with the RO
73+
* @param appName The name of the application from properties, for attribution
74+
* @param appURL The URL of the application from properties, for attribution
7075
* @throws IOException Any API errors which may have occurred
7176
*/
72-
public ROBundle(GitHubService githubService, GithubDetails githubInfo, String commitSha,
77+
public ROBundle(GitHubService githubService, GithubDetails githubInfo,
7378
String appName, String appURL, int singleFileSizeLimit) throws IOException {
7479
// File size limits
7580
this.singleFileSizeLimit = singleFileSizeLimit;
@@ -78,7 +83,6 @@ public ROBundle(GitHubService githubService, GithubDetails githubInfo, String co
7883
this.bundle = Bundles.createBundle();
7984
this.githubInfo = githubInfo;
8085
this.githubService = githubService;
81-
this.commitSha = commitSha;
8286

8387
Manifest manifest = bundle.getManifest();
8488

@@ -108,7 +112,7 @@ public ROBundle(GitHubService githubService, GithubDetails githubInfo, String co
108112
addFiles(repoContents, bundleFiles);
109113

110114
// Add combined authors
111-
manifest.setAuthoredBy(new ArrayList<Agent>(authors));
115+
manifest.setAuthoredBy(new ArrayList<>(authors));
112116
}
113117

114118
/**
@@ -140,19 +144,21 @@ private void addFiles(List<RepositoryContents> repoContents, Path path) throws I
140144
} else if (repoContent.getType().equals(GitHubService.TYPE_FILE)) {
141145

142146
try {
147+
GithubDetails githubFile = new GithubDetails(githubInfo.getOwner(),
148+
githubInfo.getRepoName(), githubInfo.getBranch(), repoContent.getPath());
149+
143150
// Where to store the new file in bundle
144151
Path bundleFilePath = path.resolve(repoContent.getName());
145152

146-
// Raw URI of the bundle
147-
GithubDetails githubFile = new GithubDetails(githubInfo.getOwner(),
148-
githubInfo.getRepoName(), githubInfo.getBranch(), repoContent.getPath());
153+
// Get commits
154+
List<RepositoryCommit> commitsOnFile = githubService.getCommits(githubFile);
155+
String commitSha = commitsOnFile.get(0).getSha();
156+
149157
URI rawURI = new URI("https://raw.githubusercontent.com/" + githubFile.getOwner() + "/" +
150158
githubFile.getRepoName() + "/" + commitSha + "/" + githubFile.getPath());
151159

152-
// Variable to store file contents
153-
String fileContent = null;
154-
155160
// Download or externally link if oversized
161+
String fileContent = null;
156162
if (repoContent.getSize() <= singleFileSizeLimit) {
157163
// Get the content of this file from Github
158164
fileContent = githubService.downloadFile(githubFile, commitSha);
@@ -185,7 +191,24 @@ private void addFiles(List<RepositoryContents> repoContents, Path path) throws I
185191
}
186192

187193
// Add authors from github commits to the file
188-
Set<HashableAgent> fileAuthors = githubService.getContributors(githubFile, commitSha);
194+
Set<HashableAgent> fileAuthors = new HashSet<>();
195+
for (RepositoryCommit commit : commitsOnFile) {
196+
User author = commit.getAuthor();
197+
CommitUser commitAuthor = commit.getCommit().getAuthor();
198+
199+
// If there is author information for this commit in some form
200+
if (author != null || commitAuthor != null) {
201+
// Create a new agent and add as much detail as possible
202+
HashableAgent newAgent = new HashableAgent();
203+
if (author != null) {
204+
newAgent.setUri(new URI(author.getHtmlUrl()));
205+
}
206+
if (commitAuthor != null) {
207+
newAgent.setName(commitAuthor.getName());
208+
}
209+
fileAuthors.add(newAgent);
210+
}
211+
}
189212
authors.addAll(fileAuthors);
190213
aggregation.setAuthoredBy(new ArrayList<Agent>(fileAuthors));
191214

src/main/java/org/commonwl/viewer/researchobject/ROBundleFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public ROBundleFactory(@Value("${applicationName}") String applicationName,
7474
* @throws IOException Any API errors which may have occurred
7575
*/
7676
@Async
77-
public void workflowROFromGithub(GitHubService githubService, GithubDetails githubInfo, String commitSha)
77+
public void workflowROFromGithub(GitHubService githubService, GithubDetails githubInfo)
7878
throws IOException, InterruptedException {
7979
logger.info("Creating Research Object Bundle");
8080

@@ -83,7 +83,7 @@ public void workflowROFromGithub(GitHubService githubService, GithubDetails gith
8383
githubInfo.getBranch(), FilenameUtils.getPath(githubInfo.getPath()));
8484

8585
// Create a new Research Object Bundle with Github contents
86-
ROBundle bundle = new ROBundle(githubService, roDetails, commitSha,
86+
ROBundle bundle = new ROBundle(githubService, roDetails,
8787
applicationName, applicationURL, singleFileSizeLimit);
8888

8989
// Save the bundle to the storage location in properties

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public Workflow createWorkflow(GithubDetails githubInfo) {
187187
*/
188188
private void generateROBundle(Workflow workflow) {
189189
try {
190-
ROBundleFactory.workflowROFromGithub(githubService, workflow.getRetrievedFrom(), workflow.getLastCommit());
190+
ROBundleFactory.workflowROFromGithub(githubService, workflow.getRetrievedFrom());
191191
} catch (Exception ex) {
192192
logger.error("Error creating RO Bundle", ex);
193193
}

src/main/resources/templates/workflow.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ <h4 class="modal-title" id="fullScreenGraphLabel">Workflow Graph</h4>
126126
<div class="col-md-12" role="main" id="main">
127127
<h2>Workflow: <span th:text="${workflow.label}">Workflow Name</span></h2>
128128
<p>
129-
<a th:href="@{${workflow.retrievedFrom.getURL(workflow.lastCommit)}}" href="#" rel="noopener" target="_blank">
129+
<a th:href="@{${workflow.retrievedFrom.getURL()}}" href="#" rel="noopener" target="_blank">
130130
<img id="githubLogo" src="../static/img/GitHub-Mark-32px.png" th:src="@{/img/GitHub-Mark-32px.png}" width="24" height="24" />
131131
</a>
132132
<i>Fetched <span th:text="${{workflow.retrievedOn}}">01/12/16 at 21:00</span></i>

src/main/resources/templates/workflows.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ <h1>Explore Workflows</h1>
5757
<p><i th:text="${workflow.doc}">Description</i></p>
5858
</td>
5959
<td>
60-
<a th:href="@{${workflow.retrievedFrom.getURL(workflow.lastCommit)}}" rel="noopener" target="_blank">
60+
<a th:href="@{${workflow.retrievedFrom.getURL()}}" rel="noopener" target="_blank">
6161
<img id="githubLogo" src="../static/img/GitHub-Mark-32px.png" th:src="@{/img/GitHub-Mark-32px.png}" width="20" height="20" />
6262
<span th:if="${workflow.retrievedFrom.path != null}" th:text="@{${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/' + ${workflow.retrievedFrom.path}}">https://github.com</span>
6363
<span th:unless="${workflow.retrievedFrom.path != null}" th:text="@{${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/'}">https://github.com</span>

0 commit comments

Comments
 (0)