Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
[#16] Fail build if new versions of specified packages are found
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Aug 8, 2015
1 parent 5c8f1b3 commit b0150a6
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -6,6 +6,11 @@ jdk:
- oraclejdk7
- oraclejdk8

cache:
directories:
- $HOME/.gradle
- $HOME/.m2

env:
- GRADLE_VERSION=1.12
- GRADLE_VERSION=2.4
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
1.6.0
-----
New features:
* [#16](https://github.com/4finance/uptodate-gradle-plugin/issues/16) Fail build if new versions of specified packages are found

1.5.2
-----
Bug fixes:
Expand Down
36 changes: 36 additions & 0 deletions README.md
Expand Up @@ -137,6 +137,42 @@ uptodate {
}
```

### How to break the build if new dependencies have been found?

You can add the possibility to break the build for new versions of dependencies by applying the following closure

```
uptodate {
breakTheBuild {
enabled = true
}
}
```

If turned on the default behaviour is such that if ANY new dependency is found then the build will be broken.

You can customize the inclusion and exclusion patterns by the following methods:

```
uptodate {
breakTheBuild {
enabled = true
include('regex for dependency group or name to break the build if newer version is found', 'another regex')
exclude('regex for dependency group or name NOT to break the build if newer version is found', 'another regex')
}
}
```

To make the functionality available on demand you can apply for example a project property:

```
uptodate {
breakTheBuild {
enabled = project.hasProperty('uptodateBuildBreakingEnabled')
}
}
```

### How to run the plugin on every build?

You can run the plugin automatically on every build, just by adding to your build.gradle
Expand Down
Expand Up @@ -10,7 +10,7 @@ class ConfigurationFilter {
projectConfigurations = project.configurations.findAll()
}

Set<Configuration> getConfigurations(UptodatePluginExtension.IncludedExcludedConfigurationsHolder configurationsHolder) {
Set<Configuration> getConfigurations(FiltersHolder configurationsHolder) {
Set<Configuration> configurations = getIncludedConfigurations(configurationsHolder.included)
return configurations.findAll { !configurationsHolder.excluded.contains(it.name) }
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/groovy/com/ofg/uptodate/FiltersHolder.groovy
@@ -0,0 +1,30 @@
package com.ofg.uptodate

class FiltersHolder {
final IncludedExcludedPatternsHolder patternsHolder = new IncludedExcludedPatternsHolder()

/**
* String names of configurations to include for checking
* @param configurations
*/
void include(String... configurations) {
this.patternsHolder.included.addAll(configurations)
}

void exclude(String... configurations) {
this.patternsHolder.excluded.addAll(configurations)
}

static class IncludedExcludedPatternsHolder {
final Set<String> included = []
final Set<String> excluded = []
}

Set<String> getIncluded() {
return patternsHolder.included
}

Set<String> getExcluded() {
return patternsHolder.excluded
}
}
4 changes: 2 additions & 2 deletions src/main/groovy/com/ofg/uptodate/UptodatePlugin.groovy
Expand Up @@ -4,7 +4,7 @@ import com.ofg.uptodate.dependency.Dependency
import com.ofg.uptodate.finder.jcenter.JCenterNewVersionFinderFactory
import com.ofg.uptodate.finder.maven.MavenNewVersionFinderFactory
import com.ofg.uptodate.finder.NewVersionFinderInAllRepositories
import com.ofg.uptodate.reporting.NewVersionLogger
import com.ofg.uptodate.reporting.NewVersionProcessor
import groovy.util.logging.Slf4j
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand Down Expand Up @@ -41,7 +41,7 @@ class UptodatePlugin implements Plugin<Project> {
[new MavenNewVersionFinderFactory().create(uptodatePluginExtension, dependencies),
new JCenterNewVersionFinderFactory().create(uptodatePluginExtension, dependencies)])
Set<Dependency> dependenciesWithNewVersions = newVersionFinder.findNewer(dependencies)
new NewVersionLogger(loggerProxy, project.name, uptodatePluginExtension.reportProjectName).reportUpdates(dependenciesWithNewVersions)
new NewVersionProcessor(loggerProxy, project.name, uptodatePluginExtension).reportUpdates(dependenciesWithNewVersions)
} else {
loggerProxy.lifecycle(log, 'No dependencies found in project configuration.')
}
Expand Down
41 changes: 35 additions & 6 deletions src/main/groovy/com/ofg/uptodate/UptodatePluginExtension.groovy
Expand Up @@ -78,26 +78,55 @@ class UptodatePluginExtension {
excludedVersionPatterns.addAll(patternsToExclude)
}

final IncludedExcludedConfigurationsHolder configurations = new IncludedExcludedConfigurationsHolder()
final FiltersHolder configurations = new FiltersHolder()

/**
* String names of configurations to include for checking
* @param configurations
*/
void includeConfigurations(String... configurations) {
this.configurations.included.addAll(configurations)
this.configurations.include(configurations)
}

/**
* String names of configurations to exclude from checking
* @param configurations
*/
void excludeConfigurations(String... configurations) {
this.configurations.excluded.addAll(configurations)
this.configurations.exclude(configurations)
}

static class IncludedExcludedConfigurationsHolder {
final Set<String> included = []
final Set<String> excluded = []
BuildBreakerConfiguration buildBreaker = new BuildBreakerConfiguration()

void breakTheBuild(Closure closure) {
closure.delegate = buildBreaker
closure.call()
}

static class BuildBreakerConfiguration {

/**
* Define if you want to break the build if new versions were found
*/
boolean enabled = false

FiltersHolder filters = new FiltersHolder()

/**
* String names of patterns for either group or dependency name to include for checking
* @param patterns
*/
void includePatterns(String... patterns) {
this.filters.include(patterns)
}

/**
* String names of patterns for either group or dependency name to exclude for checking
* @param patterns
*/
void excludedPatterns(String... patterns) {
this.filters.exclude(patterns)
}

}
}
50 changes: 50 additions & 0 deletions src/main/groovy/com/ofg/uptodate/reporting/BuildBreaker.groovy
@@ -0,0 +1,50 @@
package com.ofg.uptodate.reporting

import com.ofg.uptodate.UptodatePluginExtension
import com.ofg.uptodate.dependency.Dependency
import groovy.util.logging.Slf4j

@Slf4j
class BuildBreaker {

private static final Set<String> INCLUDE_ALL = ['.*'] as Set<String>

private final UptodatePluginExtension.BuildBreakerConfiguration buildBreakerConfiguration

BuildBreaker(UptodatePluginExtension.BuildBreakerConfiguration buildBreakerConfiguration) {
this.buildBreakerConfiguration = buildBreakerConfiguration
}

void breakTheBuildIfNecessary(List<Dependency> sortedUpdates) {
if (!buildBreakerConfiguration.enabled) {
log.debug('Build breaking is disabled')
return
}
log.debug("Build breaking is enabled")
List<Dependency> matchingDependencies = findAllDependenciesMatchingPatterns(sortedUpdates)
log.debug("Found the following matching dependencies $matchingDependencies")
breakTheBuildIfMatchingDepsWereFound(matchingDependencies)
}

private List<Dependency> findAllDependenciesMatchingPatterns(List<Dependency> sortedUpdates) {
return sortedUpdates.findAll { Dependency dependency ->
getPatternsForInclusionOrAllAsDefault().any { nameOrGroupMatchesRegex(dependency, it) } &&
!buildBreakerConfiguration.filters.excluded.any { nameOrGroupMatchesRegex(dependency, it) }
}
}

private Set<String> getPatternsForInclusionOrAllAsDefault() {
Set<String> patternsForInclusion = buildBreakerConfiguration.filters.included
return patternsForInclusion.empty ? INCLUDE_ALL : patternsForInclusion
}

private void breakTheBuildIfMatchingDepsWereFound(List<Dependency> matchingDependencies) {
if (!matchingDependencies.empty) {
throw new NewDependencyVersionsFoundException("For the following dependencies $matchingDependencies new versions have been found. Breaking the build")
}
}

private static boolean nameOrGroupMatchesRegex(Dependency dependency, String regex) {
return dependency.group.matches(regex) || dependency.name.matches(regex)
}
}
@@ -0,0 +1,7 @@
package com.ofg.uptodate.reporting

import groovy.transform.InheritConstructors

@InheritConstructors
class NewDependencyVersionsFoundException extends RuntimeException {
}
@@ -1,12 +1,13 @@
package com.ofg.uptodate.reporting

import com.ofg.uptodate.LoggerProxy
import com.ofg.uptodate.UptodatePluginExtension
import com.ofg.uptodate.dependency.Dependency
import com.ofg.uptodate.dependency.DependencyGroupAndNameComparator
import groovy.util.logging.Slf4j

@Slf4j
class NewVersionLogger {
class NewVersionProcessor {

private static final boolean DO_NOT_MUTATE_ORIGINAL_COLLECTION = false
private static final String NEW_VERSIONS_AVAILABLE = 'New versions available'
Expand All @@ -18,11 +19,13 @@ class NewVersionLogger {
private final LoggerProxy logger
private final String projectName
private final boolean reportProjectName
private final BuildBreaker buildBreaker

NewVersionLogger(LoggerProxy logger, String projectName, boolean reportProjectName) {
NewVersionProcessor(LoggerProxy logger, String projectName, UptodatePluginExtension uptodatePluginExtension) {
this.logger = logger
this.projectName = projectName
this.reportProjectName = reportProjectName
this.reportProjectName = uptodatePluginExtension.reportProjectName
this.buildBreaker = new BuildBreaker(uptodatePluginExtension.buildBreaker)
}

void reportUpdates(Set<Dependency> newVersions) {
Expand All @@ -31,6 +34,7 @@ class NewVersionLogger {
} else {
List<Dependency> sortedUpdates = newVersions.sort(DO_NOT_MUTATE_ORIGINAL_COLLECTION, new DependencyGroupAndNameComparator())
logger.lifecycle(log, newVersionsReport(sortedUpdates))
buildBreaker.breakTheBuildIfNecessary(sortedUpdates)
}
}

Expand Down
Expand Up @@ -7,7 +7,7 @@ import static com.ofg.uptodate.Jsons.JUNIT_RESPONSE
import static com.ofg.uptodate.Jsons.OLD_HIBERNATE_RESPONSE
import static com.ofg.uptodate.Xmls.HIBERNATE_CORE_META_DATA
import static com.ofg.uptodate.Xmls.OLD_JUNIT_META_DATA
import static com.ofg.uptodate.reporting.NewVersionLogger.NEW_VERSIONS_MESSAGE_HEADER
import static com.ofg.uptodate.reporting.NewVersionProcessor.NEW_VERSIONS_MESSAGE_HEADER

@Mixin([MavenResponseProvider, JCenterResponseProvider])
class AllRepositoriesnewVersionFinderSpec extends NewFinderSpec {
Expand Down
Expand Up @@ -8,8 +8,8 @@ import spock.lang.Unroll

import static com.ofg.uptodate.VersionPatterns.*
import static com.ofg.uptodate.Xmls.*
import static com.ofg.uptodate.reporting.NewVersionLogger.NO_NEW_VERSIONS_MESSAGE
import static com.ofg.uptodate.reporting.NewVersionLogger.NEW_VERSIONS_MESSAGE_HEADER
import static com.ofg.uptodate.reporting.NewVersionProcessor.NO_NEW_VERSIONS_MESSAGE
import static com.ofg.uptodate.reporting.NewVersionProcessor.NEW_VERSIONS_MESSAGE_HEADER

@Mixin([JCenterResponseProvider, HttpProxyServerProvider])
class JCenterNewVersionFinderSpec extends NewFinderSpec {
Expand Down
Expand Up @@ -8,8 +8,8 @@ import spock.lang.Unroll

import static com.ofg.uptodate.Jsons.*
import static com.ofg.uptodate.VersionPatterns.*
import static com.ofg.uptodate.reporting.NewVersionLogger.NO_NEW_VERSIONS_MESSAGE
import static com.ofg.uptodate.reporting.NewVersionLogger.NEW_VERSIONS_MESSAGE_HEADER
import static com.ofg.uptodate.reporting.NewVersionProcessor.NO_NEW_VERSIONS_MESSAGE
import static com.ofg.uptodate.reporting.NewVersionProcessor.NEW_VERSIONS_MESSAGE_HEADER

@Mixin([MavenResponseProvider, HttpProxyServerProvider])
class MavenNewVersionFinderSpec extends NewFinderSpec {
Expand Down

0 comments on commit b0150a6

Please sign in to comment.