Skip to content

Commit

Permalink
Merge pull request #1245 from newrelic/spotbugs-test
Browse files Browse the repository at this point in the history
Incorporate Spotbugs support into the agent
  • Loading branch information
jtduffy authored May 10, 2023
2 parents cb11477 + ee80cad commit e8a99e2
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 0 deletions.
104 changes: 104 additions & 0 deletions .github/workflows/GHA-Spotbugs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# GHA to run Spotbugs against top level Java agent projects. Both HTML and XML files are generated for each project
# and attached to the workflow run page under the "Artifacts" section. The files are downloaded as a zip (per project).
# The XML file can be analyzed inside the Spotbugs GUI: https://spotbugs.readthedocs.io/en/stable/running.html
name: Execute Spotbugs

on:
workflow_dispatch:
inputs:
agent-ref:
description: 'The ref (branch, SHA, tag) to run spotbugs on'
required: false
default: 'main'
type: string

jobs:
spotbugs:
timeout-minutes: 15
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
with:
ref: ${{ inputs.agent-ref || github.ref || 'main' }}

- name: Set up Java 8
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 8

- name: Save JAVA_HOME as JDK8 env variables
run: |
echo "Current JAVA_HOME = ${JAVA_HOME}"
echo "ORG_GRADLE_PROJECT_jdk8=$JAVA_HOME" >> $GITHUB_ENV
- name: Setup Gradle
uses: gradle/gradle-build-action@v2

- name: setup gradle options
run: echo "GRADLE_OPTIONS=-Porg.gradle.java.installations.auto-detect=false -Porg.gradle.java.installations.fromEnv=ORG_GRADLE_PROJECT_jdk8" >> $GITHUB_ENV

- name: set gradle.properties
run: |
sed -i -e "s|jdk8=8|jdk8=${JDK8}|" gradle.properties.gha
mv gradle.properties.gha gradle.properties
- name: Check environmental variables
run: printenv | sort -f

- name: Build newrelicJar
env:
JAVA_HOME: ${{ env.ORG_GRADLE_PROJECT_jdk8 }}
run: |
ls -la
cat settings.gradle
./gradlew $GRADLE_OPTIONS clean jar --parallel
ls -la newrelic-agent/build/
- name: Execute Spotbugs
env:
JAVA_HOME: ${{ env.ORG_GRADLE_PROJECT_jdk8 }}
run: |
./gradlew $GRADLE_OPTIONS spotbugsMain --parallel
- uses: actions/upload-artifact@v3
with:
name: newrelic-agent-spotbugs-results
path: 'newrelic-agent/build/spotbugs/main.*'
retention-days: 5

- uses: actions/upload-artifact@v3
with:
name: newrelic-weaver-spotbugs-results
path: 'newrelic-weaver/build/spotbugs/main.*'
retention-days: 5

- uses: actions/upload-artifact@v3
with:
name: agent-bridge-spotbugs-results
path: 'agent-bridge/build/spotbugs/main.*'
retention-days: 5

- uses: actions/upload-artifact@v3
with:
name: agent-bridge-datastore-spotbugs-results
path: 'agent-bridge-datastore/build/spotbugs/main.*'
retention-days: 5

- uses: actions/upload-artifact@v3
with:
name: agent-model-spotbugs-results
path: 'agent-model/build/spotbugs/main.*'
retention-days: 5

- uses: actions/upload-artifact@v3
with:
name: agent-interfaces-spotbugs-results
path: 'agent-interfaces/build/spotbugs/main.*'
retention-days: 5

- uses: actions/upload-artifact@v3
with:
name: newrelic-api-spotbugs-results
path: 'newrelic-api/build/spotbugs/main.*'
retention-days: 5
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,34 @@ Run all tests for a specific Scala instrumentation module:
./gradlew -PincludeScala instrumentation:sttp-2.13_2.2.3:test --parallel
```

## Code Quality

The agent utilizes [Jacoco](https://www.jacoco.org/jacoco/) and [Spotbugs](https://spotbugs.readthedocs.io/en/stable/introduction.html) to measure and improve code quality.

#### Jacoco

**TODO**

#### Spotbugs

[Spotbugs](https://spotbugs.readthedocs.io/en/stable/introduction.html) is a static code analysis tool to identify bugs in Java programs. It utilizes the Spotbugs
gradle plugin. The projects that are scanned by Spotbugs are:
- newrelic-agent
- newrelic-weaver
- agent-bridge
- agent-bridge-datastore
- agent-model
- agent-interfaces
- newrelic-api

To run Spotbugs, execute the following command:
```text
./gradlew spotbugsMain --parallel
```

The task will generate both XML and HTML files in the `build/spotbugs` folder underneath each subproject's top level folder.
The HTML file can be opened normally in a browser. The XML files can be imported into the [Spotbugs UI](https://spotbugs.readthedocs.io/en/stable/gui.html).

## Support

Should you need assistance with New Relic products, you are in good hands with several diagnostic tools and support channels.
Expand Down
2 changes: 2 additions & 0 deletions agent-bridge-datastore/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ plugins {
id("signing")
}

apply from: '../gradle/script/spotbugs-config.gradle'

dependencies {
implementation(project(":newrelic-api"))
implementation(project(":newrelic-weaver-api"))
Expand Down
2 changes: 2 additions & 0 deletions agent-bridge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ plugins {
id("signing")
}

apply from: '../gradle/script/spotbugs-config.gradle'

dependencies {
api(project(":newrelic-api"))
api(project(":newrelic-weaver-api"))
Expand Down
2 changes: 2 additions & 0 deletions agent-interfaces/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
id "java-library"
}

apply from: '../gradle/script/spotbugs-config.gradle'

dependencies {
api 'com.googlecode.json-simple:json-simple:1.1'
api(project(":newrelic-api"))
Expand Down
2 changes: 2 additions & 0 deletions agent-model/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
id "java-library"
}

apply from: '../gradle/script/spotbugs-config.gradle'

dependencies {
api 'com.googlecode.json-simple:json-simple:1.1'
}
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
id "org.jetbrains.kotlin.jvm" version "1.6.20" apply false
id "com.newrelic.gradle-verify-instrumentation-plugin" version "4.0" apply false
id "com.newrelic.gradle-compatibility-doc-plugin" version "1.1" apply false
id "com.github.spotbugs" version "5.0.14" apply false

}

allprojects {
Expand Down
24 changes: 24 additions & 0 deletions gradle/script/spotbugs-config.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

apply plugin: 'com.github.spotbugs'

spotbugs {
ignoreFailures = true
showStackTraces = true
showProgress = false
effort = 'default'
reportLevel = 'default'
reportsDir = file("$buildDir/spotbugs")
maxHeapSize = '1g'
includeFilter = file('../gradle/script/spotbugs-include-filter.xml')
}

spotbugsMain {
reports {
xml.enabled = true
html.enabled = true
}
}

spotbugsTest {
enabled = false
}
16 changes: 16 additions & 0 deletions gradle/script/spotbugs-include-filter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter
xmlns="https://github.com/spotbugs/filter/3.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">

<!-- Only include high priority findings, ignoring any i18n detections -->
<Match>
<Priority value="1" />
<Or>
<Not>
<Bug pattern="DM_DEFAULT_ENCODING" />
</Not>
</Or>
</Match>
</FindBugsFilter>
2 changes: 2 additions & 0 deletions newrelic-agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ plugins {
id("signing")
}

apply from: '../gradle/script/spotbugs-config.gradle'

repositories {
mavenCentral()
jcenter()
Expand Down
2 changes: 2 additions & 0 deletions newrelic-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ plugins {
id("signing")
}

apply from: '../gradle/script/spotbugs-config.gradle'

jar {
from("$rootDir/LICENSE")
manifest {
Expand Down
2 changes: 2 additions & 0 deletions newrelic-weaver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ configurations {
}
}

apply from: '../gradle/script/spotbugs-config.gradle'

repositories {
// hack to allow us to resolve the ':tools' dependency
flatDir name: 'javaHomeLib', dirs: jdk8 + "/lib"
Expand Down

0 comments on commit e8a99e2

Please sign in to comment.