66import com .embabel .agent .api .common .Ai ;
77import com .embabel .agent .api .common .OperationContext ;
88import 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 ;
1213import com .fasterxml .jackson .databind .ObjectMapper ;
1314import org .slf4j .Logger ;
1415import org .slf4j .LoggerFactory ;
16+ import org .springframework .lang .Nullable ;
1517
1618import java .util .LinkedList ;
1719import java .util .List ;
2123@ Agent (description = "Code modernization agent" )
2224public 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}
0 commit comments