Skip to content

Cicd version validation #282

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 4 commits into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.PluginContainer;
import org.gradle.util.internal.VersionNumber;

import java.time.Duration;
import java.util.regex.Pattern;

import co.elastic.apm.compile.tools.NoticeProviderPlugin;
import co.elastic.apm.compile.tools.plugins.RootNoticeProviderPlugin;
Expand Down Expand Up @@ -35,12 +37,29 @@ public void apply(Project project) {
private void configureVersion(Project project) {
String versionOverride = getVersionOverride(project);
if (versionOverride != null) {
validateVersionOverrideFormatting(versionOverride);
validateVersionPatchChangeOnly(project.getVersion().toString(), versionOverride);
System.out.println("Overriding version with: '" + versionOverride + "'");
project.setVersion(versionOverride);
project.subprojects(subproject -> subproject.setVersion(versionOverride));
}
}

private static void validateVersionPatchChangeOnly(String currentVersion, String versionOverride) {
VersionNumber comparableVersion = VersionNumber.parse(currentVersion);
VersionNumber comparableVersionOverride = VersionNumber.parse(versionOverride);
if (comparableVersionOverride.getMajor() > comparableVersion.getMajor() || comparableVersionOverride.getMinor() > comparableVersion.getMinor()) {
throw new IllegalArgumentException(String.format("The version override, '%s', cannot provide greater major or minor numbers than the existing version from the gradle.properties file: '%s'.", versionOverride, currentVersion));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what you're checking here? Is it making sure that the new version's minor number is not greater than the existing version's minor number? In that case, how do you release a new minor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what you're checking here? Is it making sure that the new version's minor number is not greater than the existing version's minor number?

Yes, checking that neither the major nor the minor version numbers are greater than the project's next version, which is located here. This code only runs when releasing a patch version, in which case we would manually pass the patch version in the release Github action parameters which will override the existing project's version. However, since it's a free-format text right now, I'm adding this validation to make sure we won't accidentally release a new minor or major version number.

how do you release a new minor?

The normal release process is unchanged in this PR, currently, the next minor version is the one defined as the project's version here. So when we run a normal release, without overriding the project's next version, the project's version will get used in the generated artifact. After the release succeeds, we automatically create a PR, like this one, where we bump the minor version for the next release.

}
}

private static void validateVersionOverrideFormatting(String versionOverride) {
Pattern semverPattern = Pattern.compile("\\d+\\.\\d+\\.\\d+");
if (!semverPattern.matcher(versionOverride).matches()) {
throw new IllegalArgumentException(String.format("The provided version override, '%s', does not have a valid format.", versionOverride));
}
}

private String getVersionOverride(Project project) {
if (!project.hasProperty(PROPERTY_VERSION_OVERRIDE)) {
return null;
Expand Down