16
16
import java .nio .charset .StandardCharsets ;
17
17
import java .util .Arrays ;
18
18
import java .util .List ;
19
+ import java .util .Objects ;
19
20
import java .util .Set ;
20
21
import javax .annotation .Nonnull ;
21
22
import org .apache .commons .lang .StringUtils ;
@@ -40,6 +41,8 @@ public class NextVersionStep extends Step {
40
41
private boolean incrementPreRelease ;
41
42
private String buildMetadata ;
42
43
private boolean writeVersion ;
44
+ // True if non annotated tag are supported
45
+ private boolean nonAnnotatedTag ;
43
46
44
47
@ DataBoundConstructor
45
48
public NextVersionStep () {
@@ -123,6 +126,11 @@ public void setIncrementPreRelease(boolean incrementPreRelease) {
123
126
this .incrementPreRelease = incrementPreRelease ;
124
127
}
125
128
129
+ @ DataBoundSetter
130
+ public void setNonAnnotatedTag (boolean nonAnnotatedTag ) {
131
+ this .nonAnnotatedTag = nonAnnotatedTag ;
132
+ }
133
+
126
134
@ Override
127
135
public StepExecution start (StepContext stepContext ) throws Exception {
128
136
return new Execution (
@@ -133,6 +141,7 @@ public StepExecution start(StepContext stepContext) throws Exception {
133
141
preRelease ,
134
142
preservePreRelease ,
135
143
incrementPreRelease ,
144
+ nonAnnotatedTag ,
136
145
stepContext );
137
146
}
138
147
@@ -179,6 +188,13 @@ public static class Execution extends SynchronousStepExecution<String> {
179
188
// True to increment prerelease information instead of the version itself
180
189
private final transient boolean incrementPreRelease ;
181
190
191
+ // True if annotated tags are supported
192
+ @ SuppressFBWarnings (
193
+ value = "SE_TRANSIENT_FIELD_NOT_RESTORED" ,
194
+ justification = "Only used when starting." )
195
+ private final transient boolean nonAnnotatedTag ;
196
+
197
+
182
198
/**
183
199
* Constructor with fields initialisation.
184
200
*
@@ -189,6 +205,7 @@ public static class Execution extends SynchronousStepExecution<String> {
189
205
* @param preRelease Pre release information to add
190
206
* @param preservePreRelease Keep existing prerelease information or not
191
207
* @param incrementPreRelease Increment prerelease information or not
208
+ * @param nonAnnotatedTag Should use or non annotated tags
192
209
* @param context Jenkins context
193
210
*/
194
211
protected Execution (
@@ -199,6 +216,7 @@ protected Execution(
199
216
String preRelease ,
200
217
boolean preservePreRelease ,
201
218
boolean incrementPreRelease ,
219
+ boolean nonAnnotatedTag ,
202
220
@ Nonnull StepContext context ) {
203
221
super (context );
204
222
this .outputFormat = outputFormat ;
@@ -208,6 +226,36 @@ protected Execution(
208
226
this .preRelease = preRelease ;
209
227
this .preservePreRelease = preservePreRelease ;
210
228
this .incrementPreRelease = incrementPreRelease ;
229
+ this .nonAnnotatedTag = nonAnnotatedTag ;
230
+ }
231
+
232
+ /**
233
+ * Return the last tag.
234
+ *
235
+ * @param dir The project's directory.
236
+ * @param includeNonAnnotatedTags If true include the non annotated tag.
237
+ *
238
+ * @return The last tag of the project.
239
+ */
240
+ private String getLatestTag (File dir , boolean includeNonAnnotatedTags )
241
+ throws InterruptedException , IOException {
242
+ Objects .requireNonNull (dir , "Directory is mandatory" );
243
+ String latestTag = "" ;
244
+ try {
245
+ if (includeNonAnnotatedTags ) {
246
+ latestTag = execute (dir , "git" , "tag" , "-l" ).trim ();
247
+ latestTag = latestTag .substring (latestTag .lastIndexOf ("\n " ) + 1 );
248
+ } else {
249
+ latestTag = execute (dir , "git" , "describe" , "--abbrev=0" , "--tags" ).trim ();
250
+ }
251
+ } catch (IOException exp ) {
252
+ if (exp .getMessage ().contains ("No names found, cannot describe anything." )) {
253
+ getContext ().get (TaskListener .class ).getLogger ().println ("No tags found" );
254
+ }
255
+ }
256
+
257
+ getContext ().get (TaskListener .class ).getLogger ().println ("Current Tag is: " + latestTag );
258
+ return latestTag ;
211
259
}
212
260
213
261
@ Override
@@ -222,16 +270,7 @@ protected String run() throws Exception {
222
270
throw new IOException ("workspace.isRemote(), not entirely sure what to do here..." );
223
271
} else {
224
272
File dir = new File (workspace .getRemote ());
225
- // git describe --abbrev=0 --tags
226
- String latestTag = "" ;
227
- try {
228
- latestTag = execute (dir , "git" , "describe" , "--abbrev=0" , "--tags" ).trim ();
229
- getContext ().get (TaskListener .class ).getLogger ().println ("Current Tag is: " + latestTag );
230
- } catch (IOException exp ) {
231
- if (exp .getMessage ().contains ("No names found, cannot describe anything." )) {
232
- getContext ().get (TaskListener .class ).getLogger ().println ("No tags found" );
233
- }
234
- }
273
+ String latestTag = getLatestTag (dir , nonAnnotatedTag );
235
274
236
275
Version currentVersion =
237
276
new CurrentVersion ()
0 commit comments