Skip to content

Commit c1e094c

Browse files
committed
chore(tests): speedup calculating if a module has git changes since last release
1 parent b72786d commit c1e094c

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

plugins/customizations/kotlin-backend-application/src/test/java/io/zenwave360/sdk/plugins/kotlin/SpringWebTestClientKotlinGeneratorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.zenwave360.sdk.plugins.SpringWebTestClientPlugin;
88
import io.zenwave360.sdk.processors.OpenApiProcessor;
99
import io.zenwave360.sdk.templating.TemplateOutput;
10+
import io.zenwave360.sdk.testutils.GitUtils;
1011
import io.zenwave360.sdk.testutils.MavenCompiler;
1112
import io.zenwave360.sdk.writers.TemplateFileWriter;
1213
import io.zenwave360.sdk.writers.TemplateStdoutWriter;
@@ -30,6 +31,15 @@ public class SpringWebTestClientKotlinGeneratorTest {
3031

3132
@AfterAll
3233
public static void testCompileAllTargetFolders() throws Exception {
34+
var hasModuleChanged = GitUtils.hasModuleChangedSinceLastTag("plugins/customizations/kotlin-backend-application/src/main/resources/io/zenwave360/sdk/plugins/kotlin/SpringWebTestClientGenerator");
35+
var hasHelpersChanged = GitUtils.hasModuleChangedSinceLastTag("plugins/customizations/kotlin-backend-application/src/main/java/io/zenwave360/sdk/plugins/kotlin/SpringWebTestClientKotlinHelpers.java");
36+
var hasTemplatesChanged = GitUtils.hasModuleChangedSinceLastTag("plugins/customizations/kotlin-backend-application/src/main/resources/io/zenwave360/sdk/plugins/kotlin/SpringWebTestClientKotlinTemplates");
37+
var hasTestsChanged = GitUtils.hasModuleChangedSinceLastTag("plugins/customizations/kotlin-backend-application/src/test/java/io/zenwave360/sdk/plugins/kotlin/SpringWebTestClientKotlinGeneratorTest.java");
38+
39+
if (!hasModuleChanged && !hasHelpersChanged && !hasTemplatesChanged && !hasTestsChanged) {
40+
return;
41+
}
42+
3343
String[] openapis = {
3444
"openapi-petstore.yml",
3545
"openapi-orders.yml",

plugins/openapi-spring-webtestclient/src/test/java/io/zenwave360/sdk/plugins/SpringWebTestClientGeneratorTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import io.zenwave360.sdk.MainGenerator;
1010
import io.zenwave360.sdk.Plugin;
11+
import io.zenwave360.sdk.testutils.GitUtils;
1112
import io.zenwave360.sdk.testutils.MavenCompiler;
1213
import io.zenwave360.sdk.writers.TemplateFileWriter;
1314
import io.zenwave360.sdk.writers.TemplateStdoutWriter;
@@ -29,6 +30,11 @@ public class SpringWebTestClientGeneratorTest {
2930

3031
@AfterAll
3132
public static void testCompileAllTargetFolders() throws Exception {
33+
var hasModuleChanged = GitUtils.hasModuleChangedSinceLastTag("plugins/openapi-spring-webtestclient");
34+
if (!hasModuleChanged) {
35+
return;
36+
}
37+
3238
String[] openapis = {
3339
"openapi-petstore.yml",
3440
"openapi-orders.yml",

zenwave-sdk-test-resources/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
<artifactId>maven-invoker</artifactId>
7070
<version>3.2.0</version>
7171
</dependency>
72+
<dependency>
73+
<groupId>org.eclipse.jgit</groupId>
74+
<artifactId>org.eclipse.jgit</artifactId>
75+
<version>7.3.0.202506031305-r</version>
76+
</dependency>
7277
</dependencies>
7378

7479
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package io.zenwave360.sdk.testutils;
2+
3+
import org.eclipse.jgit.api.Git;
4+
import org.eclipse.jgit.api.errors.GitAPIException;
5+
import org.eclipse.jgit.lib.Ref;
6+
import org.eclipse.jgit.lib.Repository;
7+
import org.eclipse.jgit.revwalk.RevCommit;
8+
import org.eclipse.jgit.revwalk.RevWalk;
9+
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
10+
import org.eclipse.jgit.treewalk.TreeWalk;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.util.List;
15+
16+
public class GitUtils {
17+
public static boolean hasModuleChangedSinceLastTag(String modulePath) throws IOException, GitAPIException {
18+
return hasModuleChangedSinceLastTag(modulePath, new File(".git"));
19+
}
20+
public static boolean hasModuleChangedSinceLastTag(String modulePath, File gitDir) throws IOException, GitAPIException {
21+
// Open the Git repository
22+
FileRepositoryBuilder builder = new FileRepositoryBuilder();
23+
try (Repository repository = builder.setGitDir(gitDir)
24+
.readEnvironment()
25+
.findGitDir()
26+
.build();
27+
Git git = new Git(repository)) {
28+
29+
// Find the latest tag
30+
List<Ref> tags = git.tagList().call();
31+
if (tags.isEmpty()) {
32+
// No tags found, assume changes exist
33+
return true;
34+
}
35+
36+
// Get the latest tag by commit date
37+
try (RevWalk revWalk = new RevWalk(repository)) {
38+
String latestTagCommitId = null;
39+
int latestCommitTime = 0;
40+
41+
for (Ref tag : tags) {
42+
try {
43+
RevCommit tagCommit = revWalk.parseCommit(tag.getObjectId());
44+
if (tagCommit.getCommitTime() > latestCommitTime) {
45+
latestCommitTime = tagCommit.getCommitTime();
46+
latestTagCommitId = tagCommit.getId().getName();
47+
}
48+
} catch (IOException e) {
49+
// Skip invalid tags
50+
continue;
51+
}
52+
}
53+
54+
if (latestTagCommitId == null) {
55+
return true; // No valid tags found
56+
}
57+
58+
// Get commits between latest tag and HEAD for the module path
59+
RevCommit latestTagCommit = revWalk.parseCommit(repository.resolve(latestTagCommitId));
60+
RevCommit headCommit = revWalk.parseCommit(repository.resolve("HEAD"));
61+
62+
// Compare trees for the module path
63+
try (TreeWalk treeWalk = new TreeWalk(repository)) {
64+
treeWalk.setRecursive(true);
65+
treeWalk.setFilter(org.eclipse.jgit.treewalk.filter.PathFilterGroup.createFromStrings(modulePath));
66+
67+
// Add trees in order: tag first, then HEAD
68+
treeWalk.addTree(latestTagCommit.getTree());
69+
treeWalk.addTree(headCommit.getTree());
70+
71+
// Use DiffFormatter to detect actual content changes
72+
org.eclipse.jgit.diff.DiffFormatter diffFormatter = new org.eclipse.jgit.diff.DiffFormatter(
73+
org.eclipse.jgit.util.io.NullOutputStream.INSTANCE);
74+
diffFormatter.setRepository(repository);
75+
diffFormatter.setPathFilter(org.eclipse.jgit.treewalk.filter.PathFilterGroup.createFromStrings(modulePath));
76+
77+
// Get the diffs between the two trees
78+
List<org.eclipse.jgit.diff.DiffEntry> diffs = diffFormatter.scan(latestTagCommit.getTree(), headCommit.getTree());
79+
80+
// If there are any diffs, the module has changed
81+
return !diffs.isEmpty();
82+
}
83+
}
84+
}
85+
}
86+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package io.zenwave360.sdk.testutils;
2+
3+
import org.eclipse.jgit.api.Git;
4+
import org.eclipse.jgit.api.errors.GitAPIException;
5+
import org.eclipse.jgit.lib.Repository;
6+
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.io.TempDir;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
17+
import static org.junit.jupiter.api.Assertions.*;
18+
19+
class GitUtilsTest {
20+
21+
@TempDir
22+
Path tempDir;
23+
24+
private Git git;
25+
private File moduleDir;
26+
private File testFile;
27+
28+
@BeforeEach
29+
void setUp() throws GitAPIException, IOException {
30+
// Initialize a git repository
31+
git = Git.init().setDirectory(tempDir.toFile()).call();
32+
33+
// Create a module directory
34+
moduleDir = new File(tempDir.toFile(), "test-module");
35+
assertTrue(moduleDir.mkdir());
36+
37+
// Create a test file in the module
38+
testFile = new File(moduleDir, "test-file.txt");
39+
Files.writeString(testFile.toPath(), "initial content");
40+
41+
// Add and commit the file
42+
git.add().addFilepattern(".").call();
43+
git.commit().setMessage("Initial commit").setSign(false).call();
44+
45+
// Create a tag
46+
git.tag().setName("v1.0.0").call();
47+
}
48+
49+
@AfterEach
50+
void tearDown() {
51+
if (git != null) {
52+
git.close();
53+
}
54+
}
55+
56+
@Test
57+
void hasModuleChangedSinceLastTag_noChanges() throws IOException, GitAPIException {
58+
// Test with no changes since tag
59+
File gitDir = new File(tempDir.toFile(), ".git");
60+
boolean hasChanged = GitUtils.hasModuleChangedSinceLastTag("test-module", gitDir);
61+
assertFalse(hasChanged, "Module should not have changes since last tag");
62+
}
63+
64+
@Test
65+
void hasModuleChangedSinceLastTag_withChanges() throws IOException, GitAPIException {
66+
// Modify the file
67+
Files.writeString(testFile.toPath(), "modified content");
68+
69+
// Add and commit the changes
70+
git.add().addFilepattern(".").call();
71+
git.commit().setMessage("Modified test file").setSign(false).call();
72+
73+
// Test with changes since tag
74+
File gitDir = new File(tempDir.toFile(), ".git");
75+
boolean hasChanged = GitUtils.hasModuleChangedSinceLastTag("test-module", gitDir);
76+
assertTrue(hasChanged, "Module should have changes since last tag");
77+
}
78+
79+
@Test
80+
void hasModuleChangedSinceLastTag_newFile() throws IOException, GitAPIException {
81+
// Create a new file in the module
82+
File newFile = new File(moduleDir, "new-file.txt");
83+
Files.writeString(newFile.toPath(), "new file content");
84+
85+
// Add and commit the new file
86+
git.add().addFilepattern(".").call();
87+
git.commit().setMessage("Added new file").setSign(false).call();
88+
89+
// Test with changes since tag
90+
File gitDir = new File(tempDir.toFile(), ".git");
91+
boolean hasChanged = GitUtils.hasModuleChangedSinceLastTag("test-module", gitDir);
92+
assertTrue(hasChanged, "Module should have changes since last tag when new file is added");
93+
}
94+
95+
@Test
96+
void hasModuleChangedSinceLastTag_differentModule() throws IOException, GitAPIException {
97+
// Create a different module
98+
File otherModuleDir = new File(tempDir.toFile(), "other-module");
99+
assertTrue(otherModuleDir.mkdir());
100+
101+
// Create a file in the other module
102+
File otherFile = new File(otherModuleDir, "other-file.txt");
103+
Files.writeString(otherFile.toPath(), "other content");
104+
105+
// Add and commit the other module
106+
git.add().addFilepattern(".").call();
107+
git.commit().setMessage("Added other module").setSign(false).call();
108+
109+
// Test original module - should not have changes
110+
File gitDir = new File(tempDir.toFile(), ".git");
111+
boolean hasChanged = GitUtils.hasModuleChangedSinceLastTag("test-module", gitDir);
112+
assertFalse(hasChanged, "Original module should not have changes");
113+
114+
// Test other module - should have changes
115+
boolean otherHasChanged = GitUtils.hasModuleChangedSinceLastTag("other-module", gitDir);
116+
assertTrue(otherHasChanged, "Other module should have changes since last tag");
117+
}
118+
}

0 commit comments

Comments
 (0)