From bf37123154c49f8f5ae12fe8e9a94e3edae0c8db Mon Sep 17 00:00:00 2001 From: Emily Ploszaj Date: Thu, 6 Jun 2024 12:13:31 -0500 Subject: [PATCH] Add changelog requirement action for major/minor bumps --- .github/workflows/check-api-changes.yml | 23 +++++++++ .../com/google/ai/client/generativeai/Chat.kt | 8 ++- .../google/gradle/plugins/ChangelogPlugin.kt | 11 +++- .../gradle/tasks/WarnVersionBumpTask.kt | 51 +++++++++++++++++++ 4 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/check-api-changes.yml create mode 100644 plugins/src/main/java/com/google/gradle/tasks/WarnVersionBumpTask.kt diff --git a/.github/workflows/check-api-changes.yml b/.github/workflows/check-api-changes.yml new file mode 100644 index 00000000..aad786ba --- /dev/null +++ b/.github/workflows/check-api-changes.yml @@ -0,0 +1,23 @@ +name: Check API Changes + +on: pull_request + +jobs: + check-format: + runs-on: ubuntu-latest + steps: + - name: Checkout branch + uses: actions/checkout@v3.5.3 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: temurin + cache: gradle + - name: Run API versioning check + run: | + if test -d .changes && git diff --quiet HEAD^..HEAD .changes ; then ./gradlew warnVersionBump ; else exit 0 ; fi + - name : Log errors + if: failure() + run: | + echo "PR contains a Major or Minor API bump without an associated changelog entry. Either generate a changelog entry for the change or revert the API changes." diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/Chat.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/Chat.kt index fbe8f346..ce0e3099 100644 --- a/generativeai/src/main/java/com/google/ai/client/generativeai/Chat.kt +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/Chat.kt @@ -45,6 +45,10 @@ import kotlinx.coroutines.flow.onEach class Chat(private val model: GenerativeModel, val history: MutableList = ArrayList()) { private var lock = Semaphore(1) + fun foo(bar: String) { + + } + /** * Generates a response from the backend with the provided [Content], and any previous ones * sent/returned from this chat. @@ -53,7 +57,7 @@ class Chat(private val model: GenerativeModel, val history: MutableList * @throws InvalidStateException if the prompt is not coming from the 'user' role * @throws InvalidStateException if the [Chat] instance has an active request. */ - suspend fun sendMessage(prompt: Content): GenerateContentResponse { + suspend fun sendMessage(prompt: Content, some: String = ""): GenerateContentResponse { prompt.assertComesFromUser() attemptLock() try { @@ -72,7 +76,7 @@ class Chat(private val model: GenerativeModel, val history: MutableList * @param prompt The text to be converted into a single piece of [Content] to send to the model. * @throws InvalidStateException if the [Chat] instance has an active request. */ - suspend fun sendMessage(prompt: String): GenerateContentResponse { + suspend fun sendMessage(prompt: String, foo: Bitmap? = null, bar: Float = 0f): GenerateContentResponse { val content = content { text(prompt) } return sendMessage(content) } diff --git a/plugins/src/main/java/com/google/gradle/plugins/ChangelogPlugin.kt b/plugins/src/main/java/com/google/gradle/plugins/ChangelogPlugin.kt index fb6b7d86..b30367e0 100644 --- a/plugins/src/main/java/com/google/gradle/plugins/ChangelogPlugin.kt +++ b/plugins/src/main/java/com/google/gradle/plugins/ChangelogPlugin.kt @@ -20,6 +20,7 @@ import com.google.gradle.tasks.FindChangesTask import com.google.gradle.tasks.MakeChangeTask import com.google.gradle.tasks.MakeReleaseNotesTask import com.google.gradle.tasks.WarnAboutApiChangesTask +import com.google.gradle.tasks.WarnVersionBumpTask import com.google.gradle.types.Changelog import com.google.gradle.types.RandomWordsGenerator import com.google.gradle.util.apply @@ -34,6 +35,7 @@ import com.google.gradle.util.regularOutputFile import com.google.gradle.util.tempFile import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.file.RegularFile import org.gradle.api.file.RegularFileProperty import org.gradle.api.tasks.Delete import org.gradle.api.tasks.Optional @@ -85,14 +87,19 @@ abstract class ChangelogPlugin : Plugin { tasks.register("makeChange") { val changeMessage = provideProperty("changeMessage") - val changeName = RandomWordsGenerator.generateString() - val changeOutput = extension.outputDirectory.childFile("$changeName.json") + val changeName = provideProperty("changeName") + .orElse(RandomWordsGenerator.generateString()) + val changeOutput = extension.outputDirectory.childFile("${changeName.get()}.json") changesFile.set(fileChanges) message.set(changeMessage) outputFile.set(changeOutput) } + tasks.register("warnVersionBump") { + changesFile.set(fileChanges) + } + tasks.register("warnAboutApiChanges") { changesFile.set(fileChanges) outputFile.set(extension.apiChangesFile) diff --git a/plugins/src/main/java/com/google/gradle/tasks/WarnVersionBumpTask.kt b/plugins/src/main/java/com/google/gradle/tasks/WarnVersionBumpTask.kt new file mode 100644 index 00000000..ec9c2c14 --- /dev/null +++ b/plugins/src/main/java/com/google/gradle/tasks/WarnVersionBumpTask.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2024 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.gradle.tasks + +import com.google.gradle.types.LinesChanged +import com.google.gradle.types.VersionType +import com.google.gradle.types.VersionType.* +import com.google.gradle.util.SkipTask +import org.gradle.api.DefaultTask +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.StopExecutionException +import org.gradle.api.tasks.TaskAction +import org.gradle.api.tasks.TaskExecutionException + +/** + * A Gradle task to warn about API version bumps beyond what is expected + * + * The task uses the provided [changesFile] to infer if merging the changes currently present in the + * repo will have an impact on the public api. + * + * @property changesFile a file containing a [LinesChanged]; representing the changes made in this + * repo + * @throws TaskExecutionException if changes cause an minor or major API bump + */ +abstract class WarnVersionBumpTask : DefaultTask() { + @get:InputFile abstract val changesFile: RegularFileProperty + + @TaskAction + fun add() { + val diff = LinesChanged.fromFile(changesFile.asFile.get()) + + if (diff.bump == MAJOR || diff.bump == MINOR) { + throw TaskExecutionException(this, Exception("Changes are ${diff.bump}, higher than PATCH")) + } + } +}