Skip to content

Commit 78c8b45

Browse files
author
Stéphane Philippart
authored
Merge pull request #110 from philippart-s/bug-non-annotated-tag
bug: Support non annotated tag
2 parents c7ba2af + 19f7b69 commit 78c8b45

File tree

6 files changed

+81
-16
lines changed

6 files changed

+81
-16
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
*.iml
33
target
44
work
5+
.classpath
6+
.settings
7+
.project

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
## Introduction
1010

1111
This plugin can be used to determine the next release version based on previous tags and the commit messages used.
12+
:warning: By default only [annotated tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging) are supported, to support non annotated tag you must use an option to activate this feature (see below).:warning:
1213
It calculates the version number based on the format of the commit message.
1314
The commit message format used is [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/).
1415

src/main/java/io/jenkins/plugins/conventionalcommits/NextVersionStep.java

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.util.Arrays;
1818
import java.util.List;
19+
import java.util.Objects;
1920
import java.util.Set;
2021
import javax.annotation.Nonnull;
2122
import org.apache.commons.lang.StringUtils;
@@ -40,6 +41,8 @@ public class NextVersionStep extends Step {
4041
private boolean incrementPreRelease;
4142
private String buildMetadata;
4243
private boolean writeVersion;
44+
// True if non annotated tag are supported
45+
private boolean nonAnnotatedTag;
4346

4447
@DataBoundConstructor
4548
public NextVersionStep() {
@@ -123,6 +126,11 @@ public void setIncrementPreRelease(boolean incrementPreRelease) {
123126
this.incrementPreRelease = incrementPreRelease;
124127
}
125128

129+
@DataBoundSetter
130+
public void setNonAnnotatedTag(boolean nonAnnotatedTag) {
131+
this.nonAnnotatedTag = nonAnnotatedTag;
132+
}
133+
126134
@Override
127135
public StepExecution start(StepContext stepContext) throws Exception {
128136
return new Execution(
@@ -133,6 +141,7 @@ public StepExecution start(StepContext stepContext) throws Exception {
133141
preRelease,
134142
preservePreRelease,
135143
incrementPreRelease,
144+
nonAnnotatedTag,
136145
stepContext);
137146
}
138147

@@ -179,6 +188,13 @@ public static class Execution extends SynchronousStepExecution<String> {
179188
// True to increment prerelease information instead of the version itself
180189
private final transient boolean incrementPreRelease;
181190

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+
182198
/**
183199
* Constructor with fields initialisation.
184200
*
@@ -189,6 +205,7 @@ public static class Execution extends SynchronousStepExecution<String> {
189205
* @param preRelease Pre release information to add
190206
* @param preservePreRelease Keep existing prerelease information or not
191207
* @param incrementPreRelease Increment prerelease information or not
208+
* @param nonAnnotatedTag Should use or non annotated tags
192209
* @param context Jenkins context
193210
*/
194211
protected Execution(
@@ -199,6 +216,7 @@ protected Execution(
199216
String preRelease,
200217
boolean preservePreRelease,
201218
boolean incrementPreRelease,
219+
boolean nonAnnotatedTag,
202220
@Nonnull StepContext context) {
203221
super(context);
204222
this.outputFormat = outputFormat;
@@ -208,6 +226,36 @@ protected Execution(
208226
this.preRelease = preRelease;
209227
this.preservePreRelease = preservePreRelease;
210228
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;
211259
}
212260

213261
@Override
@@ -222,16 +270,7 @@ protected String run() throws Exception {
222270
throw new IOException("workspace.isRemote(), not entirely sure what to do here...");
223271
} else {
224272
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);
235274

236275
Version currentVersion =
237276
new CurrentVersion()

src/test/java/io/jenkins/plugins/conventionalcommits/JenkinsTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,30 @@ public void shouldFailWriteVersion() throws Exception {
423423
containsString("Could not write the next version to the configuration file."));
424424
assertThat(JenkinsRule.getLog(b), containsString("Finished: SUCCESS"));
425425
}
426+
427+
@Test
428+
public void shouldUseNonAnnotatedTag() throws Exception {
429+
// Given : a project with last tag (1.1.0) is non annotated
430+
WorkflowJob p = rule.jenkins.createProject(WorkflowJob.class, "p");
431+
URL zipFile = getClass().getResource("simple-project-with-non-annotated-tags.zip");
432+
assertThat(zipFile, is(notNullValue()));
433+
434+
// When: ask to have nextVersion with nonAnnotatedTag option to true
435+
p.setDefinition(
436+
new CpsFlowDefinition(
437+
"node {\n"
438+
+ " unzip '"
439+
+ zipFile.getPath()
440+
+ "'\n"
441+
+ " nextVersion(nonAnnotatedTag: true)\n"
442+
+ "}\n",
443+
true));
444+
445+
// Then : the given nextVersion is based on the last tag (even if it's a non annotated tag)
446+
WorkflowRun b = rule.assertBuildStatus(Result.SUCCESS, p.scheduleBuild2(0).get());
447+
assertThat(JenkinsRule.getLog(b), containsString("Started"));
448+
assertThat(JenkinsRule.getLog(b), containsString("nextVersion"));
449+
assertThat(JenkinsRule.getLog(b), containsString("1.1.1"));
450+
assertThat(JenkinsRule.getLog(b), containsString("Finished: SUCCESS"));
451+
}
426452
}

src/test/java/io/jenkins/plugins/conventionalcommits/utils/GradleProjectTypeTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,14 @@ public void shouldWriteNextVersionToFile() throws Exception {
9595
File gradleDir = rootFolder.newFolder("SampleGradleProject");
9696
createBuildGradleFiles(gradleDir, buildGradleWithVersionContent);
9797

98-
// When : write next version tp the file
98+
// When : write next version to the file
9999
GradleProjectType gradleProjectType = new GradleProjectType();
100100
gradleProjectType.writeVersion(gradleDir, Version.valueOf("1.1.0"), null);
101101

102102
// Then : the file is updated
103-
String buildGradleExpected =
104-
"foo = foo value\n" +
105-
"version = 1.1.0\n" +
106-
"bar = bar value";
107103
assertThat(new String(
108104
Files.readAllBytes(Paths.get(gradleDir.getPath() + File.separator + "gradle.properties"))),
109-
containsString(buildGradleExpected));
105+
containsString("version = 1.1.0"));
110106
}
111107

112108
@Test(expected = IOException.class)
6.51 KB
Binary file not shown.

0 commit comments

Comments
 (0)