Skip to content

Commit bdafc49

Browse files
author
Mark Robinson
committed
Change to only support direct links to workflows and spider content
1 parent b02a6f1 commit bdafc49

File tree

9 files changed

+192
-290
lines changed

9 files changed

+192
-290
lines changed

src/main/java/org/commonwl/viewer/domain/cwl/CWLCollection.java renamed to src/main/java/org/commonwl/viewer/services/CWLService.java

Lines changed: 125 additions & 159 deletions
Large diffs are not rendered by default.

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public class GitHubService {
5656
private final CommitService commitService;
5757

5858
// URL validation for directory links
59-
private final String GITHUB_DIR_REGEX = "^https?:\\/\\/github\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:(?:tree|blob)\\/([^/]+)(?:\\/(.*))?)?$";
60-
private final Pattern githubDirPattern = Pattern.compile(GITHUB_DIR_REGEX);
59+
private final String GITHUB_CWL_REGEX = "^https?:\\/\\/github\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:tree|blob)\\/([^/]+)(?:\\/(.+\\.cwl))$";
60+
private final Pattern githubCwlPattern = Pattern.compile(GITHUB_CWL_REGEX);
6161

6262
@Autowired
6363
public GitHubService(@Value("${githubAPI.authentication}") String authSetting,
@@ -75,12 +75,12 @@ public GitHubService(@Value("${githubAPI.authentication}") String authSetting,
7575
}
7676

7777
/**
78-
* Extract the details of a Github directory URL using a regular expression
79-
* @param url The Github directory URL
78+
* Extract the details of a Github cwl file URL using a regular expression
79+
* @param url The Github URL to a cwl file
8080
* @return A list with the groups of the regex match, [owner, repo, branch, path]
8181
*/
82-
public GithubDetails detailsFromDirURL(String url) {
83-
Matcher m = githubDirPattern.matcher(url);
82+
public GithubDetails detailsFromCwlURL(String url) {
83+
Matcher m = githubCwlPattern.matcher(url);
8484
if (m.find()) {
8585
return new GithubDetails(m.group(1), m.group(2), m.group(3), m.group(4));
8686
}
@@ -120,20 +120,28 @@ public String downloadFile(GithubDetails githubInfo, String sha) throws IOExcept
120120
}
121121

122122
/**
123-
* Gets the latest commit sha hash from a particular branch
123+
* Download a single file from a repository
124+
* @param githubInfo The information to access the repository
125+
* @return A string with the contents of the file
126+
* @throws IOException Any API errors which may have occurred
127+
*/
128+
public String downloadFile(GithubDetails githubInfo) throws IOException {
129+
return downloadFile(githubInfo, githubInfo.getBranch());
130+
}
131+
132+
/**
133+
* Gets the latest commit sha hash for a file on a branch
124134
* If hash is given as a branch, this will return the same hash if valid
125135
* @param githubInfo The information to access the repository
126-
* @return A sha hash for the latest commit
136+
* @return A sha hash for the latest commit on a file
127137
* @throws IOException Any API errors which may have occurred
128138
*/
129139
public String getCommitSha(GithubDetails githubInfo) throws IOException {
130140
RepositoryId repo = new RepositoryId(githubInfo.getOwner(), githubInfo.getRepoName());
131-
RepositoryCommit currentCommit = commitService.getCommit(repo, githubInfo.getBranch());
132-
return currentCommit.getSha();
141+
return commitService.getCommits(repo, githubInfo.getBranch(), githubInfo.getPath()).get(0).getSha();
133142
}
134143

135144
/**
136-
*
137145
* Get the contributors to a specific file by their commits
138146
* @param githubInfo The information to access the repository
139147
* @return A list of unique contributors

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

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.commonwl.viewer.domain.GithubDetails;
2323
import org.commonwl.viewer.domain.WorkflowForm;
24-
import org.eclipse.egit.github.core.RepositoryContents;
2524
import org.slf4j.Logger;
2625
import org.slf4j.LoggerFactory;
2726
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,7 +29,6 @@
3029
import org.springframework.validation.ValidationUtils;
3130

3231
import java.io.IOException;
33-
import java.util.List;
3432

3533
/**
3634
* Runs validation on the workflow form from the main page
@@ -51,7 +49,7 @@ public WorkflowFormValidator(GitHubService githubService) {
5149
}
5250

5351
/**
54-
* Validates a WorkflowForm to ensure the URL is not empty and directory contains cwl files
52+
* Validates a WorkflowForm to ensure the URL is not empty and links to a cwl file
5553
* @param form The given WorkflowForm
5654
* @param e Any errors from validation
5755
*/
@@ -60,30 +58,23 @@ public GithubDetails validateAndParse(WorkflowForm form, Errors e) {
6058

6159
// Only continue if not null and isn't just whitespace
6260
if (!e.hasErrors()) {
63-
GithubDetails githubInfo = githubService.detailsFromDirURL(form.getGithubURL());
61+
GithubDetails githubInfo = githubService.detailsFromCwlURL(form.getGithubURL());
6462

6563
// If the URL is valid and details could be extracted
6664
if (githubInfo != null) {
6765

6866
// Check the repository exists and get content to ensure that branch/path exist
6967
try {
70-
// Return the Github information if it is valid
71-
List<RepositoryContents> repoContents = githubService.getContents(githubInfo);
72-
if (containsCWL(repoContents, githubInfo)) {
68+
// Downloads the workflow file to check for existence
69+
if (githubService.downloadFile(githubInfo) != null) {
7370
return githubInfo;
74-
} else {
75-
// The URL does not contain any .cwl files
76-
logger.error("No .cwl files found at Github URL");
77-
e.rejectValue("githubURL", "githubURL.missingWorkflow", "No .cwl files were found in the specified Github directory");
7871
}
7972
} catch (IOException ex) {
80-
// Given repository/branch/path does not exist or API error occured
81-
logger.error("Github API Error", ex);
82-
e.rejectValue("githubURL", "githubURL.apiError", "API Error - does the specified Github directory exist?");
73+
logger.error("Given URL " + form.getGithubURL() + " was not a .cwl file");
74+
e.rejectValue("githubURL", "githubURL.missingWorkflow", "Workflow was not found at the given Github URL");
8375
}
8476
} else {
85-
// The Github URL is not valid
86-
logger.error("The Github URL is not valid");
77+
logger.error("The Github URL " + form.getGithubURL() + " is not valid");
8778
e.rejectValue("githubURL", "githubURL.invalid");
8879
}
8980
} else {
@@ -93,34 +84,4 @@ public GithubDetails validateAndParse(WorkflowForm form, Errors e) {
9384
// Errors will stop this being used anyway
9485
return null;
9586
}
96-
97-
/**
98-
* Recursively check for CWL files within a Github repository
99-
* @param repoContents A list of contents within a path in the repository
100-
* @param githubDetails The details of the repository
101-
* @return Whether the path contains CWL files
102-
* @throws IOException Any API errors which may have occurred
103-
*/
104-
private boolean containsCWL(List<RepositoryContents> repoContents, GithubDetails githubDetails) throws IOException {
105-
// Check that there is at least 1 .cwl file
106-
for (RepositoryContents repoContent : repoContents) {
107-
if (repoContent.getType().equals(GitHubService.TYPE_DIR)) {
108-
GithubDetails githubSubdir = new GithubDetails(githubDetails.getOwner(),
109-
githubDetails.getRepoName(), githubDetails.getBranch(), repoContent.getPath());
110-
List<RepositoryContents> subdirectory = githubService.getContents(githubSubdir);
111-
if (containsCWL(subdirectory, githubSubdir)) {
112-
return true;
113-
}
114-
} else if (repoContent.getType().equals(GitHubService.TYPE_FILE)) {
115-
int eIndex = repoContent.getName().lastIndexOf('.') + 1;
116-
if (eIndex > 0) {
117-
String extension = repoContent.getName().substring(eIndex);
118-
if (extension.equals("cwl")) {
119-
return true;
120-
}
121-
}
122-
}
123-
}
124-
return false;
125-
}
12687
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ public interface WorkflowRepository extends PagingAndSortingRepository<Workflow,
3737
*/
3838
Workflow findByRetrievedFrom(GithubDetails retrievedFrom);
3939

40-
/**
41-
* Finds a workflow model in the database based on which repository it is from
42-
* @param owner The owner of the repository
43-
* @param repoName The name of the repository
44-
* @param branch The branch or commit ID of the repository
45-
* @return The workflow model
46-
*/
47-
Workflow findByRetrievedFromOwnerAndRetrievedFromRepoNameAndRetrievedFromBranch(String owner, String repoName, String branch);
48-
4940
/**
5041
* Paged request to get all workflows
5142
* @param pageable The details of the page to be retrieved

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

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.commonwl.viewer.domain.GithubDetails;
2323
import org.commonwl.viewer.domain.Workflow;
24-
import org.commonwl.viewer.domain.cwl.CWLCollection;
2524
import org.commonwl.viewer.web.ROBundleNotFoundException;
2625
import org.slf4j.Logger;
2726
import org.slf4j.LoggerFactory;
@@ -41,28 +40,25 @@ public class WorkflowService {
4140
private final Logger logger = LoggerFactory.getLogger(this.getClass());
4241

4342
private final GitHubService githubService;
43+
private final CWLService cwlService;
4444
private final WorkflowRepository workflowRepository;
4545
private final ROBundleFactory ROBundleFactory;
4646
private final GraphVizService graphVizService;
4747
private final int cacheDays;
48-
private final int singleFileSizeLimit;
49-
private final int totalFileSizeLimit;
5048

5149
@Autowired
5250
public WorkflowService(GitHubService githubService,
51+
CWLService cwlService,
5352
WorkflowRepository workflowRepository,
5453
ROBundleFactory ROBundleFactory,
5554
GraphVizService graphVizService,
56-
@Value("${cacheDays}") int cacheDays,
57-
@Value("${singleFileSizeLimit}") int singleFileSizeLimit,
58-
@Value("${totalFileSizeLimit}") int totalFileSizeLimit) {
55+
@Value("${cacheDays}") int cacheDays) {
5956
this.githubService = githubService;
57+
this.cwlService = cwlService;
6058
this.workflowRepository = workflowRepository;
6159
this.ROBundleFactory = ROBundleFactory;
6260
this.graphVizService = graphVizService;
6361
this.cacheDays = cacheDays;
64-
this.singleFileSizeLimit = singleFileSizeLimit;
65-
this.totalFileSizeLimit = totalFileSizeLimit;
6662
}
6763

6864
/**
@@ -145,7 +141,7 @@ public File getROBundle(String id) throws ROBundleNotFoundException {
145141
}
146142

147143
/**
148-
* Builds a new workflow from cwl files fetched from Github
144+
* Builds a new workflow from Github
149145
* @param githubInfo Github information for the workflow
150146
* @return The constructed model for the Workflow
151147
*/
@@ -154,12 +150,9 @@ public Workflow createWorkflow(GithubDetails githubInfo) {
154150
// Get the sha hash from a branch reference
155151
String latestCommit = githubService.getCommitSha(githubInfo);
156152

157-
// Set up CWL utility to collect the documents
158-
CWLCollection cwlFiles = new CWLCollection(githubService, githubInfo, latestCommit,
159-
singleFileSizeLimit, totalFileSizeLimit);
153+
// Get the workflow model using the cwl service
154+
Workflow workflowModel = cwlService.parseWorkflow(githubInfo, latestCommit);
160155

161-
// Get the workflow model
162-
Workflow workflowModel = cwlFiles.getWorkflow();
163156
if (workflowModel != null) {
164157
// Set origin details
165158
workflowModel.setRetrievedOn(new Date());
@@ -256,14 +249,4 @@ private boolean cacheExpired(Workflow workflow) {
256249
return false;
257250
}
258251
}
259-
260-
/**
261-
* Check for the repository already being parsed
262-
* @param details The details of the repository on Github
263-
* @return Whether the workflows from the repository have already been added
264-
*/
265-
private boolean repoAlreadyParsed(GithubDetails details) {
266-
return (workflowRepository.findByRetrievedFromOwnerAndRetrievedFromRepoNameAndRetrievedFromBranch(
267-
details.getOwner(), details.getRepoName(), details.getBranch()) != null);
268-
}
269252
}

src/main/java/org/commonwl/viewer/web/WorkflowController.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,9 @@ public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, B
115115

116116
// Redirect to the workflow
117117
GithubDetails githubDetails = workflow.getRetrievedFrom();
118-
String path = githubDetails.getPath();
119-
if (path == null) {
120-
path = "";
121-
}
122118
return new ModelAndView("redirect:/workflows/github.com/" + githubDetails.getOwner()
123119
+ "/" + githubDetails.getRepoName() + "/tree/" + githubDetails.getBranch()
124-
+ "/" + path);
120+
+ "/" + githubDetails.getPath());
125121
}
126122
}
127123

@@ -183,8 +179,6 @@ public ModelAndView getWorkflowByGithubDetails(@Value("${applicationURL}") Strin
183179
public FileSystemResource downloadROBundle(@PathVariable("workflowID") String workflowID,
184180
HttpServletResponse response) {
185181
File bundleDownload = workflowService.getROBundle(workflowID);
186-
187-
logger.info("Serving download for workflow " + workflowID + " [" + bundleDownload.toString() + "]");
188182
response.setHeader("Content-Disposition", "attachment; filename=bundle.zip;");
189183
return new FileSystemResource(bundleDownload);
190184
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Errors from validation of a github URL
22
githubURL.emptyOrWhitespace = URL may not be empty
3-
githubURL.invalid = You must enter a valid Github directory URL
4-
githubURL.apiError = API Error - does the specified Github directory exist?
5-
githubURL.missingWorkflow = No .cwl files were found in the specified Github directory
3+
githubURL.invalid = You must enter a valid Github URL to a .cwl file
4+
githubURL.missingWorkflow = Workflow was not found at the given Github URL
65
githubURL.parsingError = The workflow could not be parsed from the given URL

src/main/resources/templates/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<div class="col-md-12" role="main" id="main">
3636
<h1>Common Workflow Language Viewer</h1>
3737
<p>This tool visualises and lists the details of a workflow with its inputs, outputs and steps and
38-
packages the files into a downloadable
38+
packages the files involved into a downloadable
3939
<a href="https://researchobject.github.io/specifications/bundle/" rel="noopener" target="_blank">
4040
Research Object Bundle</a>
4141
(zip file with metadata in a manifest), allowing it to be easily viewed and shared.</p>
@@ -44,7 +44,7 @@ <h1>Common Workflow Language Viewer</h1>
4444
</div>
4545

4646
<h2>Workflow from Github URL</h2>
47-
<p>Provide a link to the directory of files making up your workflow here</p>
47+
<p>Provide a Github link to the workflow here</p>
4848
<form id="add" action="#" th:action="@{/}" th:object="${workflowForm}" method="POST">
4949
<div class="alert alert-danger" th:if="${errors} != null and ${errors.hasErrors()}">
5050
<div th:each="error : ${errors.getAllErrors()}">
@@ -56,10 +56,10 @@ <h2>Workflow from Github URL</h2>
5656
</div>
5757
<div class="alert alert-grey">
5858
<strong>Don't know what to view?</strong> Try these from <i>common-workflow-language/workflows</i>:
59-
<a class="example" href="https://github.com/common-workflow-language/workflows/tree/master/workflows/compile">compile</a>,
60-
<a class="example" href="https://github.com/common-workflow-language/workflows/tree/master/workflows/make-to-cwl">make-to-cwl</a>,
61-
<a class="example" href="https://github.com/common-workflow-language/workflows/tree/master/workflows/lobSTR">lobSTR</a>,
62-
<a class="example" href="https://github.com/common-workflow-language/workflows/tree/master/workflows/scidap">scidap</a>
59+
<a class="example" href="https://github.com/common-workflow-language/workflows/tree/master/workflows/compile/compile1.cwl">compile</a>,
60+
<a class="example" href="https://github.com/common-workflow-language/workflows/tree/master/workflows/make-to-cwl/dna.cwl">make-to-cwl</a>,
61+
<a class="example" href="https://github.com/common-workflow-language/workflows/tree/master/workflows/lobSTR/lobSTR-workflow.cwl">lobSTR</a>,
62+
<a class="example" href="https://github.com/common-workflow-language/workflows/tree/master/workflows/scidap/bam-genomecov-bigwig-rna-dutp.cwl">scidap</a>
6363
or <a href="/workflows">explore the collection</a>
6464
</div>
6565
<div class="input-group">

0 commit comments

Comments
 (0)