-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
core/enforcer-rules/src/main/groovy/enforcer/rules/RequireUrl.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2020 The author and/or original authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package enforcer.rules | ||
|
||
import groovy.transform.CompileStatic | ||
import org.gradle.api.Project | ||
import org.gradle.api.model.ObjectFactory | ||
import org.gradle.api.provider.Property | ||
import org.kordamp.gradle.plugin.enforcer.api.EnforcerContext | ||
import org.kordamp.gradle.plugin.enforcer.api.EnforcerPhase | ||
import org.kordamp.gradle.plugin.enforcer.api.EnforcerRuleException | ||
|
||
import javax.inject.Inject | ||
import java.util.function.Function | ||
import java.util.regex.Matcher | ||
import java.util.regex.Pattern | ||
|
||
import static org.apache.commons.lang3.StringUtils.isBlank | ||
import static org.kordamp.gradle.plugin.enforcer.api.EnforcerPhase.AFTER_PROJECT | ||
|
||
/** | ||
* This rule checks the given URL is present and optionally matches against a regex. | ||
* | ||
* Adapted from {@code org.apache.maven.plugins.enforcer.RequireProjectUrl} | ||
* | ||
* @author Andres Almiray | ||
* @since 0.7.0 | ||
*/ | ||
@CompileStatic | ||
class RequireUrl extends AbstractStandardEnforcerRule { | ||
final Property<String> regex | ||
final Function<Project, String> urlExtractor | ||
|
||
@Inject | ||
RequireUrl(ObjectFactory objects) throws EnforcerRuleException { | ||
super(objects, [AFTER_PROJECT] as EnforcerPhase[]) | ||
regex = objects.property(String).convention('^.+$') | ||
} | ||
|
||
@Override | ||
protected void doExecute(EnforcerContext context) throws EnforcerRuleException { | ||
if (!urlExtractor) { | ||
throw fail('You must supply a value for urlExtractor') | ||
} | ||
|
||
try { | ||
String url = urlExtractor.apply(context.project) | ||
|
||
if (!url) { | ||
String msg = message.orNull | ||
if (isBlank(msg)) { | ||
msg = 'Undefined url' | ||
} | ||
throw fail(msg) | ||
} | ||
|
||
Matcher matcher = Pattern.compile(regex.get()).matcher(url) | ||
if (!matcher.matches()) { | ||
String msg = message.orNull | ||
if (isBlank(msg)) { | ||
msg = "URL '${url}' does not match the required regex: ${regex.get()}".toString() | ||
} | ||
throw fail(msg) | ||
} | ||
} catch (EnforcerRuleException ere) { | ||
throw ere | ||
} catch (Exception e) { | ||
throw fail(e.toString()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
= RequireUrl | ||
|
||
This rule checks the given URL is present and optionally matches against a regex. | ||
|
||
== Trigger Phases | ||
* AFTER_PROJECT | ||
|
||
== Configuration | ||
[source,groovy] | ||
[subs="+macros"] | ||
---- | ||
enforce { | ||
rule(enforcer.rules.RequireUrl) { r -> | ||
r.enabled | ||
r.message | ||
r.regex | ||
r.urlExtractor | ||
} | ||
} | ||
---- | ||
|
||
== Properties | ||
|
||
[%header, cols="<,<,<,^,<4"] | ||
|=== | ||
| Name | ||
| Type | ||
| Default | ||
| Required | ||
| Description | ||
|
||
| message | ||
| Property<String> | ||
| | ||
| {icon_req_n} | ||
| An optional message to provide when the rule fails. | ||
|
||
| regex | ||
| Property<String> | ||
| `^.+$` | ||
| {icon_req_n} | ||
| The regex that the url must match. Default is a non-empty URL. | ||
|
||
| urlExtractor | ||
| Function<Project, String> | ||
| | ||
| {icon_req_y} | ||
| Extracts an URL literal from the project. | ||
|
||
|=== | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters