Skip to content

Commit

Permalink
#322 Add Git properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheman committed Apr 18, 2024
1 parent 523eef0 commit 458a709
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 111 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ spock = "org.spockframework:spock-bom:2.3-groovy-3.0"
[plugins]
gradle-versions = { id = "com.github.ben-manes.versions", version = "0.50.0" }
sonar = { id = "org.sonarqube", version = "5.0.0.4638" }
gitProperties = { id = "com.gorylenko.gradle-git-properties", version = "2.4.1" }

# Copyright Gerd Aschemann and aim42 contributors.
#
Expand Down
35 changes: 34 additions & 1 deletion htmlSanityCheck-core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import org.apache.tools.ant.filters.ReplaceTokens

plugins {
alias(libs.plugins.gitProperties)
}

dependencies {
implementation (libs.commons.validator) {
// Having a vulnerability here ...
Expand All @@ -19,6 +23,33 @@ dependencies {
testImplementation 'org.codehaus.groovy:groovy-xml'
}

gitProperties {
// specify the location of the generated file
// gitPropertiesResourceDir = file("${project.projectDir}/build/resources/main")

// specify the properties to include
keys = ['git.branch',
'git.build.host',
'git.build.user.email',
'git.build.user.name',
'git.build.version',
'git.closest.tag.commit.count',
'git.closest.tag.name',
'git.commit.id',
'git.commit.id.abbrev',
'git.commit.id.describe',
'git.commit.message.full',
'git.commit.message.short',
'git.commit.time',
'git.commit.user.email',
'git.commit.user.name',
'git.dirty',
'git.remote.origin.url',
'git.tags',
'git.total.commit.count',
]
}

publishing {
publications {
mavenJava(MavenPublication) {
Expand All @@ -35,7 +66,9 @@ tasks.register('copyResourceImages', Copy) {
}

processResources {
dependsOn copyResourceImages
dependsOn copyResourceImages, generateGitProperties

// duplicatesStrategy = DuplicatesStrategy.INHERIT

exclude '**/*.png'
inputs.property "version", project.version
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.aim42.htmlsanitycheck;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URL;
import java.util.Properties;

/**
* Provides the current product version,
* as configured in src/main/resources/product-version.properties.
* Code proposed by René Gröschke of Gradleware in
* <a href="https://discuss.gradle.org/t/access-constant-from-groovy-class-in-gradle-buildfile/10571/5">...</a>
*/
public class ProductInformation {
private ProductInformation() {

}

private static final Logger logger = LoggerFactory.getLogger(ProductInformation.class);

public static final Properties PRODUCT_PROPERTIES = getProperties("product-version.properties");
public static final String VERSION = PRODUCT_PROPERTIES.getProperty("version") == null
? "[unknown]"
: PRODUCT_PROPERTIES.getProperty("version");
public static final Properties GIT_PROPERTIES = getProperties("git.properties");

public static String getGitProperty (final String propertyKey) {
return GIT_PROPERTIES.getProperty(propertyKey);
}

private static Properties getProperties(final String propertyFilename) {
Properties props = new Properties();
try {
final URL resource = ProductInformation.class.getClassLoader().getResource(propertyFilename);
if (resource != null) {
props.load(resource.openConnection().getInputStream());
} else {
logger.warn("Couldn't obtain URL resource for '{}'", propertyFilename);
}
} catch (IOException E) {
logger.warn("'{}' cannot be obtained due to IOException.", propertyFilename);
}
return props;
}
}

/************************************************************************
* This is free software - without ANY guarantee!
*
*
* Copyright Dr. Gernot Starke, arc42.org
*
* 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
*
* http://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.
*
*********************************************************************** */

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.aim42.htmlsanitycheck.report;

import org.aim42.htmlsanitycheck.ProductVersion;
import org.aim42.htmlsanitycheck.ProductInformation;
import org.aim42.htmlsanitycheck.collect.Finding;
import org.aim42.htmlsanitycheck.collect.PerRunResults;
import org.aim42.htmlsanitycheck.collect.SingleCheckResults;
Expand Down Expand Up @@ -44,7 +44,7 @@ public void initReport() {
Long millis = runResults.checkingTookHowManyMillis();

printer.accept("********* HTML Sanity Checker findings report *********");
printer.accept(String.format("created on %s by version %s", createdOnDate, ProductVersion.getVersion()));
printer.accept(String.format("created on %s by version %s", createdOnDate, ProductInformation.VERSION));
printer.accept(String.format("checking took %s msecs.", millis));
printer.accept("");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Arrays;
import java.util.List;

import static org.aim42.htmlsanitycheck.ProductInformation.GIT_PROPERTIES;

public class HtmlReporter extends Reporter {

private static final Logger log = LoggerFactory.getLogger(HtmlReporter.class);
Expand Down Expand Up @@ -123,10 +125,24 @@ private void initWriterWithHtmlHeader() {
" $('html, body').animate({scrollTop: offsetTop}, 500, 'linear');\n" +
"}\n" +
"</script>\n" +
gitPropertiesAsComments() +
"</head>\n" +
"<body id=\"top\">\n");
}

private String gitPropertiesAsComments() {
StringBuilder sb = new StringBuilder("<!-- Git Properties -->\n");

GIT_PROPERTIES.forEach((key, value) -> sb.append("<!-- ")
.append(key)
.append(": '")
.append(value)
.append("' -->\n"));

return sb.toString();
}


/*
* copy css, javaScript and image/icon files to the html output directory,
*
Expand Down Expand Up @@ -216,7 +232,7 @@ private static String pageSummaryTableLine(SinglePageResults spr) {
String pageHref =
CreateLinkUtil.convertToLink(spr.getPageFileName());

return String.format(
return java.lang.String.format(
"<tr>%n" +
" <td class=\"%s\"><a href=\"#%s\">%s</a></td>%n" +
" <td class=\"number\">%d</td>%n" +
Expand All @@ -238,7 +254,7 @@ private static String infoBoxHeader() {
}

private static String infoBoxColumn(String id, String countStr, String label) {
return String.format(
return java.lang.String.format(
"%n<td>%n" +
" <div class=\"infoBox\" id=\"%s\"><div class=\"counter\">%s</div>%s</div>%n" +
"</td>",
Expand All @@ -253,7 +269,7 @@ private static String infoBoxSeparator() {

private static String infoBoxPercentage(int percentageSuccessful) {
String percentageClass = (percentageSuccessful != 100) ? "infoBox failures" : "infoBox success";
return String.format(
return java.lang.String.format(
"<td>%n" +
"<div class=\"%s\" id=\"successRate\"><div class=\"percent\">%d%%</div>successful</div>%n" +
"</td>%n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.aim42.htmlsanitycheck.report;

import org.aim42.htmlsanitycheck.ProductVersion;
import org.aim42.htmlsanitycheck.ProductInformation;
import org.aim42.htmlsanitycheck.collect.PerRunResults;
import org.aim42.htmlsanitycheck.collect.SingleCheckResults;
import org.aim42.htmlsanitycheck.collect.SinglePageResults;
Expand All @@ -19,10 +19,9 @@ public abstract class Reporter {
/**
* create the reporter
*/
public Reporter() {
protected Reporter() {
this.createdOnDate = new SimpleDateFormat("dd. MMMM yyyy, HH:mm").format(new Date());
this.createdByHSCVersion = ProductVersion.getVersion();

this.createdByHSCVersion = ProductInformation.VERSION;
}

/**
Expand All @@ -31,7 +30,7 @@ public Reporter() {
* @param runResults the results for the report
* @see PerRunResults, as the latter contains all findings.
*/
public Reporter(PerRunResults runResults) {
protected Reporter(PerRunResults runResults) {
this();
this.runResults = runResults;
this.pageResults = runResults.getResultsForAllPages();
Expand All @@ -43,7 +42,7 @@ public Reporter(PerRunResults runResults) {
*/
public List<SinglePageResults> addCheckingResultsForOnePage(SinglePageResults singlePageResults) {
pageResults.add(singlePageResults);
return pageResults.stream().sorted(Comparator.comparing(a -> a.getPageTitle())).collect(Collectors.toList());// enforce sorting, fixing issue #128 // Todo: XCheck if issues is solved after migration to java
return pageResults.stream().sorted(Comparator.comparing(SinglePageResults::getPageTitle)).collect(Collectors.toList());// enforce sorting, fixing issue #128 // Todo: XCheck if issues is solved after migration to java
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.aim42.htmlsanitycheck

import spock.lang.Specification
import spock.lang.Unroll

class ProductInformationTest extends Specification {

final static String BAD_VERSION = "--.--.--"

def "can get Version"() {
String version

when:
version = ProductInformation.VERSION

then:
version != BAD_VERSION
version != "version"
version != "@version@"
version != null
}

@Unroll
def "can get git property #propertyKey"() {
setup:
String propertyValue = ProductInformation.getGitProperty(propertyKey)

expect:
propertyValue != null

where:
propertyKey << [
'git.branch',
'git.build.host',
'git.build.user.email',
'git.build.user.name',
'git.build.version',
'git.closest.tag.commit.count',
'git.closest.tag.name',
'git.commit.id',
'git.commit.id.abbrev',
'git.commit.id.describe',
'git.commit.message.full',
'git.commit.message.short',
'git.commit.time',
'git.commit.user.email',
'git.commit.user.name',
'git.dirty',
'git.remote.origin.url',
'git.tags',
'git.total.commit.count'
]
}
}

/************************************************************************
* This is free software - without ANY guarantee!
*
*
* Copyright Dr. Gernot Starke, arc42.org
*
* 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
*
* http://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.
*
*********************************************************************** */

0 comments on commit 458a709

Please sign in to comment.