diff --git a/doc/Documentation.md b/doc/Documentation.md index 803c07c451..0c24e41d5c 100644 --- a/doc/Documentation.md +++ b/doc/Documentation.md @@ -228,7 +228,9 @@ text into an issue instance. Here is an example of such a Groovy based parser: #### Creating a Groovy parser programmatically The Groovy based parser can also be created using a Groovy script from within a pipeline, a Jenkins startup script or -the script console, see the following example: +the script console. + +See the following example: ```groovy def config = io.jenkins.plugins.analysis.warnings.groovy.ParserConfiguration.getInstance() @@ -241,7 +243,7 @@ if(!config.contains('pep8-groovy')){ 'return builder.setFileName(matcher.group(1)).setCategory(matcher.group(4)).setMessage(matcher.group(5)).buildOptional()', "optparse.py:69:11: E401 multiple imports on one line" ) - config.setParsers(config.getParsers().plus(newParser)) + config.addParser(newParser) } ``` diff --git a/plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfiguration.java b/plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfiguration.java index e40eae9aea..acd4206b1c 100644 --- a/plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfiguration.java +++ b/plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfiguration.java @@ -87,6 +87,39 @@ public void setParsers(final List parsers) { save(); } + /** + * Removes a GroovyParser from the list by Id. + * + * @param parserId + * the ID of the Groovy parser to be deleted + */ + public void deleteParser(final String parserId) { + if (contains(parserId)) { + GroovyParser parser = getParser(parserId); + this.parsers.remove(parser); + } + else { + throw new NoSuchElementException(String.format("No Groovy parser with ID '%s' found.", parserId)); + } + save(); + } + + /** + * Adds a GroovyParser to the list without removing other parsers. + * + * @param parser + * the new Groovy parser to be added + */ + public void addParser(final GroovyParser parser) { + String parserId = parser.getId(); + if (contains(parserId)) { + throw new IllegalArgumentException(String.format("ID '%s' already exists.", parserId)); + } + this.parsers.add(parser); + + save(); + } + /** * Says if the admin has permitted groovy parsers to scan a build's console log. * diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfigurationTest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfigurationTest.java index 597ef52d97..d3c89100c0 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfigurationTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfigurationTest.java @@ -27,7 +27,7 @@ void shouldHaveNoParsersWhenCreated() { } @Test - void shouldSaveConfigurationIfParsersAreAdded() { + void shouldSaveConfigurationIfParsersAreSet() { GlobalConfigurationFacade facade = mock(GlobalConfigurationFacade.class); ParserConfiguration configuration = new ParserConfiguration(facade); @@ -68,4 +68,45 @@ void shouldWarnUserIfConsoleLogScanningPermittedIsSet() { final FormValidation actualTrue = configuration.doCheckConsoleLogScanningPermitted(true); assertThat(actualTrue.kind).isEqualTo(FormValidation.Kind.WARNING); } + + @Test + void shouldSaveConfigurationIfParserIsAdded() { + GlobalConfigurationFacade facade = mock(GlobalConfigurationFacade.class); + GroovyParser additionalParser = new GroovyParser("1", "", "", "", ""); + + ParserConfiguration configuration = new ParserConfiguration(facade); + configuration.addParser(additionalParser); + + verify(facade).save(); + assertThat(configuration.getParsers()).containsExactly(additionalParser); + } + + @Test + void shouldThrowIfParserExists() { + GlobalConfigurationFacade facade = mock(GlobalConfigurationFacade.class); + GroovyParser testParser = new GroovyParser("1", "", "", "", ""); + + ParserConfiguration configuration = new ParserConfiguration(facade); + configuration.addParser(testParser); + verify(facade).save(); + + assertThatIllegalArgumentException().isThrownBy(() -> configuration.addParser(testParser)).withMessageContaining(testParser.getId()); + } + + @Test + void deleteShouldRemoveOnlySpecifiedParser() { + GlobalConfigurationFacade facade = mock(GlobalConfigurationFacade.class); + GroovyParser firstTestParser = new GroovyParser("1", "", "", "", ""); + GroovyParser secondTestParser = new GroovyParser("2", "", "", "", ""); + + ParserConfiguration configuration = new ParserConfiguration(facade); + configuration.addParser(firstTestParser); + configuration.addParser(secondTestParser); + + assertThat(configuration.getParsers()).containsExactly(firstTestParser, secondTestParser); + + configuration.deleteParser(firstTestParser.getId()); + assertThat(configuration.getParsers()).containsExactly(secondTestParser); + + } } diff --git a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/steps/StepsITest.java b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/steps/StepsITest.java index 1443d7c24c..70ba00875a 100644 --- a/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/steps/StepsITest.java +++ b/plugin/src/test/java/io/jenkins/plugins/analysis/warnings/steps/StepsITest.java @@ -815,6 +815,33 @@ void shouldShowWarningsOfGroovyParserWhenScanningConsoleLogWhenThatIsPermitted() testGroovyPep8JobIsSuccessful(job, id); } + /** + * Registers a new {@link GroovyParser} (a Pep8 parser) in Jenkins global configuration and runs this parser on the + * console that's showing an error log with 8 issues. + * + * @throws IOException + * if the test fails unexpectedly + */ + @Test + void shouldShowWarningsOfGroovyParserWhenScanningConsoleLogWhenThatIsPermittedAndUsingAddParser() throws IOException { + WorkflowJob job = createPipeline(); + ArrayList stages = new ArrayList<>(); + catFileContentsByAddingEchosSteps(stages, "pep8Test.txt"); + stages.add("def groovy = scanForIssues " + + "tool: groovyScript(parserId: 'another-groovy-pep8', pattern:'', reportEncoding:'UTF-8')"); + stages.add("publishIssues issues:[groovy]"); + job.setDefinition(asStage(stages.toArray(new String[0]))); + + ParserConfiguration configuration = ParserConfiguration.getInstance(); + configuration.setConsoleLogScanningPermitted(true); + String id = "another-groovy-pep8"; + + configuration.addParser(new GroovyParser(id, "Another Groovy Pep8", + "(.*):(\\d+):(\\d+): (\\D\\d*) (.*)", + toString("groovy/pep8.groovy"), "")); + testGroovyPep8JobIsSuccessful(job, id); + } + /** * Registers a new {@link GroovyParser} (a Pep8 parser) in Jenkins global configuration and runs this parser on the * console that's showing an error log with 8 issues ... but when we're configured not to allow groovy parsers to