Skip to content

Commit fdbe421

Browse files
authored
Create unit test report workflow (#7422)
Currently based off of `ep/unit-test-report`, cannot be merged until that PR is.
1 parent 813d02b commit fdbe421

File tree

6 files changed

+121
-4
lines changed

6 files changed

+121
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Generate Test Report
2+
3+
on:
4+
schedule:
5+
- cron: 51 8 * * * # Runs automatically once a day
6+
workflow_dispatch: # Allow triggering the workflow manually
7+
8+
permissions:
9+
contents: read
10+
issues: write
11+
12+
jobs:
13+
report:
14+
name: "Generate Test Report"
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
19+
with:
20+
submodules: true
21+
22+
- name: Set up JDK 17
23+
uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12 # v4.7.0
24+
with:
25+
java-version: 17
26+
distribution: temurin
27+
cache: gradle
28+
29+
- name: Generate Test Report
30+
env:
31+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
run: ./gradlew generateTestReport
33+
34+
- name: Update tracking issue
35+
run: gh issue edit 7421 --body-file test-report.md

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ plugins {
2323
alias(libs.plugins.errorprone)
2424
alias(libs.plugins.crashlytics) apply false
2525
id("PublishingPlugin")
26+
id("test-report")
2627
id("firebase-ci")
2728
id("smoke-tests")
2829
alias(libs.plugins.google.services)

plugins/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ gradlePlugin {
114114
id = "copy-google-services"
115115
implementationClass = "com.google.firebase.gradle.plugins.CopyGoogleServicesPlugin"
116116
}
117+
register("testReportPlugin") {
118+
id = "test-report"
119+
implementationClass = "com.google.firebase.gradle.plugins.report.TestReportPlugin"
120+
}
117121
}
118122
}
119123

plugins/src/main/java/com/google/firebase/gradle/plugins/report/TestReportGenerator.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import org.slf4j.LoggerFactory
3939
class TestReportGenerator(private val apiToken: String) {
4040
private val LOG: Logger = LoggerFactory.getLogger("firebase-test-report")
4141

42-
fun createReport(commitCount: Int) {
42+
fun createReport(outputFile: File, commitCount: Int) {
4343
val response: JsonObject =
4444
request(
4545
URI.create("https://api.github.com/graphql"),
@@ -75,10 +75,10 @@ class TestReportGenerator(private val apiToken: String) {
7575
?.int ?: throw RuntimeException("Couldn't find PR number for commit $obj"),
7676
)
7777
}
78-
outputReport(commits)
78+
outputReport(outputFile, commits)
7979
}
8080

81-
private fun outputReport(commits: List<ReportCommit>) {
81+
private fun outputReport(outputFile: File, commits: List<ReportCommit>) {
8282
val reports = commits.flatMap { commit -> parseTestReports(commit.sha) }
8383
val output = StringBuilder()
8484
output.append("### Unit Tests\n\n")
@@ -99,7 +99,7 @@ class TestReportGenerator(private val apiToken: String) {
9999
output.append("\n")
100100

101101
try {
102-
File("test-report.md").writeText(output.toString())
102+
outputFile.writeText(output.toString())
103103
} catch (e: Exception) {
104104
throw RuntimeException("Error writing report file", e)
105105
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.gradle.plugins.report
18+
19+
import org.gradle.api.Plugin
20+
import org.gradle.api.Project
21+
import org.gradle.kotlin.dsl.register
22+
23+
class TestReportPlugin : Plugin<Project> {
24+
override fun apply(project: Project) {
25+
project.tasks.register<TestReportTask>("generateTestReport") {
26+
outputFile.set(project.file("test-report.md"))
27+
commitCount.set(8 as Integer)
28+
apiToken.set(System.getenv("GH_TOKEN"))
29+
}
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.firebase.gradle.plugins.report
17+
18+
import org.gradle.api.DefaultTask
19+
import org.gradle.api.file.RegularFileProperty
20+
import org.gradle.api.provider.Property
21+
import org.gradle.api.tasks.Input
22+
import org.gradle.api.tasks.OutputFile
23+
import org.gradle.api.tasks.TaskAction
24+
25+
/**
26+
* Creates a markdown unit test report file based on recent runs of GitHub Actions. Task simply
27+
* aggregates live test data and does not rely on the current state of the repository.
28+
*
29+
* @property outputFile The file path to output the markdown test report to.
30+
* @property commitCount The number of remote commits to aggregate test results from.
31+
* @property apiToken The GitHub API token with adequate permissions to read test result data and
32+
* execute GraphQL queries.
33+
*/
34+
abstract class TestReportTask : DefaultTask() {
35+
@get:OutputFile abstract val outputFile: RegularFileProperty
36+
37+
@get:Input abstract val commitCount: Property<Integer>
38+
39+
@get:Input abstract val apiToken: Property<String>
40+
41+
@TaskAction
42+
fun make() {
43+
TestReportGenerator(apiToken.get())
44+
.createReport(outputFile.asFile.get(), commitCount.get().toInt())
45+
}
46+
}

0 commit comments

Comments
 (0)