-
Notifications
You must be signed in to change notification settings - Fork 2
feat: minor version strategy rules #132
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
Changes from 2 commits
d0c98f8
3e95dad
4c92f2b
7e97ee6
ee25d9b
079ebb1
3f388e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| name: Minor version bump | ||
| description: Verifies branch is ready to merge before minor version bump | ||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Configure Gradle | ||
| uses: awslabs/aws-kotlin-repo-tools/.github/actions/configure-gradle@main | ||
|
|
||
| - name: Verify minor version bump | ||
| shell: bash | ||
| run: ./gradlew minorVersionBumpScan |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package aws.sdk.kotlin.gradle.dsl | ||
|
|
||
| import aws.sdk.kotlin.gradle.util.verifyRootProject | ||
| import org.gradle.api.Project | ||
| import org.gradle.api.attributes.Bundling | ||
| import org.gradle.api.tasks.JavaExec | ||
| import org.gradle.kotlin.dsl.* | ||
| import org.gradle.language.base.plugins.LifecycleBasePlugin | ||
|
|
||
| /** | ||
| * Configures Gradle tasks that run or fix minor-version-bump-specific Ktlint rules. | ||
| */ | ||
| fun Project.configureMinorVersionStrategyRules(lintPaths: List<String>) { | ||
| verifyRootProject { "Task configuration is expected to be configured on the root project" } | ||
|
|
||
| val ktlintVersion = object {} // Can't use Project.javaClass because that's using the Gradle classloader | ||
| .javaClass | ||
| .getResource("ktlint-version.txt") | ||
| ?.readText() | ||
| ?: error("Missing ktlint-version.txt") | ||
|
|
||
| val minorVersionBumpKtlint by configurations.creating | ||
|
|
||
| dependencies { | ||
| minorVersionBumpKtlint("com.pinterest.ktlint:ktlint-cli:$ktlintVersion") { | ||
| attributes { | ||
| attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.SHADOWED)) | ||
| } | ||
| } | ||
| minorVersionBumpKtlint(project(":ktlint-rules:minor-version-strategy")) | ||
| } | ||
|
|
||
| tasks.register<JavaExec>("minorVersionBumpScan") { | ||
| group = LifecycleBasePlugin.VERIFICATION_GROUP | ||
| description = "Check minor version bump rules" | ||
| classpath = minorVersionBumpKtlint | ||
| mainClass.set("com.pinterest.ktlint.Main") | ||
| args = lintPaths | ||
| } | ||
|
|
||
| tasks.register<JavaExec>("minorVersionBumpFix") { | ||
| group = LifecycleBasePlugin.VERIFICATION_GROUP | ||
| description = "Check minor version bump rules" | ||
| classpath = minorVersionBumpKtlint | ||
| mainClass.set("com.pinterest.ktlint.Main") | ||
| args = listOf("-F") + lintPaths | ||
| jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,18 +16,31 @@ kotlin { | |
| sourceSets { | ||
| main { | ||
| dependencies { | ||
| implementation(libs.ktlint.cli.ruleset.core) | ||
| api(libs.ktlint.cli.ruleset.core) | ||
|
||
| } | ||
| } | ||
|
|
||
| test { | ||
| dependencies { | ||
| implementation(libs.ktlint.test) | ||
| implementation(kotlin("test")) | ||
| implementation(libs.ktlint.rule.engine) | ||
| implementation(libs.slf4j.simple) // Required by ktlint rule engine tests | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform() | ||
|
||
| testLogging { | ||
| events("passed", "skipped", "failed") | ||
| showStandardStreams = true | ||
| showStackTraces = true | ||
| showExceptions = true | ||
| exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL | ||
| } | ||
| } | ||
|
|
||
| tasks.withType<KotlinCompile> { | ||
| compilerOptions { | ||
| jvmTarget.set(JvmTarget.JVM_1_8) | ||
|
|
||
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.aws.ktlint.rules | ||
|
|
||
| import com.pinterest.ktlint.rule.engine.core.api.ElementType | ||
| import com.pinterest.ktlint.rule.engine.core.api.Rule | ||
| import com.pinterest.ktlint.rule.engine.core.api.RuleId | ||
| import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
|
||
| /** | ||
| * Matches @DeprecatedUntilVersion with either named args (major=x, minor=y) or positional args (x, y) | ||
| */ | ||
| internal fun deprecatedUntilVersionRegex(major: Int, minor: Int): Regex = | ||
| Regex( | ||
| """@DeprecatedUntilVersion\s*\(\s*(?:major\s*=\s*$major\s*,\s*minor\s*=\s*$minor\s*|\s*$major\s*,\s*$minor\s*)\s*\)""", | ||
| ) | ||
|
|
||
| /** | ||
| * Creates a ktlint rule that detects APIs annotated with @DeprecatedUntilVersion for the specified versions. | ||
| * If autocorrect is enabled, the API will be deleted. | ||
| */ | ||
| fun apisScheduledForRemovalRule(major: Int, minor: Int): Rule = | ||
|
||
| object : Rule(RuleId("minor-version-strategy:apis-scheduled-for-removal"), About()) { | ||
| override fun beforeVisitChildNodes( | ||
| node: ASTNode, | ||
| autoCorrect: Boolean, | ||
| emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, | ||
| ) { | ||
| if (node.elementType == ElementType.ANNOTATION_ENTRY) { | ||
| val regex = deprecatedUntilVersionRegex(major, minor) | ||
| if (regex.containsMatchIn(node.text)) { | ||
| emit( | ||
| node.startOffset, | ||
| "The deprecated API is scheduled for removal, please remove it before releasing the next minor version.", | ||
| true, | ||
| ) | ||
|
|
||
| if (autoCorrect) { | ||
| node.treeParent.treeParent?.let { | ||
| it.treeParent.removeChild(it) | ||
| } | ||
| } | ||
|
||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package software.aws.ktlint.rules | ||
|
|
||
| import com.pinterest.ktlint.rule.engine.api.Code | ||
| import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine | ||
| import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision | ||
| import com.pinterest.ktlint.rule.engine.core.api.RuleProvider | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class ApisScheduledForRemovalRuleTest { | ||
| val ruleEngine = KtLintRuleEngine( | ||
| ruleProviders = setOf( | ||
| RuleProvider { apisScheduledForRemovalRule(1, 2) }, | ||
| ), | ||
| ) | ||
|
|
||
| private fun runAutoCorrectTest(codeSnippet: String, expected: String) { | ||
| val unformattedCode = Code.fromSnippet(codeSnippet) | ||
| val formattedCode = ruleEngine | ||
| .format(unformattedCode) { AutocorrectDecision.ALLOW_AUTOCORRECT } | ||
| .trimStart() | ||
| .trimEnd() | ||
|
|
||
| assertEquals(expected, formattedCode) | ||
| } | ||
|
|
||
| @Test | ||
| fun testAutoCorrect() { | ||
| runAutoCorrectTest( | ||
| """ | ||
| @DeprecatedUntilVersion(1, 2) | ||
| class Foo { | ||
| fun foo() {} | ||
| } | ||
|
|
||
| class Bar { | ||
| fun bar() {} | ||
| } | ||
| """.trimIndent(), | ||
| """ | ||
| class Bar { | ||
| fun bar() {} | ||
| } | ||
| """.trimIndent(), | ||
| ) | ||
|
|
||
| runAutoCorrectTest( | ||
| """ | ||
| @DeprecatedUntilVersion(1, 2) | ||
| class Foo { | ||
| fun foo() {} | ||
| } | ||
| """.trimIndent(), | ||
| """ | ||
| """.trimIndent(), | ||
| ) | ||
|
|
||
| runAutoCorrectTest( | ||
| """ | ||
| @DeprecatedUntilVersion(1, 2) | ||
| fun foo() { | ||
| // foo foo | ||
| } | ||
|
|
||
| class Bar { | ||
| fun bar() {} | ||
| } | ||
| """.trimIndent(), | ||
| """ | ||
| class Bar { | ||
| fun bar() {} | ||
| } | ||
| """.trimIndent(), | ||
| ) | ||
| } | ||
|
|
||
| fun runRegexTestCases(minor: Int, major: Int) { | ||
| val regex = deprecatedUntilVersionRegex(major, minor) | ||
|
|
||
| assertTrue(regex.containsMatchIn("@DeprecatedUntilVersion($major,$minor)")) | ||
| assertTrue(regex.containsMatchIn("@DeprecatedUntilVersion($major,$minor )")) | ||
| assertTrue(regex.containsMatchIn("@DeprecatedUntilVersion($major, $minor)")) | ||
| assertTrue(regex.containsMatchIn("@DeprecatedUntilVersion($major ,$minor)")) | ||
| assertTrue(regex.containsMatchIn("@DeprecatedUntilVersion( $major,$minor)")) | ||
| assertTrue(regex.containsMatchIn("@DeprecatedUntilVersion( $major , $minor )")) | ||
| assertTrue(regex.containsMatchIn("@DeprecatedUntilVersion(major=$major,minor=$minor)")) | ||
| assertTrue(regex.containsMatchIn("@DeprecatedUntilVersion( major= $major , minor= $minor )")) | ||
|
|
||
| assertFalse(regex.containsMatchIn("@DeprecatedUntilVersion")) | ||
| assertFalse(regex.containsMatchIn("@DeprecatedUntilVersion()")) | ||
| assertFalse(regex.containsMatchIn("@DeprecatedUntilVersion($minor,$minor)")) | ||
| assertFalse(regex.containsMatchIn("@DeprecatedUntilVersion($major,$major)")) | ||
| assertFalse(regex.containsMatchIn("@DeprecatedUntilVersion($minor,$major)")) | ||
| } | ||
|
|
||
| @Test | ||
| fun testRegex() { | ||
| runRegexTestCases(0, 1) | ||
| runRegexTestCases(1, 70) | ||
| runRegexTestCases(100, 1_000_000) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit/naming:
validateMinorVersionBump?