24
24
import lombok .RequiredArgsConstructor ;
25
25
26
26
import org .springframework .beans .factory .annotation .Autowired ;
27
+ import org .springframework .core .env .Environment ;
27
28
import org .springframework .data .release .io .CommandResult ;
28
29
import org .springframework .data .release .io .OsCommandOperations ;
29
30
import org .springframework .data .release .io .Workspace ;
52
53
public class GitOperations {
53
54
54
55
private final GitServer server = new GitServer ();
55
- private final OsCommandOperations osCommandOperations ;
56
+ private final OsCommandOperations os ;
56
57
private final Workspace workspace ;
57
58
private final Logger logger ;
58
59
private final PluginRegistry <IssueTracker , Project > issueTracker ;
60
+ private final Environment environment ;
59
61
60
62
public GitProject getGitProject (Project project ) {
61
63
return new GitProject (project , server );
@@ -67,12 +69,15 @@ public GitProject getGitProject(Project project) {
67
69
* @param train must not be {@literal null}.
68
70
* @throws Exception
69
71
*/
70
- public void reset (Train train ) throws Exception {
72
+ public void reset (TrainIteration train ) throws Exception {
71
73
72
74
Assert .notNull (train , "Train must not be null!" );
73
75
74
- for (Module module : train ) {
75
- osCommandOperations .executeCommand ("git reset --hard" , module .getProject ()).get ();
76
+ for (ModuleIteration module : train ) {
77
+
78
+ Branch branch = Branch .from (module );
79
+
80
+ os .executeCommand (String .format ("git reset --hard origin/%s" , branch ), module .getProject ()).get ();
76
81
}
77
82
}
78
83
@@ -99,7 +104,7 @@ public void checkout(TrainIteration iteration) throws Exception {
99
104
artifactVersion , project ));
100
105
}
101
106
102
- osCommandOperations .executeCommand (String .format ("git checkout %s" , tag ), project ).get ();
107
+ os .executeCommand (String .format ("git checkout %s" , tag ), project ).get ();
103
108
}
104
109
105
110
logger .log (iteration , "Successfully checked out projects." );
@@ -114,7 +119,7 @@ public void prepare(TrainIteration iteration) throws Exception {
114
119
update (module .getProject ()).get ();
115
120
116
121
String checkoutCommand = String .format ("git checkout %s && git pull origin %s" , branch , branch );
117
- osCommandOperations .executeCommand (checkoutCommand , module .getProject ()).get ();
122
+ os .executeCommand (checkoutCommand , module .getProject ()).get ();
118
123
}
119
124
}
120
125
@@ -131,6 +136,22 @@ public void update(Train train) throws Exception {
131
136
}
132
137
}
133
138
139
+ public void push (TrainIteration iteration ) throws Exception {
140
+
141
+ for (ModuleIteration module : iteration ) {
142
+
143
+ Branch branch = Branch .from (module );
144
+ os .executeCommand (String .format ("git push origin %s" , branch ), module .getProject ()).get ();
145
+ }
146
+ }
147
+
148
+ public void pushTags (Train train ) throws Exception {
149
+
150
+ for (Module module : train ) {
151
+ os .executeCommand ("git push --tags" , module .getProject ()).get ();
152
+ }
153
+ }
154
+
134
155
public Future <CommandResult > update (Project project ) throws Exception {
135
156
136
157
GitProject gitProject = new GitProject (project , server );
@@ -140,8 +161,8 @@ public Future<CommandResult> update(Project project) throws Exception {
140
161
141
162
logger .log (project , "Found existing repository %s. Obtaining latest changes…" , repositoryName );
142
163
143
- return osCommandOperations .executeCommand (
144
- "git checkout master && git reset --hard && git fetch --tags && git pull origin master" , project );
164
+ return os .executeCommand ("git checkout master && git reset --hard && git fetch --tags && git pull origin master" ,
165
+ project );
145
166
146
167
} else {
147
168
@@ -150,13 +171,13 @@ public Future<CommandResult> update(Project project) throws Exception {
150
171
File projectDirectory = workspace .getProjectDirectory (project );
151
172
String command = String .format ("git clone %s %s" , gitProject .getProjectUri (), projectDirectory .getName ());
152
173
153
- return osCommandOperations .executeCommand (command );
174
+ return os .executeCommand (command );
154
175
}
155
176
}
156
177
157
178
public Tags getTags (Project project ) throws Exception {
158
179
159
- String result = osCommandOperations .executeForResult ("git tag -l" , project );
180
+ String result = os .executeForResult ("git tag -l" , project );
160
181
List <Tag > tags = new ArrayList <>();
161
182
162
183
for (String line : result .split ("\n " )) {
@@ -177,23 +198,82 @@ public void tagRelease(TrainIteration iteration) throws Exception {
177
198
Project project = module .getProject ();
178
199
179
200
String checkoutCommand = String .format ("git checkout %s" , branch );
180
- osCommandOperations .executeCommand (checkoutCommand , project ).get ();
201
+ os .executeCommand (checkoutCommand , project ).get ();
181
202
182
203
String updateCommand = String .format ("git pull origin %s" , branch );
183
- osCommandOperations .executeCommand (updateCommand , project ).get ();
204
+ os .executeCommand (updateCommand , project ).get ();
184
205
185
206
String hash = getReleaseHash (module );
186
207
Tag tag = getTags (project ).createTag (module );
187
208
String tagCommand = String .format ("git tag %s %s" , tag , hash );
188
- osCommandOperations .executeCommand (tagCommand , project ).get ();
209
+ os .executeCommand (tagCommand , project ).get ();
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Commits all changes currently made to all modules of the given {@link TrainIteration}. The summary can contain a
215
+ * single {@code %s} placeholder which the version of the current module will get replace into.
216
+ *
217
+ * @param iteration must not be {@literal null}.
218
+ * @param summary must not be {@literal null} or empty.
219
+ * @param details can be {@literal null} or empty.
220
+ * @throws Exception
221
+ */
222
+ public void commit (TrainIteration iteration , String summary , String details ) throws Exception {
223
+
224
+ Assert .notNull (iteration , "Train iteration must not be null!" );
225
+ Assert .hasText (summary , "Summary must not be null or empty!" );
226
+
227
+ for (ModuleIteration module : iteration ) {
228
+
229
+ if (summary .contains ("%s" )) {
230
+ summary = String .format (summary , module .getVersionString ());
231
+ }
232
+
233
+ commit (module , summary , details );
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Commits the given files for the given {@link ModuleIteration} using the given summary and details for the commit
239
+ * message. If no files are given, all pending changes are commited.
240
+ *
241
+ * @param module must not be {@literal null}.
242
+ * @param summary must not be {@literal null} or empty.
243
+ * @param details can be {@literal null} or empty.
244
+ * @param files can be empty.
245
+ * @throws Exception
246
+ */
247
+ public void commit (ModuleIteration module , String summary , String details , File ... files ) throws Exception {
248
+
249
+ Assert .notNull (module , "Module iteration must not be null!" );
250
+ Assert .hasText (summary , "Summary must not be null or empty!" );
251
+
252
+ Project project = module .getProject ();
253
+ IssueTracker tracker = issueTracker .getPluginFor (project );
254
+ Ticket ticket = tracker .getReleaseTicketFor (module );
255
+
256
+ Commit commit = new Commit (ticket , summary , details );
257
+ String author = environment .getProperty ("git.author" );
258
+ String commitCommand = String .format ("git commit -m \" %s\" --author \" %s\" " , commit , author );
259
+
260
+ if (files .length != 0 ) {
261
+
262
+ for (File file : files ) {
263
+ os .executeCommand (String .format ("git add %s" , file .getAbsolutePath ()), project ).get ();
264
+ }
265
+
266
+ os .executeCommand (commitCommand , project ).get ();
267
+ } else {
268
+ os .executeCommand (commitCommand .concat (" -a" ), project ).get ();
189
269
}
190
270
}
191
271
192
272
private String getReleaseHash (ModuleIteration module ) throws Exception {
193
273
194
274
Project project = module .getProject ();
195
275
196
- String result = osCommandOperations .executeForResult ("git log --pretty=format:'%h %s'" , project );
276
+ String result = os .executeForResult ("git log --pretty=format:'%h %s'" , project );
197
277
Ticket releaseTicket = issueTracker .getPluginFor (project ).getReleaseTicketFor (module );
198
278
String trigger = String .format ("%s - Release" , releaseTicket .getId ());
199
279
0 commit comments