-
Notifications
You must be signed in to change notification settings - Fork 619
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
Add Metalava SemVer Task #6725
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3826341
Add copyApiTxtFile task
emilypgoogle 1dd0650
Resolve import
emilypgoogle 68e3921
Add Metalava SemVer task
emilypgoogle 07d2f22
Merge branch 'main' into ep/metalava-semver
emilypgoogle 184d6a5
Adjust checkout
emilypgoogle 4738ab4
No clean
emilypgoogle 20b702a
Fixes
emilypgoogle 9c0c8a8
Change job name
emilypgoogle 7a3dfc2
Merge branch 'main' into ep/metalava-semver
emilypgoogle 9c4cfbe
Format
emilypgoogle 7671fb9
Update plugins/src/main/java/com/google/firebase/gradle/plugins/SemVe…
emilypgoogle 624d3c8
Update plugins/src/main/java/com/google/firebase/gradle/plugins/SemVe…
emilypgoogle 97d258d
Fix compilation
emilypgoogle ee58fbc
Format
emilypgoogle f8fa7e1
Merge branch 'main' into ep/metalava-semver
emilypgoogle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
plugins/src/main/java/com/google/firebase/gradle/plugins/SemVerTask.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.