Skip to content

Add Metalava SemVer Task #6725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/metalava-semver-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Metalava SemVer Check

on:
pull_request:

jobs:
semver-check:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout main
uses: actions/[email protected]
with:
ref: ${{ github.base_ref }}

- name: Set up JDK 17
uses: actions/[email protected]
with:
java-version: 17
distribution: temurin
cache: gradle

- name: Copy previous api.txt files
run: ./gradlew copyApiTxtFile

- name: Checkout PR
uses: actions/[email protected]
with:
ref: ${{ github.head_ref }}
clean: false

- name: Run Metalava SemVer check
run: ./gradlew metalavaSemver
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ class FirebaseAndroidLibraryPlugin : BaseFirebaseLibraryPlugin() {
apiTxtFile.set(project.file("api.txt"))
output.set(project.file("previous_api.txt"))
}

project.tasks.register<SemVerTask>("metalavaSemver") {
apiTxtFile.set(project.file("api.txt"))
otherApiFile.set(project.file("previous_api.txt"))
outputApiFile.set(project.file("opi.txt"))
currentVersionString.value(firebaseLibrary.version)
previousVersionString.value(firebaseLibrary.previousVersion)
}
}

private fun setupApiInformationAnalysis(project: Project, android: LibraryExtension) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class FirebaseJavaLibraryPlugin : BaseFirebaseLibraryPlugin() {
apiTxtFile.set(project.file("api.txt"))
output.set(project.file("previous_api.txt"))
}

project.tasks.register<SemVerTask>("metalavaSemver") {
apiTxtFile.set(project.file("api.txt"))
otherApiFile.set(project.file("previous_api.txt"))
outputApiFile.set(project.file("opi.txt"))
currentVersionString.value(firebaseLibrary.version)
previousVersionString.value(firebaseLibrary.previousVersion)
}
}

private fun setupApiInformationAnalysis(project: Project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ fun Project.runMetalavaWithArgs(
) {
val allArgs =
listOf(
"--no-banner",
"--hide",
"HiddenSuperclass", // We allow having a hidden parent class
"--hide",
Expand Down
104 changes: 104 additions & 0 deletions plugins/src/main/java/com/google/firebase/gradle/plugins/SemVerTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2025 Google LLC
*
* 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.
*/

package com.google.firebase.gradle.plugins

import com.google.firebase.gradle.plugins.semver.VersionDelta
import java.io.ByteArrayOutputStream
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

abstract class SemVerTask : DefaultTask() {
@get:InputFile abstract val apiTxtFile: RegularFileProperty
@get:InputFile abstract val otherApiFile: RegularFileProperty
@get:Input abstract val currentVersionString: Property<String>
@get:Input abstract val previousVersionString: Property<String>

@get:OutputFile abstract val outputApiFile: RegularFileProperty

@TaskAction
fun run() {
val previous = ModuleVersion.fromStringOrNull(previousVersionString.get()) ?: return
val current = ModuleVersion.fromStringOrNull(currentVersionString.get()) ?: return

val bump =
when {
previous.major != current.major -> VersionDelta.MAJOR
previous.minor != current.minor -> VersionDelta.MINOR
else -> VersionDelta.PATCH
}
val stream = ByteArrayOutputStream()
project.runMetalavaWithArgs(
listOf(
"--source-files",
apiTxtFile.get().asFile.absolutePath,
"--check-compatibility:api:released",
otherApiFile.get().asFile.absolutePath,
) +
MAJOR.flatMap { m -> listOf("--error", m) } +
MINOR.flatMap { m -> listOf("--error", m) } +
IGNORED.flatMap { m -> listOf("--hide", m) } +
listOf("--format=v3", "--no-color"),
ignoreFailure = true,
stdOut = stream,
)

val string = String(stream.toByteArray())
val reg = Regex("(.*)\\s+error:\\s+(.*\\s+\\[(.*)\\])")
val minorChanges = mutableListOf<String>()
val majorChanges = mutableListOf<String>()
for (match in reg.findAll(string)) {
val loc = match.groups[1]!!.value
val message = match.groups[2]!!.value
val type = match.groups[3]!!.value
if (IGNORED.contains(type)) {
continue // Shouldn't be possible
} else if (MINOR.contains(type)) {
minorChanges.add(message)
} else {
majorChanges.add(message)
}
}
val allChanges =
(majorChanges.joinToString(separator = "") { m -> " MAJOR: $m\n" }) +
minorChanges.joinToString(separator = "") { m -> " MINOR: $m\n" }
if (majorChanges.isNotEmpty()) {
if (bump != VersionDelta.MAJOR) {
throw GradleException(
"API has non-bumped breaking MAJOR changes\nCurrent version bump is ${bump}, update the gradle.properties or fix the changes\n$allChanges"
)
}
} else if (minorChanges.isNotEmpty()) {
if (bump != VersionDelta.MAJOR && bump != VersionDelta.MINOR) {
throw GradleException(
"API has non-bumped MINOR changes\nCurrent version bump is ${bump}, update the gradle.properties or fix the changes\n$allChanges"
)
}
}
}

companion object {
private val MAJOR = setOf("AddedFinal")
private val MINOR = setOf("AddedClass", "AddedMethod", "AddedField", "ChangedDeprecated")
private val IGNORED = setOf("ReferencesDeprecated")
}
}
Loading