Skip to content

Commit c8346bb

Browse files
committed
Gitlab support
1 parent 7d9cd28 commit c8346bb

File tree

11 files changed

+110
-65
lines changed

11 files changed

+110
-65
lines changed

src/main/java/org/commonwl/view/git/GitDetails.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package org.commonwl.view.git;
2121

2222
import java.io.Serializable;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
2325

2426
/**
2527
* Represents all the parameters necessary to access a file/directory with Git
@@ -29,9 +31,8 @@ public class GitDetails implements Serializable {
2931
private String repoUrl;
3032
private String branch;
3133
private String path;
32-
private GitType type;
3334

34-
public GitDetails(String repoUrl, String branch, String path, GitType type) {
35+
public GitDetails(String repoUrl, String branch, String path) {
3536
this.repoUrl = repoUrl;
3637

3738
// Default to the master branch
@@ -48,8 +49,6 @@ public GitDetails(String repoUrl, String branch, String path, GitType type) {
4849
} else {
4950
this.path = path;
5051
}
51-
52-
this.type = type;
5352
}
5453

5554

@@ -77,12 +76,28 @@ public void setPath(String path) {
7776
this.path = path;
7877
}
7978

79+
/**
80+
* Get the type of a repository URL this object refers to
81+
* @return The type for the URL
82+
*/
8083
public GitType getType() {
81-
return type;
82-
}
83-
84-
public void setType(GitType type) {
85-
this.type = type;
84+
try {
85+
URI uri = new URI(repoUrl);
86+
String domain = uri.getHost();
87+
if (domain.startsWith("www.")) {
88+
domain = domain.substring(4);
89+
}
90+
switch (domain) {
91+
case "github.com":
92+
return GitType.GITHUB;
93+
case "gitlab.com":
94+
return GitType.GITLAB;
95+
default:
96+
return GitType.GENERIC;
97+
}
98+
} catch (URISyntaxException ex) {
99+
return GitType.GENERIC;
100+
}
86101
}
87102

88103
/**
@@ -91,7 +106,7 @@ public void setType(GitType type) {
91106
* @return The URL
92107
*/
93108
public String getUrl(String branchOverride) {
94-
switch (this.type) {
109+
switch (getType()) {
95110
case GENERIC:
96111
return repoUrl;
97112
case GITHUB:
@@ -115,7 +130,7 @@ public String getUrl() {
115130
* @return The URL
116131
*/
117132
public String getInternalUrl() {
118-
switch (this.type) {
133+
switch (getType()) {
119134
case GENERIC:
120135
return "/workflows/" + normaliseUrl(repoUrl) + "/" + branch + "/" + path;
121136
case GITHUB:
@@ -131,7 +146,7 @@ public String getInternalUrl() {
131146
* @return The URL
132147
*/
133148
public String getRawUrl() {
134-
switch (this.type) {
149+
switch (getType()) {
135150
case GENERIC:
136151
return repoUrl;
137152
case GITHUB:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void workflowROFromGithub(Workflow workflow)
7070
// Get the whole containing folder, not just the workflow itself
7171
GitDetails githubInfo = workflow.getRetrievedFrom();
7272
GitDetails roDetails = new GitDetails(githubInfo.getRepoUrl(), githubInfo.getBranch(),
73-
FilenameUtils.getPath(githubInfo.getPath()), githubInfo.getType());
73+
FilenameUtils.getPath(githubInfo.getPath()));
7474

7575
// Create a new Research Object Bundle with Github contents
7676
Bundle bundle = roBundleService.createBundle(workflow, roDetails);

src/main/java/org/commonwl/view/workflow/WorkflowController.java

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.commonwl.view.cwl.CWLToolStatus;
2424
import org.commonwl.view.cwl.CWLValidationException;
2525
import org.commonwl.view.git.GitDetails;
26-
import org.commonwl.view.git.GitType;
2726
import org.commonwl.view.graphviz.GraphVizService;
2827
import org.eclipse.jgit.api.errors.GitAPIException;
2928
import org.slf4j.Logger;
@@ -139,15 +138,17 @@ public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, B
139138
}
140139

141140
/**
142-
* Display a page for a particular workflow from Github details
143-
* @param owner The owner of the Github repository
141+
* Display a page for a particular workflow from Github or Github format URL
142+
* @param domain The domain of the hosting site, Github or Gitlab
143+
* @param owner The owner of the repository
144144
* @param repoName The name of the repository
145145
* @param branch The branch of repository
146146
* @return The workflow view with the workflow as a model
147147
*/
148-
@GetMapping(value={"/workflows/github.com/{owner}/{repoName}/tree/{branch}/**",
149-
"/workflows/github.com/{owner}/{repoName}/blob/{branch}/**"})
148+
@GetMapping(value={"/workflows/{domain}.com/{owner}/{repoName}/tree/{branch}/**",
149+
"/workflows/{domain}.com/{owner}/{repoName}/blob/{branch}/**"})
150150
public ModelAndView getWorkflowByGithubDetails(@Value("${applicationURL}") String applicationURL,
151+
@PathVariable("domain") String domain,
151152
@PathVariable("owner") String owner,
152153
@PathVariable("repoName") String repoName,
153154
@PathVariable("branch") String branch,
@@ -159,8 +160,7 @@ public ModelAndView getWorkflowByGithubDetails(@Value("${applicationURL}") Strin
159160
path = extractPath(path, 7);
160161

161162
// Construct a GitDetails object to search for in the database
162-
GitDetails gitDetails = new GitDetails("https://github.com/" + owner + "/" +
163-
repoName + ".git", branch, path, GitType.GITHUB);
163+
GitDetails gitDetails = getGitDetails(domain, owner, repoName, branch, path);
164164

165165
// Get workflow
166166
QueuedWorkflow queued = null;
@@ -210,23 +210,24 @@ public ModelAndView getWorkflowByGithubDetails(@Value("${applicationURL}") Strin
210210

211211
/**
212212
* Download the Research Object Bundle for a particular workflow
213-
* @param owner The owner of the Github repository
213+
* @param domain The domain of the hosting site, Github or Gitlab
214+
* @param owner The owner of the repository
214215
* @param repoName The name of the repository
215216
* @param branch The branch of repository
216217
*/
217-
@GetMapping(value={"/robundle/github.com/{owner}/{repoName}/tree/{branch}/**",
218-
"/robundle/github.com/{owner}/{repoName}/blob/{branch}/**"},
218+
@GetMapping(value={"/robundle/{domain}.com/{owner}/{repoName}/tree/{branch}/**",
219+
"/robundle/{domain}.com/{owner}/{repoName}/blob/{branch}/**"},
219220
produces = "application/vnd.wf4ever.robundle+zip")
220221
@ResponseBody
221-
public FileSystemResource downloadROBundle(@PathVariable("owner") String owner,
222-
@PathVariable("repoName") String repoName,
223-
@PathVariable("branch") String branch,
224-
HttpServletRequest request,
225-
HttpServletResponse response) throws IOException {
222+
public FileSystemResource downloadROBundle(@PathVariable("domain") String domain,
223+
@PathVariable("owner") String owner,
224+
@PathVariable("repoName") String repoName,
225+
@PathVariable("branch") String branch,
226+
HttpServletRequest request,
227+
HttpServletResponse response) throws IOException {
226228
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
227229
path = extractPath(path, 7);
228-
GitDetails gitDetails = new GitDetails("https://github.com/" + owner + "/" +
229-
repoName + ".git", branch, path, GitType.GITHUB);
230+
GitDetails gitDetails = getGitDetails(domain, owner, repoName, branch, path);
230231
File bundleDownload = workflowService.getROBundle(gitDetails);
231232
response.setHeader("Content-Disposition", "attachment; filename=bundle.zip;");
232233
return new FileSystemResource(bundleDownload);
@@ -235,23 +236,24 @@ public FileSystemResource downloadROBundle(@PathVariable("owner") String owner,
235236

236237
/**
237238
* Download a generated graph for a workflow as an svg
238-
* @param owner The owner of the Github repository
239+
* @param domain The domain of the hosting site, Github or Gitlab
240+
* @param owner The owner of the repository
239241
* @param repoName The name of the repository
240242
* @param branch The branch of repository
241243
*/
242-
@GetMapping(value={"/graph/svg/github.com/{owner}/{repoName}/tree/{branch}/**",
243-
"/graph/svg/github.com/{owner}/{repoName}/blob/{branch}/**"},
244+
@GetMapping(value={"/graph/svg/{domain}.com/{owner}/{repoName}/tree/{branch}/**",
245+
"/graph/svg/{domain}.com/{owner}/{repoName}/blob/{branch}/**"},
244246
produces = "image/svg+xml")
245247
@ResponseBody
246-
public FileSystemResource getGraphAsSvg(@PathVariable("owner") String owner,
248+
public FileSystemResource getGraphAsSvg(@PathVariable("domain") String domain,
249+
@PathVariable("owner") String owner,
247250
@PathVariable("repoName") String repoName,
248251
@PathVariable("branch") String branch,
249252
HttpServletRequest request,
250253
HttpServletResponse response) throws IOException {
251254
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
252255
path = extractPath(path, 8);
253-
GitDetails gitDetails = new GitDetails("https://github.com/" + owner + "/" +
254-
repoName + ".git", branch, path, GitType.GITHUB);
256+
GitDetails gitDetails = getGitDetails(domain, owner, repoName, branch, path);
255257
Workflow workflow = workflowService.getWorkflow(gitDetails);
256258
if (workflow == null) {
257259
throw new WorkflowNotFoundException();
@@ -263,23 +265,24 @@ public FileSystemResource getGraphAsSvg(@PathVariable("owner") String owner,
263265

264266
/**
265267
* Download a generated DOT graph for a workflow as a png
266-
* @param owner The owner of the Github repository
268+
* @param domain The domain of the hosting site, Github or Gitlab
269+
* @param owner The owner of the repository
267270
* @param repoName The name of the repository
268271
* @param branch The branch of repository
269272
*/
270-
@GetMapping(value={"/graph/png/github.com/{owner}/{repoName}/tree/{branch}/**",
271-
"/graph/png/github.com/{owner}/{repoName}/blob/{branch}/**"},
273+
@GetMapping(value={"/graph/png/{domain}.com/{owner}/{repoName}/tree/{branch}/**",
274+
"/graph/png/{domain}.com/{owner}/{repoName}/blob/{branch}/**"},
272275
produces = "image/png")
273276
@ResponseBody
274-
public FileSystemResource getGraphAsPng(@PathVariable("owner") String owner,
277+
public FileSystemResource getGraphAsPng(@PathVariable("domain") String domain,
278+
@PathVariable("owner") String owner,
275279
@PathVariable("repoName") String repoName,
276280
@PathVariable("branch") String branch,
277281
HttpServletRequest request,
278282
HttpServletResponse response) throws IOException {
279283
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
280284
path = extractPath(path, 8);
281-
GitDetails gitDetails = new GitDetails("https://github.com/" + owner + "/" +
282-
repoName + ".git", branch, path, GitType.GITHUB);
285+
GitDetails gitDetails = getGitDetails(domain, owner, repoName, branch, path);
283286
Workflow workflow = workflowService.getWorkflow(gitDetails);
284287
if (workflow == null) {
285288
throw new WorkflowNotFoundException();
@@ -291,23 +294,24 @@ public FileSystemResource getGraphAsPng(@PathVariable("owner") String owner,
291294

292295
/**
293296
* Download a generated DOT graph for a workflow as xdot
294-
* @param owner The owner of the Github repository
297+
* @param domain The domain of the hosting site, Github or Gitlab
298+
* @param owner The owner of the repository
295299
* @param repoName The name of the repository
296300
* @param branch The branch of repository
297301
*/
298-
@GetMapping(value={"/graph/xdot/github.com/{owner}/{repoName}/tree/{branch}/**",
299-
"/graph/xdot/github.com/{owner}/{repoName}/blob/{branch}/**"},
302+
@GetMapping(value={"/graph/xdot/{domain}.com/{owner}/{repoName}/tree/{branch}/**",
303+
"/graph/xdot/{domain}.com/{owner}/{repoName}/blob/{branch}/**"},
300304
produces = "text/vnd.graphviz")
301305
@ResponseBody
302-
public FileSystemResource getGraphAsXdot(@PathVariable("owner") String owner,
306+
public FileSystemResource getGraphAsXdot(@PathVariable("domain") String domain,
307+
@PathVariable("owner") String owner,
303308
@PathVariable("repoName") String repoName,
304309
@PathVariable("branch") String branch,
305310
HttpServletRequest request,
306311
HttpServletResponse response) throws IOException {
307312
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
308313
path = extractPath(path, 8);
309-
GitDetails gitDetails = new GitDetails("https://github.com/" + owner + "/" +
310-
repoName + ".git", branch, path, GitType.GITHUB);
314+
GitDetails gitDetails = getGitDetails(domain, owner, repoName, branch, path);
311315
Workflow workflow = workflowService.getWorkflow(gitDetails);
312316
if (workflow == null) {
313317
throw new WorkflowNotFoundException();
@@ -352,4 +356,29 @@ public static String extractPath(String path, int startSlashNum) {
352356
return "/";
353357
}
354358
}
359+
360+
/**
361+
* Constructs a GitDetails object for Github/Gitlab details
362+
* @param domain The domain name (always .com)
363+
* @param owner The owner of the repository
364+
* @param repoName The name of the repository
365+
* @param branch The branch of the repository
366+
* @param path The path within the repository
367+
* @return A constructed GitDetails object
368+
*/
369+
public static GitDetails getGitDetails(String domain, String owner, String repoName,
370+
String branch, String path) {
371+
String repoUrl;
372+
switch (domain) {
373+
case "github":
374+
repoUrl = "https://github.com/" + owner + "/" + repoName + ".git";
375+
break;
376+
case "gitlab":
377+
repoUrl = "https://gitlab.com/" + owner + "/" + repoName + ".git";
378+
break;
379+
default:
380+
throw new WorkflowNotFoundException();
381+
}
382+
return new GitDetails(repoUrl, branch, path);
383+
}
355384
}

src/main/java/org/commonwl/view/workflow/WorkflowFormValidator.java

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

2222
import org.commonwl.view.git.GitDetails;
2323
import org.commonwl.view.git.GitService;
24-
import org.commonwl.view.git.GitType;
2524
import org.slf4j.Logger;
2625
import org.slf4j.LoggerFactory;
2726
import org.springframework.beans.factory.annotation.Autowired;
@@ -80,22 +79,22 @@ public GitDetails validateAndParse(WorkflowForm form, Errors e) {
8079
Matcher m = githubCwlPattern.matcher(form.getUrl());
8180
if (m.find()) {
8281
String repoUrl = "https://github.com/" + m.group(1) + "/" + m.group(2) + ".git";
83-
return new GitDetails(repoUrl, m.group(3), m.group(4), GitType.GITHUB);
82+
return new GitDetails(repoUrl, m.group(3), m.group(4));
8483
}
8584

8685
// Gitlab URL
8786
m = gitlabCwlPattern.matcher(form.getUrl());
8887
if (m.find()) {
8988
String repoUrl = "https://gitlab.com/" + m.group(1) + "/" + m.group(2) + ".git";
90-
return new GitDetails(repoUrl, m.group(3), m.group(4), GitType.GITLAB);
89+
return new GitDetails(repoUrl, m.group(3), m.group(4));
9190
}
9291

9392
// General Git details if didn't match the above
9493
ValidationUtils.rejectIfEmptyOrWhitespace(e, "branch", "branch.emptyOrWhitespace");
9594
ValidationUtils.rejectIfEmptyOrWhitespace(e, "path", "path.emptyOrWhitespace");
9695
m = gitRepoPattern.matcher(form.getUrl());
9796
if (m.find()) {
98-
return new GitDetails(form.getUrl(), form.getBranch(), form.getPath(), GitType.GENERIC);
97+
return new GitDetails(form.getUrl(), form.getBranch(), form.getPath());
9998
}
10099

101100
}

src/main/java/org/commonwl/view/workflow/WorkflowRESTController.java

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

2222
import org.commonwl.view.cwl.CWLValidationException;
2323
import org.commonwl.view.git.GitDetails;
24-
import org.commonwl.view.git.GitType;
2524
import org.slf4j.Logger;
2625
import org.slf4j.LoggerFactory;
2726
import org.springframework.beans.factory.annotation.Autowired;
@@ -141,15 +140,17 @@ public ResponseEntity<?> newWorkflowFromGithubURLJson(@RequestParam(value="url")
141140

142141
/**
143142
* Get the JSON representation of a workflow from Github details
143+
* @param domain The domain of the hosting site, Github or Gitlab
144144
* @param owner The owner of the Github repository
145145
* @param repoName The name of the repository
146146
* @param branch The branch of repository
147147
* @return The JSON representation of the workflow
148148
*/
149-
@GetMapping(value = {"/workflows/github.com/{owner}/{repoName}/tree/{branch}/**",
150-
"/workflows/github.com/{owner}/{repoName}/blob/{branch}/**"},
149+
@GetMapping(value = {"/workflows/{domain}.com/{owner}/{repoName}/tree/{branch}/**",
150+
"/workflows/{domain}.com/{owner}/{repoName}/blob/{branch}/**"},
151151
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
152-
public Workflow getWorkflowByGithubDetailsJson(@PathVariable("owner") String owner,
152+
public Workflow getWorkflowByGithubDetailsJson(@PathVariable("domain") String domain,
153+
@PathVariable("owner") String owner,
153154
@PathVariable("repoName") String repoName,
154155
@PathVariable("branch") String branch,
155156
HttpServletRequest request) {
@@ -158,8 +159,7 @@ public Workflow getWorkflowByGithubDetailsJson(@PathVariable("owner") String own
158159
path = WorkflowController.extractPath(path, 7);
159160

160161
// Construct a GitDetails object to search for in the database
161-
GitDetails gitDetails = new GitDetails("https://github.com/" + owner + "/" +
162-
repoName + ".git", branch, path, GitType.GITHUB);
162+
GitDetails gitDetails = WorkflowController.getGitDetails(domain, owner, repoName, branch, path);
163163

164164
// Get workflow
165165
Workflow workflowModel = workflowService.getWorkflow(gitDetails);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ private GitDetails transferPathToBranch(GitDetails githubInfo) {
378378
if (firstSlash > 0) {
379379
branch += "/" + path.substring(0, firstSlash);
380380
path = path.substring(firstSlash + 1);
381-
return new GitDetails(githubInfo.getRepoUrl(), branch,
382-
path, githubInfo.getType());
381+
return new GitDetails(githubInfo.getRepoUrl(), branch, path);
383382
} else {
384383
return null;
385384
}

src/main/resources/static/css/main-20170616.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ body {
9191
margin-top:10px;
9292
}
9393

94-
#githubLogo {
94+
#gitLogo {
9595
margin-right: 5px;
9696
}
9797

src/main/resources/static/js/loading.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ require(['jquery'],
5959
dataType: "json",
6060
cache: false,
6161
success: function(response) {
62-
console.log(response);
6362
if (response.cwltoolStatus == "RUNNING") {
6463
// Retry in 3 seconds
6564
setTimeout(function () {

0 commit comments

Comments
 (0)