From e3d642ce7cc47de1fb1b45ec524064f51421657a Mon Sep 17 00:00:00 2001 From: Marcus Hert Da Coregio Date: Wed, 26 Jun 2024 14:04:17 -0300 Subject: [PATCH] Add Task to Verify Branch Version Matches the Project Version Closes gh-15226 --- build.gradle | 1 + buildSrc/build.gradle | 4 + .../CheckExpectedBranchVersionPlugin.java | 82 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 buildSrc/src/main/java/org/springframework/security/CheckExpectedBranchVersionPlugin.java diff --git a/build.gradle b/build.gradle index 6cc59e0122..785e175dbd 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,7 @@ apply plugin: 'org.springframework.github.milestone' apply plugin: 'org.springframework.github.changelog' apply plugin: 'org.springframework.github.release' apply plugin: 'org.springframework.security.versions.verify-dependencies-versions' +apply plugin: 'org.springframework.security.check-expected-branch-version' group = 'org.springframework.security' description = 'Spring Security' diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 5a17397667..4994bd96ac 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -68,6 +68,10 @@ gradlePlugin { id = "org.springframework.security.versions.verify-dependencies-versions" implementationClass = "org.springframework.security.convention.versions.VerifyDependenciesVersionsPlugin" } + checkExpectedBranchVersion { + id = "org.springframework.security.check-expected-branch-version" + implementationClass = "org.springframework.security.CheckExpectedBranchVersionPlugin" + } } } diff --git a/buildSrc/src/main/java/org/springframework/security/CheckExpectedBranchVersionPlugin.java b/buildSrc/src/main/java/org/springframework/security/CheckExpectedBranchVersionPlugin.java new file mode 100644 index 0000000000..40b943856c --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/security/CheckExpectedBranchVersionPlugin.java @@ -0,0 +1,82 @@ +/* + * Copyright 2002-2024 the original author or authors. + * + * 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 + * + * https://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 org.springframework.security; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.gradle.api.DefaultTask; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.TaskProvider; + +/** + * @author Marcus da Coregio + */ +public class CheckExpectedBranchVersionPlugin implements Plugin { + + @Override + public void apply(Project project) { + TaskProvider checkExpectedBranchVersionTask = project.getTasks().register("checkExpectedBranchVersion", CheckExpectedBranchVersionTask.class, (task) -> { + task.setGroup("Build"); + task.setDescription("Check if the project version matches the branch version"); + }); + project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME, checkTask -> checkTask.dependsOn(checkExpectedBranchVersionTask)); + } + + public static class CheckExpectedBranchVersionTask extends DefaultTask { + + @TaskAction + public void run() throws IOException { + Project project = getProject(); + String version = (String) project.getVersion(); + String branchVersion = getBranchVersion(project); + if (!branchVersion.matches("^[0-9]+\\.[0-9]+\\.x$")) { + System.out.println("Branch version does not match *.x, ignoring"); + return; + } + if (!versionsMatch(version, branchVersion)) { + throw new IllegalStateException(String.format("Project version [%s] does not match branch version [%s]. " + + "Please verify that the branch contains the right version.", version, branchVersion)); + } + } + + private static String getBranchVersion(Project project) throws IOException { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + project.exec((exec) -> { + exec.commandLine("git", "symbolic-ref", "--short", "HEAD"); + exec.setErrorOutput(System.err); + exec.setStandardOutput(baos); + }); + return baos.toString(); + } + } + + private boolean versionsMatch(String projectVersion, String branchVersion) { + String[] projectVersionParts = projectVersion.split("\\."); + String[] branchVersionParts = branchVersion.split("\\."); + if (projectVersionParts.length < 2 || branchVersionParts.length < 2) { + return false; + } + return projectVersionParts[0].equals(branchVersionParts[0]) && projectVersionParts[1].equals(branchVersionParts[1]); + } + + } + +}