Skip to content

Commit 8c6658d

Browse files
Merge branch '6.3.x'
Closes gh-15311
2 parents dd74722 + 09909d1 commit 8c6658d

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ apply plugin: 's101'
2424
apply plugin: 'io.spring.convention.root'
2525
apply plugin: 'org.jetbrains.kotlin.jvm'
2626
apply plugin: 'org.springframework.security.versions.verify-dependencies-versions'
27+
apply plugin: 'org.springframework.security.check-expected-branch-version'
2728
apply plugin: 'io.spring.security.release'
2829

2930
group = 'org.springframework.security'

buildSrc/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ gradlePlugin {
4747
id = "org.springframework.security.versions.verify-dependencies-versions"
4848
implementationClass = "org.springframework.security.convention.versions.VerifyDependenciesVersionsPlugin"
4949
}
50+
checkExpectedBranchVersion {
51+
id = "org.springframework.security.check-expected-branch-version"
52+
implementationClass = "org.springframework.security.CheckExpectedBranchVersionPlugin"
53+
}
5054
}
5155
}
5256

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
22+
import org.gradle.api.DefaultTask;
23+
import org.gradle.api.Plugin;
24+
import org.gradle.api.Project;
25+
import org.gradle.api.plugins.JavaBasePlugin;
26+
import org.gradle.api.tasks.TaskAction;
27+
import org.gradle.api.tasks.TaskProvider;
28+
29+
/**
30+
* @author Marcus da Coregio
31+
*/
32+
public class CheckExpectedBranchVersionPlugin implements Plugin<Project> {
33+
34+
@Override
35+
public void apply(Project project) {
36+
TaskProvider<CheckExpectedBranchVersionTask> checkExpectedBranchVersionTask = project.getTasks().register("checkExpectedBranchVersion", CheckExpectedBranchVersionTask.class, (task) -> {
37+
task.setGroup("Build");
38+
task.setDescription("Check if the project version matches the branch version");
39+
});
40+
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME, checkTask -> checkTask.dependsOn(checkExpectedBranchVersionTask));
41+
}
42+
43+
public static class CheckExpectedBranchVersionTask extends DefaultTask {
44+
45+
@TaskAction
46+
public void run() throws IOException {
47+
Project project = getProject();
48+
String version = (String) project.getVersion();
49+
String branchVersion = getBranchVersion(project);
50+
if (!branchVersion.matches("^[0-9]+\\.[0-9]+\\.x$")) {
51+
System.out.println("Branch version does not match *.x, ignoring");
52+
return;
53+
}
54+
if (!versionsMatch(version, branchVersion)) {
55+
throw new IllegalStateException(String.format("Project version [%s] does not match branch version [%s]. " +
56+
"Please verify that the branch contains the right version.", version, branchVersion));
57+
}
58+
}
59+
60+
private static String getBranchVersion(Project project) throws IOException {
61+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
62+
project.exec((exec) -> {
63+
exec.commandLine("git", "symbolic-ref", "--short", "HEAD");
64+
exec.setErrorOutput(System.err);
65+
exec.setStandardOutput(baos);
66+
});
67+
return baos.toString();
68+
}
69+
}
70+
71+
private boolean versionsMatch(String projectVersion, String branchVersion) {
72+
String[] projectVersionParts = projectVersion.split("\\.");
73+
String[] branchVersionParts = branchVersion.split("\\.");
74+
if (projectVersionParts.length < 2 || branchVersionParts.length < 2) {
75+
return false;
76+
}
77+
return projectVersionParts[0].equals(branchVersionParts[0]) && projectVersionParts[1].equals(branchVersionParts[1]);
78+
}
79+
80+
}
81+
82+
}

0 commit comments

Comments
 (0)