Skip to content

Commit 7088aa1

Browse files
committed
Add shell persistence methods
1 parent 8f0df06 commit 7088aa1

16 files changed

Lines changed: 363 additions & 266 deletions

.github/workflows/maven.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# This workflow uses actions that are not certified by GitHub.
55
# They are provided by a third-party and are governed by
6-
# separate terms of service, privacy policy, and support
6+
# separate terms of persistenceService, privacy policy, and support
77
# documentation.
88

99
name: Build

src/main/java/com/embabel/modernizer/agent/MigrationPointDto.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/main/java/com/embabel/modernizer/agent/MigrationPoints.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/main/java/com/embabel/modernizer/agent/ModernizerAgent.java

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import com.embabel.agent.api.common.Ai;
77
import com.embabel.agent.api.common.OperationContext;
88
import com.embabel.coding.tools.bash.BashTools;
9-
import com.embabel.modernizer.entity.MigrationJob;
10-
import com.embabel.modernizer.entity.MigrationReport;
11-
import com.embabel.modernizer.service.MigrationService;
9+
import com.embabel.modernizer.entity.*;
10+
import com.embabel.modernizer.service.PersistenceService;
11+
import com.fasterxml.jackson.annotation.JsonProperty;
12+
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
1213
import com.fasterxml.jackson.databind.ObjectMapper;
1314
import org.slf4j.Logger;
1415
import org.slf4j.LoggerFactory;
16+
import org.springframework.lang.Nullable;
1517

1618
import java.util.LinkedList;
1719
import java.util.List;
@@ -21,11 +23,40 @@
2123
@Agent(description = "Code modernization agent")
2224
public record ModernizerAgent(
2325
ModernizerConfig config,
24-
MigrationService service
26+
PersistenceService persistenceService
2527
) {
2628

2729
private final static Logger logger = LoggerFactory.getLogger(ModernizerAgent.class);
2830

31+
private record MigrationPointDto(
32+
String filePath,
33+
String description,
34+
String recipeId) {
35+
}
36+
37+
private record MigrationPointsDto(
38+
List<MigrationPointDto> migrationPoints,
39+
@JsonPropertyDescription("High-level overview of the migration points")
40+
String overview
41+
) {
42+
}
43+
44+
private record MigrationReportDto(
45+
boolean success,
46+
String notes,
47+
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
48+
@Nullable String branch
49+
) {
50+
/**
51+
* Return a new instance identifying a branch that was used for this migration
52+
*/
53+
public MigrationReportDto withBranch(String branch) {
54+
return new MigrationReportDto(success, notes, branch);
55+
}
56+
57+
}
58+
59+
2960
@Action
3061
public MigrationPoints migrationPoints(
3162
MigrationJob migrationJob,
@@ -37,56 +68,74 @@ public MigrationPoints migrationPoints(
3768
.withToolObject(new BashTools(softwareProject.getRoot()))
3869
.withTemplate("find_migration_points")
3970
.createObject(
40-
MigrationPoints.class,
71+
MigrationPointsDto.class,
4172
Map.of(
4273
"notes", migrationJob.getNotes(),
4374
"classifications", migrationJob.getCookbook().getRecipes())
4475
);
4576
logger.info("{} migration points found: \n{}",
46-
migrationPoints.migrationPoints().size(),
77+
migrationPoints.migrationPoints.size(),
4778
new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(migrationPoints));
48-
return migrationPoints;
79+
persistenceService.saveMigrationJob(migrationJob);
80+
migrationJob.setMigrationPoints(new MigrationPoints(
81+
migrationPoints.migrationPoints.stream().map(
82+
mp -> new MigrationPoint(
83+
mp.filePath(),
84+
mp.description(),
85+
mp.recipeId()
86+
)
87+
).toList(), migrationPoints.overview));
88+
return migrationJob.getMigrationPoints();
4989
}
5090

5191
@AchievesGoal(description = "Modernize codebase")
5292
@Action
53-
public List<MigrationReport> modernize(
93+
public MigrationsReport modernize(
5494
MigrationJob migrationJob,
5595
MigrationPoints migrationPoints,
5696
OperationContext context
5797
) {
5898
var softwareProject = migrationJob.softwareProject();
59-
var migrations = new LinkedList<MigrationReport>();
99+
var migrationReports = new LinkedList<MigrationReport>();
60100
for (var classification : migrationJob.getCookbook().getRecipes()) {
61101
logger.info("Processing classification: {} - {}", classification.getRecipeId(), classification.getDescription());
62102

63103
var originalBranch = softwareProject.currentBranch();
64104
var branchName = context.getAgentProcess().getId() + "_" + classification.getRecipeId().toLowerCase();
65105
var success = softwareProject.createAndCheckoutBranch(branchName);
66106
logger.info("Classification branch {} created from branch {} - {}", branchName, originalBranch, success);
67-
migrations.addAll(context.parallelMap(
68-
migrationPoints.migrationPoints()
107+
var dtos = context.parallelMap(
108+
migrationPoints.getMigrationPoints()
69109
.stream()
70-
.filter(mp -> Objects.equals(mp.recipeId(), classification.getRecipeId())).toList(),
110+
.filter(mp -> Objects.equals(mp.getRecipeId(), classification.getRecipeId())).toList(),
71111
1,
72112
mp -> tryToFixIndividualMigrationPoint(
73113
migrationJob,
74114
mp, context.ai())
75-
));
115+
);
116+
migrationReports.addAll(dtos.stream().map(dto ->
117+
new MigrationReport(
118+
dto.success(),
119+
dto.notes(),
120+
dto.branch()
121+
)).toList());
76122
// Go back to the original branch
77123
logger.info("Switching back from classification branch {} to original branch {}", branchName, originalBranch);
78124
softwareProject.checkoutBranch(originalBranch);
79125
}
80-
return migrations;
126+
var migrationsReport = new MigrationsReport(migrationReports);
127+
migrationJob.setMigrationsReport(migrationsReport);
128+
persistenceService.saveMigrationJob(migrationJob);
129+
return migrationsReport;
81130
}
82131

83132
/**
84133
* Try to fix an individual migration point
85134
* Commit if successful, otherwise revert
86135
*/
87-
private MigrationReport tryToFixIndividualMigrationPoint(
136+
private MigrationReportDto tryToFixIndividualMigrationPoint(
88137
MigrationJob migrationJob,
89-
MigrationPointDto migrationPoint,
138+
MigrationPoint migrationPoint,
90139
Ai ai) {
91140
var softwareProject = migrationJob.softwareProject();
92141
var migrationReport = ai
@@ -95,21 +144,21 @@ private MigrationReport tryToFixIndividualMigrationPoint(
95144
.withToolObject(new BashTools(softwareProject.getRoot()))
96145
.withTemplate("fix_migration_point")
97146
.createObject(
98-
MigrationReport.class,
147+
MigrationReportDto.class,
99148
Map.of(
100149
"migrationPoint", migrationPoint
101150
)
102151
);
103-
if (migrationReport.isSuccess()) {
104-
var message = "Fix: " + migrationPoint.description();
152+
if (migrationReport.success()) {
153+
var message = "Fix: " + migrationPoint.getDescription();
105154
softwareProject.commit(message, false);
106155
logger.info("Committing branch {} - {} as migration was successful",
107156
softwareProject.currentBranch(), message);
108-
157+
migrationReport = migrationReport.withBranch(softwareProject.currentBranch());
109158
} else {
110159
logger.warn("Reverting branch {} as migration was not successful", softwareProject.currentBranch());
111160
softwareProject.revert();
112161
}
113-
return migrationReport.withBranch(softwareProject.currentBranch());
162+
return migrationReport;
114163
}
115164
}

src/main/java/com/embabel/modernizer/entity/MigrationCookbook.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* Collection of migration recipes (immutable entity)
1010
*/
1111
@Entity
12-
@Table(name = "migration_cookbook")
1312
public class MigrationCookbook {
1413

1514
@Id
@@ -52,7 +51,6 @@ public List<MigrationRecipe> getRecipes() {
5251
return recipes;
5352
}
5453

55-
// Static cookbook definitions
5654
public static final MigrationCookbook MODERNIZE_JAVA = new MigrationCookbook(
5755
"MODERNIZE_JAVA",
5856
new MigrationRecipe("Legacy", "Code that is outdated and may not follow current best practices."),

src/main/java/com/embabel/modernizer/entity/MigrationJob.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public class MigrationJob {
3939
@Column(nullable = false)
4040
private Instant updatedAt;
4141

42-
@OneToMany(mappedBy = "migrationJob", cascade = CascadeType.ALL, orphanRemoval = true)
43-
private List<MigrationPoint> migrationPoints = new ArrayList<>();
42+
@OneToOne(mappedBy = "migrationJob", cascade = CascadeType.ALL, orphanRemoval = true)
43+
private MigrationPoints migrationPoints;
4444

45-
@OneToMany(mappedBy = "migrationJob", cascade = CascadeType.ALL, orphanRemoval = true)
46-
private List<MigrationPoints> migrationPointsSets = new ArrayList<>();
45+
@OneToOne(mappedBy = "migrationJob", cascade = CascadeType.ALL, orphanRemoval = true)
46+
private MigrationsReport migrationsReport;
4747

4848
// Transient field - not persisted
4949
@Transient
@@ -116,36 +116,26 @@ public void setUpdatedAt(Instant updatedAt) {
116116
this.updatedAt = updatedAt;
117117
}
118118

119-
public List<MigrationPoint> getMigrationPoints() {
119+
public MigrationPoints getMigrationPoints() {
120120
return migrationPoints;
121121
}
122122

123-
public void setMigrationPoints(List<MigrationPoint> migrationPoints) {
123+
public void setMigrationPoints(MigrationPoints migrationPoints) {
124124
this.migrationPoints = migrationPoints;
125+
if (migrationPoints != null) {
126+
migrationPoints.setMigrationJob(this);
127+
}
125128
}
126129

127-
// Helper methods for bidirectional relationship
128-
public void addMigrationPoint(MigrationPoint migrationPoint) {
129-
migrationPoints.add(migrationPoint);
130-
migrationPoint.setMigrationJob(this);
131-
}
132-
133-
public void removeMigrationPoint(MigrationPoint migrationPoint) {
134-
migrationPoints.remove(migrationPoint);
135-
migrationPoint.setMigrationJob(null);
136-
}
137-
138-
public List<MigrationPoints> getMigrationPointsSets() {
139-
return migrationPointsSets;
140-
}
141-
142-
public void setMigrationPointsSets(List<MigrationPoints> migrationPointsSets) {
143-
this.migrationPointsSets = migrationPointsSets;
130+
public MigrationsReport getMigrationsReport() {
131+
return migrationsReport;
144132
}
145133

146-
public void addMigrationPointsSet(MigrationPoints migrationPointsSet) {
147-
migrationPointsSets.add(migrationPointsSet);
148-
migrationPointsSet.setMigrationJob(this);
134+
public void setMigrationsReport(MigrationsReport migrationsReport) {
135+
this.migrationsReport = migrationsReport;
136+
if (migrationsReport != null) {
137+
migrationsReport.setMigrationJob(this);
138+
}
149139
}
150140

151141
// Lazy getter for transient field

src/main/java/com/embabel/modernizer/entity/MigrationPoint.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,30 @@
1111

1212
@Entity
1313
@EntityListeners(AuditingEntityListener.class)
14-
@Table(name = "migration_point")
1514
public class MigrationPoint {
1615

1716
@Id
1817
@GeneratedValue(strategy = GenerationType.IDENTITY)
1918
private Long id;
2019

2120
@ManyToOne(fetch = FetchType.LAZY)
22-
@JoinColumn(name = "migration_job_id", nullable = false)
23-
private MigrationJob migrationJob;
21+
@JoinColumn(name = "migration_points_id", nullable = false)
22+
private MigrationPoints migrationPoints;
2423

25-
@Column(name = "file_path", nullable = false, length = 1024)
24+
@Column(nullable = false, length = 1024)
2625
private String filePath;
2726

28-
@Column(name = "description", columnDefinition = "CLOB")
27+
@Column(columnDefinition = "CLOB")
2928
private String description;
3029

31-
@Column(name = "recipe_id")
3230
private String recipeId;
3331

3432
@CreatedDate
35-
@Column(name = "created_at", nullable = false, updatable = false)
33+
@Column(nullable = false, updatable = false)
3634
private Instant createdAt;
3735

3836
@LastModifiedDate
39-
@Column(name = "updated_at", nullable = false)
37+
@Column(nullable = false)
4038
private Instant updatedAt;
4139

4240
@OneToMany(mappedBy = "migrationPoint", cascade = CascadeType.ALL, orphanRemoval = true)
@@ -60,14 +58,6 @@ public void setId(Long id) {
6058
this.id = id;
6159
}
6260

63-
public MigrationJob getMigrationJob() {
64-
return migrationJob;
65-
}
66-
67-
public void setMigrationJob(MigrationJob migrationJob) {
68-
this.migrationJob = migrationJob;
69-
}
70-
7161
public String getFilePath() {
7262
return filePath;
7363
}
@@ -96,18 +86,11 @@ public Instant getCreatedAt() {
9686
return createdAt;
9787
}
9888

99-
public void setCreatedAt(Instant createdAt) {
100-
this.createdAt = createdAt;
101-
}
10289

10390
public Instant getUpdatedAt() {
10491
return updatedAt;
10592
}
106-
107-
public void setUpdatedAt(Instant updatedAt) {
108-
this.updatedAt = updatedAt;
109-
}
110-
93+
11194
public List<MigrationReport> getMigrationReports() {
11295
return migrationReports;
11396
}
@@ -126,4 +109,12 @@ public void removeMigrationReport(MigrationReport migrationReport) {
126109
migrationReports.remove(migrationReport);
127110
migrationReport.setMigrationPoint(null);
128111
}
112+
113+
public MigrationPoints getMigrationPoints() {
114+
return migrationPoints;
115+
}
116+
117+
public void setMigrationPoints(MigrationPoints migrationPoints) {
118+
this.migrationPoints = migrationPoints;
119+
}
129120
}

0 commit comments

Comments
 (0)