-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix GradleConsoleAnnotator when Timestamper is globally enabled (#374)
Fix GradleConsoleAnnotator when Timestamper is globally enabled Fixes JENKINS-72411.
- Loading branch information
Showing
7 changed files
with
169 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/hudson/plugins/gradle/TimestampPrefixDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package hudson.plugins.gradle; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
final class TimestampPrefixDetector { | ||
|
||
static final String TimestampPattern = "\\[\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z\\] "; | ||
private static final Pattern TimestampPatternR = Pattern.compile("^(" + TimestampPattern + ").*(?:\r?\n)?$"); | ||
|
||
static String trimTimestampPrefix(int prefix, String line) { | ||
return line.substring(prefix); | ||
} | ||
|
||
private TimestampPrefixDetector() { | ||
} | ||
|
||
static int detectTimestampPrefix(String line) { | ||
Matcher matcher = TimestampPatternR.matcher(line); | ||
if (matcher.matches()) { | ||
return matcher.group(1).length(); | ||
} | ||
return 0; | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
src/test/groovy/hudson/plugins/gradle/GradleConsoleAnnotatorIntegrationTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package hudson.plugins.gradle | ||
|
||
import hudson.plugins.timestamper.TimestamperConfig | ||
import hudson.slaves.DumbSlave | ||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition | ||
import org.jenkinsci.plugins.workflow.job.WorkflowJob | ||
|
||
class GradleConsoleAnnotatorIntegrationTest extends BaseGradleIntegrationTest { | ||
|
||
def 'pipeline job has Gradle tasks annotated'(boolean enableTimestamper) { | ||
given: | ||
def gradleVersion = '8.1.1' | ||
gradleInstallationRule.gradleVersion = gradleVersion | ||
gradleInstallationRule.addInstallation() | ||
|
||
DumbSlave agent = createSlave('foo') | ||
def pipelineJob = j.createProject(WorkflowJob) | ||
|
||
pipelineJob.setDefinition(new CpsFlowDefinition(""" | ||
stage('Build') { | ||
node('foo') { | ||
withGradle { | ||
git branch: 'main', url: 'https://github.com/c00ler/simple-gradle-project' | ||
def gradleHome = tool name: '${gradleInstallationRule.gradleVersion}', type: 'gradle' | ||
if (isUnix()) { | ||
sh "'\${gradleHome}/bin/gradle' clean build --no-daemon --console=plain" | ||
} else { | ||
bat(/"\${gradleHome}\\bin\\gradle.bat" clean build --no-daemon --console=plain/) | ||
} | ||
} | ||
} | ||
} | ||
""", false)) | ||
|
||
if (enableTimestamper) { | ||
TimestamperConfig config = TimestamperConfig.get() | ||
config.setAllPipelines(true) | ||
config.save() | ||
} | ||
|
||
when: | ||
def b = j.buildAndAssertSuccess(pipelineJob) | ||
|
||
then: | ||
def client = j.createWebClient() | ||
def html = client.goTo(b.getUrl() + "console") | ||
html.getByXPath("//b[@class='gradle-task']")*.textContent*.toString() == [ | ||
' Task :clean', | ||
' Task :compileJava', | ||
' Task :processResources', | ||
' Task :classes', | ||
' Task :jar', | ||
' Task :assemble', | ||
' Task :compileTestJava', | ||
' Task :processTestResources', | ||
' Task :testClasses', | ||
' Task :test', | ||
' Task :check', | ||
' Task :build' | ||
] | ||
html.getByXPath("//span[@class='gradle-task-progress-status']")*.textContent*.toString() == [ | ||
'UP-TO-DATE\n', | ||
'NO-SOURCE\n', | ||
'NO-SOURCE\n' | ||
] | ||
html.getByXPath("//span[@class='gradle-outcome-success']")*.textContent*.toString() == ['BUILD SUCCESSFUL'] | ||
|
||
where: | ||
enableTimestamper << [ true, false ] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/groovy/hudson/plugins/gradle/TimestampPrefixDetectorTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package hudson.plugins.gradle | ||
|
||
import spock.lang.Specification | ||
|
||
class TimestampPrefixDetectorTest extends Specification { | ||
|
||
def "detect timestamp on line"() { | ||
when: | ||
def prefix = TimestampPrefixDetector.detectTimestampPrefix(line) | ||
|
||
then: | ||
prefix == expected | ||
|
||
where: | ||
line | expected | ||
'[2023-12-08T10:05:56.488Z] > Task :compileJava' | 27 | ||
'[2023-12-08T10:05:56.488Z] > Task :compileJava\n' | 27 | ||
'[2023-12-08T10:05:56.488Z]> Task :compileJava' | 0 | ||
'[2023-12-08T10:05:56] > Task :compileJava' | 0 | ||
'some message' | 0 | ||
} | ||
|
||
def "trim timestamp"() { | ||
when: | ||
def trimmed = TimestampPrefixDetector.trimTimestampPrefix(prefix, line) | ||
|
||
then: | ||
trimmed == expected | ||
|
||
where: | ||
line | prefix | expected | ||
'[2023-12-08T10:05:56.488Z] > Task :compileJava' | 27 | '> Task :compileJava' | ||
'> Task :compileJava' | 0 | '> Task :compileJava' | ||
} | ||
|
||
} |