Skip to content

Commit f897f15

Browse files
authored
Add gradle diffCoverage report (#248)
* Add gradle diffCoverage report Signed-off-by: Daniel Widdis <[email protected]> * Document usage in CONTRIBUTING Signed-off-by: Daniel Widdis <[email protected]> * Fail on violation, match thresholds to AD project Signed-off-by: Daniel Widdis <[email protected]> * Work around JGit uncommitted file issue by executing git diff Signed-off-by: Daniel Widdis <[email protected]> Signed-off-by: Daniel Widdis <[email protected]>
1 parent 69c8b39 commit f897f15

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

.github/workflows/build.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ jobs:
1212
steps:
1313
- name: Checkout
1414
uses: actions/checkout@v3
15-
15+
with:
16+
# needed for git diff to find main branch
17+
fetch-depth: "0"
1618
- name: Spotless Check
1719
run: ./gradlew spotlessCheck
1820
check:
@@ -27,6 +29,9 @@ jobs:
2729
run: |
2830
./gradlew publishToMavenLocal
2931
- uses: actions/checkout@v3
32+
with:
33+
# needed for git diff to find main branch
34+
fetch-depth: "0"
3035
- name: Set up JDK 11
3136
uses: actions/setup-java@v2
3237
with:

CONTRIBUTING.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
- [Contributing to OpenSearch-SDK-Java](#contributing-to-opensearch-sdk-java)
1+
- [Contributing to OpenSearch SDK for Java](#contributing-to-opensearch-sdk-for-java)
22
- [First Things First](#first-things-first)
33
- [Ways to Contribute](#ways-to-contribute)
44
- [Bug Reports](#bug-reports)
55
- [Feature Requests](#feature-requests)
66
- [Contributing Code](#contributing-code)
77
- [Developer Certificate of Origin](#developer-certificate-of-origin)
8+
- [Code Coverage](#code-coverage)
89
- [Review Process](#review-process)
910

10-
# Contributing to OpenSearch-SDK
11+
# Contributing to OpenSearch SDK for Java
1112

1213
OpenSearch-SDK is a community project that is built and maintained by people just like **you**. We're glad you're interested in helping out. There are several different ways you can do it, but before we talk about that, let's talk about how to get started.
1314

@@ -77,6 +78,12 @@ Signed-off-by: Jane Smith <[email protected]>
7778
```
7879
You may type this line on your own when writing your commit messages. However, if your user.name and user.email are set in your git configs, you can use `-s` or `--signoff` to add the `Signed-off-by` line to the end of the commit message.
7980

81+
## Code Coverage
82+
83+
Any new functionality requires testing. Your PR will trigger an automatic assessment of the code coverage of the lines you've added. You should add unit and/or integration tests to exercise as much of your new code as possible.
84+
85+
If you'd like to preview your coverage before submitting your PR, to identify lines of code which are not tested, you may run `./gradlew diffCoverage` and review the report available in the project build directory at `build/reports/jacoco/diffCoverage/html/index.html`.
86+
8087
## Review Process
8188

8289
We deeply appreciate everyone who takes the time to make a contribution. We will review all contributions as quickly as possible. As a reminder, [opening an issue](https://github.com/opensearch-project/opensearch-sdk-java/issues/new/choose) discussing your change before you make it is the best way to smooth the PR process. This will prevent a rejection because someone else is already working on the problem, or because the solution is incompatible with the architectural direction.

build.gradle

+37
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
* GitHub history for details.
1010
*/
1111

12+
import java.nio.file.Files
13+
1214
plugins {
1315
id 'java'
1416
id "com.diffplug.spotless" version "6.11.0" apply false
1517
id 'jacoco'
18+
id "com.form.diff-coverage" version "0.9.5"
1619
}
1720

1821

@@ -147,3 +150,37 @@ jacocoTestReport {
147150
xml.required = true
148151
}
149152
}
153+
154+
// Get uncommitted files via git diff
155+
// https://github.com/form-com/diff-coverage-gradle/issues/73
156+
ext.createDiffFile = { ->
157+
def file = Files.createTempFile(URLEncoder.encode(project.name, 'UTF-8'), '.diff').toFile()
158+
def diffBase = 'refs/remotes/origin/main'
159+
if (System.getenv('CI')) {
160+
diffBase = 'origin/' + System.getenv('GITHUB_BASE_REF')
161+
}
162+
file.withOutputStream { out ->
163+
exec {
164+
commandLine 'git', 'diff', '--no-color', '--minimal', diffBase
165+
standardOutput = out
166+
}
167+
}
168+
return file
169+
}
170+
171+
diffCoverageReport {
172+
afterEvaluate {
173+
diffSource.file = createDiffFile()
174+
}
175+
176+
// View report at build/reports/jacoco/diffCoverage/html/index.html
177+
reports {
178+
html = true
179+
}
180+
181+
violationRules {
182+
minBranches = 0.60
183+
minLines = 0.75
184+
failOnViolation = true
185+
}
186+
}

0 commit comments

Comments
 (0)