• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

common-workflow-language / cwlviewer / #1694

30 Sep 2024 10:51AM UTC coverage: 70.306% (-0.5%) from 70.811%
#1694

push

github

mr-c
detect non-Workflow CWL documents and give a better error message

2 of 19 new or added lines in 4 files covered. (10.53%)

106 existing lines in 4 files now uncovered.

1700 of 2418 relevant lines covered (70.31%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.43
/src/main/java/org/commonwl/view/workflow/WorkflowFormValidator.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.commonwl.view.workflow;
21

22
import java.util.regex.Matcher;
23
import java.util.regex.Pattern;
24
import org.apache.commons.lang.StringUtils;
25
import org.commonwl.view.git.GitDetails;
26
import org.springframework.stereotype.Component;
27
import org.springframework.validation.Errors;
28
import org.springframework.validation.ValidationUtils;
29

30
/** Runs validation on the workflow form from the main page */
31
@Component
32
public class WorkflowFormValidator {
1✔
33

34
  // URL validation for cwl files on github.com
35
  private static final String GITHUB_CWL_REGEX =
36
      "^https?:\\/\\/github\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:tree|blob)\\/([^/]+)(?:\\/(.+\\.cwl))$";
37
  private static final Pattern githubCwlPattern = Pattern.compile(GITHUB_CWL_REGEX);
1✔
38

39
  // URL validation for directories on github.com
40
  private static final String GITHUB_DIR_REGEX =
41
      "^https?:\\/\\/github\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:(?:tree|blob)\\/([^/]+)\\/?(.*)?)?$";
42
  private static final Pattern githubDirPattern = Pattern.compile(GITHUB_DIR_REGEX);
1✔
43

44
  // URL validation for cwl files on gitlab.com
45
  private static final String GITLAB_CWL_REGEX =
46
      "^https?:\\/\\/gitlab\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:tree|blob)\\/([^/]+)(?:\\/(.+\\.cwl))$";
47
  private static final Pattern gitlabCwlPattern = Pattern.compile(GITLAB_CWL_REGEX);
1✔
48

49
  // URL validation for directories on gitlab.com
50
  private static final String GITLAB_DIR_REGEX =
51
      "^https?:\\/\\/gitlab\\.com\\/([A-Za-z0-9_.-]+)\\/([A-Za-z0-9_.-]+)\\/?(?:(?:tree|blob)\\/([^/]+)\\/?(.*)?)?$";
52
  private static final Pattern gitlabDirPattern = Pattern.compile(GITLAB_DIR_REGEX);
1✔
53

54
  // Generic Git URL validation
55
  private static final String GIT_REPO_REGEX =
56
      "^((git|ssh|http(s)?)|(git@[\\w\\.]+))(:(//)?)([\\w\\.@\\:/\\-~]+)(\\.git)(/)?$";
57
  private static final Pattern gitRepoPattern = Pattern.compile(GIT_REPO_REGEX);
1✔
58

59
  /**
60
   * Validates a WorkflowForm to ensure the URL is not empty and links to a cwl file
61
   *
62
   * @param form The given WorkflowForm
63
   * @param e Any errors from validation
64
   */
65
  public GitDetails validateAndParse(WorkflowForm form, Errors e) {
66
    ValidationUtils.rejectIfEmptyOrWhitespace(e, "url", "url.emptyOrWhitespace");
1✔
67

68
    // If not null and isn't just whitespace
69
    if (!e.hasErrors()) {
1✔
70

71
      // Override if specific branch or path is given in the form
72
      String repoUrl = null;
1✔
73
      String branch = null;
1✔
74
      String path = null;
1✔
75
      String packedId = null;
1✔
76
      if (!isEmptyOrWhitespace(form.getBranch())) {
1✔
77
        branch = form.getBranch();
1✔
78
      }
79
      if (!isEmptyOrWhitespace(form.getPath())) {
1✔
80
        path = form.getPath();
1✔
81
      }
82
      if (!isEmptyOrWhitespace(form.getPackedId())) {
1✔
UNCOV
83
        packedId = form.getPackedId();
×
84
      }
85

86
      // github.com URL
87
      Matcher m = githubCwlPattern.matcher(form.getUrl());
1✔
88
      if (m.find()) {
1✔
89
        repoUrl = "https://github.com/" + m.group(1) + "/" + m.group(2) + ".git";
1✔
90
        if (branch == null) branch = m.group(3);
1✔
91
        if (path == null) path = m.group(4);
1✔
92
      }
93

94
      // gitlab.com URL
95
      m = gitlabCwlPattern.matcher(form.getUrl());
1✔
96
      if (m.find()) {
1✔
97
        repoUrl = "https://gitlab.com/" + m.group(1) + "/" + m.group(2) + ".git";
1✔
98
        if (branch == null) branch = m.group(3);
1✔
99
        if (path == null) path = m.group(4);
1✔
100
      }
101

102
      // github.com Dir URL
103
      m = githubDirPattern.matcher(form.getUrl());
1✔
104
      if (m.find() && !m.group(2).endsWith(".git")) {
1✔
105
        repoUrl = "https://github.com/" + m.group(1) + "/" + m.group(2) + ".git";
1✔
106
        if (branch == null) branch = m.group(3);
1✔
107
        if (path == null) path = m.group(4);
1✔
108
      }
109

110
      // gitlab.com Dir URL
111
      m = gitlabDirPattern.matcher(form.getUrl());
1✔
112
      if (m.find() && !m.group(2).endsWith(".git")) {
1✔
113
        repoUrl = "https://gitlab.com/" + m.group(1) + "/" + m.group(2) + ".git";
1✔
114
        if (branch == null) branch = m.group(3);
1✔
115
        if (path == null) path = m.group(4);
1✔
116
      }
117

118
      // Split off packed ID if present
119
      if (repoUrl != null) {
1✔
120
        GitDetails details = new GitDetails(repoUrl, branch, path);
1✔
121
        if (packedId != null) {
1✔
UNCOV
122
          details.setPackedId(packedId);
×
123
        } else {
124
          String[] pathSplit = path.split("#");
1✔
125
          if (pathSplit.length > 1) {
1✔
126
            details.setPath(pathSplit[pathSplit.length - 2]);
1✔
127
            details.setPackedId(pathSplit[pathSplit.length - 1]);
1✔
128
          }
129
        }
130
        return details;
1✔
131
      }
132

133
      // General Git details if didn't match the above
134
      ValidationUtils.rejectIfEmptyOrWhitespace(e, "branch", "branch.emptyOrWhitespace");
1✔
135
      if (!e.hasErrors()) {
1✔
136
        m = gitRepoPattern.matcher(form.getUrl());
1✔
137
        if (m.find()) {
1✔
138
          GitDetails details = new GitDetails(form.getUrl(), form.getBranch(), form.getPath());
1✔
139
          details.setPackedId(form.getPackedId());
1✔
140
          return details;
1✔
141
        }
142
      }
143
    }
144

145
    // Errors will stop this being used anyway
146
    return null;
1✔
147
  }
148

149
  /**
150
   * Checks if a string is empty or whitespace
151
   *
152
   * @param str The string to be checked
153
   * @return Whether the string is empty or whitespace
154
   */
155
  private boolean isEmptyOrWhitespace(String str) {
156
    return (str == null || str.length() == 0 || StringUtils.isWhitespace(str));
1✔
157
  }
158
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc