Skip to content
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

Add again some utilities to support incrementals via Jenkins infra pipeline-library #225

Merged
merged 3 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ jenkinsPlugin {

// plugin URL on GitHub, optional
gitHubUrl = 'https://github.com/jenkinsci/some-plugin'

// scm tag eventually set in the published pom, optional
scmTag = 'v1.0.0'

// use the plugin class loader before the core class loader, defaults to false
pluginFirstClassLoader = true
Expand Down Expand Up @@ -312,9 +315,9 @@ tasks.named('generateJenkinsManifest').configure {

### Using Git based version
[JEP-229](https://github.com/jenkinsci/jep/blob/master/jep/229/README.adoc#version-format) outlines requirements for creating sensible version numbers automatically.
The plugin registers a `generateGitVersion` task that generates a Git based version in a text file.
The plugin registers a `generateGitVersion` task that generates a Git based version in a text file (1st line an abbreviated hash, 2nd line the full hash).
This version scheme is typically used on ci.jenkins.io by first generating the version and then setting it when
building with `-Pversion=${versionFile.text}`. This works fine as long as no version is set in `build.gradle`.
building with `-Pversion=${versionFile.readLines()[0]}`. This works fine as long as no version is set in `build.gradle`.

See [Configuration](#configuration) to customize the generation.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class JpiExtension implements JpiExtensionBridge {
private final ListProperty<PluginDeveloper> pluginDevelopers
private final Property<String> incrementalsRepoUrl
private final GitVersionExtension gitVersion
private final Property<String> scmTag

@SuppressWarnings('UnnecessarySetter')
JpiExtension(Project project) {
Expand All @@ -106,6 +107,8 @@ class JpiExtension implements JpiExtensionBridge {
this.generatedTestClassName = project.objects.property(String).convention('InjectedTest')
this.gitVersion = project.objects.newInstance(GitVersionExtension)
this.incrementalsRepoUrl = project.objects.property(String).convention(JENKINS_INCREMENTALS_REPO)
this.scmTag = project.objects.property(String)
.convention(project.providers.gradleProperty('scmTag'))
}

/**
Expand Down Expand Up @@ -494,6 +497,10 @@ class JpiExtension implements JpiExtensionBridge {
incrementalsRepoUrl
}

Property<String> getScmTag() {
scmTag
}

void enableCheckstyle() {
project.plugins.apply(CheckstylePlugin)
def checkstyle = project.extensions.getByType(CheckstyleExtension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class JpiPomCustomizer {
if (github =~ /^https:\/\/github\.com/) {
s.connection.set(github.replaceFirst(~/https:/, 'scm:git:git:') + '.git')
}
s.tag.set(jpiExtension.scmTag)
}
}
if (!jpiExtension.licenses.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ public GitVersionExtension(ProjectLayout layout, ProviderFactory providers) {
providers.gradleProperty("gitVersionFile")
.map(p-> layout.getProjectDirectory().file(p))
.orElse(layout.getBuildDirectory().file("generated/version/version.txt")));
getVersionPrefix().convention("");
}

@Optional
public abstract Property<String> getVersionFormat();

@Optional
public abstract Property<String> getVersionPrefix();

@Optional
public abstract Property<Boolean> getSanitize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void generate() {
queue.submit(GenerateGitVersion.class, p -> {
p.getGitRoot().set(gitVersionExtension.getGitRoot());
p.getAbbrevLength().set(gitVersionExtension.getAbbrevLength());
p.getVersionPrefix().set(gitVersionExtension.getVersionPrefix());
p.getVersionFormat().set(gitVersionExtension.getVersionFormat());
p.getSanitize().set(gitVersionExtension.getSanitize());
p.getAllowDirty().set(gitVersionExtension.getAllowDirty());
Expand All @@ -64,8 +65,11 @@ public void generate() {
}

public interface GenerateGitVersionParameters extends WorkParameters {

DirectoryProperty getGitRoot();

Property<String> getVersionPrefix();

Property<String> getVersionFormat();

Property<Boolean> getSanitize();
Expand All @@ -83,13 +87,14 @@ public void execute() {
GenerateGitVersionParameters p = getParameters();
Path outputFile = p.getOutputFile().get().getAsFile().toPath();
try {
String version = new GitVersionGenerator(
GitVersionGenerator.GitVersion version = new GitVersionGenerator(
p.getGitRoot().get().getAsFile().toPath(),
p.getAbbrevLength().get(),
p.getVersionPrefix().get(),
p.getVersionFormat().get(),
p.getAllowDirty().get(),
p.getSanitize().get()).generate();
Files.write(outputFile, version.getBytes(StandardCharsets.UTF_8));
Files.write(outputFile, version.toString().getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException("Fail to write version file at " + outputFile, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@

public class GitVersionGenerator {
private final int abbrevLength;
private final String versionPrefix;
private final String versionFormat;
private final boolean sanitize;
private final boolean allowDirty;
private final Path gitRoot;

public GitVersionGenerator(Path gitRoot, int abbrevLength, String versionFormat, boolean allowDirty, boolean sanitize) {
public GitVersionGenerator(Path gitRoot, int abbrevLength, String versionPrefix, String versionFormat, boolean allowDirty, boolean sanitize) {
this.gitRoot = gitRoot;
// TODO abbrevLength should be 2 minimum
this.abbrevLength = abbrevLength;
this.versionPrefix = versionPrefix;
this.versionFormat = versionFormat;
this.sanitize = sanitize;
this.allowDirty = allowDirty;
}

public String generate() {
public GitVersion generate() {
try (Git git = Git.open(gitRoot.toFile())) {
Repository repo = git.getRepository();
Status status = git.status().call();
Expand All @@ -43,7 +45,7 @@ public String generate() {
if (sanitize) {
abbrevHash = sanitize(abbrevHash);
}
return String.format(versionFormat, headDepth, abbrevHash);
return new GitVersion(head.getName(), versionPrefix + String.format(versionFormat, headDepth, abbrevHash));
}
} catch (GitAPIException | IOException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -74,4 +76,28 @@ private long commitDepth(Repository repository, ObjectId objectId) throws IOExce
return StreamSupport.stream(walk.spliterator(), false).count();
}
}

static class GitVersion {
private final String fullHash;
private final String abbreviatedHash;

public GitVersion(String fullHash, String abbreviatedHash) {
this.fullHash = fullHash;
this.abbreviatedHash = abbreviatedHash;
}

@Override
public String toString() {
return abbreviatedHash + "\n" + fullHash;
}

public String getAbbreviatedHash() {
return abbreviatedHash;
}

public String getFullHash() {
return fullHash;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class JpiPomCustomizerIntegrationSpec extends IntegrationSpec {
jenkinsVersion = '${TestSupport.RECENT_JENKINS_VERSION}'
url = 'https://lorem-ipsum.org'
gitHubUrl = 'https://github.com/lorem/ipsum'
scmTag = 'my-tag'
developers {
developer {
id 'abayer'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.jenkinsci.gradle.plugins.jpi.TestSupport

import java.nio.file.Files

import static org.jenkinsci.gradle.plugins.jpi.version.Util.isGitHash

class GenerateGitVersionTaskSpec extends IntegrationSpec {
private final String projectName = TestDataGenerator.generateName()
private static final String GROUP_NAME = 'jpitest'
Expand Down Expand Up @@ -51,7 +53,8 @@ class GenerateGitVersionTaskSpec extends IntegrationSpec {
.build()

then:
inProjectDir('build/generated/version/version.txt').text ==~ /1\.\w{12}/
inProjectDir('build/generated/version/version.txt').readLines()[0] ==~ /1\.\w{12}/
isGitHash(inProjectDir('build/generated/version/version.txt').readLines()[1])
}

def 'should fail generate on non git repository'() {
Expand Down Expand Up @@ -98,7 +101,8 @@ class GenerateGitVersionTaskSpec extends IntegrationSpec {
.build()

then:
inProjectDir('build/generated/version/version.txt').text ==~ /1\.\w{12}/
inProjectDir('build/generated/version/version.txt').readLines()[0] ==~ /1\.\w{12}/
isGitHash(inProjectDir('build/generated/version/version.txt').readLines()[1])
}

def 'should generate with custom version format'() {
Expand All @@ -117,7 +121,8 @@ class GenerateGitVersionTaskSpec extends IntegrationSpec {
.build()

then:
inProjectDir('build/generated/version/version.txt').text ==~ /rc-1\.\w{10}/
inProjectDir('build/generated/version/version.txt').readLines()[0] ==~ /rc-1\.\w{10}/
isGitHash(inProjectDir('build/generated/version/version.txt').readLines()[1])
}

def 'should generate with custom version format from gradle property'() {
Expand All @@ -131,7 +136,27 @@ class GenerateGitVersionTaskSpec extends IntegrationSpec {
.build()

then:
inProjectDir('build/generated/version/version.txt').text ==~ /rc-1\.\w{12}/
inProjectDir('build/generated/version/version.txt').readLines()[0] ==~ /rc-1\.\w{12}/
isGitHash(inProjectDir('build/generated/version/version.txt').readLines()[1])
}

def 'should generate with custom prefix format'() {
given:
build.text = customBuildFile('''
gitVersion {
versionPrefix = version
}
''')
initGitRepo()

when:
gradleRunner()
.withArguments('generateGitVersion')
.build()

then:
inProjectDir('build/generated/version/version.txt').readLines()[0] ==~ /1\.0\.01\.\w{12}/
isGitHash(inProjectDir('build/generated/version/version.txt').readLines()[1])
}

def 'should generate with custom git root'() {
Expand All @@ -150,7 +175,8 @@ class GenerateGitVersionTaskSpec extends IntegrationSpec {
.build()

then:
inProjectDir('build/generated/version/version.txt').text ==~ /1\.\w{12}/
inProjectDir('build/generated/version/version.txt').readLines()[0] ==~ /1\.\w{12}/
isGitHash(inProjectDir('build/generated/version/version.txt').readLines()[1])
}

def 'should set custom version file'() {
Expand All @@ -169,7 +195,8 @@ class GenerateGitVersionTaskSpec extends IntegrationSpec {
.build()

then:
customVersionFile.text ==~ /1\.\w{12}/
customVersionFile.readLines()[0] ==~ /1\.\w{12}/
isGitHash(customVersionFile.readLines()[1])
}

def 'should set custom version file from gradle property'() {
Expand All @@ -184,29 +211,37 @@ class GenerateGitVersionTaskSpec extends IntegrationSpec {
.build()

then:
customVersionFile.text ==~ /1\.\w{12}/
customVersionFile.readLines()[0] ==~ /1\.\w{12}/
isGitHash(customVersionFile.readLines()[1])
}

def 'should generate incrementals publication according to jenkins infra expectations'() {
def customVersionFile = Files.createTempDirectory('custom-version-dir').resolve('version.txt')
def customM2 = Files.createTempDirectory('custom-m2')
given:
build.text = customBuildFile()
build.text = customBuildFile('''
gitHubUrl = 'https://github.com/foo'
gitVersion {
versionPrefix = version
}
''')
initGitRepo()

when:
gradleRunner()
.withArguments('clean', 'generateGitVersion', "-PgitVersionFile=${customVersionFile}")
.withArguments('clean', 'generateGitVersion', "-PgitVersionFile=${customVersionFile}", '-PgitVersionFormat=-rc%d.%s')
.build()

def version = customVersionFile.toFile().text
def version = customVersionFile.readLines()[0]
def tag = customVersionFile.readLines()[1]
// Note that overriding the version with `-Pversion` works only if the version is not set in the build.gradle
gradleRunner()
.withArguments('build', 'publishToMavenLocal', "-Pversion=${version}", "-Dmaven.repo.local=${customM2}")
.withArguments('build', 'publishToMavenLocal', "-Pversion=${version}", "-Dmaven.repo.local=${customM2}", "-PscmTag=${tag}")
.build()

then:
customVersionFile.text ==~ /1\.\w{12}/
customVersionFile.readLines()[0] ==~ /1.0.0-rc1\.\w{12}/
customVersionFile.readLines()[1] ==~ /\w{40}/
def prefix = "${projectName}-${version}"
customM2.resolve("${GROUP_NAME}/${projectName}/${version}").toFile().list() as Set == [
"${prefix}-javadoc.jar",
Expand All @@ -216,6 +251,7 @@ class GenerateGitVersionTaskSpec extends IntegrationSpec {
"${prefix}.module",
"${prefix}.pom",
] as Set
customM2.resolve("${GROUP_NAME}/${projectName}/${version}/${prefix}.pom").text =~ /<tag>\w{40}<\/tag>/
}

def 'should be able to pass sanitize flag'() {
Expand Down
Loading