Skip to content

Commit 88393a3

Browse files
author
Mark Robinson
committed
Fix to support null path values
Closes #81
1 parent bd7c2a0 commit 88393a3

File tree

7 files changed

+45
-14
lines changed

7 files changed

+45
-14
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,24 @@ public String getPath() {
7676
public void setPath(String path) {
7777
this.path = path;
7878
}
79+
80+
/**
81+
* Get the link to the place on github this represents
82+
* @return The Github URL including branch and path if given
83+
*/
84+
public String getURL() {
85+
return getURL(branch);
86+
}
87+
88+
/**
89+
* Get the link to the place on github this represents with set branch name/commit ID
90+
* @return The Github URL including commit ID and path if given
91+
*/
92+
public String getURL(String ref) {
93+
String url = "https://github.com/" + owner + "/" + repoName + "/tree/" + ref;
94+
if (path != null) {
95+
url += "/" + this.path;
96+
}
97+
return url;
98+
}
7999
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public ROBundle(GitHubService githubService, GithubDetails githubInfo, String co
9292
// TODO: Make this importedBy/On/From
9393
manifest.setRetrievedBy(thisApp);
9494
manifest.setRetrievedOn(manifest.getCreatedOn());
95-
manifest.setRetrievedFrom(new URI("https://github.com/" + githubInfo.getOwner() + "/"
96-
+ githubInfo.getRepoName() + "/tree/" + commitSha + "/" + githubInfo.getPath()));
95+
manifest.setRetrievedFrom(new URI(githubInfo.getURL()));
9796

9897
} catch (URISyntaxException ex) {
9998
logger.error("Error creating URI for RO Bundle", ex);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class GitHubService {
5454
private final CommitService commitService;
5555

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

6060
@Autowired

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import org.commonwl.viewer.domain.GithubDetails;
2525
import org.commonwl.viewer.domain.Workflow;
2626
import org.commonwl.viewer.domain.WorkflowForm;
27-
import org.commonwl.viewer.services.WorkflowService;
2827
import org.commonwl.viewer.services.WorkflowFormValidator;
2928
import org.commonwl.viewer.services.WorkflowRepository;
29+
import org.commonwl.viewer.services.WorkflowService;
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232
import org.springframework.beans.factory.annotation.Autowired;
@@ -123,9 +123,13 @@ public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, B
123123

124124
// Redirect to the workflow
125125
GithubDetails githubDetails = workflow.getRetrievedFrom();
126+
String path = githubDetails.getPath();
127+
if (path == null) {
128+
path = "";
129+
}
126130
return new ModelAndView("redirect:/workflows/github.com/" + githubDetails.getOwner()
127131
+ "/" + githubDetails.getRepoName() + "/tree/" + githubDetails.getBranch()
128-
+ "/" + githubDetails.getPath());
132+
+ "/" + path);
129133
}
130134
}
131135

@@ -145,8 +149,12 @@ public ModelAndView getWorkflowByGithubDetails(@Value("${applicationURL}") Strin
145149

146150
// The wildcard end of the URL is the path
147151
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
148-
int pathStartIndex = StringUtils.ordinalIndexOf(path, "/", 7) + 1;
149-
path = path.substring(pathStartIndex);
152+
int pathStartIndex = StringUtils.ordinalIndexOf(path, "/", 7);
153+
if (pathStartIndex > -1 && pathStartIndex < path.length() - 1) {
154+
path = path.substring(pathStartIndex + 1);
155+
} else {
156+
path = null;
157+
}
150158

151159
// Construct a GithubDetails object to search for in the database
152160
GithubDetails githubDetails = new GithubDetails(owner, repoName, branch, path);
@@ -177,6 +185,9 @@ public ModelAndView getWorkflowByGithubDetails(@Value("${applicationURL}") Strin
177185

178186
// Redirect to index with form autofilled if workflow does not already exist
179187
if (workflowModel == null) {
188+
if (path == null) {
189+
path = "";
190+
}
180191
return new ModelAndView("redirect:/?url=https://github.com/" +
181192
owner + "/" + repoName + "/tree/" + branch + "/" + path);
182193
}

src/main/resources/templates/fragments/footer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
distributed under <a href="https://www.apache.org/licenses/LICENSE-2.0" rel="noopener" target="_blank">Apache license, version 2.0</a>
3535
<br />
3636
<span th:if="${workflow != null}">
37-
<a th:href="@{'https://github.com/' + ${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/tree/' + ${workflow.lastCommit} + '/' + ${workflow.retrievedFrom.path}}" href="#" rel="noopener" target="_blank">Shown workflow</a> has separate copyright and license
37+
<a th:href="@{${workflow.retrievedFrom.getURL(workflow.lastCommit)}}" href="#" rel="noopener" target="_blank">Shown workflow</a> has separate copyright and license
3838
</span>
3939
</div>
4040
</div>

src/main/resources/templates/workflow.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ <h4 class="modal-title" id="fullScreenGraphLabel">Workflow Graph</h4>
121121

122122
<div class="container">
123123
<div class="row">
124-
<div class="col-md-12" role="main" id="main" th:with="workflowURL=@{'github.com/' + ${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/tree/' + ${workflow.lastCommit} + '/' + ${workflow.retrievedFrom.path}}">
124+
<div class="col-md-12" role="main" id="main">
125125
<h2>Workflow: <span th:text="${workflow.label}">Workflow Name</span></h2>
126126
<p>
127-
<a th:href="@{'https://' + ${workflowURL}}" href="#" rel="noopener" target="_blank">
127+
<a th:href="@{${workflow.retrievedFrom.getURL(workflow.lastCommit)}}" href="#" rel="noopener" target="_blank">
128128
<img id="githubLogo" src="../static/img/GitHub-Mark-32px.png" th:src="@{/img/GitHub-Mark-32px.png}" width="24" height="24" />
129129
</a>
130130
<i>Fetched <span th:text="${{workflow.retrievedOn}}">01/12/16 at 21:00</span></i>

src/main/resources/templates/workflows.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ <h1>Explore Workflows</h1>
4646
</tr>
4747
</thead>
4848
<tbody>
49-
<tr th:each="workflow : ${workflows}" th:with="workflowURL=@{'/workflows/github.com/' + ${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/tree/' + ${workflow.retrievedFrom.branch} + '/' + ${workflow.retrievedFrom.path}}">
49+
<tr th:each="workflow : ${workflows}" th:with="workflowURL=@{'/workflows/github.com/' + ${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/tree/' + ${workflow.retrievedFrom.branch}}">
5050
<td>
51-
<a th:href="${workflowURL}">
51+
<a th:href="${workflow.retrievedFrom.path == null} ? ${workflowURL} : ${workflowURL + '/' + workflow.retrievedFrom.path}">
5252
<img class="workflow-thumb" th:src="@{'/workflows/' + ${workflow.id} + '/graph/svg'}" alt="workflow graph" />
5353
</a>
5454
</td>
@@ -57,9 +57,10 @@ <h1>Explore Workflows</h1>
5757
<p><i th:text="${workflow.doc}">Description</i></p>
5858
</td>
5959
<td>
60-
<a th:href="@{'https://github.com/' + ${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/tree/' + ${workflow.lastCommit} + '/' + ${workflow.retrievedFrom.path}}" rel="noopener" target="_blank">
60+
<a th:href="@{${workflow.retrievedFrom.getURL(workflow.lastCommit)}}" 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" />
62-
<span th:text="@{${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/' + ${workflow.retrievedFrom.path}}">https://github.com</span>
62+
<span th:if="${workflow.retrievedFrom.path != null}" th:text="@{${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/' + ${workflow.retrievedFrom.path}}">https://github.com</span>
63+
<span th:unless="${workflow.retrievedFrom.path != null}" th:text="@{${workflow.retrievedFrom.owner} + '/' + ${workflow.retrievedFrom.repoName} + '/'}">https://github.com</span>
6364
</a>
6465
<p>Branch/Commit ID: <i th:text="${workflow.retrievedFrom.branch}">master</i></p>
6566
</td>

0 commit comments

Comments
 (0)