Skip to content

Commit

Permalink
Add RequireUrl rule
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Oct 6, 2020
1 parent 59feda3 commit 399c22b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
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())
}
}
}
1 change: 1 addition & 0 deletions docs/guide/src/docs/asciidoc/rules.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ include::{includedir}/rules/require-gradle-version.adoc[]
include::{includedir}/rules/require-java-version.adoc[]
include::{includedir}/rules/require-os.adoc[]
include::{includedir}/rules/require-system-property.adoc[]
include::{includedir}/rules/require-url.adoc[]

:leveloffset: 1
52 changes: 52 additions & 0 deletions docs/guide/src/docs/asciidoc/rules/require-url.adoc
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.

|===

4 changes: 4 additions & 0 deletions docs/guide/src/docs/asciidoc/usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ The following table shows rules available to both plugins
| RequireUpperBoundDeps
| {icon_req_y}
| {icon_req_n}

| RequireUrl
| {icon_req_n}
| {icon_req_y}
|===

See {link_maven_enforcer_rules}, {link_maven_extra_enforcer_rules} for reference.
Expand Down

0 comments on commit 399c22b

Please sign in to comment.