Skip to content

Commit

Permalink
Merge branch 'add-gradle-sonar-scanner' of github.com:rsksmart/rskj i…
Browse files Browse the repository at this point in the history
…nto add-gradle-sonar-scanner
  • Loading branch information
nagarev committed Nov 10, 2024
2 parents 0cca8c0 + ecb810d commit ff3aca7
Show file tree
Hide file tree
Showing 7 changed files with 1,084 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Build
run: |
./gradlew --no-daemon --stacktrace build -x test
./gradlew --no-daemon --stacktrace build -x test -x checkstyleMain -x checkstyleTest -x checkstyleJmh -x checkstyleIntegrationTest -x spotlessApply -x spotlessJavaCheck
- name: Archive build artifacts
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 #v4.4.3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/rit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ jobs:
id: set-branch-variables
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
github_event_input_powpeg_branch: ${{ github.event.inputs.powpeg-branch }}
github_event_input_rit_branch: ${{ github.event.inputs.rit-branch }}
github_event_inputs_powpeg_branch: ${{ github.event.inputs.powpeg-branch }}
github_event_inputs_rit_branch: ${{ github.event.inputs.rit-branch }}
github_event_name: ${{ github.event_name }}
github_event_pull_request_number: ${{ github.event.pull_request.number }}
github_head_ref: ${{ github.head_ref }}
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ blocksminer
jacocoHtml/

# CheckStyle Reports
config/checkstyle/reports/
rskj-core/config/checkstyle/reports/

# PMD
config/pmd/reports/
rskj-core/config/pmd/reports/

blocksminer1.txt

Expand Down
365 changes: 365 additions & 0 deletions config/checkstyle/checkstyle.xml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions config/checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.0//EN" "suppressions_1_0.dtd">
<suppressions>
<!-- Custom checkstyle suppressions -->
</suppressions>
549 changes: 549 additions & 0 deletions gradle/verification-metadata.xml

Large diffs are not rendered by default.

166 changes: 160 additions & 6 deletions rskj-core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,157 @@
plugins {
id 'application'
id 'checkstyle'
id "com.diffplug.spotless" version "6.13.0"
}

apply plugin: 'maven-publish'
apply plugin: 'jacoco'

import java.nio.file.Paths

// Define static timestamp
def staticTimestamp = '2024-10-31T10:24:00'
ext.timestamp = staticTimestamp

def filteredFiles = getFilteredFiles()

checkstyle {
toolVersion = '8.45'
configFile = file("$rootDir/config/checkstyle/checkstyle.xml")
}

spotless {
java {
googleJavaFormat()
indentWithTabs(2)
trimTrailingWhitespace()
endWithNewline()
target filteredFiles
}
}

def getFilteredFiles() {
def timestamp = ext.timestamp

def commandLog = [
'git', 'log', '--since=' + timestamp, '--name-only', '--pretty=format:', '--abbrev-commit'
]
def commandDiff = [
'git', 'diff', '--name-only'
]
def commandStaged = [
'git', 'diff', '--cached', '--name-only'
]

def outputLog = new ByteArrayOutputStream()
def outputDiff = new ByteArrayOutputStream()
def outputStaged = new ByteArrayOutputStream()

exec {
commandLine commandLog
standardOutput = outputLog
errorOutput = outputLog
ignoreExitValue = true
}

exec {
commandLine commandDiff
standardOutput = outputDiff
errorOutput = outputDiff
ignoreExitValue = true
}

exec {
commandLine commandStaged
standardOutput = outputStaged
errorOutput = outputStaged
ignoreExitValue = true
}

def committedFiles = outputLog.toString().trim().split('\n').toList().unique()
def uncommittedFiles = outputDiff.toString().trim().split('\n').toList().unique()
def stagedFiles = outputStaged.toString().trim().split('\n').toList().unique()
def allFiles = committedFiles + uncommittedFiles + stagedFiles

allFiles = allFiles.unique().findAll { file -> file }

def projectRoot = project.projectDir.toPath().toAbsolutePath()

allFiles = allFiles.findAll { file ->
file.endsWith('.java')
}.collect { file ->
def relativePath = Paths.get(file).subpath(1, Paths.get(file).getNameCount()) // Adjust the subpath as necessary
def absolutePath = projectRoot.resolve(relativePath).toFile()

return absolutePath.exists() ? absolutePath : null
}.findAll { it != null }

return allFiles
}


def configureCheckstyleTask(Checkstyle task, String sourceDir) {
def filteredFilesCheckstyle = getFilteredFilesForCheckstyle(sourceDir)

if (!filteredFilesCheckstyle.isEmpty()) {
task.source = project.files(filteredFilesCheckstyle)
} else {
println "No files to assign to Checkstyle for ${sourceDir}. Skipping task."
task.enabled = false
}

task.classpath = project.files(
sourceSets.main.output.classesDirs,
sourceSets.main.output.resourcesDir
)
task.reports {
xml.required.set(true)
html.required.set(true)
}
task.onlyIf {
!filteredFilesCheckstyle.isEmpty()
}
}

def getFilteredFilesForCheckstyle(String sourceDir) {
def files = getFilteredFiles()
return files.findAll { file ->
def filePath = file.toPath().normalize()
def isJavaFile = filePath.toString().endsWith('.java')
def containsSourceSegment = filePath.toString().contains(sourceDir)

isJavaFile && containsSourceSegment
}
}


// Configure each Checkstyle task
tasks.withType(Checkstyle).configureEach { Checkstyle task ->
def sourceDirMapping = [
'checkstyleMain': 'src/main/java',
'checkstyleTest': 'src/test/java',
'checkstyleIntegrationTest': 'src/integrationTest/java',
'checkstyleJmh': 'src/jmh/java'
]

sourceDirMapping.each { taskName, sourceDir ->
if (task.name == taskName) {
configureCheckstyleTask(task, sourceDir)
}
}

if (task.name in ['checkstyleJmh', 'checkstyleIntegrationTest']) {
task.doFirst {
def relevantSourceDir = task.name == 'checkstyleJmh' ? 'src/jmh/java' : 'src/integrationTest/java'
def checkstyleFiles = getFilteredFilesForCheckstyle(relevantSourceDir)

if (!checkstyleFiles.isEmpty()) {
task.source = project.files(checkstyleFiles)
}
}
}
}

configurations {
jmh
}
Expand Down Expand Up @@ -81,6 +228,7 @@ tasks.withType(Javadoc) {

repositories {
mavenCentral()
gradlePluginPortal()
maven {
url "https://deps.rsklabs.io"
}
Expand Down Expand Up @@ -252,8 +400,6 @@ javadoc {
options.encoding = "UTF-8"
}

def generatedResources = "$buildDir/generated-resources"

publishing {
publications {
rskj(MavenPublication) {
Expand All @@ -275,8 +421,9 @@ task generateResources {
def buildInfoFile = 'build-info.properties'

doLast {
mkdir generatedResources
def generated = new File(generatedResources as String, buildInfoFile)
def generatedDir = new File(buildDir, 'generatedResources')
mkdir(generatedDir)
def generated = new File(generatedDir, buildInfoFile)
def commitHash = gitCommitHash()
def currentBranch = gitCurrentBranch()
generated.text = """
Expand All @@ -297,7 +444,7 @@ task javadocJar(type: Jar) {
}

jar {
dependsOn 'generateResources'
dependsOn generateResources
def commitHash = gitCommitHash()
def currentBranch = gitCurrentBranch()
manifest {
Expand All @@ -306,7 +453,7 @@ jar {
}
from sourceSets.main.output.classesDirs
from sourceSets.main.output.resourcesDir
from generatedResources
from new File(buildDir, 'generatedResources') // Reference the generated resources directory
}

task generatePom(dependsOn: jar) {
Expand Down Expand Up @@ -417,3 +564,10 @@ static def amendPathIfNeeded(details) {
details.path = newPath
}
}

tasks.named('check').configure {
dependsOn 'checkstyleMain'
dependsOn 'checkstyleTest'
dependsOn 'checkstyleJmh'
dependsOn 'checkstyleIntegrationTest'
}

0 comments on commit ff3aca7

Please sign in to comment.