Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete and add methods for parsers in global configuration #1599

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions doc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,39 @@
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));

Check warning on line 102 in plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfiguration.java

View check run for this annotation

Codecov / codecov/patch

plugin/src/main/java/io/jenkins/plugins/analysis/warnings/groovy/ParserConfiguration.java#L102

Added line #L102 was not covered by tests
}
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void shouldHaveNoParsersWhenCreated() {
}

@Test
void shouldSaveConfigurationIfParsersAreAdded() {
void shouldSaveConfigurationIfParsersAreSet() {
GlobalConfigurationFacade facade = mock(GlobalConfigurationFacade.class);

ParserConfiguration configuration = new ParserConfiguration(facade);
Expand Down Expand Up @@ -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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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
Expand Down
Loading