Skip to content

Commit ab8cd62

Browse files
committed
Added validation of client workspace name (via pattern from global configuration)
Signed-off-by: Oleg Nenashev <nenashev@synopsys.com>
1 parent d8eddd2 commit ab8cd62

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed

src/main/java/hudson/plugins/perforce/PerforceSCM.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,8 @@ private String getConcurrentClientName(FilePath workspace, String p4Client) {
17471747

17481748
@Extension
17491749
public static final class PerforceSCMDescriptor extends SCMDescriptor<PerforceSCM> {
1750+
private String p4ClientPattern;
1751+
17501752
public PerforceSCMDescriptor() {
17511753
super(PerforceSCM.class, PerforceRepositoryBrowser.class);
17521754
load();
@@ -1807,6 +1809,34 @@ public List<PerforceToolInstallation> getP4Tools() {
18071809
PerforceToolInstallation[] p4ToolInstallations = Hudson.getInstance().getDescriptorByType(PerforceToolInstallation.DescriptorImpl.class).getInstallations();
18081810
return Arrays.asList(p4ToolInstallations);
18091811
}
1812+
1813+
public String getP4ClientPattern() {
1814+
if (p4ClientPattern == null) {
1815+
return ".*";
1816+
} else {
1817+
return p4ClientPattern;
1818+
}
1819+
}
1820+
1821+
1822+
@Override
1823+
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
1824+
p4ClientPattern = Util.fixEmpty(req.getParameter("p4.clientPattern").trim());
1825+
save();
1826+
return true;
1827+
}
1828+
1829+
public FormValidation doValidateNamePattern(StaplerRequest req) {
1830+
String namePattern = Util.fixEmptyAndTrim(req.getParameter("value"));
1831+
if (namePattern != null) {
1832+
try {
1833+
Pattern.compile(namePattern);
1834+
} catch (PatternSyntaxException exception) {
1835+
return FormValidation.error("Pattern format error: "+exception.getMessage());
1836+
}
1837+
}
1838+
return FormValidation.ok();
1839+
}
18101840

18111841
public String isValidProjectPath(String path) {
18121842
if (!path.startsWith("//")) {
@@ -1878,19 +1908,26 @@ public FormValidation doValidatePerforceLogin(StaplerRequest req) {
18781908
* Checks to see if the specified workspace is valid.
18791909
*/
18801910
public FormValidation doValidateP4Client(StaplerRequest req) {
1881-
Depot depot = getDepotFromRequest(req);
1882-
if (depot == null) {
1883-
return FormValidation.error(
1884-
"Unable to check workspace against depot");
1885-
}
1886-
String workspace = Util.fixEmptyAndTrim(req.getParameter("client"));
1911+
String workspace = Util.fixEmptyAndTrim(req.getParameter("client"));
18871912
if (workspace == null) {
18881913
return FormValidation.error("You must enter a workspaces name");
18891914
}
18901915
try {
1916+
// Check P4 client pattern first, because workspace check fails on valid client names with variables
1917+
if (!workspace.matches(getP4ClientPattern())) {
1918+
return FormValidation.error("Client name doesn't meet global pattern: "+getP4ClientPattern());
1919+
}
1920+
1921+
// Then, check depot
1922+
Depot depot = getDepotFromRequest(req);
1923+
if (depot == null) {
1924+
return FormValidation.error(
1925+
"Unable to check workspace against depot");
1926+
}
1927+
1928+
// Then, check workspace
18911929
Workspace p4Workspace =
18921930
depot.getWorkspaces().getWorkspace(workspace, "");
1893-
18941931
if (p4Workspace.getAccess() == null ||
18951932
p4Workspace.getAccess().equals(""))
18961933
return FormValidation.warning("Workspace does not exist. " +
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version='1.0' encoding='UTF-8' ?>
2+
<!--
3+
- The MIT License
4+
-
5+
- Copyright (c) 2013, Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
6+
-
7+
- OriginalFile: manage-project-roles.jelly
8+
- Thomas Maurel & Romain Seguy, Manufacture Française des Pneumatiques Michelin,
9+
-
10+
-
11+
- Permission is hereby granted, free of charge, to any person obtaining a copy
12+
- of this software and associated documentation files (the "Software"), to deal
13+
- in the Software without restriction, including without limitation the rights
14+
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
- copies of the Software, and to permit persons to whom the Software is
16+
- furnished to do so, subject to the following conditions:
17+
-
18+
- The above copyright notice and this permission notice shall be included in
19+
- all copies or substantial portions of the Software.
20+
-
21+
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
- THE SOFTWARE.
28+
-->
29+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
30+
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
31+
<f:section title="Perforce SCM">
32+
<f:entry title="P4 Client name pattern" help="/plugin/perforce/help/clientNamePattern.html">
33+
<f:textbox name="p4.clientPattern" value="${descriptor.p4ClientPattern}"
34+
checkUrl="'${rootURL}/scm/PerforceSCM/validateNamePattern?value='+escape(this.value)"/>
35+
</f:entry>
36+
</f:section>
37+
</j:jelly>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div>
2+
<p>
3+
Regular expression to check P4 client naming policies.
4+
</p>
5+
<p>
6+
Examples:
7+
<ul>
8+
<li>.* - All patterns</li>
9+
<li>Jenkins_.* - Workspace should start from Jenkins_ prefix</li>
10+
<li>\$\{JOB_NAME}_\$\{NODE_NAME\}_[\S]* - require two variables and non-space charecters then</li>
11+
</ul>
12+
</p>
13+
</div>

0 commit comments

Comments
 (0)